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年上海区域赛的一道水题.请原谅我现在才发出来,因为我是在太懒了.当然,主要原因是我刚刚做出来. 其实去年我就已经看到这道题了,因为我参加的就是那一场.但是当时我们爆零,伤心的我就再也没有看 ...
随机推荐
- storm trident function函数
package cn.crxy.trident; import java.util.List; import backtype.storm.Config; import backtype.storm. ...
- C#重构经典全面汇总
C#重构经典全面汇总 1. 封装集合 概念:本文所讲的封装集合就是把集合进行封装,仅仅提供调用端须要的接口. 正文:在非常多时候,我们都不希望把一些不必要的操作暴露给调用端,仅仅须要给它所须要的操作 ...
- Metasploit学习笔记(博主推荐)
不多说,直接上干货! 连接后台的数据库,当然不是必须品. 连接数据库的好处:1.可以攻击和扫描的结果,保存起来 2.将一些搜索结果做个缓存 默认数据库是postgresql. 同时要注意的是 ...
- spring boot整合mail
1.添加依赖 </dependency> <dependency> <groupId>org.springframework.boot</groupId> ...
- PostgreSQL Replication之第五章 设置同步复制(3)
5.3 冗余和停止复制 谈到同步复制,有一个现象一定不能被遗漏.想象一下,我们有一个同步复制的双节点集群.如果slave故障会发生什么?答案是master不能容易地区分慢slave和故障slave,因 ...
- UI Framework-1: Aura Client API
Client API The Aura Client API is an API Aura uses to communicate with the client application using ...
- 解决mongodb TypeError: Cannot read property 'XXX' of null 问题
有时候我们的字段里的数据为空而去查询就会报错. 比如以下形式: “cartList”:[] “cartList”:[{}] “cartList”:{} “cartList”:null 在查询的时候加上 ...
- 【noip2016】蚯蚓(单调性+队列)
题目贼长 大意是你有n个线段,每一秒你要拿出来最长的一个线段切成两段长度为[p*u](向下取整)和u-[p*u]两段(其中u是线段长,p是一个大于0小于1的实数)没被切的线段长度加q(0<q&l ...
- 基于vue的无缝滚动组件
vue-seamless-scroll A simple, Seamless scrolling for Vue.js 在awesome上一直没有发现vue的无缝滚动组件,在工作之余写了个组件,分享出 ...
- 【转】C# HttpWebRequest提交数据方式
[转]C# HttpWebRequest提交数据方式 HttpWebRequest和HttpWebResponse类是用于发送和接收HTTP数据的最好选择.它们支持一系列有用的属性.这两个类位 于Sy ...