Fight Against Monsters

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 nn 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 nn fierce and cruel monsters. The health point of the ii-th monster wasiHPi, 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 kk (its health point would decrease by kk) 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 11, and the damage of Huriyyah’s second attack to this monster was 22, the third time to this monster was 33, 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 103103.
For each test case, the first line contains an integers n (1≤n≤105)n (1≤n≤10^5) which is the number of monsters.
The i-th line of the following n lines contains two integersHPi and ATKi (1≤HPi,ATKi≤10^5) which describe a monster.
We guarantee that the sum of nn 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 11, and y is the minimum amount of total damages the hero should suffer.

Example
Input
2
3
1 1
2 2
3 3
3
3 1
2 2
1 3
Output
Case #1: 19
Case #2: 14

本题题意:首先这里存活了n个怪物,在第一秒时,这n个怪物先攻击了英雄一下,然后第二秒换英雄进攻,英雄可以挑选一个怪物来打,直到打死为止(英雄每轮造成的伤害为1,2,3,4,,,)当怪物生命值小于等于零时视为怪物死亡,打倒所有怪物视为英雄胜利,要求出英雄所受的最少伤害。

本题思路:本题的关键是要找出先去弄死哪些怪物,才能使得伤害最小,首先我们想的思路是直接筛选出伤害最高的怪物先砍死,但是这个思路明显是错误的,因为可能有的怪物伤害略小于最高的伤害,但它的生命值很低,应先砍死这个怪物而不是伤害最高的那个怪物,所以我们应考虑求砍死怪物的次数与伤害的比值,比值越小的话应先砍死,就得出一个式子:(num1/atk1<num2/atk2==>num1atk2<num2atk1),由此得出最终结果。

注意为longlong 型(每个的取值都可能为1e5次方,会爆int)。

AC代码如下:

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1e5 + 5;

struct node{
	int hp;
	int atk;
	int step;
}a[maxn];

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


int step1(int m)
{
	int m1 = 1;
	int count = 0;
	while (m>0)
	{
		m -= m1;
		m1++;
	   count++;
	}
	return count;
}

int main()
{
	int t;
	cin >> t;
	int t1 = 1;
	while (t--)
	{	
		int n;
		cin >> n;
		for (int i = 0; i < n; i++)
		{
			cin >> a[i].hp >> a[i].atk;
			a[i].step = step1(a[i].hp);
		}
		sort(a, a + n, cmp);
		long long sum = 0;
		long long k = 0;
		for (int i = 0; i < n; i++)
		{
			k+= a[i].step;
			sum += k*a[i].atk;
		}
		cout << "Case #" << t1++ << ": ";
		cout << sum << endl;
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值