UVALive 7146 Defeat the Enemy(模拟)
2016-05-12 16:56
399 查看
思路:按攻击方的攻击力排序,防御方的防御力排序,然后对于每一个防御方肯定是在攻击方找一个攻击力比它防御力高并且防御力比防御方的攻击力稍微大一点点的最优,所以用个multiset搞一下就OK了
Long long ago there is a strong tribe living on the earth. They always have wars and eonquer others.
One day, there is another tribe become their target. The strong tribe has decide to terminate them!!!
There are m villages in the other tribe. Each village contains a troop with attack power EAttacki
,
and defense power EDefensei
. Our tribe has n troops to attack the enemy. Each troop also has the
attack power Attacki
, and defense power Defensei
. We can use at most one troop to attack one enemy
village and a troop can only be used to attack only one enemy village. Even if a troop survives an
attack, it can’t be used again in another attack.
The battle between 2 troops are really simple. The troops use their attack power to attack against
the other troop simultaneously. If a troop’s defense power is less than or equal to the other troop’s
attack power, it will be destroyed. It’s possible that both troops survive or destroy.
The main target of our tribe is to destroy all the enemy troops. Also, our tribe would like to have
most number of troops survive in this war.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case start
with 2 numbers n and m, the number of our troops and the number of enemy villages. n lines follow,
each with Attacki and Defensei
, the attack power and defense power of our troops. The next m
lines describe the enemy troops. Each line consist of EAttacki and EDefensei
, the attack power and
defense power of enemy troops
Output
For each test ease, output one line containing ‘Case #x: y’, where x is the test case number (starting
from 1) and y is the max number of survive troops of our tribe. If it‘s impossible to destroy all enemy
troops, output ‘-1’ instead.
Limits:
1 ≤ T ≤ 100,
1 ≤ n, m ≤ 105
,
1 ≤ Attacki
, Defensei
, EAttacki
, EDefensei ≤ 109
Sample Input23 25 77 31 24 42 22 13 41 105 6Sample OutputCase #1: 3Case #2: -1
#include <cstdio> #include <queue> #include <cstring> #include <iostream> #include <cstdlib> #include <algorithm> #include <vector> #include <map> #include <set> #include <ctime> #include <cmath> #include <cctype> using namespace std; const int maxn = 1e5+6; struct en { int gongji; int fangyu; }a[maxn],b[maxn]; bool cmp1(en a,en b) { return a.gongji>b.gongji; } bool cmp2(en a,en b) { return a.fangyu > b.fangyu; } int main() { int T,cas=1; scanf("%d",&T); while (T--) { int n,m; scanf("%d%d",&n,&m); for (int i = 0;i<n;i++) scanf("%d%d",&a[i].gongji,&a[i].fangyu); for (int i = 0;i<m;i++) scanf("%d%d",&b[i].gongji,&b[i].fangyu); if (n<m) { printf("Case #%d: -1\n",cas++); continue; } sort(a,a+n,cmp1); sort(b,b+m,cmp2); multiset<int>s; int cnt,pos,flag=1; pos=0; cnt=0; for (int i = 0;i<m;i++) { for (int j = pos;j<n;j++) { if (a[j].gongji>=b[i].fangyu) { s.insert(a[j].fangyu); pos++; } else break; } if (s.empty()) { flag=0; break; } else { multiset<int>::iterator it; it = s.upper_bound(b[i].gongji); if (it != s.end()) { s.erase(it); } else { s.erase(s.begin()); cnt++; } } } if (flag) printf("Case #%d: %d\n",cas++,n-cnt); else printf("Case #%d: -1\n",cas++); } }
Long long ago there is a strong tribe living on the earth. They always have wars and eonquer others.
One day, there is another tribe become their target. The strong tribe has decide to terminate them!!!
There are m villages in the other tribe. Each village contains a troop with attack power EAttacki
,
and defense power EDefensei
. Our tribe has n troops to attack the enemy. Each troop also has the
attack power Attacki
, and defense power Defensei
. We can use at most one troop to attack one enemy
village and a troop can only be used to attack only one enemy village. Even if a troop survives an
attack, it can’t be used again in another attack.
The battle between 2 troops are really simple. The troops use their attack power to attack against
the other troop simultaneously. If a troop’s defense power is less than or equal to the other troop’s
attack power, it will be destroyed. It’s possible that both troops survive or destroy.
The main target of our tribe is to destroy all the enemy troops. Also, our tribe would like to have
most number of troops survive in this war.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case start
with 2 numbers n and m, the number of our troops and the number of enemy villages. n lines follow,
each with Attacki and Defensei
, the attack power and defense power of our troops. The next m
lines describe the enemy troops. Each line consist of EAttacki and EDefensei
, the attack power and
defense power of enemy troops
Output
For each test ease, output one line containing ‘Case #x: y’, where x is the test case number (starting
from 1) and y is the max number of survive troops of our tribe. If it‘s impossible to destroy all enemy
troops, output ‘-1’ instead.
Limits:
1 ≤ T ≤ 100,
1 ≤ n, m ≤ 105
,
1 ≤ Attacki
, Defensei
, EAttacki
, EDefensei ≤ 109
Sample Input23 25 77 31 24 42 22 13 41 105 6Sample OutputCase #1: 3Case #2: -1
相关文章推荐
- 利用jsp+uploadify插件实现删除上传到ftp服务器里面的文件
- Bootstrap 静态分页 和 jquery_pagination插件 动态分页
- node.js的process进程和子进程
- 使用jQuery制作遮罩层弹出效果的极简实例分享
- CSS学习之样式继承(Inheritance)和优先级 - CSS: The Missing Manual
- 项目中使用的jqGrid
- phantomjs使用说明
- JS函数的定义与调用方法推荐
- 图解JSP与Servlet的关系
- HTML5 之前的视频播放格式
- JSP应用导出Excel报表的简单实现以及中文乱码彻底解决(HTML)
- pyspider 爬虫教程(三):使用 PhantomJS 渲染带 JS 的页面
- 2016.5.12 JavaScript笔记
- Codeforces Round #352 (Div. 2) B. Different is Good 水题
- servlet jsp 经典总结
- RadioButton自定义样式
- 使用jQuery实现Web页面换肤功能的要点解析
- Git中的AutoCRLF与SafeCRLF换行符问题
- OpenGL编程指南第十章:Frame buffer
- JavaScript性能优化技巧之函数节流