Codeforces Round #390(Div. 2)D. Fedor and coupons【优先队列】
2017-01-07 13:20
525 查看
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
n discount 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
k coupons 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
Output
Input
Output
Input
Output
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个区间,让你找到K个区间,使其交集最大,输出编号以及交集区间长度。
思路:
1、首先我们按照区间左端点从小到大排序,接下来我们进行动态维护。我们建立起一个优先队列,将每一个区间都扔进去,保证队列中只有K个元素,那么每一次队头元素和每一次进来的元素的左端点,就能够确定当前选的K个区间的交集,那么我们每次都维护这个部分的最大即可。
2、记录交集最大的同时,记录一下此时交集的左端点和右端点,那么在取哪K个区间的时候,只要能够保证那个区间能够包含住这个交集区间即可,选出K个输出。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
struct node
{
int l,r;
int pos;
}a[300050];
int cmp(node a,node b)
{
return a.l<b.l;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=0;i<n;i++)
{
scanf("%d%d",&a[i].l,&a[i].r);
a[i].pos=i+1;
}
int L;
int R;
int ans=0;
priority_queue<int, vector<int>, greater<int> >s;
sort(a,a+n,cmp);
for(int i=0;i<n;i++)
{
int len=0;
s.push(a[i].r);
if(s.size()>m)s.pop();
if(s.size()==m)len=s.top()-a[i].l+1;
if(ans<len)
{
ans=len;
L=a[i].l;
R=s.top();
}
}
printf("%d\n",ans);
if(ans==0)
{
for(int i=1;i<=m;i++)
{
printf("%d ",i);
}
printf("\n");
}
else
{
for(int i=0;i<n;i++)
{
if(m>0)
{
if(a[i].l<=L&&a[i].r>=R)
{
printf("%d ",a[i].pos);
m--;
}
}
}
printf("\n");
}
}
}
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
n discount 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
k coupons 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个区间,让你找到K个区间,使其交集最大,输出编号以及交集区间长度。
思路:
1、首先我们按照区间左端点从小到大排序,接下来我们进行动态维护。我们建立起一个优先队列,将每一个区间都扔进去,保证队列中只有K个元素,那么每一次队头元素和每一次进来的元素的左端点,就能够确定当前选的K个区间的交集,那么我们每次都维护这个部分的最大即可。
2、记录交集最大的同时,记录一下此时交集的左端点和右端点,那么在取哪K个区间的时候,只要能够保证那个区间能够包含住这个交集区间即可,选出K个输出。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
struct node
{
int l,r;
int pos;
}a[300050];
int cmp(node a,node b)
{
return a.l<b.l;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=0;i<n;i++)
{
scanf("%d%d",&a[i].l,&a[i].r);
a[i].pos=i+1;
}
int L;
int R;
int ans=0;
priority_queue<int, vector<int>, greater<int> >s;
sort(a,a+n,cmp);
for(int i=0;i<n;i++)
{
int len=0;
s.push(a[i].r);
if(s.size()>m)s.pop();
if(s.size()==m)len=s.top()-a[i].l+1;
if(ans<len)
{
ans=len;
L=a[i].l;
R=s.top();
}
}
printf("%d\n",ans);
if(ans==0)
{
for(int i=1;i<=m;i++)
{
printf("%d ",i);
}
printf("\n");
}
else
{
for(int i=0;i<n;i++)
{
if(m>0)
{
if(a[i].l<=L&&a[i].r>=R)
{
printf("%d ",a[i].pos);
m--;
}
}
}
printf("\n");
}
}
}
相关文章推荐
- Fedor and coupons CodeForces - 754D (优先队列,贪心)
- Codeforces 754 D Fedor and coupons【优先队列】
- Codeforces Round #243 (Div. 2) C. Sereja and Swaps(优先队列 暴力)
- Codeforces Round #390 (Div. 2)D Fedor and coupons
- Codeforces Round #390 (Div. 2) D. Fedor and coupons
- Codeforces Round #390 (Div. 2) D. Fedor and coupons 贪心+优先队列
- D. Fedor and coupons Codeforces Round #390 (Div. 2)(优先队列)
- Codeforces Round #390 (Div. 2) D. Fedor and coupons
- #Codeforces 343 [div2] D. Babaei and Birthday Cake 【线段树优先队列查询优化】
- 【VK Cup 2016 - Round 1 (Div 2 Edition)E】【目标线思想差值相减 优先队列】Bear and Contribution c5成本+5 c1成本+1使得至少k人贡献相
- Codeforces Round #390 (Div. 2)-DFedor and coupons(优先队列)
- #Codeforces 381 [div2] D. Alyona and a tree 【树链剖分 + 优先队列】
- Codeforces Round #390 (Div. 2) D. Fedor and coupons(区间最大交集+优先队列)
- E. President and Roads (CF Round #Pi (Div. 2)) 优先队列的Dij+Tarjan找桥
- CF 257 DIV 2 D. Jzzhu and Cities (学会了使用优先队列优化的Dij 和 静态链表存储)
- Codeforces Round #390 (Div. 2) D - Fedor and coupons (贪心)
- codeforces round390 div2 C Fedor and coupons
- Codeforces Round #390 (Div. 2)DFedor and coupons (排序+优先队列)
- Codeforces Round #390 (Div. 2) D. Fedor and coupons
- UVA 540 and UVA 136 队列及优先队列