题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82662#problem/B
贪心的题,acm的东西一直以来就没学的太扎实,这道题WA无数次
题目大意:有那么个怪物,要吃糖,糖果有两种1和0,每颗糖有一个高度,怪物吃了糖会获得对应的能量,而怪物有个初始能量值可以保证怪物的跳起吃糖,且怪物很作,吃糖要交替着吃。现在问,怪物最多能吃到几颗糖
解题思路:贪心,对糖果的能量从高到低进行排序,然后,每次都去吃能吃到的能量最高的糖果,这样可以保证能吃到最多的糖果,先吃1和0的结果会有不同,所以要分情况讨论
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 2005
struct candy
{
int t;
int h;
int m;
};
bool cmp(candy a, candy b)
{
return a.m > b.m;
}
int main()
{
int n, x;
while(cin >> n >> x)
{
candy a[N], b[N];
int t, h, m;
for(int i = 0; i < n; i++)
{
cin >> t >> h >> m;
a[i].t = b[i].t = t;
a[i].h = b[i].h = h;
a[i].m = b[i].m = m;
}
sort(a,a+n,cmp);
sort(b,b+n,cmp);
int sum = 0, flag = 0;
int c = x;
for(int i = 0; i < n; i++)
{
if(a[i].t == flag && a[i].h <= x)
{
x += a[i].m;
sum++;
a[i].t = -1;
flag = 1-flag;
i = -1;
}
}
int ans = 0;
x = c;
flag = 1;
for(int i = 0; i < n; i++)
{
if(b[i].t == flag && b[i].h <= x)
{
x += b[i].m;
ans ++;
b[i].t = -1;
flag = 1 - flag;
i = -1;
}
}
if( sum > ans )
ans = sum;
cout << ans << endl;
}
return 0;
}