HDU 4415 Assassin's Creed(贪心)
pid=4415">HDU 4415
题意:
壮哉我Assassin!
E叔有一柄耐久度为m的袖剑,以及n个目标士兵要去解决。
每解决掉一个士兵,消耗袖剑Ai的耐久度。且获得该士兵的武器,能够使用该武器解决Bi名其它士兵。
E叔要尽可能地消耗更少耐久度解决很多其它的敌人,求最小消耗与最大杀敌数。
思路:
我们把士兵分为两个集合:e1与e2。e1的士兵 Bi = 0 。 e2 的 Bi > 0.
我们发现。假设能解决e2的随意一个,e2就可以全灭,那么我们对e2依据消耗进行升序排序,消灭e2集合的消耗即为排序后的第一个士兵的Ai。
其次。要尽可能多地解决e1的士兵,则相同对e1依据消耗进行升序排序,杀到耐久度小于下一士兵Ai为止;
乍一看,最优解就是先全灭e2再用剩下的耐久度消灭e1较小Ai的敌人,用e2剩下的Bi消耗e1较大Ai的敌人;
假设不能全灭e2,则用全部耐久度去解决e1的敌人。
这样做的确非常优了。但还不是最优。考虑这组例子:
1
4 5
2 1
3 1
5 0
100 0
依照刚才的贪心策略,先解决Bi > 0 的前两个士兵(用袖剑杀死士兵1,用士兵1的武器杀死士兵2),消耗2耐久度。然后用士兵2的武器解决Ai最大的士兵4。士兵3由于耐久度不够则无法解决,答案是3 2。
可是我们能够先用袖剑杀死士兵1与士兵2。用这两个士兵的武器去解决士兵3,4,明显最优解为4 5。
于是我们对之前贪心策略做出改进:
设用袖剑杀死一个 Bi = 0 的士兵1消耗为a。设用士兵武器杀死一个在 Bi > 0 的集合里的士兵2。士兵2的最小消耗为b;
假设a < b,则用袖剑解决士兵1,用士兵武器解决士兵2;
否则。则用袖剑解决士兵2。保留士兵武器解决更大消耗的士兵(可能是士兵1)。
换而言之。就是E叔面对一个 Bi > 0 的士兵,他说:本来准备用士兵武器杀死你。如今我又不想了,我要用袖剑杀死你,由于你的消耗并不大,我要换回用以杀死你的士兵武器。去杀死消耗更大的敌人。
可能叙述还是有点不清。
。这个贪心策略看起来极其像dp,然而它真心不是dp。
。以后思路更清晰的时候再改改吧~
代码:
/*
* @author FreeWifi_novicer
* language : C++/C
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>
using namespace std;
#define clr( x , y ) memset(x,y,sizeof(x))
#define cls( x ) memset(x,0,sizeof(x))
#define mp make_pair
#define pb push_back
typedef long long lint;
typedef long long ll;
typedef long long LL;
/*Nothing is true , everything is permitted*/
const int maxn = 1e5 + 100 ;
lint n , m ;
lint kill_e1 , cost1 ;
lint ans_cost , ans_kill ;
lint e1[maxn] ;
struct em{
lint cos , kil ;
}e2[maxn] ;
bool cmp( em a , em b ){
return a.cos < b.cos ;
}
void only_kill_e1( int len1 ){
lint dur = m ;
cost1 = 0 ;
kill_e1 = 0 ;
int k = 0 ;
while( k < len1 && dur >= e1[k] ){
cost1 += e1[k] ;
kill_e1 ++ ;
dur -= e1[k++] ;
}
}
bool kill_all_e2( int len2 , int len1 ){
if( !len2 )
return false ;
lint dur = m ;
if( e2[0].cos > dur )
return false ;
dur -= e2[0].cos ;
lint sumb = 0 ;
for( int i = 0 ; i < len2 ; i++ )
sumb += e2[i].kil ;
lint rest = sumb - len2 + 1 ;
if( rest >= len1 ){//假设多余的sword能够解决掉全部b = 0的敌人,则可解决全部敌人
ans_cost = e2[0].cos ;
ans_kill = n ;
return true ;
}
else{
int i = 0 , j = 1 ; //比較e2[j].cos 与 e1[i]的大小,选择消耗耐久度小的去杀
lint tmp = len1 - rest ;
ans_cost = e2[0].cos ;
ans_kill = len2 + rest ;
while( i < tmp ){
if( dur < e1[i] && dur < e2[j].cos ) break ;
if( j < len2 && e2[j].cos <= e1[i] ){
ans_cost += e2[j].cos ;
dur -= e2[j].cos ;
tmp-- ; j++ ;
}
else{
ans_cost += e1[i] ;
dur -= e1[i] ;
i++ ;
}
ans_kill ++ ;
}
return true ;
}
}
int main(){
// freopen("input.txt","r",stdin);
int t ; cin >> t ; int kase = 1 ;
while( t-- ){
cin >> n >> m ;
ans_kill = kill_e1 = 0 ;
ans_cost = cost1 = 0 ;
lint len1 = 0 , len2 = 0 ;
for( int i = 1 ; i <= n ; i++ ){
lint a , b ;
scanf( "%I64d%I64d" , &a , &b ) ;
if( !b ){
e1[len1++] = a ;
}
else{
e2[len2].cos = a ;
e2[len2++].kil = b ;
}
}
printf( "Case %d: " , kase++ ) ;
sort( e1 , e1 + len1 ) ;
sort( e2 , e2 + len2 , cmp ) ;
only_kill_e1( len1 ) ;
if( !kill_all_e2( len2 , len1 ) ){
printf( "%I64d %I64d\n" , kill_e1 , cost1 ) ;
continue ;
}
if( ans_kill > kill_e1 ){
printf( "%I64d %I64d\n" , ans_kill , ans_cost ) ;
}
else{
if( ans_kill == kill_e1 )
printf( "%I64d %I64d\n" , ans_kill , min( ans_cost , cost1 ) ) ;
else
printf( "%I64d %I64d\n" , kill_e1 , cost1 ) ;
}
}
return 0;
}
HDU 4415 Assassin's Creed(贪心)的更多相关文章
- HDU 4415 - Assassin’s Creed
Problem Description Ezio Auditore is a great master as an assassin. Now he has prowled in the enemie ...
- HDU-4415 Assassin’s Creed 贪心
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4415 用贪心来解,开始分为两个集合的方法错了,没有考虑之间的相互影响,正确的姿势应该是这样的,分两种情 ...
- HDU 5371 Hotaru's problem(Manacher算法+贪心)
manacher算法详见 http://blog.csdn.net/u014664226/article/details/47428293 题意:给一个序列,让求其最大子序列,这个子序列由三段组成, ...
- hdu 1257 最少拦截系统(简单贪心)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1257 虽然分类是dp感觉还是贪心 比较水 #include <iostream> #inclu ...
- HDU 5783 Divide the Sequence (贪心)
Divide the Sequence 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5783 Description Alice has a seq ...
- HDU 5969 最大的位或 【贪心】 (2016年中国大学生程序设计竞赛(合肥))
最大的位或 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem De ...
- POJ 1065 Wooden Sticks / hdu 1257 最少拦截系统 DP 贪心
参考链接:http://blog.csdn.net/xiaohuan1991/article/details/6956629 (HDU 1257 解题思路一样就不继续讲解) POJ 1065题意:给你 ...
- HDU 4974 A simple water problem(贪心)
HDU 4974 A simple water problem pid=4974" target="_blank" style="">题目链接 ...
- HDU 3177 Crixalis's Equipment(贪婪)
主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=3177 Problem Description Crixalis - Sand King used t ...
随机推荐
- [Angular] Getting to Know the @Attribute Decorator in Angular
So when you using input binding in Angular, it will always check for update. If you want to improve ...
- Debian以下的ntp服务(ntpdate)的安装
/********************************************************************* * Author : Samson * Date ...
- win32 Service memory leak
https://stackoverflow.com/questions/2728578/how-to-get-phyiscal-path-of-windows-service-using-net ht ...
- Linux操作系统下Oracle主要监控工具介绍
Oracle监控包括有效且完全地监控Oracle数据库的性能.可用性和使用率等统计量,还包括即时的错误通知和纠正措施,并提供全面的报表和图表.本文中主要介绍几种Linux操作系统下Oracle主要监控 ...
- Java专业技能面试问题(不定时更新)
刚看到园友五月的仓颉<面试感悟----一名3年工作经验的程序员应该具备的技能>感觉很不错,不论是为面试跳槽准备,还是打算深化精进自己的技术都可以参考一下.面向工资编程多少也有点道理,虽然技 ...
- PHP中出现Notice: Undefined index的三种解决办法
前一段做的一个PHP程序在服务器运行正常,被别人拿到本机测试的时候总是出现“Notice: Undefined index:”这样的警告,这只是一个因为PHP版本不同而产生的警告(NOTICE或者WA ...
- 使用Jquery Ajax请求 下载压缩文件
使用第三方组件: ICSharpCode.SharpZipLib 给按钮绑定一个点击事件 后台处理: public ActionResult DownZip(string ids) { if (st ...
- BZOJ 4522 Pollard-rho+exgcd
思路: N=P*Q 求出来P和Q 模拟就好- //By SiriusRen #include <cstdio> #include <algorithm> using names ...
- week7_notebooke
回顾:类:具有相同属相和技能的一类事物对象:类的具体表现class A: country = 'China' #静态变量,静态字段 def __init__(self): #动态变量,方法 self. ...
- C# 3.0的新特性
自动属性. 之前定义属性的步骤: private filed + public property. 现在的形式:int id{get;set;}. 可以分别设置get/set的保护级别(protect ...