• HDU-4310 Hero 贪心问题


    题目链接:https://cn.vjudge.net/problem/HDU-4310

    题意

    打dota,队友太菜,局势变成1vN。还好你开了挂,hp无限大(攻击却只有一点每秒-_-)。
    但是你并不想被A太多下,所以问题来了
    给出对面的血量和每秒输出大小
    问怎么安排,使得打败所有人后掉血最少

    思路

    首先可以想到我们必须一个一个打,这样所有人的总输出时间最少
    因为如果打A一下,B一下,AB总输出肯定更大
    其次考虑1v2的情况,因为1vN同理可得

    现有ab两敌人
    先打a的总输出为

    [DPS_a*HP_a + DPS_b*(HP_a+HP_b) ]

    先打b的总输出为

    [DPS_b*HP_b + DPS_a*(HP_a+HP_b) ]

    差别就在 $$DPS_bHP_a , DPS_aHP_b $$

    代码

    #include <cstdio>
    #include <algorithm>
    using namespace std;
    struct Hero{
        int hp, dps;
        Hero(int hp=0, int dps=0):hp(hp),dps(dps) {}
        bool operator < (const Hero &a) const{
            return dps*a.hp>a.dps*hp;
        }
    }heroes[25];
    
    int main(void){
        int n;
        while (scanf("%d", &n)==1 && n){
            for (int i=0; i<n; i++)
                scanf("%d%d", &heroes[i].dps, &heroes[i].hp);
            sort(heroes, heroes+n);
            
            int sum=0, time=0;
            for (int i=0; i<n; i++){
                time+=heroes[i].hp;
                sum+=heroes[i].dps*time;
            }printf("%d
    ", sum);
        }
    
        return 0;        
    }
    
    
    Time Memory Length Lang Submitted
    None 1512kB 621 G++ 2018-02-08 21:55:55
  • 相关阅读:
    cookie,sessionStorage,localStorage
    存储方式与传输方式
    为什么css放在顶部而js写在后面
    常见的web安全及防护原理
    web缓存
    http协议理解
    http与https
    get/post的区别
    JZOJ 3571. 【GDKOI2014】内存分配
    JZOJ 3570. 【GDKOI2014】壕壕的寒假作业
  • 原文地址:https://www.cnblogs.com/tanglizi/p/8437204.html
Copyright © 2020-2023  润新知