Codeforces--626C--Block Towers

題目描述:
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. n of the students use pieces made of two blocks and m 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 n and m (0 ≤ n, m ≤ 1 000 000, n + m > 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 3
3 2
5 0
輸出:
9
8
10
題意:
n+m個人用積木搭堆(每個人搭一堆),n個人只能用高度爲2的積木, m個人只能用高度爲3的積木。兩種積木均有無限多個。

這些人都不希望自己搭出的堆的高度與其他任何人的相同。請找出在所有人搭出的積木高度都不同的情況下,這些人中搭出的最高的堆的高度最小值。
題解
簡單貪心
代碼:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int main(){
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        int two = 2 * n;
        int three = 3 * m;
        for(int i = 6; i <= min(two,three); i += 6){
            if(two <= three){
                two += 2;
            }
            else three += 3;
        }
        printf("%d\n",max(two,three));
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章