题目链接: 传送门

Defeat the Enemy

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

  1. 2
  2. 3 2
  3. 5 7
  4. 7 3
  5. 1 2
  6. 4 4
  7. 2 2
  8. 2 1
  9. 3 4
  10. 1 10
  11. 5 6

Sample Output

  1. Case #1: 3
  2. Case #2: -1

解题思路:

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

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<set>
  4. #include<cstring>
  5. #include<algorithm>
  6. using namespace std;
  7. struct Node{
  8. int first,second;
  9. };
  10. bool cmp1(Node x,Node y)
  11. {
  12. return x.first > y.first;
  13. }
  14. bool cmp2(Node x,Node y)
  15. {
  16. return x.second > y.second;
  17. }
  18. int main()
  19. {
  20. int T,Case = 1;
  21. scanf("%d",&T);
  22. while (T--)
  23. {
  24. int N,M,kill = 0;
  25. bool flag = true;
  26. Node our[100005],you[100005];
  27. memset(our,0,sizeof(our));
  28. memset(you,0,sizeof(you));
  29. multiset<int>s;
  30. multiset<int>::iterator it;
  31. scanf("%d%d",&N,&M);
  32. for (int i = 0;i < N;i++)
  33. {
  34. scanf("%d%d",&our[i].first,&our[i].second);
  35. }
  36. for (int i = 0;i < M;i++)
  37. {
  38. scanf("%d%d",&you[i].first,&you[i].second);
  39. }
  40. sort(our,our+N,cmp1);
  41. sort(you,you+M,cmp2);
  42. int j = 0;
  43. for (int i = 0;i < M;i++)
  44. {
  45. while (j < N && our[j].first >= you[i].second)
  46. {
  47. s.insert(our[j++].second);
  48. }
  49. if (s.empty())
  50. {
  51. flag = false;
  52. break;
  53. }
  54. it = s.upper_bound(you[i].first);
  55. if (it == s.end())
  56. {
  57. it = s.begin();
  58. }
  59. if (*it <= you[i].first)
  60. {
  61. kill++;
  62. }
  63. s.erase(it);
  64. }
  65. printf("Case #%d: %d\n",Case++,flag?(N-kill):-1);
  66. }
  67. return 0;
  68. }

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. 教你如何反编译Android安装文件apk来偷窥源代码

    本文章首发于浩瀚先森博客,地址:http://www.guohao1206.com/2016/08/23/970.html 1. 准备 - java环境 安装java并配置环境 => JAVA环 ...

  2. Android中的Semaphore

    信号量,了解过操作系统的人都知道,信号量是用来做什么的··· 在Android中,已经提供了Semaphore来帮助我们使用~ 那么,在开发中这家伙有什么用呢? 用的地方不多,但是却真的是好用至极! ...

  3. ASP.NET 系列:单元测试

    单元测试可以有效的可以在编码.设计.调试到重构等多方面显著提升我们的工作效率和质量.github上可供参考和学习的各种开源项目众多,NopCommerce.Orchard等以及微软的asp.net m ...

  4. C#根据当前时间获取周,月,季度,年度等时间段的起止时间

    最近有个统计分布的需求,需要按统计本周,上周,本月,上月,本季度,上季度,本年度,上年度等时间统计分布趋势,所以这里就涉及到计算周,月,季度,年度等的起止时间了,下面总结一下C#中关于根据当前时间获取 ...

  5. nios II--实验2——led硬件部分

    Led 硬件开发 新建原理图 1.打开Quartus II 11.0,新建一个工程,File -> New Project Wizard…,忽略Introduction,之间单击 Next> ...

  6. Windows系统防火墙用法

    1.按下“Win+X”组合键呼出系统快捷菜单,点击打开“控制面板”: 2.将“查看方式”修改为[大图标],然后点击“Windows 防火墙”: 3.在防火墙窗口左侧点击“高级设置”:(若防火墙未开启, ...

  7. MVC认知路【点点滴滴支离破碎】【二】----Razor服务器标记语言

    Razor 代码块包含在 @{....}中 内嵌表达式(变量和函数)已 @ 开头 代码语句用分号结束 变量使用 var 关键字声明 字符创用引号括起来 C#代码区分大小写 C#文件的扩展是 .csht ...

  8. HashTable、HashMap、HashSet

    1. HashMap 1)  hashmap的数据结构 Hashmap是一个数组和链表的结合体(在数据结构称“链表散列“),如下图示: 当我们往hashmap中put元素的时候,先根据key的hash ...

  9. PHP之function_handling 函数

    function_handling 函数 function_handling 函数 需求: 编写一个函数,传入的参数个数不确定,请求出其和. 使用到 以下几个函数: 代码说明: 函数的参数可以是另外一 ...

  10. A query was run and no Result Maps were found for the Mapped Statement 'user.insertUser!selectKey'. It's likely that neither a Result Type nor a Result Map was specified.

    使用mybatis时出现异常问题: 有如下的错误 Error querying database. Cause: org.apache.ibatis.executor.ExecutorExceptio ...