您的位置:首页 > Web前端

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

2017-08-11 13:56 387 查看
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5158

这样的受到两个东西限制的,很容易想到要排序,然后加进去multiset,加的时候保证一个key是成立的,就转化为一个key的问题了。

这题需要打死m个全部的军队。

那么应该是枚举m个军队,去找一个n打死它,或者同归于尽。

开始的时候我是枚举n去选m了,这是错误的。

因为可以同归于尽,也可以杀死别人的时候(而且那个别人可以和自己一个同归于尽),不知道如何选择,

看数据吧。

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
const int maxn = 1e5 + 20;
struct Node {
int a, b;
}one[maxn], two[maxn];
bool cmp1(Node a, Node b) {
if (a.a != b.a) return a.a > b.a;
else return a.b < b.b;
}
bool cmp2(Node a, Node b) {
if (a.b != b.b) return a.b > b.b;
else return a.a > b.a;
}
multiset< int > ss;
int f;
void work() {
ss.clear();
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%d%d", &one[i].a, &one[i].b);
}
for (int i = 1; i <= m; ++i) {
scanf("%d%d", &two[i].a, &two[i].b);
}
sort(one + 1, one + 1 + n, cmp1);
sort(two + 1, two + 1 + m, cmp2);
int ans = n;
multiset< int > :: iterator it;
if (n < m || one[1].a < two[1].b) {
ans = -1;
} else {
int p = 1;
for (int i = 1; i <= m; ++i) {
while (p <= n && one[p].a >= two[i].b) {
ss.insert(one[p].b);
p++;
}
if (ss.empty()) {
ans = -1;
break;
}
it = ss.upper_bound(two[i].a);
if (it == ss.end()) {
it = ss.begin();
ss.erase(it);
ans--;
} else {
ss.erase(it);
}
}
}
printf("Case #%d: %d\n", ++f, ans);
}

int main() {
#ifdef local
freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return 0;
}


View Code
toshiba大佬的数据

2
5 5
16 10
19 8
20 18
8 20
6 1

13 19
5 1
8 3
1 1
13 9

10 10
3 1
15 12
4 16
13 2
20 10
12 17
17 7
14 17
18 15
12 17
6 2
7 11
6 2
16 2
16 10
9 18
9 17
13 10
9 4
16 12
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: