codeforces gym102222 Fight Against Monsters 贪心

http://codeforces.com/gym/102222/problem/H
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 103.

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 106.

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.

Example
inputCopy
2
3
1 1
2 2
3 3
3
3 1
2 2
1 3
outputCopy
Case #1: 19
Case #2: 14
题目大意:给出 n n n个怪兽的生命值和攻击,你每秒钟会受到所有存活的怪物的攻击,同时你可以选择一个怪物攻击,攻击同一个怪物的攻击力分别为 1 , 2 , 3 … … 1,2,3…… 1,2,3,问杀死所有怪物所受的最低伤害。
思路:首先肯定要把一只怪物打死,那么不妨计算出杀死每只怪物所需要的次数,设其为 t i m e time time,设怪物攻击为 a t k atk atk,假设现在有两只怪物,那么现在有 t i m e a , a t k a , t i m e b , a t k b time_{a},atk_{a},time_{b},atk_{b} timeaatkatimebatkb,设其余所有怪物的攻击力之和为 s u m sum sum,先杀怪物 a a a的答案为: s u m ∗ t i m e a + ( s u m − a t k a ) ∗ t i m e b sum*time_{a}+(sum-atk_{a})*time_{b} sumtimea+(sumatka)timeb,先杀怪物 b b b的答案为: s u m ∗ t i m e b + ( s u m − a t k b ) ∗ t i m e a sum*time_{b}+(sum-atk_{b})*time_{a} sumtimeb+(sumatkb)timea,假设先杀怪物a更优,那么将上述式子化简可得: t i m e a ∗ a t k b &lt; t i m e b ∗ a t k a time_{a}*atk_{b}&lt;time_{b}*atk_{a} timeaatkb<timebatka,且这个关系是可以传递的,那么我们就得到了贪心的写法了。

#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
#include<cmath>
using namespace std;
typedef long long ll;

const int maxn=1e6+5;

struct node
{
	ll hp,atk,time;
}a[maxn];

bool cmp(node a,node b)
{
	return a.time*b.atk<a.atk*b.time;
}

int n,t,times;

int main()
{
	scanf("%d",&t);
	while(t--)
	{
		ll tot=0;
		scanf("%d",&n);
		for(int i=0;i<n;i++)
		{
			scanf("%lld%lld",&a[i].hp,&a[i].atk);
			tot+=a[i].atk;
			ll tmp=1;
			while(tmp*(tmp+1)/2<a[i].hp)
				tmp++;
			a[i].time=tmp;
		}
		sort(a,a+n,cmp);
		ll ans=0;
		for(int i=0;i<n;i++)
		{
			ans+=tot*a[i].time;
			tot-=a[i].atk;
		}
		printf("Case #%d: %lld\n",++times,ans);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值