2018 宁夏邀请赛 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 HPi, 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 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 1, and the damage of Huriyyah’s second attack to this monster was 2, the third time to this monster was 3, 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 103.For each test case, the first line contains an integers n (1≤n≤105) which is the number of monsters.
The i-th line of the following n lines contains two integers HPi and ATKi (1≤HPi,ATKi≤105) which describe a monster.We guarantee that the sum of n in all test cases is up to 106.

Output
For each test case, output a line containing Case #x: y, where x is the test case number starting from 1, and y is the minimum amount of total damages the hero should suffer.

1.观察可以发现,HP是没有办法直接去算的,需要转化为它能在英雄手中存活的回合数,即次数。构造结构体存放三个变量。输入时计算出time

2.接下来需要确定攻击的顺序,那到底怎么攻击才能让英雄受到的总伤害最小呢?首先肯定是假设直接与结构体的三个变量有关,但均被推翻。我们不难想到,每当一个怪物被选为攻击对象后,它能攻击的回合总数是包括它在内的之前所有被击杀怪物的存活回合数,造成的伤害也是直接攻击力乘以该总回合数,所以就是单位时间内攻击力高的怪物应该被优先消灭

3 重构结构体比较函数,s1/time1>s2/time2 ----> s1*time2>s2*time1

另外我下面的代码有一点疑惑,希望有dalao可以帮我解决,不胜感谢

代码

#include <cstdio>
#include <iostream>
#include<algorithm>
#include <cstring>
using namespace std;
const int N = 1e5+10;
struct node{
    int hp;
    int s; ///怪物攻击力
    int time; ///击杀需要的回合数
}a[N];
bool cmp(node &m,node &n){
    return (m.s*n.time)>(m.time*n.s);
}
int main(){
    int n,N;
    scanf("%d",&N);
    int x,y,k=1; ///k是Case数
    while(N--){
        scanf("%d",&n);
        long long sum=0;
        memset(a,0,sizeof(a));
        for(int i=0;i<n;i++){
            scanf("%d%d",&x,&y);
            a[i].hp=x;
            a[i].s=y;
            int attack=1;
            while(x>0){ ///记录HP降为0以下的次数
                a[i].time++;
                x-=attack;
                attack++;
            }
        }
        sort(a,a+n,cmp);
        ///这里是从第二个怪物开始累加上一个怪物的回合数,和下面的思想是差不多的,但是写这个就是WA
        /*for(int i=0;i<n;i++){
            if(i>=1){
                a[i].time+=a[i-1].time;
            }
            cout<<a[i].time<<a[i].s<<endl;
            sum+=a[i].time*a[i].s;
        }*/
        long long ans=0;
        for(int i=0;i<n;i++){
            ans+=a[i].time;
            sum+=ans*a[i].s;
        }
        printf("Case #%d: %lld\n",k++,sum);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值