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

思路:按防御力从大到小考虑敌方的军队由我们哪只军队去摧毁,对每个敌方军队,维护我方军队可以摧毁它的集合,用S表示,从大到小考虑保证了S更容易维护,只需要记录防御力就够了,不需要大量的删除操作。我们需要选择S中防御力大于当前敌方军队且最小的军队,那么这个军队是可以摧毁敌方军队而自己不被摧毁的。如果这样的军队不存在,那么直接用S中防御力最小的去和敌方军队同归于尽,因为这样做到了自己损失最小。如果中间某个时刻S为空了,则说明我方派不出摧毁当前敌方军队的军队了。

(忠告:维护集合时先想想是用set呢还是multiset,否则又该怀疑数据有问题了。。。)

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  1. #pragma comment(linker, "/STACK:10240000")
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. #define X first
  6. #define Y second
  7. #define pb push_back
  8. #define mp make_pair
  9. #define all(a) (a).begin(), (a).end()
  10. #define fillchar(a, x) memset(a, x, sizeof(a))
  11.  
  12. typedef long long ll;
  13. typedef pair<int, int> pii;
  14.  
  15. #ifndef ONLINE_JUDGE
  16. namespace Debug {
  17. void print(){cout<<endl;}template<typename T>
  18. void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
  19. void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
  20. void print(T*p, T*q){int d=p<q?:-;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
  21. }
  22. #endif // ONLINE_JUDGE
  23. template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
  24. template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}
  25. /* -------------------------------------------------------------------------------- */
  26.  
  27. const int maxn = 1e5 + ;
  28.  
  29. multiset<int> s;
  30. int n, m;
  31. pii a[maxn], b[maxn];
  32.  
  33. void work() {
  34. int ans = n - m;
  35. s.clear();
  36. int now = n - ;
  37. for (int i = m - ; i >= ; i --) {
  38. while (now >= && a[now].X >= b[i].X) s.insert(a[now --].Y);
  39. if (!s.size()) {
  40. puts("-1");
  41. return ;
  42. }
  43. multiset<int>::iterator iter = s.upper_bound(b[i].Y);
  44. if (iter == s.end()) s.erase(s.begin());
  45. else {
  46. ans ++;
  47. s.erase(iter);
  48. }
  49. }
  50. printf("%d\n", ans);
  51. }
  52.  
  53. int main() {
  54. #ifndef ONLINE_JUDGE
  55. freopen("in.txt", "r", stdin);
  56. //freopen("out.txt", "w", stdout);
  57. #endif // ONLINE_JUDGE
  58. int T, cas = ;
  59. cin >> T;
  60. while (T --) {
  61. printf("Case #%d: ", ++ cas);
  62. cin >> n >> m;
  63. for (int i = ; i < n; i ++) {
  64. scanf("%d%d", &a[i].X, &a[i].Y);
  65. }
  66. for (int i = ; i < m; i ++) {
  67. scanf("%d%d", &b[i].Y, &b[i].X);
  68. }
  69. if (n < m) puts("-1");
  70. else {
  71. sort(a, a + n);
  72. sort(b, b + m);
  73. work();
  74. }
  75. }
  76. return ;
  77. }

[uva_la7146 Defeat the Enemy(2014 shanghai onsite)]贪心的更多相关文章

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

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

  2. [LA7139 Rotation(2014 shanghai onsite)]二维树状数组

    题意:有一个n*m的矩形,一辆车从左上角出发,沿一条路径走,路径是由矩形上每个单元格的边构成的,最后回到左上角,求车子在每个格子转过圈数的平方和. 思路:假设需要记录每个格子转的顺时针的圈数(为负表示 ...

  3. UVa 7146 Defeat the Enemy(贪心)

    题目链接: 传送门 Defeat the Enemy Time Limit: 3000MS     Memory Limit: 32768 KB Description Long long ago t ...

  4. UVA LIVE 7146 Defeat the Enemy

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

  5. UVALive 7146 Defeat The Enemy

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

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

  7. 2014 Shanghai Invitation Contest

    题目链接 http://acm.hdu.edu.cn/search.php?field=problem&key=2014%C9%CF%BA%A3%C8%AB%B9%FA%D1%FB%C7%EB ...

  8. Defeat the Enemy UVALive - 7146

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

  9. BZOJ 3671 NOI 2014 随机数生成器 贪心

    题目大意:实在是太难说明了,自己看pdf吧.. 思路:优先依照它说明的方法处理数组,然后为了让数列中尽可能多的出现小的数字,所以1是必需要出现的,这样才干使整个数列的排序后字典序最小. 我们思考,假设 ...

随机推荐

  1. Equalizing by Division

    The only difference between easy and hard versions is the number of elements in the array. You are g ...

  2. Linux下nginx自启动配置

    1.在linux系统的/etc/init.d/目录下创建nginx文件 vim /etc/init.d/nginx 在脚本中添加一下命令(内容主要参考官方文档) #!/bin/sh # # nginx ...

  3. Chrome 浏览器安装 ChroPath 插件

    1.下载地址 http://www.cnplugins.com/devtool/chropath/download.html 2.安装方法 a.把下载的文件更改后缀名变为压缩包,然后解压到本地:如下图 ...

  4. SpringBoot系列(十一)拦截器与拦截器链的配置与使用详解,你知道多少?

    往期推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件详解 SpringBoot系列(四)web静 ...

  5. 关于暴力破解的一些学习笔记(pikachu)

    这几天的笔记都懒得发博客都写在本地了,随缘搬上来 什么是暴力破解 就是在攻击者不知道目标账号密码情况下的,对目标系统的常识性登陆 一般会采用一些工具+特定的字典 来实现高效的连续的尝试性登陆 一个有效 ...

  6. 【ubuntu】windows+ubuntu 设置windows为第一启动项

    进入ubuntu系统 sudo su vim /etc/default/grub 更改GRUB_DEFAULT=后的值默认是0,如果你的windows启动项在第5个就改成4.改完之后退出保存输入 up ...

  7. var、let、const

    var.let.const之间的区别和使用 1.var声明变量可以重复声明,而let不可以重复声明 let a = 1; let a = 2; var b = 3; var b = 4; a // I ...

  8. java中文乱码解决之道(七)—–JSP页面编码过程

    我们知道JSP页面是需要转换为servlet的,在转换过程中肯定是要进行编码的.在JSP转换为servlet过程中下面一段代码起到至关重要的作用. <%@ page language=" ...

  9. 关于LinearLayout设置权重后width或height不设置0dp的影响说明

    摘要 平时没那么注意LinearLayout布局时权重的问题,设置了权重属性后,通常建议将width或height的属性值设置为0dp,有时候设置权重后,还是习惯将width或height的属性设置为 ...

  10. Vue项目中设置每个单页面的标题

    两种实现方法,第一种方法引入插件,第二种为编程方式实现(推荐) 首先在路由文件index.js中给每个单页面路由添加title routes: [{     path: '/',     name: ...