Codeforces Round #190 (Div. 2).D

一道贪心题。

可以分两种情况

1 、是没有把对面的牌全打败,那么只要用最大的可能去打攻击状态的牌。

2、 是将对面的牌全打败,那么只要保证打对面防守状态的花费最小,就可以保证最后的结果最大

两种情况下的最大值就我们要的答案.

 

D. Ciel and Duel
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Fox Ciel is playing a card game with her friend Jiro.

Jiro has n cards, each one has two attributes: position (Attack or Defense) and strength. Fox Ciel has m cards, each one has these two attributes too. It's known that position of all Ciel's cards is Attack.

Now is Ciel's battle phase, Ciel can do the following operation many times:

 

  1. Choose one of her cards X. This card mustn't be chosen before.
  2. If Jiro has no alive cards at that moment, he gets the damage equal to (X's strength). Otherwise, Ciel needs to choose one Jiro's alive card Y, then:
    • If Y's position is Attack, then (X's strength)  ≥  (Y's strength) must hold. After this attack, card Y dies, and Jiro gets the damage equal to (X's strength) - (Y's strength).
    • If Y's position is Defense, then (X's strength)  >  (Y's strength) must hold. After this attack, card Y dies, but Jiro gets no damage.

 

Ciel can end her battle phase at any moment (so, she can use not all her cards). Help the Fox to calculate the maximal sum of damage Jiro can get.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of cards Jiro and Ciel have.

Each of the next n lines contains a string position and an integer strength (0 ≤ strength ≤ 8000) — the position and strength of Jiro's current card. Position is the string "ATK" for attack, and the string "DEF" for defense.

Each of the next m lines contains an integer strength (0 ≤ strength ≤ 8000) — the strength of Ciel's current card.

Output

Output an integer: the maximal damage Jiro can get.

Sample test(s)
input
2 3
ATK 2000
DEF 1700
2500
2500
2500
output
3000
input
3 4
ATK 10
ATK 100
ATK 1000
1
11
101
1001
output
992
input
2 4
DEF 0
ATK 0
0
0
1
1
output
1
Note

In the first test case, Ciel has 3 cards with same strength. The best strategy is as follows. First she uses one of these 3 cards to attack "ATK 2000" card first, this attack destroys that card and Jiro gets 2500 - 2000 = 500 damage. Then she uses the second card to destroy the "DEF 1700" card. Jiro doesn't get damage that time. Now Jiro has no cards so she can use the third card to attack and Jiro gets2500 damage. So the answer is 500 + 2500 = 3000.

In the second test case, she should use the "1001" card to attack the "ATK 100" card, then use the "101" card to attack the "ATK 10" card. Now Ciel still has cards but she can choose to end her battle phase. The total damage equals (1001 - 100) + (101 - 10) = 992.

In the third test case note that she can destroy the "ATK 0" card by a card with strength equal to 0, but she can't destroy a "DEF 0" card with that card.

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#include <sstream>
#include <iostream>
using namespace std;
#define INF 0x3fffffff

// 抗住! 一切都会好的!

struct node
{
    int key,id;
}g[110];

int k[110];
int def[110],atk[110];

int cmp(int t,int t1)
{
    return t>t1;
}

int main()
{
    //freopen("//home//chen//Desktop//ACM//in.text","r",stdin);
    //freopen("//home//chen//Desktop//ACM//out.text","w",stdout);
    int n,m;
    scanf("%d%d",&n,&m);
    int cntd=0,cnta=0;
    int sumd=0,suma=0;
    for(int i=0;i<n;i++)
    {
        string str;
        int tmp;
        cin>>str>>tmp;
        if(str=="ATK")
        {
            suma+=tmp;
            atk[cnta++]=tmp;
            g[i].key=tmp;
            g[i].id=0;
        }
        else
        {
            sumd+=tmp;
            def[cntd++]=tmp;
            g[i].key=tmp;
            g[i].id=1;
        }
    }
    int sum=0;
    for(int i=0;i<m;i++)
    {
        cin>>k[i];
        sum+=k[i];
    }
    int mx=-1;
    //求全攻击时的最大伤害
    int tmp=0;
    sort(atk,atk+cnta);
    sort(k,k+m,cmp);
    int tend=min(m,cnta);
    for(int i=0;i<tend;i++)
    {
        if(k[i]>=atk[i])
        {
            tmp += k[i]-atk[i];
        }
        else break;
    }
    mx = max(mx,tmp);

    //////////////// 先判断 能不能把对面的全部清除掉
    int flag[110];
    memset(flag,0,sizeof(flag));
    sort(k,k+m);
    int sign=0;
    for(int i=0;i<n;i++)
    {
        int flag1=0;
        for(int j=0;j<m;j++)
        {
            if(flag[j]==1) continue;
            if(g[i].id==0)
            {
                if(k[j]>=g[i].key)
                {
                    flag1=1;
                    flag[j]=1;
                    break;
                }
            }
            else
            {
                if(k[j]>g[i].key)
                {
                    flag1=1;
                    flag[j]=1;
                    break;
                }
            }
        }
        if(flag1==0)
        {
            sign=1;
            break;
        }
    }
    if(sign==1)
    {
        printf("%d\n",mx);
        return 0;
    }
    //////////////// 求将对面全清时的最大值
    tmp = 0;
    memset(flag,0,sizeof(flag));
    sort(def,def+cntd);
    for(int i=0;i<cntd;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(flag[j]==1) continue;
            if(k[j] > def[i])
            {
                flag[j]=1;
                tmp+=k[j];
                break;
            }
        }
    }
    printf("%d\n",max(sum-tmp-suma,mx)); // 大概就是这个意思
    return 0;
}

 

posted @ 2013-06-30 17:28  chenhuan001  阅读(339)  评论(0编辑  收藏  举报