您的位置:首页 > Web前端

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

2017-06-04 20:40 323 查看
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 2
1 100
40 70
120 130
125 180


output
31
1 2


input
3 2
1 12
15 20
25 30


output
0
1 2


input
5 2
1 10
5 15
14 50
30 70
99 100


output
21
3 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?

#include <stdio.h>  
#include <queue>  
#include <string.h>  
#include <algorithm>  
typedef long long ll;  
using namespace std;  
  
struct node{  
    ll l;  
    ll r;   
    ll index;  
}a[300005];  
int cmp(node x,node y){  
    if (x.l==y.l)  
    return x.r<y.r;  
    return x.l<y.l;  
}  
struct cmp2{  
      
    bool operator()(ll x,ll y)  
    {  
        return x > y;  
    }  
};  
int main (){  
    int n,k;  
    while (scanf("%d%d",&n,&k)!=EOF){  
        memset(a,0,sizeof(a));  
        for (int i=0;i<n;i++){  
            scanf ("%lld%lld",&a[i].l,&a[i].r);  
            a[i].index=i+1;  
        }  
        sort(a,a+n,cmp);  
        priority_queue<ll,vector<ll>,cmp2> Q;  
        ll left=0;   
        ll right=0;  
        ll len=0;  
        ll ans=-1;  
        for (int i=0;i<n;i++){  
            Q.push(a[i].r);  
            if (Q.size()>k)  
                Q.pop();  
          
            if (Q.size()==k){//    求最大重叠区域    记录左端点     
                len=Q.top()-a[i].l+1;//   左端点最大值减去右端点最小值+1 == 区间长度   
                //printf ("%lld####\n",len);  
            }  
            if(ans<len){  
                ans=len;  
                //记住最大左端点和最小右端点   
                left=a[i].l;  
                right=Q.top();  
            }   
        }  
          
        printf ("%lld\n",ans);  
        if (!ans){// 区间大小为0  任意选取k个区间        
            printf ("1");  
            for (int i=2;i<=k;i++)  
            printf (" %d",i);  
            continue;  
        }   
        for (int i=0;i<n&&k>0;i++){//       输出区间   
            if (a[i].r>=right&&a[i].l<=left){  
                printf ("%d ",a[i].index);  
                k--;  
            }  
        }  
        putchar('\n');  
    }  
    return 0;  
}  

#include <stdio.h>
#include <queue>
#include <string.h>
#include <algorithm>
typedef long long ll;
using namespace std;

struct node{
ll l;
ll r;
ll index;
}a[300005];
int cmp(node x,node y){
if (x.l==y.l)
return x.r<y.r;
return x.l<y.l;
}
struct cmp2{

bool operator()(ll x,ll y)
{
return x > y;
}
};
int main (){
int n,k;
while (scanf("%d%d",&n,&k)!=EOF){
memset(a,0,sizeof(a));
for (int i=0;i<n;i++){
scanf ("%lld%lld",&a[i].l,&a[i].r);
a[i].index=i+1;
}
sort(a,a+n,cmp);
priority_queue<ll,vector<ll>,cmp2> Q;
ll left=0;
ll right=0;
ll len=0;
ll ans=-1;
for (int i=0;i<n;i++){
Q.push(a[i].r);
if (Q.size()>k)
Q.pop();

if (Q.size()==k){//    求最大重叠区域    记录左端点
len=Q.top()-a[i].l+1;//   左端点最大值减去右端点最小值+1 == 区间长度
//printf ("%lld####\n",len);
}
if(ans<len){
ans=len;
//记住最大左端点和最小右端点
left=a[i].l;
right=Q.top();
}
}

printf ("%lld\n",ans);
if (!ans){// 区间大小为0  任意选取k个区间
printf ("1");
for (int i=2;i<=k;i++)
printf (" %d",i);
continue;
}
for (int i=0;i<n&&k>0;i++){//       输出区间
if (a[i].r>=right&&a[i].l<=left){
printf ("%d ",a[i].index);
k--;
}
}
putchar('\n');
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: