CF 626C [Block Towers] 贪心

题目链接:http://codeforces.com/problemset/problem/626/C

题目大意: 有n个人用高度为2的砖往上搭,有m个人用高度为3的砖往上搭。每个人可用的砖块数是无限的,但是要求每个人搭的塔的高度,求所有可行情况中塔的最高高度的最小值。

关键思想:贪心,当且仅当高度为6的倍数时,他们的高度会相同。这时必须有一个人搭更高的,哪个人呢?此时就贪心;另外一种思想是数学的,首先答案x一定是大于等于2n也是大于等于3m的(有6的倍数取等号)对吧,而且m+n<=(x/2+x/3-x/6)【仔细想象是不是这样】。通过这个不等式求出符合要求的x的最小值。

代码如下:

//贪心
#include <iostream>
using namespace std;

int main(){
    long long n,m;
    while(cin>>n>>m){
        long long d1=2*n,d2=3*m;
        for(long long i=6;i<=min(d1,d2);i+=6){//i是6的倍数,它比终点小说明还有重复的情况。
            if(d1<=d2){
                d1+=2;
            }else{
                d2+=3;
            }//贪心
        }
        cout<<max(d1,d2)<<endl;
    }

    return 0;
}

  

#include <cstdio>

int main()
{
	int i, a, b;
	scanf("%d%d", &a, &b);
	for (i = 0;; i++) if (i / 2 >= a && i / 3 >= b && i / 2 + i / 3 - i / 6 >= a + b) break;
	printf("%d", i);
}

  

时间: 2024-08-06 19:42:30

CF 626C [Block Towers] 贪心的相关文章

Codeforces 626C. Block Towers

C. 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

Codeforces 626C Block Towers「贪心」「二分」「数学规律」

题意: 一堆人用方块盖塔,有n个人每次只能加两块方块,有m个人每次只能加三块方块.要求每个人盖的塔的高度都不一样,保证所用方块数最少,求最高的塔的高度. 0<=n+m  0<=n,m<=1e6 思路: 根据容斥原理,n和m个人如果都按照等差为2或者3的序列盖塔的话那么重复的个数应该是塔高较小的那组除以6,然后....一开始顺着这个思路想把自己坑了... 其实可能的塔高是有规律的 2 3 4 6 8 9 10 12...每六个中有三个,所以干脆先打表了,那么知道n和m之后,至少需要n+m种

【CodeForces 626C】Block Towers

题意 给你n,m,如果 n个2的倍数和m个3的倍数,这n+m个数各不相同,那么求最大的数的最小值. 分析 方法1:枚举最大值为i,直到 i/2+i/3-i/6(不重复的2或3的倍数)≥n+m,并且要i/2(2的倍数)≥n,i/3(3的倍数)≥m. 方法2:枚举重复的数字i,i最小为6,每次增加6,设置两个结尾初始值为2*n,3*m,当两个结尾都比i大时,那就是还有重复.然后加在比较短的结尾.直到不再有重复. 代码 方法1 #include<cstdio> #include<algorit

cf 12C Fruits(贪心【简单数学】)

题意: m个水果,n个价格.每种水果只有一个价格. 问如果给每种水果分配价格,使得买的m个水果总价格最小.最大. 输出最小值和最大值. 思路: 贪心. 代码: bool cmp(int a,int b){ return a>b; } string name; map<string,int> mp; int price[200],fruit[200]; int cn; int n,m; int main(){ cin>>n>>m; mp.clear(); cn=0;

cf 540b School Marks 贪心

B. School Marks time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Little Vova studies programming in an elite school. Vova and his classmates are supposed to write n progress tests, for each

Block Towers (思维实现)

个人心得:这题没怎么看,题意难懂.后面比完再看的时候发现很好做但是怎么卡时间是个问题. 题意:就是有m个可以用2层积木的,n个可以用三层积木的,但是他们不允许重复所以可以无限添加. 比如 3 2 一开始是2层的开始2,然后 3,然后 4,此时再添加都一样了,为了保证最小高度所以3+3=6,此时的2层的就要添加2个才不重样 网上大神多,这个代码我服,按照函数关系俩着重复的地点都有规律,所以只要找到此时最大m*2,n*3然后碰到一次相同就让最小的最大值增加就可以了 题目: Students in a

cf 853 A planning [贪心]

题面: 传送门 思路: 一眼看得,这是贪心[雾] 实际上,我们要求的答案就是sigma(ci*(ti-i))(i=1~n),这其中sigma(ci*i)是确定的 那么我们就要最小化sigma(ci*ti) 所以在新的每一秒,就把这一秒开始可以起飞的飞机中,cost最大的那一个拿出来,让他起飞就可以了 证明: 设最大的为m,我们取得另一个为n 那么n*ti+m*(ti+1) >= n*(ti+1)+m*ti 所以取m最好 这个过程用堆实现,懒得手打了,就用了priority_queue Code:

CF 545C Woodcutters(贪心)

题目链接:http://codeforces.com/problemset/problem/545/C 题目: Little Susie listens to fairy tales before bed every day. Today's fairy tale was about wood cutters and the little girl immediately started imagining the choppers cutting wood. She imagined the

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