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 ...
随机推荐
- django 支持跨域请求配置
参考:https://www.jianshu.com/p/63fb55bee142 核心注意点:
- C#面向对象11 里氏转换
里氏转换 1.子类可以赋值给父类. using System; using System.Collections.Generic; using System.Linq; using System.Te ...
- 学习笔记--Tarjan算法之割点与桥
前言 图论中联通性相关问题往往会牵扯到无向图的割点与桥或是下一篇博客会讲的强连通分量,强有力的\(Tarjan\)算法能在\(O(n)\)的时间找到割点与桥 定义 若您是第一次了解\(Tarjan\) ...
- 记项目管理大作业Web项目Mandrian的全流程[其一] 整体分析: 功能划分, 组织结构
Mandrian是个图书管理系统, 具体需求老师给出 这个项目的目的主要是管理过程和高层设计的学习和实践 11人小组, 路人局 成员调查 这里由于很多人我都不认识, 所以我提前发了一个能力调查表, 调 ...
- ES6新增内容总结
ES6新增内容有:1,模块化思想.2,关于变量let和const.3,解构赋值.4,字符串的扩展.5,函数的扩展.6,箭头函数.7,继承apply的用法 以下就是详解: 1:模块化思想 非模块化有命名 ...
- linux安装RabbitMQ yum
一.RabbitMQ概念RabbitMQ是流行的开源消息队列系统,是AMQP(Advanced Message Queuing Protocol高级消息队列协议)的标准实现,用erlang语言开发 ...
- 如何关闭Win10系统的时间轴功能?
Win10系统新增了时间轴的功能,可以根据用户使用电脑的情况来进行记录,以方便用户查找之前的电脑使用记录,并且可以打开之前的任务状态. 但有些用户不想让系统记录下自己的活动记录,那我们该怎么清除这些记 ...
- Delphi TIdUDPClient组件
- 3.Shell 接收用户的参数
1.Shell 传递参数 我们可以在执行 Shell 脚本时,向脚本传递参数,Linux系统中的Shell脚本语言已经内设了用于接收参数的变量,变量之间可以使用空格间隔. 例如$0对应的是当前Shel ...
- unbutu apt-get update失败及解决办法
今天在用apt-get update更新时一直失败,报的错误为”Failed to fetch 404 Not Found”,因为我的镜像改为了mirrors.aliyun.com 我试着ping了一 ...