Codeforces - 102222H - Fight Against Monsters - 贪心

https://codeforc.es/gym/102222/problem/H
题意:有一堆怪兽,怪兽有HP和ATK。你有一个英雄,英雄每次先被所有怪兽打,然后打其中一个怪兽。打的伤害递增,第一次1,第二次2,以此类推。
为什么感觉是贪心呢?证明一波。

首先开始打一个怪兽肯定一直打到死为止。那么打死他要求的次数可以二分出来(其实暴力也可以)。两只怪兽交换打的顺序会不会变好?

先打第一只怪兽:
\(num_1*sumatk+num_2*(sumatk-atk_1)\)

先打第二只怪兽:
\(num_2*sumatk+num_1*(sumatk-atk_2)\)

那么什么情况下先打第二只会更好呢?当然是上式大于下式:
\(num_1*sumatk+num_2*(sumatk-atk_1)>num_2*sumatk+num_1*(sumatk-atk_2)\)

化简:
\(num_1*atk_2>num_2*atk_1\)

当上式成立时需要交换。

插进优先队列里面一搞,又不会爆longlong。一波搞定。

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

inline int read() {
    int x=0;
    int f=0;
    char c;
    do {
        c=getchar();
        if(c=='-')
            f=1;
    } while(c<'0'||c>'9');
    do {
        x=(x<<3)+(x<<1)+c-'0';
        c=getchar();
    } while(c>='0'&&c<='9');
    return f?-x:x;
}

inline void _write(int x) {
    if(x>9)
        _write(x/10);
    putchar(x%10+'0');
}

inline void write(int x) {
    if(x<0) {
        putchar('-');
        x=-x;
    }
    _write(x);
    putchar('\n');
}

void TestCase(int ti);

int main() {
#ifdef Yinku
    freopen("Yinku.in","r",stdin);
    //freopen("Yinku.out","w",stdout);
#endif // Yinku
    int T=read();
    for(int ti=1;ti<=T;ti++)
        TestCase(ti);
}

/*---  ---*/

inline int s1(int x){
    return ((x+1)*x)>>1;
}

struct Monster{
    int hp;
    int atk;
    int num;
    inline void calc(){
        int l=1,r=1000,m;
        while(1){
            m=l+r>>1;
            if(m==l){
                int s=s1(l);
                if(s>=hp){
                    num=l;
                    return;
                }
                else{
                    num=r;
                    return;
                }
            }
            int s=s1(m);
            if(s==hp){
                num=m;
                return;
            }
            else if(s<hp)
                l=m+1;
            else
                r=m;
        }
    }

    inline bool operator<(const Monster &m)const{
        return !(atk*m.num>=m.atk*num);
    }
}monster;

priority_queue<Monster> pq;

void TestCase(int ti) {
    ll sumatk=0;
    int n=read();
    for(int i=1;i<=n;i++){
        monster.hp=read();
        monster.atk=read();
        sumatk+=monster.atk;
        monster.calc();
        pq.push(monster);
    }
    ll sumdamage=0;
    while(!pq.empty()){
        monster=pq.top();
        pq.pop();
        sumdamage+=sumatk*monster.num;
        sumatk-=monster.atk;
    }
    printf("Case #%d: %lld\n",ti,sumdamage);
}

原文地址:https://www.cnblogs.com/Yinku/p/11037486.html

时间: 2024-11-06 17:35:09

Codeforces - 102222H - Fight Against Monsters - 贪心的相关文章

Codeforces 1296D - Fight with Monsters

题目大意: n 只怪兽,每只的血量为 h[i] ,你的攻击力为 a ,你的对手攻击力为 b 打每只怪兽时,都是你先出手,然后你的对手出手,这样轮流攻击 如果是你给予了怪兽最后一击,你就能得到一分 你还有 k 次机会能让你的对手暂停行动一回合 问你最多能拿到多少分 解题思路: 记你加上你的对手两个人各攻击一次造成的伤害为 s=a+b 贪心可得,如果想节省那 k 次机会,应该和对手一起把怪兽耗到只剩一点血,即能在最后一回合杀死的情况时 即怪兽减去的血量为 s 的倍数,表示经过了这么多回合后,怪兽的血

Codeforces 442B Andrey and Problem(贪心)

题目链接:Codeforces 442B Andrey and Problem 题目大意:Andrey有一个问题,想要朋友们为自己出一道题,现在他有n个朋友,每个朋友想出题目的概率为pi,但是他可以同时向多个人寻求帮助,不过他只能要一道题,也就是如果他向两个人寻求帮助,如果两个人都成功出题,也是不可以的. 解题思路:贪心,从概率最大的人开始考虑,如果询问他使得概率变大,则要询问. #include <cstdio> #include <cstring> #include <a

poj1827A Bunch Of Monsters(贪心)

题目链接: 啊哈哈,点我点我 题目意思: 给出n个怪物可以拿到卡片的范围和这个怪物对主人公造成的伤害..然后求最后得到怪物对主人公的最小伤害.. 思路:对怪物造成的伤害从大到小排序,然后对n个怪物进行逐一枚举,枚举时对它的时间进行逆向枚举,然后没有被访问到的将其伤害降为0,最后统计一下即可. 题目: A Bunch Of Monsters Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 938   Accepted: 35

Codeforces Round #300-Tourist&#39;s Notes(贪心)

Tourist's Notes Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level

Codeforces 432E Square Tiling(构造+贪心)

我们通常这么写 using (SqlDataReader drm = sqlComm.ExecuteReader()) { drm.Read();//以下把数据库中读出的Image流在图片框中显示出来. MemoryStream ms = new MemoryStream((byte[])drm["Logo"]); Image img = Image.FromStream(ms); this.pictureBox1.Image = img; } 我的写数据 private void b

poj 1827 A Bunch Of Monsters 贪心(并查集优化)

Description Background Jim is a brave explorer. One day, he set out for his next destination, a mysterious hill. When he arrived at the foot of the hill, he was told that there were a bunch of monsters living in that hill, and was dissuaded from cont

Codeforces #617 (Div. 3) D. Fight with Monsters(贪心,排序)

There are nn monsters standing in a row numbered from 11 to nn . The ii -th monster has hihi health points (hp). You have your attack power equal to aa hp and your opponent has his attack power equal to bb hp. You and your opponent are fighting these

2018 ACM-ICPC 宁夏 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 Huri

Codeforces Round #617 (Div. 3) D. Fight with Monsters

题意:打怪,拿分,不同就是可以让别人跳过打怪哪一个环节,不过只有k次 题解:算出每个怪消耗的k,排序 ,贪心 原文地址:https://www.cnblogs.com/RE-TLE/p/12288935.html