码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-08-11 14:34:23      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:node   icp   const   names   ons   ret   icpc   uvalive   错误   

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

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

标签:node   icp   const   names   ons   ret   icpc   uvalive   错误   

原文地址:http://www.cnblogs.com/liuweimingcprogram/p/7345579.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!