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 ???, and its attack value was ????.

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 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 integersn (1≤n≤105) which is the number of monsters.

The ?i-th line of the following ? lines contains two integers ???HPi and ???? (1≤???,????≤105) which describe a monster.

We guarantee that the sum of ? in all test cases is up to 1e6.

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.

INPUT
2
3
1 1
2 2
3 3
3
3 1
2 2
1 3
OUTPUT
Case #1: 19
Case #2: 14

题意:

现在有 n 只怪兽,每只怪兽有一个体力值 HPi 和一个攻击值 ATKi。英雄需要同时和这 n 只怪兽进行战斗。
在每一秒,首先英雄会被当前未被打倒的所有怪兽攻击,受到与这些怪兽的攻击值之和等量的伤害。
然后他要选择一只未被打倒的怪兽进行攻击。对同一只怪物进行的第 i 次攻击能对其造成 i 点伤害。 当怪兽的体力值 ≤ 0 的时候就会倒下,当所有怪兽都被打倒时战斗立即结束。 英雄需要合理地进行攻击以使战斗过程中受到的伤害之和最小,请你求出这个伤害总量。

思路:贪心,每次打最实惠的怪兽,也就是所花时间少共计还高的怪兽,因此我们计算出打每个怪兽所需要的时间,按照时间/攻击的顺序从小到大排列;(注意里面的整型数据最好用long long)

 AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int mod=1e9+7;
const int M=1e5 + 10;
struct A
{
    ll x;//血量
    ll y;//攻击力
    double z;
    ll time;//需要打几次打倒
} a[M];
ll cmp(struct A p,struct A q)
{
    if(p.z==q.z)
        return p.y>q.y;
    return p.z>q.z;
}
ll solve(ll xx){//计算需要该怪兽打几次
    ll q,ans=0,i;
    for(i=1;;i++)
    {
        ans+=i;
        if(ans>=xx){
            q=i;
            break;
        }
    }
    return q;
}
int main()
{
    ll T,n;
    scanf("%lld",&T);
    ll i=2,j,ss=0;
    for(ll kk=1; kk<=T; kk++)
    {
        ss=0;
        scanf("%lld",&n);
        for(j=1; j<=n; j++)
        {
            scanf("%lld %lld",&a[j].x,&a[j].y);
            a[j].time=solve(a[j].x);
            a[j].z=a[j].y*1.0/a[j].time;
            ss+=a[j].y;//刚开始的总伤害
        }
        sort(a+1,a+n+1,cmp);
        ll ans=0;
        for(j=1; j<=n; j++)
        {
            ans+=ss*a[j].time;
            ss-=a[j].y;//打败一个就少一个怪兽的伤害
        }
        printf("Case #%lld: %lld\n",kk,ans);
    }
    return 0;
}
发布了350 篇原创文章 · 获赞 715 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/ZCY19990813/article/details/99713169