Codeforces Round #390 (Div. 2) D. Fedor and coupons
题意:题目简化了就是要给你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的更多相关文章
- Codeforces Round #390 (Div. 2) D. Fedor and coupons(区间最大交集+优先队列)
http://codeforces.com/contest/754/problem/D 题意: 给定几组区间,找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 #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 ...
- Codeforces Round #390 (Div. 2) D
All our characters have hobbies. The same is true for Fedor. He enjoys shopping in the neighboring s ...
随机推荐
- Jenkins-SVN + Maven + Docker
第1步:安装插件 Subversion Plug-inMaven Integration pluginCloudBees Docker Build and Publish pluginDeploy t ...
- 前端开发之旅- 移动端HTML5实现文件上传
一. 在一个客户的webapp项目中需要用到 html5调用手机摄像头,找了很多资料,大都是 js调用api 然后怎样怎样,做了几个demo测试发现根本不行, 后来恍然大悟,用html5自带的 in ...
- JS——高级各行换色
1.获取tbody下的子元素 2.注册鼠标覆盖事件时存储当时的背景颜色,注册鼠标离开事件时把存储的颜色赋值注册事件对象 <!DOCTYPE html> <html> <h ...
- 大白_uva10795_新汉诺塔
题意:给出所有盘子的初态和终态,问最少多少步能从初态走到终态,其余规则和老汉诺塔一样. 思路: 若要把当前最大的盘子m从1移动到3,那么首先必须把剩下的所有盘子1~m-1放到2上,然后把m放到3上. ...
- SpringMVC参数绑定、Post乱码解决方法
从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上. springmvc中,接收页面提交的数据是通过方法形参来接收.而不是在control ...
- 零基础转行Linux云计算运维工程师获得20万年薪的超级学习技巧
云计算概念一旦产生便一发不可收拾,成为移动互联网时代最为火热的行业之一.国内各大互联网公司例如阿里.腾讯.百度.网易等纷纷推出自己的云计算产品,3月10日,腾讯云0.01元投标时间更是让云计算在普罗大 ...
- json和pickle的序列化
PICKle模块:
- java web 基本属性
page指令 属性 描述 默认值 language 指定JSP页面使用的脚本语言 java import contenType include指令 taglib注释 <!--我是html注释-- ...
- Python基础-List找重复数
请从L=[1,10,20,50,20,20,1]中找出重复数. L=[1,10,20,50,20,20,1] L1=[] for i in L: if(L.count(i)>1): L1.app ...
- BFOA
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> ...