码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 754D Fedor and coupons 优先队列

时间:2017-10-24 00:02:10      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:integer   ble   bit   line   struct   type   ref   sts   ret   

题意:给出一个n和一个k,分别表示给出的区间个数以及要选出的符合要求的区间个数,那么什么样的区间是符合要求的呢?要求选出的k个区间拥有最大的重叠区间。注意这k个区间必须都是重叠的
不能存在其中含有不重叠的存在。
 
思路:要找出k个区间的最长重叠区间,可以维护一个大小为k的优先队列,队列的排序方式是按照右边从小到大排序的。每次将所有的区间加入到这个队列中去,当然所有的区间首先需要按照左边从小到达排序。这样就可以在求最大重叠区间的时候,就可以不用考虑左边的影响,因为每加入一个区间,都会使得最大的重叠区间的左边随着新加入的区间的左边变化。这样我们也可以知道了,最大重叠区间的大小
就会是,每次队列头部的区间的右边减去每次新加入的区间的左边。当然要能加入必须满足要加入的区间的右边要大于队列头部区间的右边,只要记录下最大值就好了。
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.

#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 优先队列

标签:integer   ble   bit   line   struct   type   ref   sts   ret   

原文地址:http://www.cnblogs.com/yewa/p/7719606.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!