codeforces #436 A Feed with Candy(贪心)

来源:互联网 发布:知乎体格式 编辑:程序博客网 时间:2024/06/10 06:46

题目地址:http://codeforces.com/contest/436/problem/A

自己笨的要死。。。WA了好多次,还是看题解才明白了。。。一直在纠结该先选0好还是先选1好,但是就是没想到可以枚举这两种情况都试一试。。。

分别枚举这两种情况,然后每次选的时候从另一种糖果里从可以够到的糖果里选出m最大的那个,贪心就可以了。

代码如下:

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;struct node{    int x, h, m;} fei[3000];int _hash[3000], a[3];int cmp(node x, node y){    return x.m>y.m;}int main(){    int n, h, i, j, tot, x, m, max1=-1;    scanf("%d%d",&n,&m);    for(i=0; i<n; i++)    {        scanf("%d%d%d",&fei[i].x,&fei[i].h,&fei[i].m);    }    sort(fei,fei+n,cmp);    for(j=0; j<2; j++)    {        tot=0;        h=m;        memset(_hash,0,sizeof(_hash));        x=j;        while(1)        {            int flag=0;            for(i=0; i<n; i++)            {                if(!_hash[i]&&fei[i].h<=h&&fei[i].x!=x)                {                    h+=fei[i].m;                    tot++;                    flag=1;                    x=fei[i].x;                    _hash[i]=1;                    break;                }            }            if(!flag) break;        }        max1=max(max1,tot);    }    printf("%d\n",max1);    return 0;}


2 0
原创粉丝点击