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 was HP_iHPi​, and its attack value was ATK_iATKi​.

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 Format

The input contains several test cases, and the first line is a positive integer TT indicating the number of test cases which is up to 10^3103.

For each test case, the first line contains an integers n~(1\le n\le 10^5)n (1≤n≤105) which is the number of monsters. The ii-th line of the following nn lines contains two integers HP_iHPi​ and ATK_i~(1\le HP_i, ATK_i\le 10^5)ATKi​ (1≤HPi​,ATKi​≤105) which describe a monster.

We guarantee that the sum of nn in all test cases is up to 10^6106.

Output Format

For each test case, output a line containing Case #x: y, where xx is the test case number starting from 11, and yy 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 只怪兽,每只怪兽有一个体力值 HPi 和一个攻击值 ATKi。
英雄需要同时和这 n 只怪兽进行战斗。
在每一秒,首先英雄会被当前未被打倒的所有怪兽攻击,受到与这些怪兽的攻击值之和等量的伤害。
然后他要选择一只未被打倒的怪兽进行攻击。对同一只怪物进行的第 i 次攻击能对其造成 i 点伤害。 当怪兽的体力值 ≤ 0 的时候就会倒下,当所有怪兽都被打倒时战斗立即结束。 英雄需要合理地进行攻击以使战斗过程中受到的伤害之和最小,请你求出这个伤害总量。 输入格式 第一行包含一个整数 T,表示有 T 组测试数据。
接下来依次描述 T 组测试数据。对于每组测试数据:
第一行包含一个整数 n,表示怪兽的数量。
接下来 n 行,每行包含两个整数 HPi,ATKi,表示每只怪兽的体力值和攻击值。 输出格式 对于每组测试数据,输出一行信息 "Case #x: y"(不含引号),其中 x 表示这是第 x 组测试数据, y 表示战斗过程中受到的伤害之和的最小值。
分析

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

代码:

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int t,n;
struct node
{
    long long hp;
    long long atk;
    long long time;
    double bi;
}a[100005];
long long solve(int x)
{
    long long q,ans=0;
    for(int i=1;;i++)
    {
       ans=ans+i;
       if(ans>=x)
       {
           q=i;
           break;
       }
    }
    return q;
}
bool cmp(const node &b,const node &c)
{
    if(b.bi!=c.bi) return b.bi<c.bi;
    else  return b.atk>c.atk;
}
int main()
{
    scanf("%d",&t);
    int i,casee=0,j;
    while(t--)
    {
        memset(a,0,sizeof(a));
        scanf("%d",&n);
        for(i=0;i<n;i++)
            {
                scanf("%lld %lld",&a[i].hp,&a[i].atk);
                a[i].time=solve(a[i].hp);
                a[i].bi=a[i].time*1.0/a[i].atk;
            }
            sort(a,a+n,cmp);
            /*for(i=0;i<n;i++)
                {
                    for(j=i;j<n;j++)
                       {
                          dis[i]+=a[j].atk;
                       }
                       dis[i]*=a[i].time;
                }*/
            long long sum=0;
            /*for(i=0;i<n;i++)
                sum+=a[i].atk;*///总攻击
            long long ans=0;
            for(i=n-1;i>=0;i--)
            {
               sum=sum+a[i].atk;
               ans=ans+sum*a[i].time;
            }
           /* for(i=0;i<n;i++)
            {
                printf("%lld %lld %lld %lf\n",a[i].hp,a[i].atk,a[i].time,a[i].bi);
            }*/
            printf("Case #%d: %lld\n",++casee,ans);
    }
    return 0;
}
 

猜你喜欢

转载自blog.csdn.net/stdio_xuege/article/details/81099079