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: 1, 5, 3, 2.
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;
}

Codeforces Zepto Code Rush 2014 Feed with Candy 贪心,布布扣,bubuko.com

时间: 2024-10-18 00:10:11

Codeforces Zepto Code Rush 2014 Feed with Candy 贪心的相关文章

Zepto Code Rush 2014——Dungeons and Candies

题目链接 题意: k个点,每个点都是一个n * m的char型矩阵.对与每个点,权值为n * m或者找到一个之前的点,取两个矩阵对应位置不同的字符个数乘以w.找到一个序列,使得所有点的权值和最小 分析: 首先,这个图是一个无向图.求权值和最小,每个权值对应的是一条边,且每个点只能有一个权值即一条边,一个k个边,和生成树很像,但是需要证明不能有环形.不妨假设现在有三个点,每个点的最小边成环,这时候是不能找到一个序列使得每个点都取到它的最小边值的,所以,k个点k个边不能有环且边值和最小,就是最小生成

Codeforces ZeptoLab Code Rush 2015

比赛链接:http://codeforces.com/contest/526/ A. King of Thieves time limit per test:1 second memory limit per test:256 megabytes In this problem you will meet the simplified model of game King of Thieves. In a new ZeptoLab game called "King of Thieves&quo

【codeforces ZeptoLab Code Rush 2015】ABCD题解

A. King of Thieves time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output In this problem you will meet the simplified model of game King of Thieves. In a new ZeptoLab game called "King of Thieves&quo

Code Rush插件

code rush 是微软推出的一款VS2008上的插件.他有强大的文件和代码导航功能,易于访问的重构和代码创建功能.一组编辑器.选择.剪贴板工具等. 教程链接 http://www.devexpresscn.com/Resources/CodeExamples-314.html http://www.devexpresscn.com/Resources/Documentation-316.html 下面的是单键模板. Most of these have narrow contexts ass

Codeforces Avito Code Challenge 2018 D. Bookshelves

Codeforces Avito Code Challenge 2018 D. Bookshelves 题目连接: http://codeforces.com/contest/981/problem/D Description Mr Keks is a typical white-collar in Byteland. He has a bookshelf in his office with some books on it, each book has an integer positive

CodeForces 706D Vasiliy&#39;s Multiset (字典树查询+贪心)

题意:最开始的时候有一个集合,集合里面只有一个元素0,现在有q次操作,操作分为3种: + x: 表示向集合中添加一个元素x - x:表示删除集合中值为x的一个元素 ? x:表示查询集合中与x异或的最大值为多少 析:这是一个字典树的应用,不过确实没看出来....主要思想是这样,先用10进制数,转成二进制数,记下每个结点的0,1的个数,这样增加和删除,就是对01的删除, 剩下的就是查询,那么尽量让0和1XOR是最大的,所以,对于给定数,我们要去尽量他的XOR数,如果找到就加上,找不到,就找下一个.这

Codeforces Round #FF/#255 D DZY Loves Modification --贪心+优先队列

题意:给你一个矩阵,每次选某一行或者某一列,得到的价值为那一行或列的和,然后该行每个元素减去p.问连续取k次能得到的最大总价值为多少. 解法: 如果p=0,即永远不减数,那么最优肯定是取每行或每列那个最大的取k次,所以最优解由此推出. 如果不管p,先拿,最后再减去那些行列交叉点,因为每个点的值只能取一次,而交叉点的值被加了两次,所以要减掉1次,如果取行A次,取列B次,那么最后答案为: res = dp1[A] + dp2[B] - B*(k-A)*p,可以细细体会一下后面那部分. 其中: dp1

Codeforces Gym 101174 A Within Arm&#39;s Reach 贪心 手臂

#include<iostream> #include<stdio.h> #include <string.h> #include <algorithm> #include <vector> #include <math.h> using namespace std; #define LL long long const int maxn=25; double a[maxn],l[maxn],r[maxn]; double ex,ey

【codeforces】ZeptoLab Code Rush 2015 E 跳跃表?

题意就是给n个数,围成一圈,就是1和n是相邻的,然后给一个数b,总和不超过b的一段连续的数可以组成一组,问最少可以将n个数分成几组. 可以将n个数后面再接n个数,就变成n+n个数,然后以每个数为开头的组最远能到哪也是很容易求的,O(n)维护个指针可以处理.把远的位置视为跳一步能到的吧,这样问题就转化为1到n中的第i个数至少到第n+i个数要跳多少次.这个如果是一般图的话,就是类似树上求k步的祖先在哪,可以用倍增法,n*log(n).但是这题图比较特殊,i<j的话,i跳到的下一点位置不会超过j跳到的