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

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;
}

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

时间: 2025-01-07 18:32:33

I - Defeat the Enemy UVALive - 7146 二分 + 贪心的相关文章

UVALive 7146 (贪心+少许数据结构基础)2014acm/icpc区域赛上海站

这是2014年上海区域赛的一道水题.请原谅我现在才发出来,因为我是在太懒了.当然,主要原因是我刚刚做出来. 其实去年我就已经看到这道题了,因为我参加的就是那一场.但是当时我们爆零,伤心的我就再也没有看过那一场的题了.昨天我的队友的高中同学建议我们一起来打一打这场比赛吧,然后我才再次回顾这场比赛.结果一堆琐事,我一共也没有做多久的题,我的队友扎扎实实看了5个小时的题,把另一道水题给过了.全场我们也只过了那么一道题.学姐说,做重现赛和现场赛比较,需要去掉一题,那么我们又爆零了. 题意: 我方有n个人

[uva_la7146 Defeat the Enemy(2014 shanghai onsite)]贪心

题意:我方n个军队和敌方m个军队进行一对一的对战,每个军队都有一个攻击力和防御力,只要攻击力不小于对方就可以将对方摧毁.问在能完全摧毁敌方的基础上最多能有多少军队不被摧毁. 思路:按防御力从大到小考虑敌方的军队由我们哪只军队去摧毁,对每个敌方军队,维护我方军队可以摧毁它的集合,用S表示,从大到小考虑保证了S更容易维护,只需要记录防御力就够了,不需要大量的删除操作.我们需要选择S中防御力大于当前敌方军队且最小的军队,那么这个军队是可以摧毁敌方军队而自己不被摧毁的.如果这样的军队不存在,那么直接用S

UVALive 7146 Defeat The Enemy

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 stron

UVA LIVE 7146 Defeat the Enemy

这个题跟codeforces 556 D Case of Fugitive思路一样 关于codeforces 556 D Case of Fugitive的做法的链接http://blog.csdn.net/stl112514/article/details/46868749 题意大概我方有n个军队,敌方有m个军队,军队有两个属性:攻击力和防御力 一个军队能打败另一个军队的条件:一军队攻击力不低于另一个军队防御力 大概是2014上海区域赛最简单的一个题? #include<map> #incl

UVA 12124 UVAlive 3971 Assemble(二分 + 贪心)

先从中找出性能最好的那个数, 在用钱比较少的去组合,能组出来就表明答案在mid的右边,反之在左边, #include<string.h> #include<map> #include<stdio.h> #include<iostream> #include<algorithm> using namespace std; map<string,int> vic;//以字符映射数字 int end,start; int num; int

二分+贪心

上海邀请赛热身时候,C题是一个二分+贪心的题目.起初并不会,问了旁边的复旦大神.这几天无意发现VJ上一个专题.擦原来是一个经典类型. 二分+贪心 这类题目注意数据范围,1e8,1e9一般都是这样. 注意事项 二分法有很多写法,推荐用lf+1 < rf的写法.这个也符合计算机中数据存取的原则.对于浮点数,直接就循环100次,精度绝对够. 一般有两种类型,一种是询问最优,即数列中无重复.一种是多个即lower_bound ,upper_bound这类函数问题. 贪心使用,就是这个问题枚举答案可被验证

nyoj586||poj2456 二分+贪心

完全看不懂题意....百度搜搜才看懂题意  然后就参考代码了 和yougth的最大化()nyoj914差不多的方法 二分+贪心 #include <stdio.h> #include <algorithm> using namespace std; int c,a[100005],n; bool judge(int k) { int p=a[0],cnt=1;//也就这里注意点 从1开始 自己想想为啥 for(int i=1;i<n;i++) { if(a[i]-p>=

HDU 4004 The Frog&#39;s Games 二分 贪心

戳这里:HDU 4004 //思路:二分经典入门题...贪心判方案是否可行 1 #include "bits/stdc++.h" 2 using namespace std; 3 int L, n, m; 4 int pos[500010], dis[500010]; 5 6 bool Cant(int Dis_Jump) 7 { 8 int i, Dis_Sum = 0, Count = 0; 9 for(i = 1; i <= n + 1; ++i) { 10 if(dis[

贪心(bnuoj49103+二分+贪心)

贪心 小明喜欢养小鸡,小鸡喜欢吃小米.小明很贪心,希望养s只不同种类的小鸡,小鸡也很贪心,每天除了吃固定的ai粒小米外,还想多吃bi*s粒小米. 小明每天有M(0<=M<=10^9)粒小米可以喂小鸡,小鸡共有N(0<=N<=1000)种.问小明最多可以养多少只小鸡? Input 多组数据,请读到文件尾 第一行,整数N,M,以空格分隔,之后两行,第一行为N个整数ai,第二行为N个整数bi. ai.bi都在int范围内 Output 一行一个整数,s. Sample Input 2 4