CodeForces 754D Fedor and coupons ——(k段线段最大交集)
还记得lyf说过k=2的方法,但是推广到k是其他的话有点麻烦。现在这里采取另外一种方法。
先将所有线段按照L进行排序,然后优先队列保存R的值,然后每次用最小的R值,和当前的L来维护答案即可。同时,如果Q的size()比k大,那么就弹出最小的R。
具体见代码:
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <set>
#include <vector>
#include <map>
#include <vector>
#include <queue>
using namespace std;
const int N = (int)3e5+; int n,k;
struct seg
{
int l, r, id;
bool operator < (const seg & A) const
{
return l < A.l;
}
}p[N]; int main()
{
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>,greater<int> > Q;
int L = ;
int ans = ;
for(int i=;i<=n;i++)
{
Q.push(p[i].r);
if(Q.size() > k) Q.pop();
if(Q.size() == k && ans < Q.top() - p[i].l + )
{
ans = Q.top() - p[i].l + ;
L = p[i].l;
}
}
if(ans == )
{
puts("");
int first = ;
for(int i=;i<=k;i++)
{
if(first) printf(" ");
else first = ;
printf("%d",p[i].id);
}
puts("");
}
else
{
printf("%d\n",ans);
int first = ;
for(int i=;i<=n && k>;i++)
{
int l = p[i].l, r = p[i].r;
if(l<=L && L+ans-<=r)
{
if(first) printf(" ");
else first = ;
printf("%d",p[i].id);
k--;
}
}
puts("");
}
return ;
}
另外,输出方案的方式也值得注意一下。
CodeForces 754D Fedor and coupons ——(k段线段最大交集)的更多相关文章
- codeforces 754D. Fedor and coupons
D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- CodeForces 754D Fedor and coupons&&CodeForces 822C Hacker, pack your bags!
D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- CodeForces 754D Fedor and coupons (优先队列)
题意:给定n个优惠券,每张都有一定的优惠区间,然后要选k张,保证k张共同的优惠区间最大. 析:先把所有的优惠券按左端点排序,然后维护一个容量为k的优先队列,每次更新优先队列中的最小值,和当前的右端点, ...
- Codeforces 390Div2-754D. Fedor and coupons(贪心+优先队列)
D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- cf754 754D - Fedor and coupons
2个多小时,弱智了..(连A都做不对,就不要做D了(迷)) #include<bits/stdc++.h> #define lowbit(x) x&(-x) #define LL ...
- 求区间连续不超过K段的最大和--线段树+大量代码
题目描述: 这是一道数据结构题. 我们拥有一个长度为n的数组a[i]. 我们有m次操作.操作有两种类型: 0 i val:表示我们要把a[i]修改为val; 1 l r k:表示我们要求出区间[l,r ...
- D. Fedor and coupons 二分暴力
http://codeforces.com/contest/754/problem/D 给出n条线段,选出k条,使得他们的公共部分长度最大. 公共部分的长度,可以二分出来,为val.那么怎么判断有k条 ...
- eduCF#61 C. Painting the Fence /// DP 选取k段能覆盖的格数
题目大意: 给定n m 接下来给定m个在n范围内的段的左右端 l r 求选取m-2段 最多能覆盖多少格 #include <bits/stdc++.h> using namespace s ...
- 【codeforces 754D】Fedor and coupons
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- 使用SSH连接AWS服务器
使用SSH连接AWS服务器 一直有一台AWS云主机,但是之前在Windows平台都是使用Xshell连接的,换到Ubuntu环境之后还没有试,昨天试了一下,终于使用SSH连接成功了,这里记录一下步骤: ...
- pymssql文档(转)
pymssql methods set_max_connections(number) -- Sets maximum number of simultaneous database connecti ...
- Mongoose 使用Node操作MongoDB
Mongoose好处 可以为文档创建一个模式结构(Schema) 可以对模型中的对象/文档进行验证 数据可以通过类型转换转换为对象模型 可以使用中间件来应用业务逻辑挂钩 比Node原生的MongoDB ...
- npm 设置淘宝镜像
永久 npm config set registry https://registry.npm.taobao.org 直接安装 cnpm 替代 npm npm install -g cnpm --re ...
- vue 编译警告 Compiled with 4 warnings
问题原因: windows下盘符的大小写导致的. 我在cmd里运行的时候,是切换到小写,改成大写的E盘符就没问题了
- promises的深入学习
Promise 的含义 § ⇧ Promise 是异步编程的一种解决方案,比传统的解决方案——回调函数和事件——更合理和更强大.它由社区最早提出和实现,ES6 将其写进了语言标准,统一了用法,原生提供 ...
- mysql_jdbc连接说明
mysql JDBC Driver 常用的有两个,一个是gjt(Giant Java Tree)组织提供的mysql驱动,其JDBC Driver名称(JAVA类名)为:org.gjt.mm.mysq ...
- Delphi GetCommModemStatus函数
- Delphi ActiveX的使用
樊伟胜
- IPC之ipc_sysctl.c源码解读
// SPDX-License-Identifier: GPL-2.0-only /* * Copyright (C) 2007 * * Author: Eric Biederman <ebie ...