hdu6563 Strength(贪心)

Problem Description

Strength gives you the confidence within yourself to overcome any fears, challenges or doubts. Feel the fear and do it anyway! If you have been going through a rough time and feel burnt out or stressed, the Strength card encourages you to find the strength within yourself and keep going. You have got what it takes to see this situation through to its eventual end. You might also feel compelled to hold space for someone else who is going through a difficult period and needs your strength and support.

Alice and Bob are playing “Yu-Gi-Oh!”, a famous turn-based trading card game, in which two players perform their turns alternatively. After several turns, Alice and Bob have many monsters respectively.
Alice has n and Bob has m monsters under their own control. Each monster’s strength is measured by a non-negative integer si . To be specific, the larger si is, the more power the monster has.
During each turn, for every single monster under control, the player can give a command to it at most once, driving it to battle with an enemy monster (given that opposite player has no monsters as a shield, the monster can directly attack him).
Additionally, the process of the battle is also quite simple. When two monsters battle with each other, the stronger one (i.e. the one with larger si) will overwhelm the other and destroy it and the winner’s strength will remain unchanged. Meanwhile, the difference of their strength will produce equivalent damage to the player who loses the battle. If the player is directly attacked by a monster, he will suffer from the damage equal to the monster’s strength. Notice that when two monsters have the same strength, both of them will vanish and no damage will be dealt.
Right now it is Alice’s turn to play, having known the strength of all monsters, she wants to calculate the maximal damage she can deal towards Bob in one turn. Unfortunately, Bob has great foresight and is well-prepared for the upcoming attack. Bob has converted several of his monsters into defense position,
in which even if the monster is destroyed, he wouldn’t get any damage.
Now you are informed of the strength of all the monsters and whether it is in defense position for each Bob’s monster, you are expected to figure out the maximal damage that could be dealt in this turn.

Input

The first line contains a single integer T ≤ 20 indicating the number of test cases.
For each test case, the first line includes two integer 0 ≤ n, m ≤ 100000, representing the number of monsters owned by Alice and Bob.
In next three lines, the first two lines include n and m integers 0 ≤ si ≤ 109 indicating the strength of the i-th monster, separated by spaces. The last line contains m integers 0 or 1 indicating the position of Bob’s i-th monsters.In other words, 0 represents the normal position and 1 represents the defense position.

Output

For the ith test, output a single line in beginning of “Case i:”, followed by an integer indicating the answer, separated by a single space.

Sample Input

2 4 2 10 10 10 20 5 15 0 1 4 2 10 10 10 20 5 25 0 1

Sample Output

Case 1: 25 Case 2: 15

题意:

alice有n个怪,攻击力分别为a[i],bob有m个怪,攻击力分别为b[i],现在轮到alice打bob,bob的怪分为普通状态和防守状态,alice打bob普通状态的怪会对bob造成a[i] - b[i]的伤害(a[i] > b[i]),打防守状态的怪不会对bob造成多余的伤害,一旦a[i] > b[i],bob的这只怪就会死亡且消失。如果bob已经没有怪了,alice剩下的所有怪都可以对bob造成伤害。问最大可以对bob造成多少伤害。

思路:

分两种方法求解,取最大值。

(1)不打防守状态的怪,只用和普通状态怪的攻击力差值去伤害bob

(2)先打防守,再打普通,再直接攻击bob

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;

int a[N], b[N], op[N], x[N], y[N];
bool vis[N];

int main() {
    int t, n, m, kcase = 0;
    scanf("%d", &t);
    while(t--) {
        scanf("%d%d", &n, &m);
        int tot1 = 0, tot2 = 0;
        for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
        for(int i = 1; i <= m; ++i) scanf("%d", &b[i]);
        for(int i = 1; i <= m; ++i) {
            scanf("%d", &op[i]);
            if(op[i] == 0) x[++tot1] = b[i];
            else y[++tot2] = b[i];
        }
        sort(a + 1, a + n + 1, greater<int>());
        sort(x + 1, x + tot1 + 1);
        sort(y + 1, y + tot2 + 1);
        ll ans1 = 0, ans2 = 0;
        for(int i = 1; i <= n && i <= tot1; ++i) {
            if(a[i] <= x[i]) break;
            ans1 += a[i] - x[i];
        }
        memset(vis, 0, sizeof(vis));
        sort(a + 1, a + n + 1);
        int it = lower_bound(a + 1, a + n + 1, y[1]) - a;
        int i;
        for(i = 1; i <= tot2; ++i) {
            while(it <= n && a[it] < y[i]) it++;
            if(it == n + 1) break;
            vis[it] = 1;
            it++;
        }
        bool flag = 0;
        if(i > tot2) flag = 1;
        for(int i = n; i >= 1; --i) {
            if(a[i] >= x[1] && !vis[i]) {
                it = i;
                break;
            }
        }
        for(i = 1; i <= tot1; ++i) {
            while(it >= 1 && !(a[it] >= x[i] && !vis[it])) it++;
            if(it == 1) break;
            vis[it] = 1;
            ans2 += a[it] - x[i];
        }
        if(i >= tot1 && flag) {
            for(int j = 1; j <= n; ++j) {
                if(!vis[j])
                    ans2 += a[j];
            }
        }
//        cout<<ans1<<' '<<ans2<<'\n';
        printf("Case %d: %d\n", ++kcase, max(ans1, ans2));
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值