HDU 4310 Hero (贪心算法)
Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
There are two key attributes for the heroes in the game, health point (HP) and damage per shot (DPS). Your hero has almost infinite HP, but only 1 DPS.
To simplify the problem, we assume the game is turn-based, but not real-time. In each round, you can choose one enemy hero to attack, and his HP will decrease by 1. While at the same time, all the lived enemy heroes will attack you, and your HP will decrease by the sum of their DPS. If one hero's HP fall equal to (or below) zero, he will die after this round, and cannot attack you in the following rounds.
Although your hero is undefeated, you want to choose best strategy to kill all the enemy heroes with minimum HP loss.
Input
Output
Sample Input
10 2
2
100 1
1 100
Sample Output
201
分析:用贪心解决。血量少攻击高的要先消灭,所以以HP/DPS作为参数排序,即将所有的敌人
根据DPS/HP从大到小排序,如果相等,则按HP从小到大排序。记得要把测试样例删了,
忘了删测试样例WA了一次之后一血没了- -。
#include <iostream>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
struct sa
{
int hp,dps;
double c;
}data[];
bool cmp(sa x,sa y)
{
return x.c>y.c;
}
int main()
{
int i,n,t,sum,ans;
while(cin>>n)
{
sum=;ans=;
memset(data,,sizeof(data));
for(i=;i<n;i++)
{
cin>>data[i].dps>>data[i].hp;
data[i].c=double(data[i].dps)/double(data[i].hp);
sum+=data[i].dps;
}
sort(data,data+n,cmp);
for(i=;i<n;i++)
{
ans+=data[i].hp*sum;
sum-=data[i].dps;
//cout<<ans<<endl;
}
cout<<ans<<endl;
}
return ;
}
HDU 4310 Hero (贪心算法)的更多相关文章
- HDU 4310 Hero (贪心)
题意:给定你有 n 个敌人,你的伤害是 1,给出每个敌人的伤害,和敌人的血量,每一回合你可以攻击一个敌人,并且所有敌人都会攻击你,除非它已经死了,问你最少要多少要消耗多少血量. 析:一个很明显的贪心问 ...
- hdu 4310 Hero
这道题是道算是一道很简单的贪心题了,但是要注意排序的依据,这道题是按照dps/hp的从大到小排序的,然后计算总的sumhp即可. #include"iostream" #inclu ...
- hdu 1789 Doing HomeWork Again (贪心算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789 /*Doing Homework again Time Limit: 1000/1000 MS ...
- 『嗨威说』算法设计与分析 - 贪心算法思想小结(HDU 2088 Box of Bricks)
本文索引目录: 一.贪心算法的基本思想以及个人理解 二.汽车加油问题的贪心选择性质 三.一道贪心算法题点拨升华贪心思想 四.结对编程情况 一.贪心算法的基本思想以及个人理解: 1.1 基本概念: 首先 ...
- HDU 4726 Kia's Calculation (贪心算法)
Kia's Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 1009 FatMouse' Trade (贪心算法)
题意:就是老鼠要用猫粮换粮食,第i个房间一些东西,要用东西去换,可以不全换.问给定的猫粮最多能换多少粮食. 析:贪心算法.我们先算出来每个房间物品的平均价格是多少,肯定越低越好,并且如果能全换就全换, ...
- hdu 2037简单贪心--活动安排问题
活动安排问题就是要在所给的活动集合中选出最大的相容活动子集合,是可以用贪心算法有效求解的很好例子.该问题要求高效地安排一系列争用某一公共资源的活动.贪心算法提供了一个简单.漂亮的方法使得尽可能多的活动 ...
- HDU2037 今年暑假不AC 贪心算法
贪心算法 : 贪心算法就是只考虑眼前最优解而忽略整体的算法, 它所做出的仅是在某种意义上的局部最优解, 然后通过迭代的方法相继求出整体最优解. 但是不是所有问题都可以得到整体最优解, 所以选择贪心策略 ...
- 贪心算法(Greedy Algorithm)
参考: 五大常用算法之三:贪心算法 算法系列:贪心算法 贪心算法详解 从零开始学贪心算法 一.基本概念: 所谓贪心算法是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以 ...
随机推荐
- SQL Server中,Numric,Decimal,Money三种字段类型的区别
都是精确数据类型, 前两个可以自己定义长度和小数位数, Money的定义相当于Numric(19,4) numeric(10,2) 表示最大可以放10位数,但这10位数里有2位是小数如: 123456 ...
- Linux Kernel中获取当前目录方法(undone)
目录 . 引言 . 基于进程内存镜像信息struct mm_struct获取struct path调用d_path()获取当前进程的"绝对路径" . 基于文件描述符(fd).tas ...
- KPROCESS IDT PEB Ldr 《寒江独钓》内核学习笔记(3)
继续上一篇(2)未完成的研究,我们接下来学习 KPROCESS这个数据结构. 1. 相关阅读材料 <深入理解计算机系统(原书第2版)> 二. KPROCESS KPROCESS,也叫内核进 ...
- POJ3267 The Cow Lexicon(DP+删词)
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9041 Accepted: 4293 D ...
- Java web小记
1.Java Web设置页面刷新的方法(两种): response.setHeader("refresh", "0.3," + request.getHeade ...
- CSS小记
1.元素居中 (1)水平居中:指定宽度,然后margin auto 即可 .middle{ max-width:400px; //width:400px;//当浏览器被缩小,宽度小于元素宽度时,元素会 ...
- hdu 1013 Digital Roots
#include <stdio.h> int main(void) { int m,i;char n[10000]; while(scanf("%s",&n)= ...
- DedeCMS全版本通杀SQL注入漏洞利用代码
EXP: Exp:plus/recommend.php?action=&aid=1&_FILES[type][tmp_name]=\' or mid=@`\'` /*!50000u ...
- mysql 索引 详解
索引是快速搜索的关键.MySQL索引的建立对于MySQL的高效运行是很重要的.下面介绍几种常见的MySQL索引类型. 在数据库表中,对字段建立索引可以大大提高查询速度.假如我们创建了一个 mytabl ...
- WPF 屏蔽Alt+F4强制退出
if (e.KeyStates == Keyboard.GetKeyStates(Key.F4) && Keyboard.Modifiers == ModifierKeys.Alt) ...