codeforces 754D Fedor and coupons【优先队列+贪心*好题】

本文介绍了一种通过优先队列维护区间覆盖的问题解决方案。具体地,对于拥有n张优惠券的用户,每张优惠券可优惠指定范围内的商品,目标是在选择k张优惠券的情况下使能够同时享受这k张优惠券优惠的商品数量最大化,并输出最优的优惠券组合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Fedor and coupons
time limit per test
4 seconds

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 wi 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 thei-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.

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 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张优惠券,每张优惠券可以可以优惠 L 到 R 区间中的商品。 现在仅选择k张优惠券,问能优惠k次的商品最多有多少个,以及输出优惠的区间编号。

思路:贪心预处理一下(把左端sort一下,遍历一遍压入队列),然后用优先队列不断维护K个区间(优先右端排序,此K个区间的值 = 队顶.R - S【i】.L + 1),求更新区间的最大值就可以了;

priority_queue<int, vector<int>, greater<int> >q1;
priority_queue<int, vcetor<int>, less<int> >q2;

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
using namespace std;

const int MAXN = 3 * 1e5 + 5;

struct node {
	int L;
	int R;
	bool friend operator <(node x, node y) {
		return x.R > y.R;
	} 
}s[MAXN], ss[MAXN];

bool cmp(node x, node y) {
	return x.L < y.L;
}

int main() {
	int n, k;
	priority_queue<node> que; //priority_queue<int, vector<int>, greater<int> > que; 
	scanf("%d %d", &n, &k);
	for(int i = 1; i <= n; i++) {
		scanf("%d %d", &s[i].L, &s[i].R);
		ss[i] = s[i];
	}
	sort(s + 1, s + n + 1, cmp);
	int cnt = 0, maxn = 0, left, right;
	for(int i = 1; i <= n; i++) { //遍历左端,用队列维护K个区间; 
		que.push(s[i]);
		if(que.size() < k) continue;
		node e = que.top();
		cnt = e.R - s[i].L + 1;
		if(cnt > maxn) {
			maxn = cnt;
			left = s[i].L;
			right = e.R;
		}
		que.pop();
	}
	printf("%d\n", maxn);
	if(maxn == 0) {
		for(int i = 1; i < k; i++) {
			printf("%d ", i);
		}
		printf("%d\n", k);
		return 0;
	}
	int ans = 0;
	for(int i = 1; i <= n; i++) {
		if(ss[i].L <= left && ss[i].R >= right) {
			if(ans) printf(" ");
			printf("%d", i);
			if(++ans == k) break; //可能有多于K个区间的值,注意结束输出 
		}
	}
	printf("\n");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值