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 ...
随机推荐
- Android内存管理(8)Dalvik,ART和 .dex 是什么*
什么是Dalvik: Dalvik是Google公司自己设计用于Android平台的Java虚拟机.Dalvik虚拟机是Google等厂商合作开发的Android移动设备平台的核心组成部分之一. Da ...
- day03_12/13/2016_bean属性的设置之构造器方式注入
- 【Leetcode】92. Reverse Linked List II && 206. Reverse Linked List
The task is reversing a list in range m to n(92) or a whole list(206). All in one : U need three poi ...
- 关于MVC视图下拉菜单绑定与取值的问题
绑定视图中dropdownlist: 视图中的代码: @Html.DropDownList("select1") 此处的slect1也就是页面上的<select>< ...
- MaBatis配置(单配置 之一)
注: 此文中的实体类还是沿用上一章的Emp和Dept两个类 .引入需要的jar包文件:http://pan.baidu.com/s/1qYy9nUc :mybatis-3.2.2.jar .编写MyB ...
- Laravel5.1学习笔记15 数据库1 数据库使用入门
简介 运行原生SQL查询 监听查询事件 数据库事务 使用多数据库连接 简介 Laravel makes connecting with databases and running queries e ...
- 前端h5开发调试神奇vconsole
(1)项目中安装vconcole插件 npm install vconcole (2)在vue项目中main.js中引入插件 import Vconsole from 'vconsole'; cons ...
- Android基础TOP2_1:输出系统时间
Activity: <TextView android:id="@+id/tv" android:layout_width="wrap_content" ...
- JS——缓慢动画封装
在知道如何获取内嵌式和外链式的标签属性值之后,我们再次封装缓慢动画: 单个属性 <!DOCTYPE html> <html> <head lang="en&qu ...
- 剔除重复jar包,查找重复类
作为程序员,没有遇到过jar包冲突,不是你幸运,就是你还年轻. jar包冲突有着无穷的魔力,一提到就会有说不完的怨愤,道不清的忧伤,这都是内伤.jar 包冲突就像深藏地底的地雷,看上去平常无奇,一切是 ...