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. SQLSERVER数据库管理员的专用连接DAC

    出处: http://www.cnblogs.com/lyhabc/archive/2012/09/23/2698702.html DAC:Dedicated Admin Connection 当SQ ...

  2. Add Two Numbers(链表)

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  3. Elasticsearch自定义客户端(TransportClient)资源池

    前言: java中调用TransportClient时,我们一般都会设置成单例,为了避免多次的创建与关闭造成的内存占用及关闭缓慢问题.而TransportClient本身也是实现了线程池threadP ...

  4. Mac 系统引导过程概述 & BootCamp 的秘密

    http://bbs.feng.com/read-htm-tid-6890655.html

  5. Jmeter执行java脚本结束时提示:The JVM should have exited but did not.

    使用jmeter对dubbo进行压测时,需要使用jmeter的sampler里的java请求 使用./jmeter.sh -n -t test.jmx -l test.jmx -o -e test后台 ...

  6. Ubuntu 16.04 LTS 搭建LAMP

    1. LAMP是一系列自由和开源软件的集合,包含了Linux.Web服务器(Apache).数据库服务器(MySQL)和PHP(脚本语言). Apache2 Web 服务器的安装 sudo apt i ...

  7. UG如何把语言改成中文,UG如何把界面语言改成中文

    1 高级系统设置,高级,新建一个用户变量(变量名为lang,变量值为chs)   2 高级系统设置,高级,环境变量,系统变量中,查看变量名为UGII_LANG的值是否为simpl_chinese,如果 ...

  8. 字符串处理(正则表达式、NSScanner扫描、CoreParse解析器)-b

    搜索 在一个字符串中搜索子字符串 最灵活的方法 1 - (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptio ...

  9. 【bzoj4597】 [Shoi2016]随机序列

    可以发现加减号之间可以互相抵消. 真正加到答案里的只有一些前缀积. 记s[i]为a[1]*a[2]*a[3]...*a[i].那s[i]在答案中出现的次数就是2*3^(n-i-1); 修改一个数只会对 ...

  10. 步长为float

    import numpy as np for i in np.arange(0.005, 0.05, 1): print(i)