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] ). 编写一个函数来判断给定的目标值是否存在于数组中. ...
随机推荐
- 调用系统相机拍照,保存照片,调用系统裁剪API对照片处理,显示裁剪之后的照片
package com.pingyijinren.test; import android.annotation.TargetApi; import android.app.Notification; ...
- zoj2479 Cover the Rectangular Ground
肯定是dfs搜一下的,但是呢存在一个很大的剪枝,也就是面积必定要是相等的,那么如何去操作呢,可以想到的是二进制枚举选取的方法,然后把方法中选取的矩形面积求和并判断一下即可,然后dfs搜索,要注意的是, ...
- (工具类)Linux笔记之终端日志记录工具script
在学习Linux时,有时候终端的打印消息对于我们很重要,可是终端显示也是有一定的缓冲空间的.当信息打印许多时,前面的信息就会被覆盖掉.所以这里网上搜索了一下这方面的介绍.现总结例如以下: script ...
- js_html_input中autocomplete="off"在chrom中失效的解决办法 使用JS模拟锚点跳转 js如何获取url参数 C#模拟httpwebrequest请求_向服务器模拟cookie发送 实习期学到的技术(一) LinqPad的变量比较功能 ASP.NET EF 使用LinqPad 快速学习Linq
js_html_input中autocomplete="off"在chrom中失效的解决办法 分享网上的2种办法: 1-可以在不需要默认填写的input框中设置 autocompl ...
- Python中flatten用法
Python中flatten用法 原创 2014年04月16日 10:20:02 标签: Python / flatten 22667 一.用在数组 >>> a = [[1,3],[ ...
- android深入之设计模式(一)托付模式
(一)托付模式简单介绍 托付模式是主要的设计模式之中的一个.托付.即是让还有一个对象帮你做事情. 更多的模式,如状态模式.策略模式.訪问者模式本质上是在更特殊的场合採用了托付模式. 托付模式使得我们能 ...
- Vim i和a差别
i是当前位置插入 a是当前文字的后面插入
- 【codeforces379F】 New Year Tree
距离一个点最远的点一定是直径的一个端点.考虑运用这个原理,每次维护一下直径端点即可. #include<algorithm> #include<iostream> #inclu ...
- HDU 5976 Detachment 【贪心】 (2016ACM/ICPC亚洲区大连站)
Detachment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- spring cloud-spring boot 文档信息
官网: spring boot springcloud 学习资源 使用IDEA创建SpringBoot项目 Spring Boot教程https://blog.csdn.net/forezp/arti ...