http://codeforces.com/contest/754/problem/D

给出n条线段,选出k条,使得他们的公共部分长度最大。

公共部分的长度,可以二分出来,为val。那么怎么判断有k条线段有共同的这个长度,而且选他们出来呢?

可以把右端点减去val - 1,那么以后就只需要k条线段至少有一个交点就可以了。

那么怎么确定这个交点呢?

我的做法是直接离散,然后暴力找出覆盖次数>=k的那个点。

复杂度好像有点高,

log2e10 * nlogn

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 6e5 + ;
int L[maxn], R[maxn];
int pos[maxn];
int book[maxn];
int n, k;
map<LL, int>save;
bool check(LL val) {
if (val == ) return true;
int lenpos = ;
for (int i = ; i <= n; ++i) {
if (R[i] - L[i] + < val) continue;
pos[++lenpos] = L[i];
pos[++lenpos] = R[i] - val + ;
}
int mx = -inf;
if (lenpos == ) return false;
sort(pos + , pos + + lenpos);
for (int i = ; i <= n; ++i) {
if (R[i] - L[i] + < val) continue;
int t1 = lower_bound(pos + , pos + + lenpos, L[i]) - pos;
int t2 = lower_bound(pos + , pos + + lenpos, R[i] - val + ) - pos;
book[t1]++;
book[t2 + ]--;
mx = max(mx, t2 + );
}
bool flag = false;
for (int i = ; i <= mx; ++i) {
book[i] += book[i - ];
if (book[i] >= k) {
// assert(save[val] == 0);
save[val] = pos[i];
flag = true;
break;
}
}
for (int i = ; i <= mx; ++i) book[i] = ;
return flag;
}
void work() {
cin >> n >> k;
for (int i = ; i <= n; ++i) {
cin >> L[i] >> R[i];
}
LL be = , en = 2e10;
while (be <= en) {
LL mid = (be + en) >> ;
if (check(mid)) {
be = mid + ;
} else en = mid - ;
}
cout << en << endl;
if (en == ) {
for (int i = ; i <= k; ++i) {
cout << i << " ";
}
} else {
int use = ;
int tpoint = save[en];
// cout << tpoint << endl;
for (int i = ; i <= n && use < k; ++i) {
if (R[i] - tpoint + < en) continue;
if (tpoint >= L[i] && tpoint <= R[i]) {
use++;
cout << i << " ";
}
}
// assert(use == k);
}
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
IOS;
work();
return ;
}

其实这题关键要把判断k条线段相交于同一个区间,化简为相交于同一个点

D. Fedor and coupons 二分暴力的更多相关文章

  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 390Div2-754D. Fedor and coupons(贪心+优先队列)

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

  3. 【bzoj5085】最大 二分+暴力

    题目描述 给你一个n×m的矩形,要你找一个子矩形,价值为左上角左下角右上角右下角这四个数的最小值,要你最大化矩形的价值. 输入 第一行两个数n,m,接下来n行每行m个数,用来描述矩形 n, m ≤ 1 ...

  4. 【BZOJ4716】假摔 二分+暴力

    [BZOJ4716]假摔 Description [题目背景] 小Q最近喜欢上了一款游戏,名为<舰队connection>,在游戏中,小Q指挥强大的舰队南征北战,从而成为了一名dalao. ...

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

  6. Codeforces 778A:String Game(二分暴力)

    http://codeforces.com/problemset/problem/778/A 题意:给出字符串s和字符串p,还有n个位置,每一个位置代表删除s串中的第i个字符,问最多可以删除多少个字符 ...

  7. codeforces754D Fedor and coupons

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  8. 10年省赛-Greatest Number (二分+暴力) + 12年省赛-Pick apples(DP) + UVA 12325(暴力-2次枚举)

    题意:给你n个数,在里面取4个数,可以重复取数,使和不超过M,求能得到的最大的数是多少: 思路:比赛时,和之前的一个题目很像,一直以为是体积为4(最多选择四次)的完全背包,结果并不是,两两求和,然后二 ...

  9. LeetCode 81 - 搜索旋转排序数组 II - [二分+暴力]

    假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,0,1,2,2,5,6] 可能变为 [2,5,6,0,0,1,2] ). 编写一个函数来判断给定的目标值是否存在于数组中. ...

随机推荐

  1. hdu - 2851 Lode Runner (最短路)

    http://acm.hdu.edu.cn/showproblem.php?pid=2851 首先有n层,每层的路径都有一个起点和终点和对应的危险值,如果某两层之间有交集,就能从这一层上到另外一层,不 ...

  2. Oracle表空间 ORA-01653:

    --1.查看表空间USERS使用情况SELECT T.TABLESPACE_NAME,D.FILE_NAME, D.AUTOEXTENSIBLE,D.BYTES,D.MAXBYTES,D.STATUS ...

  3. SLF4J 和 Logback 在 Maven 项目中的使用方法

    原文:http://blog.csdn.net/llmmll08/article/details/70217120 本文介绍 SLF4J 和 Logback 在 Maven 项目中的用法,包括日志框架 ...

  4. hdu4057 Rescue the Rabbit(AC自己主动机+DP)

    Rescue the Rabbit Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  5. Android架构的简单探讨(一)

    在CSDN上看到这样一篇译文,虽然最终的解决方案要按照自己特定的项目去设计,但该文还是引起了很多自己的共鸣,原文猛戳这里. 这是他提出的基于Messaging的MVC框架: 其中包含的设计思想在于:哪 ...

  6. 创建类模式大PK(总结)

    创建类模式包含工厂方法模式.建造者模式.抽象工厂模式.单例模式和原型模式,它们都可以提供对象的创建和管理职责.当中的单例模式和原型模式很easy理解,单例模式是要保持在内存中仅仅有一个对象,原型模式是 ...

  7. web前端和后端的区别

    一句话,展示ui相关的就是前端,否则就是后端. 前端语言:javascript.css和html. 后端就是一些服务.

  8. DEDE织梦 后台特别卡,有时响应超时的解决办法

    跟大家一样,大致情况是: 1.打开后台首页第一次没问题,但是刷新或者点其他菜单就一直卡着了. 2.关掉浏览器重新进首页没问题,但是一旦进了首页再打开php页面就卡死了. 3.服务器返回Maximum ...

  9. Ubuntu14.04 x64 zabbix 3.0 安装

    U buntu14.04 x64   zabbix 3.0 安装 苦于网上的文档很多,但是对初学者来说,很多都搭建不成功,我重新安装一下.记录一下. 下载deb wget http://repo.za ...

  10. 【T^T 1871】获取敌情

    获取敌情 在公元4484年,人类展开了对外界星球的征途和探索,但也不可避免的展开了和外星人之间的战争.偶然的机遇之下,美国联邦调查局截获了一串来自外星球的信息.但不知道有什么特殊的意义.所以就委托你, ...