问题描述
When playing DotA with god-like rivals and pig-like team members, you have to face an embarrassing situation: All your teammates are killed, and you have to fight 1vN.

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.

 
输入要求
The first line of each test case contains the number of enemy heroes N (1 <= N <= 20). Then N lines followed, each contains two integers DPSi and HPi, which are the DPS and HP for each hero. (1 <= DPSi, HPi <= 1000)
 
输出要求
Output one line for each test, indicates the minimum HP loss.
 
题目大意:
题目意思大概是:每个英雄有2个属性,HP血量和DPS单次平A伤害。而你是一个可以无限掉血还不会死的bug英雄QAQ,但是你的DPS只有1。而你的n个敌人血量有限制,DPS也不一样。你每次只能打一个英雄,问:当你杀完所有敌人,最少掉多少滴血?(虽然你玩的是无敌版游戏,但你还是要讲点技术的嘛)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4310
 贪心思想: 
 
误区:先杀DPS大的enemy。
试想有敌人A:HP 1,DPS 100;敌人B:HP 100,DPS 1000;先杀A掉血:101100,先杀B掉血:110100。看吧,如果先把最吊的B干掉你反而掉更多的血。下面我讲讲一种正确的贪心思想:
一种正确贪心思想:先杀单位血量DPS高的enemy。即:先杀DPS/HP大的enemy
因为你的DPS为1,每次只能让敌人掉一滴血,你只有把这一滴血的伤害用在一滴血可以制造更高破坏的enemy身上。
然后你在攻击一个enemy时,其他enemy会攻击你。你要想想这个地方怎么处理,我提供一种思路,代码如下:
(第一次写博客,好不知所措啊,不知道讲清楚没有,望采纳,提建议)
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 25
#define eps 1e-8
using namespace std;
//用一个结构体来储存敌人的2个属性
typedef struct cw{
int dps;
int hp;
} sc;
sc enemy[N];
int cmp(sc x,sc y)
{
return 1.0*x.dps/x.hp-1.0*y.dps/y.hp<eps;//比较浮点数大小会有误差
}
int main()
{
int n;
while(~scanf("%d",&n)){
for(int i=;i<n;i++){
scanf("%d %d",&enemy[i].dps,&enemy[i].hp);
}
if(n==)printf("%d\n",enemy[].dps*enemy[].hp);
else {
sort(enemy,enemy+n,cmp);//以单位血量DPS大小排序
int sum=;
for(int i=n-;i>=;i--){
//这是你杀一个enemy时受到的伤害
sum+=enemy[i].dps*enemy[i].hp;
for(int j=;j<i;j++){
//这是其他enemy同时对你造成的伤害
sum+=enemy[j].dps*enemy[i].hp;
}
}
printf("%d\n",sum);
}
}
return ;
}
 

HDU4310HERO贪心问题的更多相关文章

  1. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  2. HDOJ 1051. Wooden Sticks 贪心 结构体排序

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  3. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]

    1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 786  Solved: 391[Submit][S ...

  5. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  6. 【BZOJ-4245】OR-XOR 按位贪心

    4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 486  Solved: 266[Submit][Sta ...

  7. code vs 1098 均分纸牌(贪心)

    1098 均分纸牌 2002年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解   题目描述 Description 有 N 堆纸牌 ...

  8. 【BZOJ1623】 [Usaco2008 Open]Cow Cars 奶牛飞车 贪心

    SB贪心,一开始还想着用二分,看了眼黄学长的blog,发现自己SB了... 最小道路=已选取的奶牛/道路总数. #include <iostream> #include <cstdi ...

  9. 【贪心】HDU 1257

    HDU 1257 最少拦截系统 题意:中文题不解释. 思路:网上有说贪心有说DP,想法就是开一个数组存每个拦截系统当前最高能拦截的导弹高度.输入每个导弹高度的时候就开始处理,遍历每一个拦截系统,一旦最 ...

随机推荐

  1. 【Unity3D】Unity3D开发《我的世界》之四、创建一个Block

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/unity_minecraft_04.html 一.新建Block类 我们的Block类用来存储跟Block相关的信 ...

  2. file和file文件流

    **io流是程序中比较常用的功能,基本上涉及到文件上传下载的都要用到这功能,比如上传头像,上传附件等等. 对于一个java程序员来说,io流也是必须掌握的,因此这里对比较常用的或者说曾经用过的方法进行 ...

  3. Java Web项目(Extjs)报错七

    1.Java Web项目(Extjs)报错七 具体报错如下:

  4. app_offline.htm的作用

    如果你要COPY站点,进行站点维护,部署,和进行大量修改,有可能要停掉你的WEB应用程序了,而以一个友好的方式提示给用户,比如什么"本网站正在更新"等等的信息可以建立一个叫app_ ...

  5. nest exception is java.sql.SQLException:ORA-01476:除数为0

    1.错误描述 nest exception is java.sql.SQLException:ORA-01476:除数为0 2.错误原因 3.解决办法

  6. web开发性能优化---安全篇

    1.权限管理 从模块.表单.数据审核.功能按钮全面数据安全验证及管理. 2.ip验证 数据接口访问进行IP校验 3.登录.操作日志.程序安全日志  系统所有用户登录.操作全部日志记录. 程序安全日志操 ...

  7. hdu2242 考研路茫茫——空调教室

    弱联通 #include<iostream> #include<cstdio> #include<cstring> #include<map> #inc ...

  8. PyCharm运行报编码错误

    运行报如下错误: SyntaxError: Non-ASCII character '\xe8' in file /home/ubuntu/code/201803091253-text.py on l ...

  9. C#图解教程 第十三章 委托

    委托 什么是委托委托概述声明委托类型创建委托对象给委托赋值组合委托为委托添加方法从委托移除方法调用委托委托示例调用带返回值的委托调用带引用参数的委托匿名方法 使用匿名方法匿名方法的语法 Lambda ...

  10. 省市区/国籍 多级联动-jq

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...