首页 > 代码库 > Codeforces Zepto Code Rush 2014 Feed with Candy 贪心

Codeforces Zepto Code Rush 2014 Feed with Candy 贪心

A. Feed with Candy
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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)?

Input

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.

Output

Print a single integer — the maximum number of candies Om Nom can eat.

Sample test(s)
input
5 3
0 2 4
1 3 1
0 8 3
0 20 10
1 5 5
output
4
Note

One of the possible ways to eat 4 candies is to eat them in the order: 1532. Let‘s assume the following scenario:

  1. Initially, the height of Om Nom‘s jump equals 3. He can reach candies 1 and 2. Let‘s assume that he eats candy 1. As the mass of this candy equals 4, the height of his jump will rise to 3?+?4?=?7.
  2. Now Om Nom can reach candies 2 and 5. Let‘s assume that he eats candy 5. Then the height of his jump will be 7?+?5?=?12.
  3. At this moment, Om Nom can reach two candies, 2 and 3. He won‘t eat candy 2 as its type matches the type of the previously eaten candy. Om Nom eats candy 3, the height of his jump is 12?+?3?=?15.
  4. Om Nom eats candy 2, the height of his jump is 15?+?1?=?16. He cannot reach candy 4.

题解

一开始想的很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;
}