D. Fedor and coupons(贪心+优先队列)

来源:互联网 发布:mac 安装mysql tar.gz 编辑:程序博客网 时间:2024/06/16 17:50
http://blog.csdn.net/mm__1997/article/details/72830106
D. Fedor and coupons
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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!

Input

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.

Output

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.

Examples
input
4 21 10040 70120 130125 180
output
311 2 
input
3 21 1215 2025 30
output
01 2 
input
5 21 105 1514 5030 7099 100
output
213 4 
Note

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.




题目大意:

每个商品有对应的编号    有n张优惠券     每张优惠券可以可以优惠 li ri 区间中的商品     现在选择k张优惠

券     问能优惠k次的商品最多有多少个   怎么选择优惠券


分析:

题目的本质意思是    从n个区间中任意选取k个区间使重叠区域为k次    求出k次重叠区域的最大值     并输出所选区域的编号   分析后可发现     选取的k个区域 k次重叠区间大小 == 右端点最小值 - 左端点最大值 + 1    若所选k个区间重叠次数不为k次   得出区间大小为负数  符合题目要求   所以   按左端点将区间降序排列    用优先队列维护一个k值为区间个数  便可解决





AC代码:

[cpp] view plain copy
print?
  1. #include <stdio.h>  
  2. #include <queue>  
  3. #include <string.h>  
  4. #include <algorithm>  
  5. typedef long long ll;  
  6. using namespace std;  
  7.   
  8. struct node{  
  9.     ll l;  
  10.     ll r;   
  11.     ll index;  
  12. }a[300005];  
  13. int cmp(node x,node y){  
  14.     if (x.l==y.l)  
  15.     return x.r<y.r;  
  16.     return x.l<y.l;  
  17. }  
  18. struct cmp2{  
  19.       
  20.     bool operator()(ll x,ll y)  
  21.     {  
  22.         return x > y;  
  23.     }  
  24. };  
  25. int main (){  
  26.     int n,k;  
  27.     while (scanf("%d%d",&n,&k)!=EOF){  
  28.         memset(a,0,sizeof(a));  
  29.         for (int i=0;i<n;i++){  
  30.             scanf ("%lld%lld",&a[i].l,&a[i].r);  
  31.             a[i].index=i+1;  
  32.         }  
  33.         sort(a,a+n,cmp);  
  34.         priority_queue<ll,vector<ll>,cmp2> Q;  
  35.         ll left=0;   
  36.         ll right=0;  
  37.         ll len=0;  
  38.         ll ans=-1;  
  39.         for (int i=0;i<n;i++){  
  40.             Q.push(a[i].r);  
  41.             if (Q.size()>k)  
  42.                 Q.pop();  
  43.           
  44.             if (Q.size()==k){//    求最大重叠区域    记录左端点     
  45.                 len=Q.top()-a[i].l+1;//   左端点最大值减去右端点最小值+1 == 区间长度   
  46.                 //printf ("%lld####\n",len);  
  47.             }  
  48.             if(ans<len){  
  49.                 ans=len;  
  50.                 //记住最大左端点和最小右端点   
  51.                 left=a[i].l;  
  52.                 right=Q.top();  
  53.             }   
  54.         }  
  55.           
  56.         printf ("%lld\n",ans);  
  57.         if (!ans){// 区间大小为0  任意选取k个区间        
  58.             printf ("1");  
  59.             for (int i=2;i<=k;i++)  
  60.             printf (" %d",i);  
  61.             continue;  
  62.         }   
  63.         for (int i=0;i<n&&k>0;i++){//       输出区间   
  64.             if (a[i].r>=right&&a[i].l<=left){  
  65.                 printf ("%d ",a[i].index);  
  66.                 k--;  
  67.             }  
  68.         }  
  69.         putchar('\n');  
  70.     }  
  71.     return 0;  
  72. }  


原创粉丝点击