Gym 101510C
题意略。
思路:
由于xi的选取是任意的,所以我们不用去理会题目中的xi数列条件。主要是把关注点放在长度为L的线段覆盖至少k个整数这个条件上。
像这种取到最小的合法解的问题,我们应该要想到使用二分法来试探。
那么在验证时,如果我们要验证下标为i的的这个项是否能被一个包含k个元素的区间覆盖,就要枚举这个区间左边端点 <= i 并且 右边端点 >= i的所有情况,
在这些情况中,只要存在一个合法的解,那么该项就可以找到一个合法的xi。
如果我们对于每一个项都这么验证,那么我们的时间复杂度无法承受。
我们发现,如果当前区间已经包含了 i 和 i + 1 ,那么如果对于i来说,这个区间的r - l + 1 <= L,那么对于i + 1来说,该区间也是合法的。
也就是说,我们可以利用上前面的结果,当需要的时候,我们才去移动这个验证区间。
如果当前区间的长度大于L,那么我们也需要右移这个区间,直到找到一个合法的区间,如果在包含 i 的前提下,该区间不存在,
那么说明这个L不行。
详见代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = ; LL store[maxn],minn,maxx;
int n,k; bool jud(LL x)
{
int l,r;
bool ret = true;
l = ,r = k - ;
for (int i = ;i < n;++i)
{
while ((store[r] - store[l] > x && l < i && r < n - ) || r < i){
++l,++r;
}
if (store[r] - store[l] > x)
{
ret = false;
break;
}
}
return ret;
} int main(){
scanf("%d%d",&n,&k);
scanf("%lld",&store[]);
minn = maxx = store[];
for(int i = ;i < n;++i){
scanf("%lld",&store[i]);
minn = min(minn,store[i]);
maxx = max(maxx,store[i]);
}
sort(store,store + n);
LL l = ,r = maxx - minn;
while(l < r){
LL mid = (l + r)>>;
if(jud(mid)) r = mid;
else l = mid + ;
//printf("mid == %lld\n",mid);
}
printf("%lld\n",l);
return ;
}
Gym 101510C的更多相关文章
- ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力
Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS Memory Limit:65536KB 64bit IO Fo ...
- ACM: Gym 101047K Training with Phuket's larvae - 思维题
Gym 101047K Training with Phuket's larvae Time Limit:2000MS Memory Limit:65536KB 64bit IO F ...
- ACM: Gym 101047E Escape from Ayutthaya - BFS
Gym 101047E Escape from Ayutthaya Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- ACM: Gym 101047B Renzo and the palindromic decoration - 手速题
Gym 101047B Renzo and the palindromic decoration Time Limit:2000MS Memory Limit:65536KB 64 ...
- Gym 101102J---Divisible Numbers(反推技巧题)
题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...
- Gym 100917J---Judgement(01背包+bitset)
题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...
- Gym 100917J---dir -C(RMQ--ST)
题目链接 http://codeforces.com/gym/100917/problem/D problem description Famous Berland coder and IT mana ...
- Gym 101102D---Rectangles(单调栈)
题目链接 http://codeforces.com/gym/101102/problem/D problem description Given an R×C grid with each cel ...
- Gym 101102C---Bored Judge(区间最大值)
题目链接 http://codeforces.com/gym/101102/problem/C problem description Judge Bahosain was bored at ACM ...
随机推荐
- Spring Cloud 之 Config与动态路由.
一.简介 Spring Cloud Confg 是用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分.其中服务端也称为分布式配置中心,它是一个独立的微服务 ...
- C#托盘程序设置
打开Microsoft Visual Studio 2010,新建解决方案--添加项目--托盘的实现 从工具栏里,把NotifyIcon控件拖到窗体上,并设置属性: 1.visible 设置默 ...
- Shell基本语法---shell脚本的输入以及脚本拥有特效地输出
shell脚本的输入 语法:read -参数 -p:给出提示符.默认不支持"\n"换行 -s:隐藏输入的内容 -t:给出等待的时间,超时会退出read,单位是秒 -n:限制读取字符 ...
- poj 1455 Crazy tea party
这道题第一眼看去很难,其实不然,短短几行代码就搞定了. 说一下大概思路,如果是排成一排的n个人,如 1 2 3 4 5 6 7 8 我们要变成 8 7 6 5 4 3 2 1 需要交换 28次,找规律 ...
- 使用hexo中遇到的问题
- 拉格朗日对偶性(Lagrange duality)
目录 拉格朗日对偶性(Lagrange duality) 1. 从原始问题到对偶问题 2. 弱对偶与强对偶 3. KKT条件 Reference: 拉格朗日对偶性(Lagrange duality) ...
- 最全面的改造Zuul网关为Spring Cloud Gateway(包含Zuul核心实现和Spring Cloud Gateway核心实现)
前言: 最近开发了Zuul网关的实现和Spring Cloud Gateway实现,对比Spring Cloud Gateway发现后者性能好支持场景也丰富.在高并发或者复杂的分布式下,后者限流和自定 ...
- python自动化测试框架unittest
对于刚学习python自动化测试的小伙伴来说,unittest是一个非常适合的框架: 通过unittest,可以管理测试用例的执行,自动生成简单的自动化测试报告: 首先我们尝试编写编写一个最简单的un ...
- spring aop 解决模糊查询参数 % - /等特殊符号问题
import com.hsq.common.utils.StringUtil;import org.aspectj.lang.ProceedingJoinPoint;import org.aspect ...
- Docker最简单入门之(二)——简单使用Docker
0.前言 本章主要写一些怎么使用Docker,拉取镜像和创建容器等之类的Docker的常用基本操作.在开始写之前,大家需要明白一下几个名词的含义 1.镜像:镜像是指一个类似于安装包的东西,尝试安装过电 ...