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 ...
随机推荐
- js 中的定时器
在js中的定时器分两种:1.setTimeout() 2.setInterval() 1.setTimeOut() 只在指定时间后执行一次 /定时器 异步运行 function hello(){ al ...
- 6.11---multipartfile在哪个jar包下---6.11---uuid---swagger上传图片包错去掉注解响应体
<dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupl ...
- [ SCOI 2009 ] 最长距离
\(\\\) \(Description\) 一个\(N\times M\)的网格图中有一些坏点,图是四联通的. 你至多可以拿走\(K\)个坏点,求拿走后联通的点对中欧几里得距离最大是多少. \(N, ...
- [hihocoder][Offer收割]编程练习赛64
公平划分 若条件满足,则所有数异或和为零,这时候随便分都可以,答案为2^n-2,否则答案为0 #pragma comment(linker, "/STACK:102400000,102400 ...
- 关于debug.keystore文件用法以及错误处理
在开发过程中需要频繁的为测试的同事签名apk,非常很麻烦,把默认debug.keystore文件替换成发布用(生产环境)的签名文件,不用频繁地签名apk文件了. 如果直接使用生产keysto ...
- Android项目实战_手机安全卫士手机防盗界面
#安全卫士手机防盗# ###1.Activity的任务栈 1.类似一个木桶,每层只能放一个木块,我们放入木块和取出木块的时候只能从最上面开始操作 ###2.Android中的坐标系 from t8) select @i --第二种 declare @i int select ...
- 【译】x86程序员手册21-6.3.5为操作系统保留的指令
6.3.5 Some Instructions are Reserved for Operating System 为操作系统保留的一些指令 Instructions that have the po ...