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

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

  这题是用优先队列来处理区间问题的,感觉挺典型的所以记录下来。

  首先,要知道 选取的k个区间的最大交集=区间右端点中的最小值-区间左端点中的最大值。那么,要求得这这么k个区间是公共交集最大,就创建一个最小堆的优先队列(只存放区间的右端点);然后按左端点从小到大(先将区间按左端点排序)将区间放入优先队列中。每当优先队列的大小为k+1时,就pop出最小的右端点。当区间[l,r]作为第k+1个区间push进优先队列的时候,又因为区间是按左端点从小到大放入的,所以这个区间的l一定当前最大的左端点,然后将最小的右端点pop后就检查是否比已记录的max长度更长;如果是,就更新最长公共交集的l和r,说明这第i个区间有用到(注:我们只用记录最长长度和对应的l和r即可),否则就不更新,即没用到这个区间。

  博主文笔实在拙劣,上面思路没懂的话直接看代码吧:

 1 /**
 2 * @author Wixson
 3 */
 4 #include <iostream>
 5 #include <cstdio>
 6 #include <cstring>
 7 #include <cmath>
 8 #include <algorithm>
 9 #include <queue>
10 #include <stack>
11 #include <vector>
12 #include <utility>
13 #include <map>
14 #include <set>
15 const int inf=0x3f3f3f3f;
16 const double PI=acos(-1.0);
17 const double EPS=1e-8;
18 using namespace std;
19 typedef long long ll;
20 typedef pair<int,int> P;
21 
22 int n,k;
23 typedef struct node
24 {
25     int l,r;
26     int pos;
27 } node;
28 node a[300050];
29 bool cmp(node a,node b)
30 {
31     return a.l<b.l;
32 }
33 priority_queue<int,vector<int>,greater<int> > pq;
34 int main()
35 {
36     //freopen("input.txt","r",stdin);
37     scanf("%d%d",&n,&k);
38     for(int i=1; i<=n; i++)
39     {
40         scanf("%d%d",&a[i].l,&a[i].r);
41         a[i].pos=i;
42     }
43     //
44     int ans=0,len=0,ans_l,ans_r;
45     sort(a+1,a+1+n,cmp);
46     for(int i=1; i<=n; i++)
47     {
48         pq.push(a[i].r);
49         if(pq.size()>k) pq.pop(); //当第k+1个区间放入后,弹出最小的右端点
50         if(pq.size()==k)
51         {
52             len=pq.top()-a[i].l+1; //取当前公共交集长度
53         }
54         //
55         if(ans<len)  //更新公共交集的最大长度及对应的L和R
56         {
57             ans=len;
58             ans_l=a[i].l;
59             ans_r=pq.top();
60         }
61     }
62     //
63     printf("%d
",ans);
64     if(ans==0) //如果不存在解
65     {
66         for(int i=1;i<=k;i++) printf("%d ",i);
67     }
68     else
69     {
70         for(int i=1; i<=n; i++)
71         {                    //选取位于最大公共交集L和R之间的区间
72             if(a[i].l<=ans_l&&a[i].r>=ans_r)
73             {
74                 printf("%d ",a[i].pos);
75                 k--;
76             }
77             if(!k) break;
78         }
79     }
80     return 0;
81 }
原文地址:https://www.cnblogs.com/geek1116/p/6360499.html