Defeat The Enemy

Time Limit:
 3000MS Memory Limit: Unknown
64bit IO Format: %lld & %llu

  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的更多相关文章

  1. 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. ...

  2. UVA LIVE 7146 Defeat the Enemy

    这个题跟codeforces 556 D Case of Fugitive思路一样 关于codeforces 556 D Case of Fugitive的做法的链接http://blog.csdn. ...

  3. UVa 7146 Defeat the Enemy(贪心)

    题目链接: 传送门 Defeat the Enemy Time Limit: 3000MS     Memory Limit: 32768 KB Description Long long ago t ...

  4. Defeat the Enemy UVALive - 7146

      Long long ago there is a strong tribe living on the earth. They always have wars and eonquer other ...

  5. I - Defeat the Enemy UVALive - 7146 二分 + 贪心

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  6. UVALive 4426 Blast the Enemy! 计算几何求重心

    D - Blast the Enemy! Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Subm ...

  7. UVALive 7146

    Long long ago there is a strong tribe living on the earth. They always have wars and eonquer others. ...

  8. UVALive 4426 Blast the Enemy! --求多边形重心

    题意:求一个不规则简单多边形的重心. 解法:多边形的重心就是所有三角形的重心对面积的加权平均数. 关于求多边形重心的文章: 求多边形重心 用叉积搞一搞就行了. 代码: #include <ios ...

  9. UVALive 7146 (贪心+少许数据结构基础)2014acm/icpc区域赛上海站

    这是2014年上海区域赛的一道水题.请原谅我现在才发出来,因为我是在太懒了.当然,主要原因是我刚刚做出来. 其实去年我就已经看到这道题了,因为我参加的就是那一场.但是当时我们爆零,伤心的我就再也没有看 ...

随机推荐

  1. spring data redis jackson 配置,工具类

    spring data redis 序列化有jdk .jackson.string 等几种类型,自带的jackson不熟悉怎么使用,于是用string类型序列化,把对象先用工具类转成string,代码 ...

  2. 漫谈linux之文件IO篇(SSD写性能和机械硬盘差不多,读是4到10倍)

    前同事的文章,觉得写得很清晰,收藏了. http://blog.chinaunix.net/uid-27105712-id-3270102.html 在Linux 开发中,有几个关系到性能的东西,技术 ...

  3. 47.使用 RequireJS 加载 AngularJS

    转自:https://www.cnblogs.com/best/tag/Angular/ AngularJS 目前的版本没有遵循 Javascript 约定的 AMD 模块化规范, 因此使用 Requ ...

  4. 微信小程序领取卡券

    微信小程序领取卡券 标签(空格分隔): php 开发前需要准备的工作 1 小程序和公众号要有绑定 2 小程序和该公众号要绑定到同一个开发平台下 [https://open.weixin.qq.com/ ...

  5. ViewPager设置不能滚动

    设置ViewPager不能滑动 1:设置当前选中的页面 public void setCurrentItem(int item) { mPopulatePending = false; setCurr ...

  6. Android——PullToRefresh自动刷新

    需求:强制刷新 方法一: PullToRefreshListView本身提供了一个setRefreshing()接口,调用该接口会自动触发下拉刷新的操作(前提是支持下拉刷新).按照一般的操作我们直接在 ...

  7. rails 开发随手记 7

    jQuery 1.9 中如何修改选择项 $("select option:contains(teacher5)").prop('selected', 'selected'); 效果 ...

  8. 51Nod 1010 只包含因子2 3 5的数(打表+二分)

    K的因子中只包含2 3 5.满足条件的前10个数是:2,3,4,5,6,8,9,10,12,15. 所有这样的K组成了一个序列S,现在给出一个数n,求S中 >= 给定数的最小的数. 例如:n = ...

  9. php实现自动加载类

    PHP 实现自动加载类:

  10. HTML5按键打开摄像头和拍照

    HTML5实现按键打开摄像头和拍照 步骤: 1.创建一个打开摄像头按钮的标签.video标签.拍照的按钮标签.画布 2.实现打开摄像头的功能 3.实现拍照功能   具体实现代码: <!DOCTY ...