The 2018 ACM-ICPC Chinese Collegiate Programming Contest - H. 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

 

解题思路:

集中攻击一只怪才有意义。设击破次数为times,按照atk / times从大到小贪心。数据类型这有个坑,数值较大,得用 long long  或是 unsigned long long才行

 

AC代码:

#include <bits/stdc++.h>
using namespace std;

struct monster
{
    int hp;
    int atk;
    int value;
}mon[100002];

bool cmp(monster a, monster b)
{
    return (double)a.atk/a.value > (double)b.atk/b.value ;
}

int main()
{
    int t;
    scanf("%d",&t);
    int c=t;
    while(t--)
    {
        int n;
        scanf("%d",&n);
        int p = n;
        while(n--)
        {
            scanf("%d %d", &mon[n].hp, &mon[n].atk);
        }

        long long i, d, times;
        unsigned long long sum = 0;
        for(i=0;i<p;i++)
        {
            d = 1;
            times = 0;
            while(mon[i].hp > 0)
            {
                mon[i].hp -= d;
                d++;
                times++;
            }
            mon[i].value = times;
        }

        sort(mon,mon+p,cmp);

        times=0;

        for(i=0;i<p;i++)
        {
            sum += mon[i].atk * (times + mon[i].value);
            times += mon[i].value;
        }

        printf("Case #%d: %llu\n",c-t,sum);
    }
    return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值