codeforces754D Fedor and coupons
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!
Description
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!
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.
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.
4 2
1 100
40 70
120 130
125 180
31
1 2
3 2
1 12
15 20
25 30
0
1 2
5 2
1 10
5 15
14 50
30 70
99 100
21
3 4
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条使得交集长度尽可能长,输出最优值和方案。
我开始想了很久的单调性,但是并不能实现单调决策,更不能还原历史版本。所以我就想了想,似乎带个log就很可做了?
考虑先按左端点排序,维护一个右端点坐标的小根堆,那么很容易发现我只需要保证堆的大小始终小于等于k即可。当我每次扫到一个左端点时,将其右端点与堆顶作比较,如果比堆顶小则不作考虑,否则,删除堆顶,把这个新的右端点坐标加入堆中。每次只需用堆顶减去当前处理的线段的左端点来更新答案(当且仅当堆中恰好有k个元素)。输出方案的话,用同样方法再做一次即可。
//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
using namespace std;
typedef long long LL;
const int MAXN = 300011;
int n,k,ans,dui[MAXN];
struct node{int pos,id; inline bool operator < (const node &a)const{ return a.pos<pos; } }tmp;
priority_queue<node>Q;
struct seq{int l,r,id;}a[MAXN];
inline bool cmp(seq q,seq qq){ return q.l<qq.l; }
inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} inline void work(){
n=getint(); k=getint(); for(int i=1;i<=n;i++) a[i].l=getint(),a[i].r=getint(),a[i].id=i;
sort(a+1,a+n+1,cmp); ans=-1;//!!!
for(int i=1;i<=n;i++) {
if(!Q.empty())tmp=Q.top();
if((int)Q.size()<k) {
tmp.pos=a[i].r;
tmp.id=i;
Q.push(tmp);
}
else {
if(a[i].r>tmp.pos) {
Q.pop();
tmp.pos=a[i].r;
tmp.id=i;
Q.push(tmp);
}
}
if((int)Q.size()>=k) ans=max(Q.top().pos-a[i].l,ans);
}
printf("%d\n",ans+1);
if(ans==-1) { for(int i=1;i<=k;i++) printf("%d ",i); return ; } int lans=ans; ans=-1;
while(!Q.empty()) Q.pop();
for(int i=1;i<=n;i++) {
if(!Q.empty()) tmp=Q.top();
if((int)Q.size()<k) {
tmp.pos=a[i].r;
tmp.id=a[i].id;//!!!
Q.push(tmp);
}
else {
if(a[i].r>tmp.pos) {
Q.pop();
tmp.pos=a[i].r;
tmp.id=a[i].id;//!!!
Q.push(tmp);
}
}
if((int)Q.size()>=k) {
ans=max(Q.top().pos-a[i].l,ans);
if(ans==lans) {
int cnt=0;
while(!Q.empty()) {
tmp=Q.top();
dui[++cnt]=tmp.id;
Q.pop();
}
sort(dui+1,dui+k+1);
for(int i=1;i<=k;i++) printf("%d ",dui[i]);
return ;
}
}
}
} int main()
{
work();
return 0;
}
codeforces754D Fedor and coupons的更多相关文章
- codeforces 754D. Fedor and coupons
D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- Codeforces 390Div2-754D. Fedor and coupons(贪心+优先队列)
D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- CodeForces 754D Fedor and coupons&&CodeForces 822C Hacker, pack your bags!
D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- 【codeforces 754D】Fedor and coupons
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- CodeForces 754D Fedor and coupons (优先队列)
题意:给定n个优惠券,每张都有一定的优惠区间,然后要选k张,保证k张共同的优惠区间最大. 析:先把所有的优惠券按左端点排序,然后维护一个容量为k的优先队列,每次更新优先队列中的最小值,和当前的右端点, ...
- Codeforces Round #390 (Div. 2) D. Fedor and coupons(区间最大交集+优先队列)
http://codeforces.com/contest/754/problem/D 题意: 给定几组区间,找k组区间,使得它们的公共交集最大. 思路: 在k组区间中,它们的公共交集=k组区间中右端 ...
- D. Fedor and coupons 二分暴力
http://codeforces.com/contest/754/problem/D 给出n条线段,选出k条,使得他们的公共部分长度最大. 公共部分的长度,可以二分出来,为val.那么怎么判断有k条 ...
- Codeforces Round #390 (Div. 2) D. Fedor and coupons
题意:题目简化了就是要给你n个区间,然后让你选出k个区间 使得这k个区间有公共交集:问这个公共交集最大能是多少,并且输出所选的k个区间.如果有多组答案,则输出任意一种. 这题是用优先队列来处理区 ...
- CodeForces 754D Fedor and coupons ——(k段线段最大交集)
还记得lyf说过k=2的方法,但是推广到k是其他的话有点麻烦.现在这里采取另外一种方法. 先将所有线段按照L进行排序,然后优先队列保存R的值,然后每次用最小的R值,和当前的L来维护答案即可.同时,如果 ...
随机推荐
- Java面试题整理一(侧重多线程并发)
1..是否可以在static环境中访问非static变量? 答:static变量在Java中是属于类的,它在所有的实例中的值是一样的.当类被Java虚拟机载入的时候,会对static变量进行初始化.如 ...
- Hibernnate延迟加载策略(这么详细你还看不懂)
好久没有认真写过博客了,今天就好好的写一篇吧!!!!!!!!! 当Hibernate 从数据库中加载某个对象(例如:Dept对象)时,如果同时自动加载所有的关联的某个对象(例如:Emp对象),而程序实 ...
- JavaScript学习笔记4之 ByClass&json
一.通过class获取标签 var out=document.getElementsByClassName(‘out’);IE 6 7 8 不支持 getElementsName 是否有办法既能通过c ...
- ArcGIS Engine开发之鹰眼视图
鹰眼是GIS软件的必备功能之一.它是一个MapControl控件,主要用来表示数据视图中的地理范围在全图中的位置. 鹰眼一般具有的功能: 1)鹰眼视图与数据视图的地理范围保持同步. 2)数据视图的当前 ...
- IT菜鸟的生存指南(三)流行还是经典
经常被刚入行的新人请教,想学一门开发语言,最好又简单工资又高又有发展前途.那门语言最好这个话题能在程序员群里吵一下午,所以我也就不掀起战争了. 个人建议如下: 工资高不高不在于学那门语言,而在于你的行 ...
- AngularJS API
AngularJS 全局 API 用于执行常见任务的 JavaScript 函数集合 angular.lowercase() 转换字符串为小写 angular.uppercase() 转换字符串为大写 ...
- JAVA NIO Buffer
所谓的输入,输出,就是把数据移除或移入缓冲区. 硬件不能直接访问用户控件(JVM). 基于存储的硬件设备操控的是固定大小的数据块儿,用户请求的是任意大小的或非对齐的数据块儿. 虚拟内存:使用虚 ...
- 3.raid基础应用
raid分为软备份和硬备份 软备份主要用来实验 应备份用于生产环境 raid0(带区卷) 具有很高的数据传输率,没有数据的冗余 1块磁盘 raid1(镜像卷) 提供数据冗余,利用率低 2块 ...
- MPLS与LDP从入门到了解
多协议标签交换(MPLS)是一种用于快速转发数据包的技术,它的出现就是为了提高转发效率.因为IP转发大多靠软件进行,在转发的每一跳都要进行至少一次最长匹配查找,操作复杂导致转发速度比较慢.有些厂商借鉴 ...
- Caffe Python MemoryDataLayer Segmentation Fault
转载请注明出处,楼燚(yì)航的blog,http://home.cnblogs.com/louyihang-loves-baiyan/ 因为利用Pyhon来做数据的预处理比较方便,因此在data_l ...