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:
  3. 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).
  4. 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 gets 2500 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.

那三个条件是。

防守卡需要数字大于它的才能击破,攻击卡需要数字大于等于它的才能击破,并能直接计算分数。如果对方没卡,直接扣血

所以思路就是分成2种情况,第一种是把对方卡全部击破,计算分数,第2种是选择击破1张,2张一直到natk张.。natk表示对方所拥有的攻击卡的数目。 因为防御卡全部击破放在第一种情况,防御卡只击破几张是没有用处的。。所以就只有这两种情况了。

不过今天写的时候。。发现个很无语的问题。。如果高手能解答下就好了。

bool cmd(struct node a,struct node b){
if((a.p>b.p)||(a.p==b.p&&a.st>b.st))
return false;
return true;
}
sort(B,B+n,cmd);

int comp(const void *a,const void *b){
if(((struct node *)a)->p>((node *)b)->p
||((node *)a)->p==((node *)b)->p&&((node *)a)->st>((node *)b)->st)
return 1;
else return -1;
}
qsort(B,n,sizeof(B[0]),comp);

这两种方法竟然是不等价的。。我很无语。。前面一种WA掉了。它竟然会把我B[1].st的大小变成0.  我真的不知道为什么。后来改成后面这种写法,就AC掉了。。

提供那组测试数据

99 100
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
ATK 8000
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001
8001

最后。。附上代码:

/*
* @author ipqhjjybj
* @date 20130712
*
*/
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
using namespace std;
struct node{
int p,st; //p=0:Atack p=1,defense
}B[2000];
int A[2000];
int flag[2000];
bool cmd(struct node a,struct node b){
if((a.p>b.p)||(a.p==b.p&&a.st>b.st))
return false;
return true;
}
int comp(const void *a,const void *b){
if(((struct node *)a)->p>((node *)b)->p
||((node *)a)->p==((node *)b)->p&&((node *)a)->st>((node *)b)->st)
return 1;
else return -1;
}
int main(){
// freopen("322D.in","r",stdin);
int i,j;
int n,m,natk;
int ans;
char s[1000];
scanf("%d %d",&n,&m);
natk=0;
for(i = 0;i<n;i++){
scanf("%s %d",s,&B[i].st);
if(s[0]=='A') B[i].p=0,natk++;
else B[i].p=1;
}
for(i = 0;i<m;i++){
scanf("%d",&A[i]);
flag[i]=0;
}
sort(A,A+m);
//sort(B,B+n,cmd);
//我不知道为什么。。这样用是错的。。
qsort(B,n,sizeof(B[0]),comp);
ans = 0;
if(n<=m){ //看下能否全部摧毁
for(i=natk,j=0;i<n && j<m;i++){
while(A[j]<=B[i].st)
j++;
if(j<m){
flag[j++]=1;
}
}
if(j<m){
for(j=i=0;i<natk&&j<m;i++){
while(A[j]<B[i].st||flag[j])
j++;
if(j<m){
flag[j]=1;
ans+=A[j++]-B[i].st;
}
}
if(j<m)
for(j=0;j<m;j++)
if(!flag[j]){
ans+=A[j];
}
} }
//部分摧毁
int temp,k;
for(k=1;k<=natk && k<=m && k <=n;k++){
temp = 0;
for(i=k-1,j=m-1;B[i].st<=A[j] && i>=0;i--,j--)
temp+=A[j]-B[i].st;
if(i<0)
ans=max(ans,temp);
}
printf("%d\n",ans);
return 0;
}

