题意:题目简化了就是要给你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. 6CSS之文本

    CSS文本:文本缩进(text-indent).文本对齐(text-align).文本修饰(text-decoration).文本大小写(text-transform).字符距离(letter-spa ...

  2. Spring boot中的定时任务(计划任务)

    从Spring3.1开始,计划任务在Spring中实现变得异常的简单.首先通过配置类注解@EnableScheduling来开启对计划任务的支持,然后再要执行的计划任务的方法上注释@Scheduled ...

  3. [ USACO 2018 OPEN ] Out of Sorts (Platinum)

    \(\\\) \(Description\) 对一长为\(N\)的数列\(A\)排序,不保证数列元素互异: 数列\(A\)中\(A[1...i]\)的最大值不大于\(A[i+1-N]\)的最小值,我们 ...

  4. [ HAOI 2008 ] 玩具取名

    \(\\\) \(Description\) 在一个只有\(W,I,N,G\)的字符集中,给出四个字符的若干映射,每个映射为一个字符映射到两个字符,现给你一个假定由一个字符经过多次映射产生的字符串,问 ...

  5. html5——DOM扩展

    元素获取 1.document.getElementsByClassName ('class') 通过类名获取元素,以类数组形式存在. 2.document.querySelector(‘div’) ...

  6. pycharm之gitignore设置

    首先检查pycharm是否安装了ignore插件 项目目录如图: 选中项目automationTest名称,右击-->New-->查看是否有ignore file选项,如果有表示Pycah ...

  7. redis下载安装配置教程

    参考 https://www.cnblogs.com/taostaryu/p/9481749.html 上面做完后, 打开客户端 $ redis-cli 以上命令将打开以下终端: redis 127. ...

  8. LINQ简记(1):基本语法

    关于LINQ(语言集成查询)是.NET 3.5和Visual Studio 2008以上版本中引入的一种有趣的全新概念,语言版本有VB和C#,由于C#与.NET平台结合最为紧密,也是MS当初首推的语言 ...

  9. Linux - nginx基础及常用操作

    目录 Linux - nginx基础及常用操作 Tengine淘宝nginx安装流程 nginx的主配置文件nginx.conf 基于域名的多虚拟主机实战 nginx的访问日志功能 网站的404页面优 ...

  10. hdu 5182 PM2.5

    问题描述 目前,我们用PM2.5的含量来描述空气质量的好坏.一个城市的PM2.5含量越低,它的空气质量就越好.所以我们经常按照PM2.5的含量从小到大对城市排序.一些时候某个城市的排名可能上升,但是他 ...