CodeForces 626 C. Block Towers(贪心)

来源:互联网 发布:企业数据库买什么品牌 编辑:程序博客网 时间:2024/04/29 03:24

Description
n个小孩搭高度为2k的塔,m个小孩搭高度为3k的塔,每个小孩的塔高不同,问这群小孩搭的最高塔塔高的最小值
Input
两个整数n,m(0<=n,m<=1000000,n+m>0)
Output
输出这群小孩搭的最高塔塔高的最小值
Sample Input
3 2
Sample Output
8
Solution
贪心,问题转化为求一个只由2a,3b构成的数列中第n+m大的数(a,b=0,1,2…),令n=2n,m=3m后每次比较n和m的大小,n小就给n加2,m小就给m加3,操作min(n,m)/6次(2,4,6,…,2n和3,6,9,…3m重复了这么多次)之后输出n和m的最大值即可
Code

#include<cstdio>#include<iostream>#include<algorithm>using namespace std;int n,m;int main(){    while(~scanf("%d%d",&n,&m))    {        n*=2,m*=3;        for(int i=1;i<=min(n,m)/6;i++)            if(n>m)m+=3;            else n+=2;        printf("%d\n",max(n,m));        }       return 0;}
0 0
原创粉丝点击