It is my great honour to introduce myself to you here. My name is Aloysius Benjy Cobweb Dartagnan Egbert Felix Gaspar Humbert Ignatius Jayden Kasper Leroy Maximilian. As a storyteller, today I decide to tell you and others a story about the hero Huriyyah, and the monsters.
Once upon a time, citizens in the city were suffering from n powerful monsters. They ate small children who went out alone and even killed innocent persons. Before the hero appeared, the apprehension had overwhelmed the people for several decades. For the good of these unfortunate citizens, Huriyyah set off to the forest which was the main lair of monsters and fought with n fierce and cruel monsters. The health point of the i-th monster was HPi , and its attack value was ATKi .
They fought in a cave through a turn-based battle. During each second, the hero Huriyyah was attacked by monsters at first, and the damage was the sum of attack values of all alive monsters. Then he selected a monster and attacked it. The monster would suffer the damage of k (its health point would decrease by k) which was the times of attacks it had been came under. That is to say, for each monster, the damage of the first time that Huriyyah attacked it was 1, and the damage of Huriyyah’s second attack to this monster was 2, the third time to this monster was 3, and so on. If at some time, the health point of a monster was less than or equal to zero, it died. The hero won if all monsters were killed.
Now, my smart audience, can you calculate the minimum amount of total damages our hero should suffer before he won the battle?
Input
The input contains several test cases, and the first line is a positive integer T indicating the number of test cases which is up to 10^3 .
For each test case, the first line contains an integers n (1 ≤ n ≤ 105 ) which is the number of monsters. The i-th line of the following n lines contains two integers HPi and ATKi (1 ≤ HPi , ATKi ≤ 105 ) which describe a monster. We guarantee that the sum of n in all test cases is up to 10^6 .
Output
For each test case, output a line containing Case #x: y, where x is the test case number starting from 1, and y is the minimum amount of total damages the hero should suffer.
样例输入
2 3 1 1 2 2 3 3 3 3 1 2 2 1 3
样例输出
Case #1: 19 Case #2: 14
题意:
有 n 个怪物 , 每个怪物都有v的生命值 和 w 的攻击值,现在有个英雄要为民除害 ,杀掉这些怪物。
英雄对每个怪物的伤害值都是从1 开始依次加一(比如英雄第一次伤害A , 伤害值是1 , 第二次是2 ……, 然后伤害B 是1 ,再次伤害B 是2……)英雄每次会受到所有活着的怪物的攻击值总和。
问英雄受到的最小攻击值是多少?
思路:
由怪物的生命值可以得到英雄杀死该怪物需要的次数 ,按照这个怪物的攻击值 / 怪物被杀的次数 从大到小排序贪心,计算总的伤害值即可
注意:
1. 记得开long long
2. 比值排序有精度误差 , 改成这样: a/b > c/d= a*d > b*c
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <string> 5 #include <math.h> 6 #include <algorithm> 7 #include <vector> 8 #include <queue> 9 #include <set> 10 #include <stack> 11 #include <map> 12 #include <math.h> 13 const int INF=0x3f3f3f3f; 14 typedef long long LL; 15 const int mod=1e9+7; 16 const int maxn=1e5+10; 17 using namespace std; 18 19 struct E_node 20 { 21 int HP; 22 int ATK; 23 int num; 24 }E[maxn]; 25 26 bool cmp(E_node a,E_node b) 27 { 28 return a.ATK*b.num>b.ATK*a.num; 29 } 30 31 int main() 32 { 33 int T; 34 scanf("%d",&T); 35 for(int k=1;k<=T;k++) 36 { 37 int n; 38 scanf("%d",&n); 39 LL all_ATK=0; 40 for(int i=1;i<=n;i++) 41 { 42 scanf("%d %d",&E[i].HP,&E[i].ATK); 43 all_ATK+=E[i].ATK; 44 int sum=0; 45 int j; 46 for(j=1;sum<E[i].HP;j++) 47 { 48 sum+=j; 49 } 50 E[i].num=j-1; 51 } 52 sort(E+1,E+1+n,cmp); 53 LL ans=0; 54 for(int i=1;i<=n;i++) 55 { 56 ans+=all_ATK*E[i].num; 57 all_ATK-=E[i].ATK; 58 } 59 printf("Case #%d: %lld\n",k,ans); 60 } 61 return 0; 62 }
原文地址:https://www.cnblogs.com/jiamian/p/11438812.html