还记得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段线段最大交集)的更多相关文章

  1. codeforces 754D. Fedor and coupons

    D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  2. 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 ...

  3. CodeForces 754D Fedor and coupons (优先队列)

    题意:给定n个优惠券,每张都有一定的优惠区间,然后要选k张,保证k张共同的优惠区间最大. 析:先把所有的优惠券按左端点排序,然后维护一个容量为k的优先队列,每次更新优先队列中的最小值,和当前的右端点, ...

  4. Codeforces 390Div2-754D. Fedor and coupons(贪心+优先队列)

    D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  5. cf754 754D - Fedor and coupons

    2个多小时,弱智了..(连A都做不对,就不要做D了(迷)) #include<bits/stdc++.h> #define lowbit(x) x&(-x) #define LL ...

  6. 求区间连续不超过K段的最大和--线段树+大量代码

    题目描述: 这是一道数据结构题. 我们拥有一个长度为n的数组a[i]. 我们有m次操作.操作有两种类型: 0 i val:表示我们要把a[i]修改为val; 1 l r k:表示我们要求出区间[l,r ...

  7. D. Fedor and coupons 二分暴力

    http://codeforces.com/contest/754/problem/D 给出n条线段,选出k条,使得他们的公共部分长度最大. 公共部分的长度,可以二分出来,为val.那么怎么判断有k条 ...

  8. eduCF#61 C. Painting the Fence /// DP 选取k段能覆盖的格数

    题目大意: 给定n m 接下来给定m个在n范围内的段的左右端 l r 求选取m-2段 最多能覆盖多少格 #include <bits/stdc++.h> using namespace s ...

  9. 【codeforces 754D】Fedor and coupons

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

随机推荐

  1. 根据返回数据, 迭代数组, 构造HTML结构

    首先需要引入jQuery哈! 1. 要求用下面的格式制作目录, 结构如下: <ul> <li>xxxx</li> <li>xxxx</li> ...

  2. StringUtils类API及使用方法详解

    StringUtils类API及使用方法详解 StringUtils方法概览 判空函数 1)StringUtils.isEmpty(String str) 2)StringUtils.isNotEmp ...

  3. Docker Ubuntu容器安装ping

    apt-get update apt-get install iputils-ping apt-get install net-tools

  4. 操作MongoDB好用的图形化工具,Robomongo -> 下载 -> 安装

    一 下载 点击下载 -> https://robomongo.org/download 二 安装 直接下一步就行了 -> 择安装位置之后 -> 确认安装

  5. mint-ui下拉加载(demo实例)

    <template> <div class="share"> <div class="header"> <div cl ...

  6. SQL 多表查询展示

    ########################多表########################SELECT COUNT(*) FROM MEMBER1 A; 查询出来的结果为43行数据: SEL ...

  7. 02-【servlet】

    1.什么是Servlet Servlet是JavaWeb的三大组件之一[Servlet,Filter,Listener],它属于动态资源.Servlet的作用是处理请求,服务器会把接收到的请求交给Se ...

  8. iptables实现内外网端口映射及转发上网

    前两天在工作中遇到一个需求,某192.168.1.0/24内网网段内只有一台主机A连接到了公网,A的两块网卡分别有一个公网地址(123.234.345.456)和一个内网地址(192.168.1.10 ...

  9. UVA 1482 SG打表

    打出SG表来可以很容易的发现i为偶数时 SG[i]=i/2 i为奇数时 SG[i]=SG[i/2] #include<bits/stdc++.h> typedef long long ll ...

  10. 第八章 watch监听 85 computed-计算属性的使用和3个特点

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...