1. 程式人生 > >CF626C Block Towers

CF626C Block Towers

題意 als making UNC color 簡單判斷 集中 alt second

鏈接: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

CF626C Block Towers