CF:322D - Ciel and Duel 贪心 或者 DP 我用的贪心 。。难道sort跟qsort是不一样的么?的更多相关文章

  1. CF 321B Ciel and Duel(费用流)

    题目链接:http://codeforces.com/problemset/problem/321/B 题意:两个人,分别有n.m张牌.每张牌有两个属性类型和能力,类型为攻击或者防守.B的m张牌的属性 ...

  2. 贪心/构造/DP 杂题选做Ⅱ

    由于换了台电脑,而我的贪心 & 构造能力依然很拉跨,所以决定再开一个坑( 前传: 贪心/构造/DP 杂题选做 u1s1 我预感还有Ⅲ(欸,这不是我在多项式Ⅱ中说过的原话吗) 24. P5912 ...

  3. ALGO-13_蓝桥杯_算法训练_拦截导弹(贪心,DP)

    问题描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国的导弹 ...

  4. 【bzoj4922】[Lydsy六月月赛]Karp-de-Chant Number 贪心+背包dp

    题目描述 给出 $n$ 个括号序列,从中选出任意个并将它们按照任意顺序连接起来,求以这种方式得到匹配括号序列的最大长度. 输入 第一行包含一个正整数n(1<=n<=300),表示括号序列的 ...

  5. 【贪心优化dp决策】bzoj1571: [Usaco2009 Open]滑雪课Ski

    还有贪心优化dp决策的操作…… Description Farmer John 想要带着 Bessie 一起在科罗拉多州一起滑雪.很不幸,Bessie滑雪技术并不精湛. Bessie了解到,在滑雪场里 ...

  6. bzoj 1907: 树的路径覆盖【贪心+树形dp】

    我是在在做网络流最小路径覆盖的时候找到这道题的 然后发现是个贪心+树形dp \( f[i] \)表示在\( i \)为根的子树中最少有几条链,\( v[i] \) 表示在\( i \)为根的子树中\( ...

  7. 贪心/构造/DP 杂题选做Ⅲ

    颓!颓!颓!(bushi 前传: 贪心/构造/DP 杂题选做 贪心/构造/DP 杂题选做Ⅱ 51. CF758E Broken Tree 讲个笑话,这道题是 11.3 模拟赛的 T2,模拟赛里那道题的 ...

  8. 贪心/构造/DP 杂题选做

    本博客将会收录一些贪心/构造的我认为较有价值的题目,这样可以有效的避免日后碰到 P7115 或者 P7915 这样的题就束手无策进而垫底的情况/dk 某些题目虽然跟贪心关系不大,但是在 CF 上有个 ...

  9. Moving Tables(贪心或Dp POJ1083)

    Moving Tables Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28304   Accepted: 9446 De ...

随机推荐

  1. POJ 3352 Road Construction(边双连通分量,桥,tarjan)

    题解转自http://blog.csdn.net/lyy289065406/article/details/6762370   文中部分思路或定义模糊,重写的红色部分为修改过的. 大致题意: 某个企业 ...

  2. noip2002提高组题解

    再次280滚粗.今天早上有点事情,所以做题的时候一直心不在焉,应该是三天以来状态最差的一次,所以这个分数也还算满意了.状态真的太重要了. 第一题:均分纸牌 贪心.(昨天看BYVoid的noip2001 ...

  3. POJ2402 Palindrome Numbers 回文数

    题目链接: http://poj.org/problem?id=2402 题目大意就是让你找到第n个回文数是什么. 第一个思路当然是一个一个地构造回文数直到找到第n个回文数为止(也许大部分人一开始都是 ...

  4. 图像、帧、片、NALU

    图像.帧.片.NALU 是学习 H.264 的人常常感到困惑的一些概念,我在这里对自己的理解做一些阐述,欢迎大家讨论: H.264 是一次概念的革新,它打破常规,完全没有 I 帧.P帧.B 帧的概念, ...

  5. linux面试题2

    1. 在Linux系统中,以文件的方式访问设备 2. Linux内核引导时,从文件 /etc/fstab 中读取要加载的文件系统 3. Linux文件系统中每个文件用inode来标识 4.  链接分为 ...

  6. T-SQL 数据库笔试题

    1.说明:创建数据库 Create DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- 创建备份数 ...

  7. web自动化框架之一介绍与环境搭建(Selenium+Eclipse+Python)

    看到一篇环境搭建文章,详细又全面,这里就不一一重复了 http://blog.csdn.net/dyllove98/article/details/9390649 其它: 1.框架介绍      整个 ...

  8. Android自带CalendarView类实现日历视图

    文章由来:学习一下日历控件的实现,Android3.0以下的版本请查阅其他文章. 本文主要是介绍Android日历视图控件CalendarView相关的内容,然后在后面会给出一个简单的Demo. Ca ...

  9. duilib入门之贴图描述、类html文本描述、动态换肤、Dll插件、资源打包

    转载自duilib入门文档 贴图描述: Duilib的表现力丰富很大程度上得益于贴图描述的简单强大.Duilib的贴图描述分为简单模式和复杂模式两种. 简单模式使用文件名做为贴图描述内容,在这种方式下 ...

  10. 基于wke封装的duilib的webkit浏览器控件,可以c++与js互交,源码及demo下载地址

    转载请说明原出处,谢谢~~ 前些日子用wke内核封装了duilib的webkit浏览器控件,好多群里朋友私聊我希望可以我公布源码,今天把这个控件的源码和使用demo公布.其实这个控件封装起来没什么难度 ...