Codeforces Round #390 (Div. 2) D. Fedor and coupons(区间最大交集+优先队列)
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(区间最大交集+优先队列)的更多相关文章
- Codeforces Round #390 (Div. 2) D. Fedor and coupons
题意:题目简化了就是要给你n个区间,然后让你选出k个区间 使得这k个区间有公共交集:问这个公共交集最大能是多少,并且输出所选的k个区间.如果有多组答案,则输出任意一种. 这题是用优先队列来处理区 ...
- Codeforces Round #390 (Div. 2)
时隔一个月重返coding…… 期末复习了一个月也不亏 倒是都过了…… 就是计组61有点亏 复变68也太低了 其他都还好…… 假期做的第一场cf 三道题 还可以…… 最后room第三 standing ...
- Codeforces Round #390 (Div. 2) A+B+D!
A. Lesha and array splitting 水题模拟.(0:10) 题意:给你一个n个元素的数组,求能否把这个数组分成若干连续小段,使得每段的和不为0.如有多种解输出任意一个. 思路:搞 ...
- Codeforces Round #538 (Div. 2) F 欧拉函数 + 区间修改线段树
https://codeforces.com/contest/1114/problem/F 欧拉函数 + 区间更新线段树 题意 对一个序列(n<=4e5,a[i]<=300)两种操作: 1 ...
- 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 ...
- 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 ...
- Codeforces Round #390 (Div. 2) A. Lesha and array splitting
http://codeforces.com/contest/754/problem/A 题意: 给出一串序列,现在要把这串序列分成多个序列,使得每一个序列的sum都不为0. 思路: 先统计一下不为0的 ...
- 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 ...
- 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 ...
随机推荐
- 如何学习 cocos2d-x ?
发表于 04/23/2014 作者 zrong — 24 条评论 ↓ 11,687 次查看 本站文章除注明转载外,均为本站原创或者翻译. 本站文章欢迎各种形式的转载,但请18岁以上的转载者注明文章出处 ...
- ios 设置委托delegate
为了进行页面传值,也可以用委托的方法. 下面以时间控件为例. 1.首先,在.h 文件设置委托 #import <UIKit/UIKit.h> @protocol DatePickerVie ...
- 项目中启动另外的一个app
NSMutableString *webViewContent = [[NSMutableStringalloc] init]; [webViewContent appendString:@" ...
- oracle的分页查询,mabatis的sql配置
<select id="getCardcaseByPage" resultType="Cardcase" > select * from ( sel ...
- ThinkPHP分类查询(获取当前分类的子分类,获取父分类,下一级分类)
获取指定分类的所有子分类ID号 //获取指定分类的所有子分类ID号 function getAllChildcateIds($categoryID){ //初始化ID数组 $array[] = $ca ...
- 170511、Spring IOC和AOP 原理彻底搞懂
Spring提供了很多轻量级应用开发实践的工具集合,这些工具集以接口.抽象类.或工具类的形式存在于Spring中.通过使用这些工具集,可以实现应用程序与各种开源技术及框架间的友好整合.比如有关jdbc ...
- input输入框制定输入数据类型匹配
<input type="text" id="price_169" value="97" style="max-width: ...
- flight framework 核心解读
http://blog.csdn.net/sky_zhe/article/details/38906689?utm_source=tuicool&utm_medium=referral
- 崩溃block
[__NSGlobalBlock__ setHidden:]: unrecognized selector sent to instance 0x10dbb9090(null)注释掉 sethidde ...
- Keras-图片预处理
图片预处理 图片生成器ImageDataGenerator keras.preprocessing.image.ImageDataGenerator(featurewise_center=False, ...