UVALive 7146 Defeat The Enemy
Time Limit: 3000MS Memory Limit: Unknown
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 Input
2
3 2
5 7
7 3
1 2
4 4
2 2
2 1
3 4
1 10
5 6
Sample Output
Case #1: 3
Case #2: -1
解题:贪心策略
将我方部落依战斗力,防御力小大排列,将敌方防御力,战斗力大到小排列。。
如果能打死敌人,看是否存在足够的防御力使得我方不致死,否则,使用能干掉对方的,防御力最低的我方部落
这份代码确实有问题 感谢原封大神的数据
#include <bits/stdc++.h>
#define pii pair<int,int>
using namespace std;
multiset< pii,less< pii > >ourSide;
multiset< pii,greater< pii > >enemy;
int n,m;
int main() {
int attack,defense,cs = ,T;
scanf("%d",&T);
while(T--) {
ourSide.clear();
enemy.clear();
scanf("%d %d",&n,&m);
for(int i = ; i < n; ++i) {
scanf("%d %d",&attack,&defense);
ourSide.insert(make_pair(attack,defense));
}
for(int i = ; i < m; ++i) {
scanf("%d %d",&attack,&defense);
enemy.insert(make_pair(defense,attack));
}
for(auto it = enemy.begin(); it != enemy.end(); ++it) {
if(ourSide.empty() || ourSide.rbegin()->first < it->first) {
n = -;
break;
}
auto cur = ourSide.upper_bound(make_pair(it->first,it->second));
if(cur == ourSide.end())
cur = ourSide.lower_bound(make_pair(it->first,));
if(cur->second <= it->second) n--;
ourSide.erase(cur);
}
printf("Case #%d: %d\n",cs++,n);
}
return ;
}
/*
2
3 2
5 7
7 3
1 2
4 4
2 2 2 1
3 4
1 10
5 6
*/
这是更正后的代码,感谢原封大大的指点
上面的策略是没错吧,不过不利于编程实现
将我方战斗力大到小排,敌方防御力大到小排
然后每次把我放的战斗力大于地方的防御力的战士的防御力加入到multiset中
查找比敌方攻击力大的最小的防御值,如存在,则用这个干掉敌人,自己能幸存,
否则,用能干掉敌方的防御力最小的士兵,要死一起死。。。。
感谢ACdream群中的各位巨巨的提示
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct node {
int attack,defence;
} ourSide[maxn],enemy[maxn];
multiset<int>ms;
int main() {
int T,n,m,cs = ;
scanf("%d",&T);
while(T--) {
scanf("%d %d",&n,&m);
for(int i = ; i < n; ++i)
scanf("%d %d",&ourSide[i].attack,&ourSide[i].defence);
for(int i = ; i < m; ++i)
scanf("%d %d",&enemy[i].attack,&enemy[i].defence);
sort(ourSide,ourSide+n,[](node &x,node &y)->bool{return x.attack > y.attack;});
sort(enemy,enemy+m,[](node &x,node &y)->bool{return x.defence > y.defence;});
int k = ,ret = ;
ms.clear();
for(int i = ; i < m; ++i){
while(k < n && ourSide[k].attack >= enemy[i].defence)
ms.insert(ourSide[k++].defence);
if(ms.empty()){
ret = -;
break;
}
auto cur = ms.upper_bound(enemy[i].attack);
if(cur == ms.end()) cur = ms.begin();
if(*cur <= enemy[i].attack) ret++;
ms.erase(cur);
}
printf("Case #%d: %d\n",cs++,ret == -?ret:n - ret);
}
return ;
}
UVALive 7146 Defeat The Enemy的更多相关文章
- UVALive 7146 Defeat the Enemy(贪心+STL)(2014 Asia Shanghai Regional Contest)
Long long ago there is a strong tribe living on the earth. They always have wars and eonquer others. ...
- UVA LIVE 7146 Defeat the Enemy
这个题跟codeforces 556 D Case of Fugitive思路一样 关于codeforces 556 D Case of Fugitive的做法的链接http://blog.csdn. ...
- UVa 7146 Defeat the Enemy(贪心)
题目链接: 传送门 Defeat the Enemy Time Limit: 3000MS Memory Limit: 32768 KB Description Long long ago t ...
- Defeat the Enemy UVALive - 7146
Long long ago there is a strong tribe living on the earth. They always have wars and eonquer other ...
- I - Defeat the Enemy UVALive - 7146 二分 + 贪心
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVALive 4426 Blast the Enemy! 计算几何求重心
D - Blast the Enemy! Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Subm ...
- UVALive 7146
Long long ago there is a strong tribe living on the earth. They always have wars and eonquer others. ...
- UVALive 4426 Blast the Enemy! --求多边形重心
题意:求一个不规则简单多边形的重心. 解法:多边形的重心就是所有三角形的重心对面积的加权平均数. 关于求多边形重心的文章: 求多边形重心 用叉积搞一搞就行了. 代码: #include <ios ...
- UVALive 7146 (贪心+少许数据结构基础)2014acm/icpc区域赛上海站
这是2014年上海区域赛的一道水题.请原谅我现在才发出来,因为我是在太懒了.当然,主要原因是我刚刚做出来. 其实去年我就已经看到这道题了,因为我参加的就是那一场.但是当时我们爆零,伤心的我就再也没有看 ...
随机推荐
- HDOJ 5296 Annoying problem LCA+数据结构
dfs一遍得到每一个节点的dfs序,对于要插入的节点x分两种情况考虑: 1,假设x能够在集合中的某些点之间,找到左边和右边距离x近期的两个点,即DFS序小于x的DFS序最大点,和大于x的DFS序最小的 ...
- 【BZOJ 1660】 [Usaco2006 Nov]Bad Hair Day 乱发节
1660: [Usaco2006 Nov]Bad Hair Day 乱发节 Time Limit: 2 Sec Memory Limit: 64 MB Submit: 678 Solved: 32 ...
- 51nod-1310: Chandrima and XOR
[传送门:51nod-1310] 简要题意: 有一个数组S,保证里面的数是从小到大的,而且每一个数的二进制中都没有连续的1,如:1,2,4,5,8... 给出n,然后给出n个位置,求出S数组中n个位置 ...
- Cms WebSite 编译非常慢
第一次编译非常慢 如果遇到错误,中途中断的话. 下一次编译的时候,上一次已经编译过的文件,会非常快的略过.很快就会到上一次遇到错误的地方,继续往下进行编译.
- rest_framework 节流功能(访问频率)
访问记录 = { 身份证号: [ :: ,::, ::] } #:: ,::,:: ,::, #:: #[::, ::, ::] #访问记录 = { 用户IP: [...] } import time ...
- Codeforces 708D 费用流 (呃我们的考试题)
NB的题目背景 输入输出一样 考试的时候貌似只有gzz一个人搞出来了 %gzz 思路: 分情况讨论 add(x,y,C,E) C是费用 E是流量 1. f>c add(x,y,2,inf),ad ...
- MNIST手写数字数据集
下载python源代码之后,使用: import input_data mnist = input_data.read_data_sets('MNIST_data/',one_hot=True) 下载 ...
- vue 退出动画无效解决方法
遇到一个问题:给模态框加入退出动画,进入的动画效果是有的,可是退出的动画就没有了. 写个简单的结构: <div class="mask" v-show="warni ...
- [NOIP2009提高组]靶形数独
题目:洛谷P1074.Vijos P1755.codevs1174. 题目大意:给你一个数独,让你填完这个数独,并要求得分最大,问这个得分是多少(不能填完输出-1). 每个格子的得分是当前格子所填的数 ...
- [NOIP2013提高组]火柴排队
题目:洛谷P1966.Vijos P1842.codevs3286. 题目大意:有两排火柴,每根都有一个高度.设a.b分别表示两排火柴的高度,现在要令$\sum(a_i-b_i)^2$最小.现两排火柴 ...