Codeforces 754D Fedor and coupons 优先队列
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 754D Fedor and coupons 优先队列相关的知识,希望对你有一定的参考价值。
All our characters have hobbies. The same is true for Fedor. He enjoys shopping in the neighboring supermarket.
The goods in the supermarket have unique integer ids. Also, for every integer there is a product with id equal to this integer. Fedor has ndiscount coupons, the i-th of them can be used with products with ids ranging from li to ri, inclusive. Today Fedor wants to take exactly kcoupons with him.
Fedor wants to choose the k coupons in such a way that the number of such products x that all coupons can be used with this product x is as large as possible (for better understanding, see examples). Fedor wants to save his time as well, so he asks you to choose coupons for him. Help Fedor!
The first line contains two integers n and k (1?≤?k?≤?n?≤?3·105) — the number of coupons Fedor has, and the number of coupons he wants to choose.
Each of the next n lines contains two integers li and ri (?-?109?≤?li?≤?ri?≤?109) — the description of the i-th coupon. The coupons can be equal.
In the first line print single integer — the maximum number of products with which all the chosen coupons can be used. The products with which at least one coupon cannot be used shouldn‘t be counted.
In the second line print k distinct integers p1,?p2,?...,?pk (1?≤?pi?≤?n) — the ids of the coupons which Fedor should choose.
If there are multiple answers, print any of them.
4 2
1 100
40 70
120 130
125 180
31
1 2
3 2
1 12
15 20
25 30
0
1 2
5 2
1 10
5 15
14 50
30 70
99 100
21
3 4
In the first example if we take the first two coupons then all the products with ids in range [40,?70] can be bought with both coupons. There are 31 products in total.
In the second example, no product can be bought with two coupons, that is why the answer is 0. Fedor can choose any two coupons in this example.
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #include<vector> #include<stack> #include<bitset> #include<cstdlib> #include<cmath> #include<set> #include<list> #include<deque> #include<map> #include<queue> using namespace std; typedef long long ll; const double PI = acos(-1.0); const double eps = 1e-6; const int mod =1000000; const int maxn =300110; struct point { int nend; int nsta; int flag; }; point aa[maxn]; bool cmp(point a,point b) { return a.nsta<b.nsta; } int bb[maxn]; struct cmp1 { bool operator ()(point x,point y) { return x.nend>y.nend; } }; int main() { int n,k; priority_queue<point,vector<point>,cmp1>q1; scanf("%d%d",&n,&k); int i,j; for(i=0;i<n;i++) { scanf("%d%d",&aa[i].nsta,&aa[i].nend); aa[i].flag=i+1; } sort(aa,aa+n,cmp); int sum=0; int max_x=0; int num2=0; for(i=0;i<n;i++) { q1.push(aa[i]); if(q1.size()>k) q1.pop(); point p1; p1=q1.top(); sum=p1.nend-aa[i].nsta+1; if(max_x<sum&&q1.size()==k) { max_x=sum; num2=aa[i].nsta; } } if(max_x==0) { printf("0\n"); for(int ii=1;ii<k;ii++) { printf("%d ",ii); } printf("%d\n",k); } else { printf("%d\n",max_x); int num3=k; for(i=0;i<n;i++) { if(aa[i].nsta<=num2&&max_x+num2-1<=aa[i].nend&&num3>1) { printf("%d ",aa[i].flag); num3--; } else if(aa[i].nsta<=num2&&max_x+num2-1<=aa[i].nend&&num3==1) { printf("%d\n",aa[i].flag); num3--; break; } } } return 0; }
以上是关于Codeforces 754D Fedor and coupons 优先队列的主要内容,如果未能解决你的问题,请参考以下文章
CodeForces 754D Fedor and coupons (优先队列)
CodeForces 754D Fedor and coupons ——(k段线段最大交集)
cf754 754D - Fedor and coupons