D. Fedor and coupons 二分暴力
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 二分暴力的更多相关文章
- codeforces 754D. Fedor and coupons
D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- Codeforces 390Div2-754D. Fedor and coupons(贪心+优先队列)
D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- 【bzoj5085】最大 二分+暴力
题目描述 给你一个n×m的矩形,要你找一个子矩形,价值为左上角左下角右上角右下角这四个数的最小值,要你最大化矩形的价值. 输入 第一行两个数n,m,接下来n行每行m个数,用来描述矩形 n, m ≤ 1 ...
- 【BZOJ4716】假摔 二分+暴力
[BZOJ4716]假摔 Description [题目背景] 小Q最近喜欢上了一款游戏,名为<舰队connection>,在游戏中,小Q指挥强大的舰队南征北战,从而成为了一名dalao. ...
- 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 778A:String Game(二分暴力)
http://codeforces.com/problemset/problem/778/A 题意:给出字符串s和字符串p,还有n个位置,每一个位置代表删除s串中的第i个字符,问最多可以删除多少个字符 ...
- codeforces754D Fedor and coupons
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- 10年省赛-Greatest Number (二分+暴力) + 12年省赛-Pick apples(DP) + UVA 12325(暴力-2次枚举)
题意:给你n个数,在里面取4个数,可以重复取数,使和不超过M,求能得到的最大的数是多少: 思路:比赛时,和之前的一个题目很像,一直以为是体积为4(最多选择四次)的完全背包,结果并不是,两两求和,然后二 ...
- LeetCode 81 - 搜索旋转排序数组 II - [二分+暴力]
假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,0,1,2,2,5,6] 可能变为 [2,5,6,0,0,1,2] ). 编写一个函数来判断给定的目标值是否存在于数组中. ...
随机推荐
- 安装最新版本的zabbix
1. 先安装php5.4 最新版本: yum安装php5.4或5.5 https://blog.csdn.net/MarkBoo/article/details/49424183 2. 然后参照官网或 ...
- CSU - 1115 最短的名字(字典树模板题)
Description 在一个奇怪的村子中,很多人的名字都很长,比如aaaaa, bbb and abababab. 名字这么长,叫全名显然起来很不方便.所以村民之间一般只叫名字的前缀.比如叫'aaa ...
- springMVC多数据源使用 跨库跨连接
原文:http://blog.itpub.net/9399028/viewspace-2106641/ http://blog.csdn.net/a973893384/article/details/ ...
- python性能优化、内存优化、内存泄露;与其他语音比较效率如何?
1.内存泄露:http://www.cnblogs.com/xybaby/p/7491656.html 2.内存优化:http://www.cnblogs.com/xybaby/p/7488216.h ...
- Hadoop 执行过程中出现 name node is in safe mode 问题
解决方法: 1.进入hadoop安装根目录 如 :我的hadoop 安装在/usr/local/hadoop 执行 cd /usr/local/hadoop bin/hadoop dfsadmin - ...
- CNN卷积神经网络的改进(15年最新paper)
回归正题,今天要跟大家分享的是一些 Convolutional Neural Networks(CNN)的工作. 大家都知道,CNN 最早提出时,是以一定的人眼生理结构为基础,然后逐渐定下来了一些经典 ...
- ORA-00942:表或视图不存在 低级错误一例
ORA-00942:表或视图不存在 低级错误一例 运行查询语句,报ORA-00942错误 检查后发现没有指定表的所属用户.加入用户.再次查询,查询正常,截图例如以下: *************** ...
- Angularjs1.x 项目结构
大部分的项目结构是以 directives , service, controller 为基础来搭建的项目架构的,但这里更偏向于以应用场景来进行项目架构,因此这里的文件夹结构可能与您之前遇到的结构不同 ...
- 使用adb在电脑和手机间传文件
首先须要root手机. 然后,"Win + R",打开cmd窗体.以下以copy d:\1.txt到/system/文件夹为例说明. adb push source(localpa ...
- HDU 5428 分解质因数
The F ...