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 ...
随机推荐
- 巡风代码架构简介以及Flask的项目文件结构简介
一.巡风: 巡风是一款什么东西,想必安全同行都不陌生吧.用它作为内网漏洞扫描管理架构是一种很好的选择,扫描快,开源,还可自己编写符合规则的POC直接放入相应目录来扩展.今天下午趁着有点时间捋了一下巡风 ...
- http://blog.csdn.net/ym19860303/article/details/8462450
http://www.himigame.com/iphone-cocos2d/444.html 很多游戏中,便于开发就会直接采用Cocos2d提供的字体库来使用,但是因为提供的种类很多,那么Himi利 ...
- 优雅的go语言--入门篇
1.特点 1.静态类型,编译型的开源语言 2.脚本华的语法,支持多种编程范式(函数式&面向对象) 3.原生,给力的并发编程的支持 2.优势 1.脚本化的语法 2.静态类型+编译型,程序运行速度 ...
- C语言实现日历输出
这个还是挺实用的.... 头文件: #ifndef MAIN_H #define MAIN_H #include "stdio.h" #include "math.h&q ...
- .NET获取Html字符串中指定标签的指定属性的值
using System.Text; using System.Text.RegularExpressions; //以上为要用到的命名空间 /// <summary> /// 获取Htm ...
- Maven搭建Nexus私有仓库
下载压缩包nexus-2.13.0-01-bundle.tar.gz 解压后有两个目录 进入程序目录启动 ./nexus start 启动告警(确认用root启动把以下加入到环境变量) export ...
- vs 2015
基于应用要求和要使用的语言选择所需工具. Xamarin for Visual Studio:针对所有设备的 C# 中的常用基本代码 Apache Cordova with Visual Studio ...
- Jury Compromise---poj1015(动态规划,dp,)
题目链接:http://poj.org/problem?id=1015 大致题意: 在遥远的国家佛罗布尼亚,嫌犯是否有罪,须由陪审团决定.陪审团是由法官从公众中挑选的.先随机挑选n 个人作为陪审团的候 ...
- 14.Git忽略特殊文件.gitignore
有些时候,你必须把某些文件放到Git工作目录中,但又不能提交它们,比如保存了数据库密码的配置文件啦,等等,每次git status都会显示Untracked files ...,有强迫症的童鞋心里肯定 ...
- api收录
ip地址查询api http://ip.taobao.com/service/getIpInfo.php?ip= 如: http://ip.taobao.com/service/getIpInfo.p ...