标签:acm c++ codeforces 贪心
The hero of the Cut the Rope game is a little monster named Om Nom. He loves candies. And what a coincidence! He also is the hero of today‘s problem.
One day, Om Nom visited his friend Evan. Evan has n candies of two types (fruit drops and caramel drops), the i-th candy hangs at the height of hi centimeters above the floor of the house, its mass is mi. Om Nom wants to eat as many candies as possible. At the beginning Om Nom can make at most x centimeter high jumps. When Om Nom eats a candy of mass y, he gets stronger and the height of his jump increases by y centimeters.
What maximum number of candies can Om Nom eat if he never eats two candies of the same type in a row (Om Nom finds it too boring)?
The first line contains two integers, n and x (1?≤?n,?x?≤?2000) — the number of sweets Evan has and the initial height of Om Nom‘s jump.
Each of the following n lines contains three integers ti,?hi,?mi (0?≤?ti?≤?1; 1?≤?hi,?mi?≤?2000) — the type, height and the mass of the i-th candy. If number ti equals 0, then the current candy is a caramel drop, otherwise it is a fruit drop.
Print a single integer — the maximum number of candies Om Nom can eat.
5 3 0 2 4 1 3 1 0 8 3 0 20 10 1 5 5
4
One of the possible ways to eat 4 candies is to eat them in the order: 1, 5, 3, 2. Let‘s assume the following scenario:
一开始想的很native,就是想简单贪心排一下序去搞。。。然后玩脱了。。。先是无脑WA8,后来好不容易过了就被适牛hack了。。。
正解是,每次选取所有可能的糖果中,质量最大的那个。
具体实现的时候可以用优先队列去维护。
/*============================================================================= # COPYRIGHT NOTICE # Copyright (c) 2014 # All rights rserved # # @author :Shen # @name :A # @file :G:\My Source Code\比赛与日常练习\0613 - CF\A.cpp # @date :2014-06-14 01:12 # @algorithm :Greedy =============================================================================*/ #include <queue> #include <cstdio> #include <vector> #include <algorithm> using namespace std; typedef long long int64; int n, x, tmp; int htmp, mtmp; typedef pair<int, int> candy; vector<candy> a[2]; priority_queue<int> f[2]; int work(int p) { int h = x, j[2] = {0, 0}, ans = 0; for (int i = 0; i < 2; i++) while (!f[i].empty()) f[i].pop(); while (1) { while (j[p] < a[p].size() && a[p][j[p]].first <= h) f[p].push(a[p][j[p]++].second); if (f[p].empty()) break; h += f[p].top(), f[p].pop(); ans++; p ^= 1; } return ans; } void solve() { int rs1 = 0, rs2 = 0, ans = 0; for (int i = 0; i < 2; i++) if (a[i].size() > 0) sort(a[i].begin(), a[i].end()); rs1 = work(1); rs2 = work(0); ans = max(rs1, rs2); printf("%d\n", ans); } int main() { scanf("%d%d", &n, &x); for (int i = 0; i < n; i++) { scanf("%d%d%d", &tmp, &htmp, &mtmp); a[tmp].push_back(make_pair(htmp, mtmp)); } solve(); return 0; }
Codeforces Zepto Code Rush 2014 Feed with Candy 贪心,布布扣,bubuko.com
Codeforces Zepto Code Rush 2014 Feed with Candy 贪心
标签:acm c++ codeforces 贪心
原文地址:http://blog.csdn.net/polossk/article/details/30638785