CF626C Block Towers

链接:https://www.luogu.org/problemnew/show/CF626C

题目描述

Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. nn of the students use pieces made of two blocks and mm of the students use pieces made of three blocks.

The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers.

输入输出格式

输入格式:

The first line of the input contains two space-separated integers nn and mm ( 0<=n,m<=10000000<=n,m<=1000000 , $ n+m&gt;0 $ ) — the number of students using two-block pieces and the number of students using three-block pieces, respectively.

输出格式:

Print a single integer, denoting the minimum possible height of the tallest tower.

输入输出样例

输入样例#1:  复制
1 3
输出样例#1:  复制
9
输入样例#2:  复制
3 2
输出样例#2:  复制
8
输入样例#3:  复制
5 0
输出样例#3:  复制
10

说明

In the first case, the student using two-block pieces can make a tower of height 44 , and the students using three-block pieces can make towers of height 33 , 66 , and 99 blocks. The tallest tower has a height of 99 blocks.

In the second case, the students can make towers of heights 22 , 44 , and 88 with two-block pieces and towers of heights 33 and 66 with three-block pieces, for a maximum height of 88 blocks.

题意:

给两个数字n,m。
• 求构造出n 个2 的倍数,m 个3 的倍数,数
字各不相同,求最大值的最小值。

题解:

最大值最小or 最小值最大
• 首先确定题目能否用二分思想:
• 1. 是否满足单调性.
• 2. 是否有可行的判断条件。
• 对于这道题,简单判断来看,对应出的“构造出n 个2 的倍数,
m 个3 的倍数”这个解都有多个,而解集中有一个最小的,就是
答案,而下一个解集对应的最小值,又比这个答案大。
• 所以答案是单调递增。满足单调性

#include<bits/stdc++.h>
using namespace std;

const double eps = 1e-6;
int n, m;
bool check(int x){
    int num1 = x/2, num2 = x/3, num3 = x/6;
    if(num1 < n || num2 < m)return 0;
    if(num1 + num2 - num3 < n + m) return 0;
    return 1;
    
}
int main(){
    cin>>n>>m;
    int lf = 0, ans, rg = 2000000000;
    while(lf <= rg){
        int mid = (lf + rg) >> 1;
        if(check(mid))rg = mid - 1, ans = mid;
        else lf = mid + 1;
    } 
    printf("%d\n", ans);
}
View Code

猜你喜欢

转载自www.cnblogs.com/EdSheeran/p/9277874.html