优先队列 CodeForces

来源:互联网 发布:webpackdevserver端口 编辑:程序博客网 时间:2024/06/06 20:04

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 hasn discount coupons, thei-th of them can be used with products with ids ranging fromli tori, inclusive. Today Fedor wants to take exactlyk coupons with him.

Fedor wants to choose the k coupons in such a way that the number of such productsx that all coupons can be used with this productx 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 andk (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 integersli andri ( - 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 integersp1, p2, ..., pk (1 ≤ pi ≤ n) — the ids of the coupons which Fedor should choose.

If there are multiple answers, print any of them.

Example
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 are31 products in total.

In the second example, no product can be bought with two coupons, that is why the answer is0. Fedor can choose any two coupons in this example.


题目大意就是在n个数据里,找到k个数据,使这k个数据的重叠区域最大。


优先队列里放right,优先队列从小到大排序,容量为k,top即为最小的元素。

对left排序,从小到大。

重叠区域最大即     最小的right-最大的left

遍历left,依次往优先队列里面放入对应的right值


#include <iostream>#include <algorithm>#include <cstring>#include <queue> #include <stdio.h>using namespace std;struct node{int r,l,index;}a[300005];bool cmp(node x,node y){if(x.l==y.l)return x.r<y.r;return x.l<y.l;}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("%d%d",&a[i].l,&a[i].r);a[i].index=i+1;}sort(a,a+n,cmp);priority_queue< int,vector<int>,greater<int> >que;int ans=0; int left=0;int right=0;int len=0;for(int i=0;i<n;i++){que.push(a[i].r);if(que.size()>k)que.pop();int len=que.top()-a[i].l+1;if(len>ans&&que.size()==k){ans=len;left=a[i].l;right=que.top();}}cout<<ans<<endl;if(!ans){cout<<"1";for(int i=2;i<=k;i++)cout<<" "<<i<<endl;continue;}else{for(int i=0;i<n&&k>0;i++){if(a[i].r>=right&&a[i].l<=left){cout<<a[i].index<<" ";k--;}}}    cout<<endl;}return 0;}


C++ Priority Queues(优先队列)

再讲解一下优先队列

头文件 <queue>


定义一个优先队列

1. 标准库默认 从大到小,大者优先

priority_queue<int> pq;

2. 数据越小,优先级越高

priority_queue<int, vector<int>, greater<int> >pq;

将greater改为less和1功能相同。

3. 自定义优先级,重载比较符号

struct node{    friend bool operator< (node n1, node n2)    {        return n1.priority < n2.priority;    }    int priority;    int value;}; 


相关函数
1.empty() 如果优先队列为空,则返回真
2.pop() 删除第一个元素
3.push() 加入一个元素
4.size() 返回优先队列中拥有的元素的个数
5.top() 返回优先队列中有最高优先级的元素


C++ Queues(队列)

C++队列是一种容器适配器,它给予程序员一种先进先出(FIFO)的数据结构。
1.back() 返回一个引用,指向最后一个元素
2.empty() 如果队列空则返回真
3.front() 返回第一个元素
4.pop() 删除第一个元素
5.push() 在末尾加入一个元素
6.size() 返回队列中元素的个数

队列可以用线性表(list)或双向队列(deque)来实现(注意vector container 不能用来实现queue,因为vector 没有成员函数pop_front!):
queue<list<int>> q1;
queue<deque<int>> q2;
其成员函数有“判空(empty)” 、“尺寸(Size)” 、“首元(front)” 、“尾元(backt)” 、“加入队列(push)” 、“弹出队列(pop)”等操作。

1 int main()2 {3     queue<int> q;4     q.push(4);5     q.push(5);6     printf("%d\n",q.front());7     q.pop();8 }



原创粉丝点击