题目链接: 传送门

Defeat the Enemy

Time Limit: 3000MS     Memory Limit: 32768 KB

Description

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 theattack 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

解题思路:

将我方战斗力从大到小排,敌方防御力从大到小排 然后每次把我方的战斗力大于敌方的防御力的战士的防御力加入到multiset中 在集合中查找比敌方攻击力大的最小的防御值,如存在,则用这个干掉敌人,自己能幸存,否则,用能干掉敌方的防御力最小的士兵,同归于尽

#include<iostream>
#include<cstdio>
#include<set>
#include<cstring>
#include<algorithm>
using namespace std;

struct Node{
    int first,second;
};

bool cmp1(Node x,Node y)
{
    return x.first > y.first;
}

bool cmp2(Node x,Node y)
{
    return x.second > y.second;
}

int main()
{
    int T,Case = 1;
    scanf("%d",&T);
    while (T--)
    {
        int N,M,kill = 0;
        bool flag = true;
        Node our[100005],you[100005];
        memset(our,0,sizeof(our));
        memset(you,0,sizeof(you));
        multiset<int>s;
        multiset<int>::iterator it;
        scanf("%d%d",&N,&M);
        for (int i = 0;i < N;i++)
        {
            scanf("%d%d",&our[i].first,&our[i].second);
        }
        for (int i = 0;i < M;i++)
        {
            scanf("%d%d",&you[i].first,&you[i].second);
        }
        sort(our,our+N,cmp1);
        sort(you,you+M,cmp2);
        int j = 0;
        for (int i = 0;i < M;i++)
        {
            while (j < N && our[j].first >= you[i].second)
            {
                s.insert(our[j++].second);
            }
            if (s.empty())
            {
                flag = false;
                break;
            }
            it = s.upper_bound(you[i].first);
            if (it == s.end())
            {
                it = s.begin();
            }
            if (*it <= you[i].first)
            {
                kill++;
            }
            s.erase(it);
        }
        printf("Case #%d: %d\n",Case++,flag?(N-kill):-1);
    }
    return 0;
}

UVa 7146 Defeat the Enemy(贪心)的更多相关文章

  1. UVA LIVE 7146 Defeat the Enemy

    这个题跟codeforces 556 D Case of Fugitive思路一样 关于codeforces 556 D Case of Fugitive的做法的链接http://blog.csdn. ...

  2. UVALive 7146 Defeat The Enemy

    Defeat The Enemy Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Long long ...

  3. 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. ...

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

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  5. Defeat the Enemy UVALive - 7146

      Long long ago there is a strong tribe living on the earth. They always have wars and eonquer other ...

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

    题意:我方n个军队和敌方m个军队进行一对一的对战,每个军队都有一个攻击力和防御力,只要攻击力不小于对方就可以将对方摧毁.问在能完全摧毁敌方的基础上最多能有多少军队不被摧毁. 思路:按防御力从大到小考虑 ...

  7. UVA 11729 - Commando War(贪心 相邻交换法)

    Commando War There is a war and it doesn't look very promising for your country. Now it's time to ac ...

  8. UVA 11292-Dragon of Loowater (贪心)

    Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem. The shor ...

  9. 【NOIP合并果子】uva 10954 add all【贪心】——yhx

    Yup!! The problem name reects your task; just add a set of numbers. But you may feel yourselvesconde ...

随机推荐

  1. 纯手工打造漂亮的瀑布流,五大插件一个都不少Bootstrap+jQuery+Masonry+imagesLoaded+Lightbox!

    前两天写的文章<纯手工打造漂亮的垂直时间轴,使用最简单的HTML+CSS+JQUERY完成100个版本更新记录的华丽转身!>受到很多网友的喜爱,今天特别推出姊妹篇<纯手工打造漂亮的瀑 ...

  2. Linux学习笔记-Ubuntu添加右键菜单打开终端

    1.进入个人目录(如/home/batsing,下文缩写成 ~ ):设置显示隐藏文件,或使用命令行:2.进入 ~/.gnome2/nautilus-scripts 文件夹,新建一个文件,名为 term ...

  3. 快速备份和还原 MySQL 数据库的另一种方法

    一直使用 SQL Server 作为公司产品的数据库来存储系统数据,所以备份还原一直都不是问题,因为 SQL Server 的备份还原非常迅速和易用.但今年公司改变策略,使用起 MySQL 数据库作为 ...

  4. C# StopWatch的使用

    在做项目的时候,需要输出数据库操作的耗时,自己写了个方法.老大看到后,奇怪我为什么不用现成的.才知道有StopWatch这个类. 属性       名称 说明 Elapsed 获取当前实例测量得出的总 ...

  5. BASH 命令以及使用方法小结

    最近工作中需要写一个Linux脚本,用到了很多BASH命令,为了防止以后忘记,在这里把它们一一记下来.可能会比较乱,随便看看就好了.如果有说的不对的地方也欢迎大家指正. 1,export VAR=.. ...

  6. Linux进程间通信之管道

    1,进程间通信 (IPC ) Inter-Process Communication 比较好理解概念的就是进程间通信就是在不同进程之间传播或交换信息. 2,linux下IPC机制的分类:管道.信号.共 ...

  7. 怎样关闭google的自动更新

    谷歌的自动更新很烦人的,只要你点击关于Google Chrome,谷歌就会自动更新成最新版本. 但是sencha框架好像与谷歌29.0以上的兼容性不是很好,所以关闭谷歌自动更新的需求来了,网上很多人说 ...

  8. [HDU5904]LCIS(DP)

    题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的 n,m<=1e5,a[i],b[i]<=1e6分析:dp[i]表示以数字i结尾的序列最长长度 dp[a ...

  9. MVC———用自定义扩展类实现验证

    废话少说,直接上图 →_→ NO.1 NO.2 NO.3 NO.4 NO.5 NO.6 NO.7 NO.8 NO.9 NO.10 NO.11 NO.12 NO.13 NO.14 NO.15 NO.16 ...

  10. javascript位置相关知识点整理

    1.css指定元素的位置采用的是文档坐标: 2.js查询元素位置的方法返回的是元素在视口中的位置,即视口坐标: 如何获得元素的位置和尺寸 获得元素的位置和尺寸可以通过getBoundingClient ...