题意:给定n个优惠券,每张都有一定的优惠区间,然后要选k张,保证k张共同的优惠区间最大。

析:先把所有的优惠券按左端点排序,然后维护一个容量为k的优先队列,每次更新优先队列中的最小值,和当前的右端点,

之间的距离。优先队列只要存储右端点就好。

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #include <sstream>
  17. #include <unordered_map>
  18. #include <unordered_set>
  19. #define debug() puts("++++");
  20. #define freopenr freopen("in.txt", "r", stdin)
  21. #define freopenw freopen("out.txt", "w", stdout)
  22. using namespace std;
  23.  
  24. typedef long long LL;
  25. typedef unsigned long long ULL;
  26. typedef pair<int, int> P;
  27. const int INF = 0x3f3f3f3f;
  28. const LL LNF = 1LL << 60;
  29. const double inf = 0x3f3f3f3f3f3f;
  30. const double PI = acos(-1.0);
  31. const double eps = 1e-8;
  32. const int maxn = 3e5 + 5;
  33. const int mod = 2000;
  34. const int dr[] = {-1, 1, 0, 0};
  35. const int dc[] = {0, 0, 1, -1};
  36. const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  37. int n, m;
  38. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  39. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  40. inline bool is_in(int r, int c){
  41. return r >= 0 && r < n && c >= 0 && c < m;
  42. }
  43. struct Node{
  44. int l, r, id;
  45. bool operator < (const Node &p) const{
  46. return l < p.l;
  47. }
  48. };
  49. Node a[maxn];
  50.  
  51. int main(){
  52. while(scanf("%d %d", &n, &m) == 2){
  53. for(int i = 0; i < n; ++i){
  54. scanf("%d %d", &a[i].l, &a[i].r);
  55. a[i].id = i + 1;
  56. }
  57. sort(a, a + n);
  58. priority_queue<int, vector<int>, greater<int> >pq;
  59. int ans = 0;
  60. int t = 0;
  61. for(int i = 0; i < n; ++i){
  62. pq.push(a[i].r);
  63. if(pq.size() > m) pq.pop();
  64. int tmp = pq.top() - a[i].l + 1;
  65. if(pq.size() == m && ans < tmp){
  66. ans = tmp;
  67. t = a[i].l;
  68. }
  69. }
  70.  
  71. printf("%d\n", ans);
  72. if(!ans){
  73. printf("1");
  74. for(int i = 2; i <= m; ++i) printf(" %d", i);
  75. continue;
  76. }
  77. else{
  78. int cnt = 0;
  79. for(int i = 0; i < n && m; ++i) if(a[i].l <= t && a[i].r >= t + ans - 1){
  80. if(cnt) putchar(' ');
  81. printf("%d", a[i].id);
  82. --m;
  83. ++cnt;
  84. }
  85. }
  86. printf("\n");
  87. }
  88. return 0;
  89. }

CodeForces 754D Fedor and coupons (优先队列)的更多相关文章

  1. CodeForces 754D Fedor and coupons&&CodeForces 822C Hacker, pack your bags!

    D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  2. codeforces 754D. Fedor and coupons

    D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  3. CodeForces 754D Fedor and coupons ——(k段线段最大交集)

    还记得lyf说过k=2的方法,但是推广到k是其他的话有点麻烦.现在这里采取另外一种方法. 先将所有线段按照L进行排序,然后优先队列保存R的值,然后每次用最小的R值,和当前的L来维护答案即可.同时,如果 ...

  4. Codeforces 390Div2-754D. Fedor and coupons(贪心+优先队列)

    D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  5. cf754 754D - Fedor and coupons

    2个多小时,弱智了..(连A都做不对,就不要做D了(迷)) #include<bits/stdc++.h> #define lowbit(x) x&(-x) #define LL ...

  6. Codeforces I. Producing Snow(优先队列)

    题目描述: C. Producing Snow time limit per test 1 second memory limit per test 256 megabytes input stand ...

  7. 【codeforces 754D】Fedor and coupons

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. Codeforces Round #390 (Div. 2) D. Fedor and coupons(区间最大交集+优先队列)

    http://codeforces.com/contest/754/problem/D 题意: 给定几组区间,找k组区间,使得它们的公共交集最大. 思路: 在k组区间中,它们的公共交集=k组区间中右端 ...

  9. Codeforces Round #390 (Div. 2) D. Fedor and coupons

    题意:题目简化了就是要给你n个区间,然后让你选出k个区间  使得这k个区间有公共交集:问这个公共交集最大能是多少,并且输出所选的k个区间.如果有多组答案,则输出任意一种.   这题是用优先队列来处理区 ...

随机推荐

  1. php下载文件的一种方式

    <?php ob_start(); // $file_name="cookie.jpg"; $file_name="abc.jpg"; //用以解决中文不 ...

  2. 初识Selenium(四)

    用Selenium实现页面自动化测试 引言 要不要做页面测试自动化的争议由来已久,不做或少做的主要原因是其成本太高,其中一个成本就是自动化脚本的编写和维护,那么有没有办法降低这种成本呢?童战同学在其博 ...

  3. Java传参

    1.  如果参数是基本数据类型(int.long等),传值.方法内部改变参数值,外部值不变. 2.  如果参数是对象类型,传地址.方法内部改变对象值,外部对象值改变.但是,如果方法内部调用new重新构 ...

  4. IE6下整站的bug详解

    <1>文字不居中:加一个行高: <2>png文件作为背景不显示: <!--[if IE 6]> <script src="js/DD_belated ...

  5. android 原生的DownloadManager

    代码: public class MainActivity extends Activity { private DownloadManager downloadManager; public sta ...

  6. J2SE网络编程之 TCP与UDP

    1.什么是TCP TCP(Transmission Control Protocol传输控制协议)是一种面向连接的.可靠的.基于字节流的通信协议,位于传输层.这三个特点中,面向连接就如同打电话,双方的 ...

  7. 阅读 LdrInitializeThunk

    参考: http://blog.csdn.net/hw_henry2008/article/details/6568255 Windows 的 DLL 装入(除 ntdll.dll 外)和连接是通过 ...

  8. JSP文件上传--FileUpload组件

    如果使用上传操作,并且没有使用框架之类,最好使用Smartupload,因为FileUpdate太难使用. 下载组件: fileupload包:http://commons.apache.org/pr ...

  9. xmppserver

    http://highscalability.com/blog/2014/1/6/how-hipchat-stores-and-indexes-billions-of-messages-using-e ...

  10. 选择LDO的方法(转)

    http://www.micro-bridge.com/news/sort.asp?dy1=技术资料&dy2=产品相关资料&page=2 作者:LANDA PHAM   来源:德州仪器 ...