Description
When the playing DotA with god-like rivals and Pig-like team members, you had to face an embarrassing situation:all your tea Mmates is killed, and you had to fight 1vN.
There is the key attributes for the heroes in the game, Health Point (HP) and damage per shot (DPS). Your hero has almost infinite HP and only 1 DPS.
To simplify the problem, we assume the game was turn-based, but not real-time. In each round, you can choose one enemy hero to attack, and his HP would decrease by 1. While in the same time, all the lived enemy heroes would attack you, and your HP would decrease by the sum of their DPS. If one hero ' s HP fall equal to (or below) zero, he'll die after this round, and cannot attack your in the following round S.
Although your hero is undefeated, you want to choose best strategy to kill all the enemy heroes with minimum HP loss.
Input
The first line of all test case contains the number of enemy heroes N (1 <= n <= 20). Then N lines followed, each contains integers DPSi and HPi, which is the DPS and HP for each hero. (1 <= DPSi, HPi <= 1000)
Output
Output one line for each test, indicates the minimum HP loss.
Sample Input
110 22100 11 100
Sample Output
20201 Explanation: This question is a bit like the inside of the furnace of the turn-based attack, but this game is opposite, so we want to keep the minimum amount of blood to survive, we should put on the field dp/hp (or dp*hp) value of the largest first kill, and then slowly solve the rest of the entourage.
#include <stdio.h>#include<algorithm>using namespacestd;intN;structshow{intDP; intHP; floatsum; Show () {};//Get rid of the words below pro[] will not find the appropriate constructorShow (intIintAintb) {pro[i]. DP=A; Pro[i]. HP=b; Pro[i].sum=a*b;//Undesirable , because the attack can be taken very large, will be out of range, unless with __int64 };} pro[101];intcmp (show X,show y) {returnx.hp*y.dp<y.hp*x.dp;//If you use the Fabienne, you will encounter values equal and cannot be ranked by the number of blood, resulting in the final result is not correct (small)}intMain () {intT,i; intSum,ans; while(~SCANF ("%d",&t)) {sum= ans =0; for(i =0; i<t;i++) {scanf ("%d%d", &pro[i]. dp,&Pro[i]. HP); Sum+=Pro[i]. DP; } sort (Pro,pro+t,cmp); for(i =0; i<t;i++) {ans+=sum*Pro[i]. Gpt Sum-=Pro[i]. DP; } printf ("%d\n", ans); } return 0; }
Acm--hero (similar to greedy algorithm)