ACM学习历程—HDU4415 Assassin’s Creed(贪心)
Problem Description
Ezio Auditore is a great master as an assassin. Now he has prowled in the enemies’ base successfully. He finds that the only weapon he can use is his cuff sword and the sword has durability m. There are n enemies he wants to kill and killing each enemy needs Ai durability. Every time Ezio kills an enemy he can use the enemy’s sword to kill any other Bi enemies without wasting his cuff sword’s durability. Then the enemy’s sword will break. As a master, Ezio always want to do things perfectly. He decides to kill as many enemies as he can using the minimum durability cost.
Input
The first line contains an integer T, the number of test cases.
For each test case:
The first line contains two integers, above mentioned n and m
(1<=n<=10^5, 1<=m<=10^9).
Next n lines, each line contains two integers Ai, Bi. (0<=Ai<=10^9,
0<=Bi<=10).
Output
For each case, output "Case X: "
(X is the case number starting from 1) followed by the number of the enemies
Ezio can kill and the minimum durability cost.
Sample
Input
2
3 5
4 1
5 1
7 7
2 1
2 2
4 0
Sample
Output
Case 1:
3 4
Case 2: 0 0
题目大意是用m点耐久度去杀人,杀死一个人需要Ai耐久度,但是可以额外再杀死Bi个人。
题目要求的首先是杀人最多,其次是消耗耐久度最少。
首先会想到对人进行分类,一类是B为0的,一类是B不为0的。
因为杀死一个B不为0的,一定能杀死所有B不为0的,而且还能再额外杀死B为0的。
于是就先分出第一种情况:不杀B不为0的人;
这样就是对B为0的人进行贪心,优先杀消耗耐久度最小的即可。
考虑第二种情况:杀死至少一个B不为0的人。
那么首先前提自然是保证能杀死B不为0的人里面消耗耐久度最少的。
然后就可以杀死所有B不为0的人。而且还能再额外杀死B为0的。
最后就是用剩余耐久度去杀死剩下的B为0的人。
但是这样会出现一种情况。就是虽然B不为0的人,可以自动杀死,但是如果我手动杀死一个B不为0的人,可以多出一个名额杀死一个B为0的人。但是也许这样我会消耗更低的耐久度去手动杀死一个人。
于是就发现,肯定可以杀死所有B不为0的人,但是可以优先手动杀死耐久度小的。
得出结论:杀死第一个耐久度最小的B不为0的人后,我便按照耐久度从小到大手动杀人。
此处还要注意的是,我依次杀人后,如果剩下的人都能被自动杀死,我便不需要再手动杀死人了。
最后就是两种情况取最值了。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <utility>
#include <queue>
#include <vector>
#define LL long long using namespace std; typedef pair<int, int> pii;
const int maxN = 1e5+;
int n, m;
struct node
{
int need;
int kill;
}p[][maxN];
int top[];
int can; bool cmpNeedLest(node x, node y)
{
return x.need < y.need;
} void input()
{
scanf("%d%d", &n, &m);
can = ;
top[] = top[] = ;
int need, kill;
for (int i = ; i < n; ++i)
{
scanf("%d%d", &need, &kill);
if (kill == )
{
p[][top[]].need = need;
p[][top[]].kill = ;
top[]++;
}
else
{
p[][top[]].need = need;
p[][top[]].kill = kill;
top[]++;
can += kill;
}
}
sort(p[], p[]+top[], cmpNeedLest);
sort(p[], p[]+top[], cmpNeedLest);
} pii workOne()
{
//p->p+top;
int num = , rest = m;
for (int i = ; i < top[]; ++i)
{
if (rest >= p[][i].need)
{
num++;
rest -= p[][i].need;
}
else
break;
}
return pii(num, m-rest);
} pii workTwo()
{
int rest = m, num = ;
int from[];
from[] = from[] = ;
if (top[] == || rest < p[][].need)
return pii(, );
rest -= p[][].need;
from[]++;
bool flag;
for (;;)
{
if (n-(from[]+from[]) <= can)
break;
flag = false;
if (from[] < top[] && p[][from[]].need > p[][from[]].need)
{
if (rest >= p[][from[]].need)
{
flag = true;
rest -= p[][from[]].need;
from[]++;
num++;
}
}
else if (from[] < top[])
{
if (rest >= p[][from[]].need)
{
flag = true;
rest -= p[][from[]].need;
from[]++;
num++;
}
}
if (!flag)
break;
}
return pii(min(from[]+from[]+can, n), m-rest);
} void work()
{
pii one, two;
one = workOne();
two = workTwo();
if (one.first > two.first)
printf("%d %d\n", one.first, one.second);
else if (one.first == two.first)
printf("%d %d\n", one.first, min(one.second, two.second));
else
printf("%d %d\n", two.first, two.second);
} int main()
{
//freopen("test.in", "r", stdin);
int T;
scanf("%d", &T);
for (int times = ; times <= T; ++times)
{
input();
printf("Case %d: ", times);
work();
}
return ;
}
ACM学习历程—HDU4415 Assassin’s Creed(贪心)的更多相关文章
- HDU-4415 Assassin’s Creed 贪心
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4415 用贪心来解,开始分为两个集合的方法错了,没有考虑之间的相互影响,正确的姿势应该是这样的,分两种情 ...
- ACM学习历程——NOJ1113 Game I(贪心 || 线段树)
Description 尼克发明了这样一个游戏:在一个坐标轴上,有一些圆,这些圆的圆心都在x轴上,现在给定一个x轴上的点,保证该点没有在这些圆内(以及圆上),尼克可以以这个点为圆心做任意大小的圆,他想 ...
- ACM学习历程—HDU5422 Rikka with Graph(贪心)
Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...
- ACM学习历程—CSU 1216 异或最大值(xor && 贪心 && 字典树)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1216 题目大意是给了n个数,然后取出两个数,使得xor值最大. 首先暴力枚举是C(n, ...
- ACM学习历程—HihoCoder1309任务分配(排序 && 贪心)
http://hihocoder.com/problemset/problem/1309 题目大意是给定n个任务的起始时间,求问最少需要多少台机器. 有一个贪心的策略就是,如果说对于一个任务结束,必然 ...
- ACM学习历程—FZU 2144 Shooting Game(计算几何 && 贪心 && 排序)
Description Fat brother and Maze are playing a kind of special (hentai) game in the playground. (May ...
- ACM学习历程—HDU 4726 Kia's Calculation( 贪心&&计数排序)
DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so carel ...
- ACM学习历程——HDU 5014 Number Sequence (贪心)(2014西安网赛)
Description There is a special number sequence which has n+1 integers. For each number in sequence, ...
- ACM学习历程——POJ 1700 Crossing River(贪心)
Description A group of N people wishes to go across a river with only one boat, which can at most ca ...
随机推荐
- vptr
#include <stdio.h> class Point3d { public: virtual ~Point3d(){} public: static Point3d origin; ...
- zoj 3721 Final Exam Arrangement【贪心】
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3721 来源:http://acm.hust.edu.cn/vjudg ...
- IntelliJ IDEA 添加类注释模板
效果展示 /** * Created with IntelliJ IDEA * USER:jacun * CLASSNAME: HalloWorldController * DATE: 2019/1/ ...
- 如何让Jackson JSON生成的数据包含的中文以unicode方式编码
我们都知道,Jackson JSON以高速.方便和灵活著称.之前的文章中介绍过使用注解的形式来规定如何将一个对象序列化成JSON的方法,以及如何将一个JSON数据反序列化到一个对象上.但是美中不足的一 ...
- python 删除文件中指定行
代码适用情况:xml文件,循环出现某几行,根据这几行中的某个字段删掉这几行这段代码的作用删除jenkins中config.xml中在自动生成pipline报错的时的回滚 start = '<se ...
- Google员工自述:在哈佛教书和在Google工作的差别
感谢伯乐在线的投递编者按:2003年到2010年期间,原文作者Matt Welsh 是哈佛大学工程和应用科学学院的计算机科学系教授.2010年加入Google,是一名高级工程师.他当前的工作重点是广域 ...
- 转的es6 =>函数
原文地址 箭头函数=>无疑是ES6中最受关注的一个新特性了,通过它可以简写 function 函数表达式,你也可以在各种提及箭头函数的地方看到这样的观点--"=> 就是一个新的 ...
- htmlParser的使用-链接
基于htmlparser实现网页内容解析:http://www.cnblogs.com/coding-hundredOfYears/archive/2012/12/15/2819217.html ht ...
- eDocEngine_3.0.4.273的手动安装
1.安装FastReport 5: 2.编译Shared3(如对Delphi2007,打开gtSharedD11.groupproj项目文件),将产生的bpl.dcp文件分别拷贝到C:\Users\P ...
- unigui中TUniDBEdit的OnEndDrag问题
非常奇怪,unigui中TUniDBEdit未发布OnEndDrag属性,包括其子类:TUniDBNumberEdit.TUniDBFormattedNumberEdit.而其他数据感知组件都有OnE ...