题意:题目简化了就是要给你n个区间,然后让你选出k个区间  使得这k个区间有公共交集;问这个公共交集最大能是多少,并且输出所选的k个区间。如果有多组答案,则输出任意一种。

  这题是用优先队列来处理区间问题的,感觉挺典型的所以记录下来。

  首先,要知道 选取的k个区间的最大交集=区间右端点中的最小值-区间左端点中的最大值。那么,要求得这这么k个区间是公共交集最大,就创建一个最小堆的优先队列(只存放区间的右端点);然后按左端点从小到大(先将区间按左端点排序)将区间放入优先队列中。每当优先队列的大小为k+1时,就pop出最小的右端点。当区间[l,r]作为第k+1个区间push进优先队列的时候,又因为区间是按左端点从小到大放入的,所以这个区间的l一定当前最大的左端点,然后将最小的右端点pop后就检查是否比已记录的max长度更长;如果是,就更新最长公共交集的l和r,说明这第i个区间有用到(注:我们只用记录最长长度和对应的l和r即可),否则就不更新,即没用到这个区间。

  博主文笔实在拙劣,上面思路没懂的话直接看代码吧:

 /**
* @author Wixson
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <map>
#include <set>
const int inf=0x3f3f3f3f;
const double PI=acos(-1.0);
const double EPS=1e-;
using namespace std;
typedef long long ll;
typedef pair<int,int> P; int n,k;
typedef struct node
{
int l,r;
int pos;
} node;
node a[];
bool cmp(node a,node b)
{
return a.l<b.l;
}
priority_queue<int,vector<int>,greater<int> > pq;
int main()
{
//freopen("input.txt","r",stdin);
scanf("%d%d",&n,&k);
for(int i=; i<=n; i++)
{
scanf("%d%d",&a[i].l,&a[i].r);
a[i].pos=i;
}
//
int ans=,len=,ans_l,ans_r;
sort(a+,a++n,cmp);
for(int i=; i<=n; i++)
{
pq.push(a[i].r);
if(pq.size()>k) pq.pop(); //当第k+1个区间放入后,弹出最小的右端点
if(pq.size()==k)
{
len=pq.top()-a[i].l+; //取当前公共交集长度
}
//
if(ans<len) //更新公共交集的最大长度及对应的L和R
{
ans=len;
ans_l=a[i].l;
ans_r=pq.top();
}
}
//
printf("%d\n",ans);
if(ans==) //如果不存在解
{
for(int i=;i<=k;i++) printf("%d ",i);
}
else
{
for(int i=; i<=n; i++)
{ //选取位于最大公共交集L和R之间的区间
if(a[i].l<=ans_l&&a[i].r>=ans_r)
{
printf("%d ",a[i].pos);
k--;
}
if(!k) break;
}
}
return ;
}

Codeforces Round #390 (Div. 2) D. Fedor and coupons的更多相关文章

  1. Codeforces Round #390 (Div. 2) D. Fedor and coupons(区间最大交集+优先队列)

    http://codeforces.com/contest/754/problem/D 题意: 给定几组区间,找k组区间,使得它们的公共交集最大. 思路: 在k组区间中,它们的公共交集=k组区间中右端 ...

  2. Codeforces Round #390 (Div. 2)

    时隔一个月重返coding…… 期末复习了一个月也不亏 倒是都过了…… 就是计组61有点亏 复变68也太低了 其他都还好…… 假期做的第一场cf 三道题 还可以…… 最后room第三 standing ...

  3. Codeforces Round #390 (Div. 2) A+B+D!

    A. Lesha and array splitting 水题模拟.(0:10) 题意:给你一个n个元素的数组,求能否把这个数组分成若干连续小段,使得每段的和不为0.如有多种解输出任意一个. 思路:搞 ...

  4. Codeforces Round #267 (Div. 2) D. Fedor and Essay tarjan缩点

    D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. Codeforces Round #390 (Div. 2) C. Vladik and chat(dp)

    http://codeforces.com/contest/754/problem/C C. Vladik and chat time limit per test 2 seconds memory ...

  6. Codeforces Round #390 (Div. 2) A. Lesha and array splitting

    http://codeforces.com/contest/754/problem/A 题意: 给出一串序列,现在要把这串序列分成多个序列,使得每一个序列的sum都不为0. 思路: 先统计一下不为0的 ...

  7. Codeforces Round #267 (Div. 2) B. Fedor and New Game【位运算/给你m+1个数让你判断所给数的二进制形式与第m+1个数不相同的位数是不是小于等于k,是的话就累计起来】

    After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play ...

  8. Codeforces Round #267 (Div. 2) B. Fedor and New Game

    After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play ...

  9. Codeforces Round #390 (Div. 2) D

    All our characters have hobbies. The same is true for Fedor. He enjoys shopping in the neighboring s ...

随机推荐

  1. flask中的蓝图(BluePrint)

    蓝图,听起来就是一个很宏伟的东西 在Flask中的蓝图 blueprint 也是非常宏伟的 它的作用就是将 功能 与 主服务 分开怎么理解呢? 比如说,你有一个客户管理系统,最开始的时候,只有一个查看 ...

  2. Linux查找目录下的按时间过滤的文件

    在维护项目中,有时会指定都一些条件进行过滤文件,并对该批文件进行操作:这时我们将使用shell命令进行操作:直接上代码 #!/bin/sh #BEGIN #`find ./ ! -name " ...

  3. ie9以下的浏览器兼容性问题

    .bind不兼容的问题Function.prototype.bind = function () { var fn = this, args = Array.prototype.slice.call( ...

  4. tomcat报错org.springframework.web.context.ContextLoaderListener找不到

    tomcat报错org.springframework.web.context.ContextLoaderListener找不到. 最后解决办法:将jar包copy到web-inf下面的lib中. 你 ...

  5. HDU_1532_最大流

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. C# 计算百分比

    //计算比率 decimal A =(decimal) 200.20; decimal B = (decimal)1000.20; decimal t = decimal.Parse((A/B).To ...

  7. day02-操作系统、编程语言分类及python安装

    目录 操作系统 编程语言分类 安装python解释器 操作系统 操作系统有什么用 操作系统能接受外部指令转化成0和1,并把一些对硬件的复杂操作简化成一个个简单的接口,作为中间人连接硬件和软件 计算机三 ...

  8. python socket 接口

    一.简介 socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求.socket起 ...

  9. resize监听div的size变化

    具体实现分两类, ie9-10 默认支持div的resize事件,可以直接通过div.attachEvent('onresize', handler);的方式实现 其它浏览器 通过在div中添加一个内 ...

  10. ModelBinder 请求容错性

    代码 //using System.Web.Mvc; public class TrimToDBCModelBinder : DefaultModelBinder { public override ...