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. 介绍Oracle自带的一些ASM维护工具 (kfod/kfed/amdu)

    1.前言 ASM(Automatic Storage Management)是Oracle主推的一种面向Oracle的存储解决方式,它是一个管理卷组或者文件系统的软件.眼下已经被RAC环境广泛使用,可 ...

  2. 用LogParser分析Windows日志

    用LogParser分析Windows日志 实战案例分享 假设你已具有上面的基础知识,那么以下为你准备了更加深入的应用操作视频(从安装到使用的全程记录): http://www.tudou.com/p ...

  3. CUDA笔记(六)

    dim3是NVIDIA的CUDA编程中一种自定义的整型向量类型,基于用于指定维度的uint3 忽然发现需要再搞多机MPI的配置,多机GPU集群.好麻烦.. 这两天考完两门了,还剩下三门,并行计算太多了 ...

  4. Intellij使用"easyexplore"

    刚开始接触Intellij,里面有很多东西还不太会用,平时在eclipse里面用的很方便的easyexplore能帮助快速打开文件目录,Intellij中本身就有这样的功能,只是默认没有开启,需要我们 ...

  5. WIFI 测试和调试

    WIFI测试和调试 本文将介绍如何使用 ASOP 中提供的工具测试和调试 WLAN 实现. 测试 为了测试 WLAN 框架,AOSP 提供了一系列单元测试.集成测试 (ACTS) 和 CTS 测试. ...

  6. ES6学习5 字符串的扩展

    1.ES6 为字符串添加了遍历器接口,使得字符串可以被for...of循环遍历. for (let codePoint of 'foo') { console.log(codePoint) } // ...

  7. 昼猫笔记 JavaScript -- 异步执行 | 定时器真的定时执行?

      本篇主要内容:异步.定时器引发的思考 预计阅读时间:8分钟 了解 我们都知道在js中定时器有两种  setInterval()  . setTimeout()   setInterval() :按 ...

  8. MySql系列之单表查询

    单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 关键字的执行 ...

  9. TCP的连接管理

    创建连接:(三次握手) 第一步: 客户端向服务器发送一个报文,该报文不含有数据段,SYN=1,随机产生sequence number(随机产生可用于避免某些安全性攻击) 第二步: 服务器收到报文,为这 ...

  10. php计算两个时间相差的天数、小时数、分钟数、秒数

    $startdate="2011-3-15 11:50:00";//开始时间 $enddate="2012-12-12 12:12:12";//结束时间 $da ...