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. mysql-汇总(聚集)函数

    我们需要汇总数据而不用把他们实际检索出来,他们主要用来进行分析和报表数据的生成. 1.AVG:通过对表中行数计数并计算特定列值之和,求得该列的平均值.可用来返回所有列的平均值,也可以用来返回特定列或行 ...

  2. 以下三种下载方式有什么不同?如何用python模拟下载器下载?

    问题始于一个链接https://i1.pixiv.net/img-zip-...这个链接在浏览器打开,会直接下载一个不完整的zip文件 但是,使用下载器下载却是完整文件 而当我尝试使用python下载 ...

  3. The view 'Index' or its master was not found or no view engine supports the

    ASP.net  MVC 5  WebApi部署IIS提示: 未找到视图“索引”或其母版视图,或没有视图引擎支持搜索的位置.搜索了以下位置: 其他设置一切正常 这种情况很有可能是,1.部署的路径中空格 ...

  4. 爬虫--pyquery使用

    强大又灵活的网页解析库. 初始化   字符串初始化 html = ''' <div> <ul> <li class="item-0">first ...

  5. WLAN 感知

    WLAN 感知 通过 Android 8.0 中新增的 WLAN 感知功能,支持设备可以直接使用 WLAN 感知协议发现其他设备.与其他设备进行互连,以及将覆盖范围扩展到其他设备(Android 9 ...

  6. Chromium Graphics: Compositor Thread Architecture

    Compositor Thread Architecture <jamesr, enne, vangelis, nduca> @chromium.org Goals The main re ...

  7. 二、Fast-R-CNN

    一.概括 Fast R-cnn的主要亮点有:Fast R-CNN将借助多任务损失函数,将物体识别和位置修正合成到一个网络中,不再对网络进行分步训练,不需要大量内存来存储训练过程中特征的数据:用RoI层 ...

  8. 13-Linux中进程与线程的概念以及区别

    linux进程与线程的区别,早已成为IT界经常讨论但热度不减的话题.无论你是初级程序员,还是资深专家,都应该考虑过这个问题,只是层次角度不同罢了.对于一般的程序员,搞清楚二者的概念并在工作中学会运用是 ...

  9. 用Python讲述冯绍峰和赵丽颖的爱情故事

    昨天刷头条时得知赵丽颖当妈妈了.作为一名程序员突发奇想,不如用Python简单叙述一下冯绍峰和赵丽颖的爱情故事,于是有了本文. 代码十分简单,适合编程小白和有一些Python基础的准程序员,其中用到了 ...

  10. Express的初步使用

    废话不多说直接上步骤: 1. 首先建立一个新文件夹,进入此文件夹的命令窗口通过 npm init 命令为你的应用创建一个           package.json 文件,然后下载express模块 ...