20180717 H. Fight Against Monsters

The 2018 ACM-ICPC Chinese Collegiate Programming Contest

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

#include<stdio.h>
#include<stdlib.h>

//开始算的hp/atk并排序,但一直过不了
//后面算次数/atk并排序,才过了。,不知道有什么区别

typedef struct li
{
    long long hp;
    long long atk;
    double rate;
    long long con;
} li;
li sz[100010];
int cmp(const void *a,const void *b)
{
    li *aa=(li*)a;
    li *bb=(li*)b;
    if(aa->rate!=bb->rate)
        return ((aa->rate)>(bb->rate)?1:-1);
    else
        return (aa->atk)>(bb->atk)?-1:1;

}


int main()
{
    int n,m;
    long long total;
    long long cont;
    scanf("%d",&n);
    for(int k=1; k<=n; k++)
    {
        total=0;
        scanf("%d",&m);
        for(int i=0; i<m; i++)
        {
            scanf("%lld %lld",&sz[i].hp,&sz[i].atk);

        }

        for(int i=0; i<m; i++)
        {
            long long  myatk;
            myatk=1;
            cont=0;

            while(sz[i].hp>0)
            {

                sz[i].hp=sz[i].hp-myatk;
                myatk++;
                cont++;

            }
            sz[i].con=cont;
            sz[i].rate=cont/(double)sz[i].atk;
        }

        // printf("sss\n");
        qsort(sz,m,sizeof(sz[0]),cmp);
        /*
                for(int i=0; i<m; i++)
                    printf("%.2f %.2f %.2f\n",sz[i].hp,sz[i].atk,sz[i].rate);
        */
        cont =0;
        for(int i=0; i<m; i++)
        {
            cont =cont+sz[i].con;
            total=total+cont*sz[i].atk;
        }

        printf("Case #%d: %lld\n",k,total);


    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/z20172123/article/details/81084735