题目链接: 传送门

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. <实训|第六天>偷偷让新手的Linux无限重启附linux主机名称不是随便乱改的!

    先说个事情:这几天我正在忙一个项目的设计,8月1号之前要弄出来,所以每天都要弄到很晚,可能更新就有点跟不上了,不过我如果有时间的话,我就更新,没时间的话,我会在8月1号之后统一更新出来,希望大家谅解! ...

  2. 青瓷引擎使用心得——修改引擎的loading界面

    一. 修改引擎的Loading界面之使用进度条显示1. 双击打开引擎包中的lib/qc-loading-debug.js,如下图所示: 2. 只需要修改qici.init函数即可改变loading界面 ...

  3. How to build windows azure PowerShell Source Code

    Download any version source code of Windows Azure Powershell from https://github.com/Azure/azure-sdk ...

  4. SqlServer——批量插入数据

    像Major表里面批量插入数据演示: 代码如下: Declare @I int Set @I= Begin Tran InsertData: Insert into Major values(@I,' ...

  5. Hibernate用注解实现实体类和表的映射

    数据库mysql: 1.一对一 person50表password50表是一对一的关系: password50表中有外键 person_id person实体类: package com.c50.en ...

  6. java设计优化--单例模式

    单例模式是一种对象创建模式,确保系统中一个类只有一个实例. 在java语言中,这样做有两大好处: 1.对于频繁使用的对象,可以省略创建对象所话费的时间: 2.由于new操作的次数减少,对于系统内存的使 ...

  7. oracle如何获取每个月的最后一天

    SELECT LAST_DAY(DATE'2016-09-23') FROM DUAL;

  8. nginx安装配置+清缓存模块安装

    经过一段时间的使用,发现nginx在并发与负载能力方面确实优于apache,现在已经将大部分站点从apache转到了nginx了.以下是nginx的一些简单的安装配置. 环境 操作系统:CentOS. ...

  9. safe RGB colors

    RGB是面向机器的一种颜色空间. 虽然它表示\(256 \times 256 \times 256\)种不同的颜色, 但在实际中, 大部分机器都只实现了256种颜色. 安全色(Safe RGB col ...

  10. Fiddler捕获 iPhone/Android 流量

    由于 Fiddler 作为代理的形式来捕获流量,因此理论上来说是不区分客户端的(当然 Fiddler 要安装在 Windows,毕竟是 .NET 开发的工具). 这一篇文章教你如何利用 Fiddler ...