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] ). 编写一个函数来判断给定的目标值是否存在于数组中. ...
随机推荐
- iLBC 压缩比
*---------------------------------------------------* * ...
- poj_2524_Ubiquitous Religions_201407211506
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23390 Accepted: ...
- mysql 安装与卸载
mysql用了也好几年了,但每次安装完或者卸载完就忘记了安装步骤以及卸载步骤,因此将关键的步骤记录下来,供以后参考. 1.mysql安装 ①安装类型有typical,complete,custom,一 ...
- java设计模式——单例设计模式
/*设计模式:对问题行之有效的解决方式.其实它是一种思想. 1,单例设计模式. 解决的问题:就是可以保证一个类在内存中的对象唯一性. 必须对于多个程序使用同一个配置信息对象时,就需要保证该对象的 ...
- NOIP 2010 关押罪犯
P1525 关押罪犯 题目描述 SS 城现有两座监狱,一共关押着 NN 名罪犯,编号分别为 1-N1−N .他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突. ...
- Hive之Order,Sort,Cluster and Distribute By
测试数据 create table sort_test( id int, name string ) row format delimited fields terminated by '\t' li ...
- crontab not running
there are mutliple ways to describle this issue 1. crontab not running 2. crontab not running and no ...
- 移动硬盘/U盘上装Windows 7旗舰版(VHD版)
真正的移动版WIN7,在移动硬盘/U盘上运行的WIN7 工具准备 - 联想Y450本本,已安装Windows 7旗舰版(或者WINPE3.0版),用来给移动WIN7做引导 -Win7.vhd,15G, ...
- [Java Sprint] Spring Configuration Using Java
There is no applicationContext.xml file. Too much XML Namespaces helped Enter Java Configuration Cre ...
- css的white-space属性导致了空格问题——查看十六进制发现2020变成了c2a0
今天发现了一个奇怪的问题.从文本编辑器(notepad++)中把一段文本输入到easyui的textbox文本框(textarea)中,不进行不论什么的操作.直接再从文本框中把文本拷贝出来贴到文本编译 ...