CF A. Feed with Candy

来源:互联网 发布:安全网络知识竞赛登录 编辑:程序博客网 时间:2024/05/20 13:36

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 30 2 41 3 10 8 30 20 101 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.



小贪心。。我的思路是把每个糖果初始的高度按从小到大排序,然后两个for循环嵌套找最大的m。。。然后pass。。然后 Hack。。原来还要讨论第一个选0最多还是选1最多。。。然后就没有然后了==

下面是PASS的代码。。AC的以后在上传


#include <stdio.h>#include <stdlib.h>struct node{    int x;    int h;    int w;    int f;} p[2010];int cmp (const void *a,const void *b){    if ((*(struct node *)a).h != (*(struct node *)b).h)        return (*(struct node *)a).h - (*(struct node*)b).h;    else        return (*(struct node *)a).w - (*(struct node*)b).w;}int main(){    int m,n,i,j;    int f=-1;    scanf ("%d%d",&m,&n);    for(i=0; i<m; i++)    {        scanf ("%d%d%d",&p[i].x,&p[i].h,&p[i].w);    }    qsort (p,m,sizeof (p[0]),cmp);    int ww=p[0].w,count=0;    for (i=0; i<m; i++)    {        int max=0,z=0;        for (j=0; j<m; j++)        {            if (p[j].x!=-1&&p[j].h <= n && f!=p[j].x )            {                if (max < p[j].w)                {                    max=p[j].w;                    z=j;                }            }        }        if (p[z].h <= n && f!=p[z].x && p[z].x!=-1 )        {            f=p[z].x;            n+=max;            p[z].x=-1;            count++;        }    }    printf ("%d\n",count);    return 0;}




0 0
原创粉丝点击