codeforces #436 A Feed with Candy(贪心)

题目地址: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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值