http://codeforces.com/contest/754/problem/D

题意:

给定几组区间,找k组区间,使得它们的公共交集最大。

思路:

在k组区间中,它们的公共交集=k组区间中右端点最小值-k组区间中左端点最大值。如果我们要区间大,那我们应该尽量让左端点小,右端点大。

先对区间按照左端点排序,然后用优先队列处理。

将区间按照左端点从小到大的顺序一一进队列,只需要进右端点即可,如果此时队列内已有k个数,则队首就是这k组区间的最小右端点,而因为左端点是从小到大的顺序进队列的,所以这k组区间中左端点最大的就是当前进队列的区间,这样一来就可以算出在这种情况下的区间交集。如果此时队列内已有k+1个数,那么就弹出队首元素,因为此时弹出的区间右端点是最小的,而我们想要是的尽量大的右端点。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn=*1e5+; int n, k; struct node
{
int l, r;
int id;
bool operator < (const node& rhs) const
{
return l<rhs.l || (l==rhs.l && r<rhs.r);
}
}p[maxn]; struct cmp
{
bool operator() (const int a, const int b) const
{
return a > b;
}
}; int main()
{
// freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n, &k))
{
for(int i=; i<=n; i++)
{
scanf("%d%d",&p[i].l, &p[i].r);
p[i].id = i;
} sort(p+,p+n+); priority_queue<int, vector<int>, cmp > Q;
int ans=, left, right; for(int i=;i<=n;i++)
{
Q.push(p[i].r);
if(Q.size()>k) Q.pop();
if(Q.size()==k)
{
int tmp=Q.top()-p[i].l+;
if(tmp>ans)
{
ans=tmp;
left=p[i].l;
right=Q.top();
}
}
} if(ans<=)
{
puts("");
for(int i=;i<=k;i++)
{
printf("%d%c",i,i==k?'\n':' ');
}
}
else
{
printf("%d\n",ans);
for(int i=;i<=n;i++)
{
if(p[i].l<=left && p[i].r>=right)
{
printf("%d",p[i].id);
k--;
if(k) printf(" ");
else {printf("\n");break;}
}
}
}
}
return ;
}

Codeforces Round #390 (Div. 2) D. Fedor and coupons(区间最大交集+优先队列)的更多相关文章

  1. Codeforces Round #390 (Div. 2) D. Fedor and coupons

    题意:题目简化了就是要给你n个区间,然后让你选出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 #538 (Div. 2) F 欧拉函数 + 区间修改线段树

    https://codeforces.com/contest/1114/problem/F 欧拉函数 + 区间更新线段树 题意 对一个序列(n<=4e5,a[i]<=300)两种操作: 1 ...

  5. 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 ...

  6. 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 ...

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

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

  8. 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 ...

  9. 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 ...

随机推荐

  1. c# SQL Server数据库操作-数据适配器类:SqlDataAdapter

    SqlDataAdapter类主要在MSSQL与DataSet之间执行数据传输工具,本节将介绍如何使用SqlDataAdapter类来填充DataSet和MSSQL执行新增.修改..删除等操作. 功能 ...

  2. mysql存储过程基础示例

    转自:http://database.51cto.com/art/201608/516661.htm http://www.cnblogs.com/mark-chan/p/5384139.html D ...

  3. Linux系统下 Supervisor 安装搭建

    在 web 应用部署到线上后,需要保证应用一直处于运行状态,在遇到程序异常.报错等情况,导致 web 应用终止时,需要保证程序可以立刻重启,继续提供服务. 所以,就需要一个工具,时刻监控 web 应用 ...

  4. Oracle安装部署之 6节点11g cluster环境搭建

    **********************集群规划*************************************** --配置主机,共需要8台主机,其中6台做grid集群,1台作为存储服 ...

  5. collectionView itemW宽度计算不对

    ([[UIScreen mainScreen] bounds].size.width - 28) / 4.00 没加括号 collectioView不能正常撑开 用flowLayout 不要用 代理方 ...

  6. Python开发【Django】:重构Admin

    自定义KingAdmin 通过admin样式自己做KingAdmin 提前需知道的model操作 # 获取app名 >>> models.Customer._meta.app_lab ...

  7. Python并行编程(十三):进程池和mpi4py模块

    1.基本概念 多进程库提供了Pool类来实现简单的多进程任务.Pool类有以下方法: - apply():直到得到结果之前一直阻塞. - apply_async():这是apply()方法的一个变体, ...

  8. OC如何跳到系统设置里的各种设置界面

    当 iOS系统版本 <= iOS7时 , 只能跳转到 系统设置页面 ,楼主试了下,非真机是没有任何效果的 当iOS系统版本 < iOS 10.0 时 NSURL *url= [NSURL ...

  9. sqlserver三种分页方式性能比较

    Liwu_Items表,CreateTime列建立聚集索引 第一种,sqlserver2005特有的分页语法 declare @page intdeclare @pagesize intset @pa ...

  10. Ubuntu下virtualenv 的安装及使用

    按照这个命令做下来基本是ok的. https://blog.csdn.net/qq_33371343/article/details/78047853