2018 ACM-ICPC 宁夏 H.Fight Against Monsters(贪心)

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

时间: 2024-10-03 22:56:05

2018 ACM-ICPC 宁夏 H.Fight Against Monsters(贪心)的相关文章

2018 ACM/ICPC 南京 I题 Magic Potion

题解:最大流板题:增加两个源点,一个汇点.第一个源点到第二个源点连边,权为K,然后第一个源点再连其他点(英雄点)边权各为1,然后英雄和怪物之间按照所给连边(边权为1). 每个怪物连终点,边权为1: 参考代码: 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define INF 0x3f3f3f3f 4 const int maxn = 2100; 5 int n,m,k,s,t,u,v,w,num,num1; 6 struct Edge

Codeforces - 102222H - Fight Against Monsters - 贪心

https://codeforc.es/gym/102222/problem/H 题意:有一堆怪兽,怪兽有HP和ATK.你有一个英雄,英雄每次先被所有怪兽打,然后打其中一个怪兽.打的伤害递增,第一次1,第二次2,以此类推. 为什么感觉是贪心呢?证明一波. 首先开始打一个怪兽肯定一直打到死为止.那么打死他要求的次数可以二分出来(其实暴力也可以).两只怪兽交换打的顺序会不会变好? 先打第一只怪兽: \(num_1*sumatk+num_2*(sumatk-atk_1)\) 先打第二只怪兽: \(nu

HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)

Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 997    Accepted Submission(s): 306 Problem Description The empire is under attack again. The general of empire is planning to defend his

2014 ACM/ICPC Asia Regional Guangzhou Online Wang Xifeng&#39;s Little Plot HDU5024

一道好枚举+模拟题目.转换思维视角 这道题是我做的,规模不大N<=100,以为正常DFS搜索,于是傻乎乎的写了起来.各种条件限制模拟过程 但仔细一分析发现对每个点进行全部八个方向的遍历100X100X100^8 .100X100个点,每个点在走的时候8中选择,TLE 于是改为另一个角度: 以符合要求的点为拐弯点,朝两个垂直的方向走,求出最远的距离.这样只要对每个点各个方向的长度知道,组合一下对应的就OK. 避免了每个点深搜. PS:搜索的时候x,y写反了,导致构图出现问题,以后用[dy][dx]

2016 ACM/ICPC Asia Regional Shenyang Online 1007/HDU 5898 数位dp

odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 388    Accepted Submission(s): 212 Problem Description For a number,if the length of continuous odd digits is even and the length

hduoj 4710 Balls Rearrangement 2013 ACM/ICPC Asia Regional Online —— Warmup

http://acm.hdu.edu.cn/showproblem.php?pid=4710 Balls Rearrangement Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 735    Accepted Submission(s): 305 Problem Description Bob has N balls and A b

hdu 5008(2014 ACM/ICPC Asia Regional Xi&#39;an Online ) Boring String Problem(后缀数组&amp;二分)

Boring String Problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 219    Accepted Submission(s): 45 Problem Description In this problem, you are given a string s and q queries. For each que

2014 ACM/ICPC Asia Regional Xi&#39;an Online

03 hdu5009 状态转移方程很好想,dp[i] = min(dp[j]+o[j~i]^2,dp[i]) ,o[j~i]表示从j到i颜色的种数. 普通的O(n*n)是会超时的,可以想到o[]最大为sqrt(n),问题是怎么快速找到从i开始往前2种颜色.三种.四种...o[]种的位置. 离散化之后,可以边走边记录某个数最后一个出现的位置,初始为-1,而所要求的位置就等于 if(last[a[i]]==-1) 该数没有出现过,num[i][1] = i,num[i][j+1] = num[i-1

2016 ACM/ICPC Asia Regional Shenyang Online 1003/HDU 5894 数学/组合数/逆元

hannnnah_j’s Biological Test Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 802    Accepted Submission(s): 269 Problem Description hannnnah_j is a teacher in WL High school who teaches biolog