题意略。

思路:

由于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的更多相关文章

  1. 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 ...

  2. ACM: Gym 101047K Training with Phuket's larvae - 思维题

     Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO F ...

  3. ACM: Gym 101047E Escape from Ayutthaya - BFS

    Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  4. ACM: Gym 101047B Renzo and the palindromic decoration - 手速题

     Gym 101047B  Renzo and the palindromic decoration Time Limit:2000MS     Memory Limit:65536KB     64 ...

  5. Gym 101102J---Divisible Numbers(反推技巧题)

    题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...

  6. Gym 100917J---Judgement(01背包+bitset)

    题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...

  7. Gym 100917J---dir -C(RMQ--ST)

    题目链接 http://codeforces.com/gym/100917/problem/D problem description Famous Berland coder and IT mana ...

  8. Gym 101102D---Rectangles(单调栈)

    题目链接 http://codeforces.com/gym/101102/problem/D problem  description Given an R×C grid with each cel ...

  9. Gym 101102C---Bored Judge(区间最大值)

    题目链接 http://codeforces.com/gym/101102/problem/C problem description Judge Bahosain was bored at ACM ...

随机推荐

  1. Python文件的两种用途

    目录 一.Python文件的两种用途 一.Python文件的两种用途 python文件总共有两种用途,一种是执行文件:另一种是被当做模块导入. 编写好的一个python文件可以有两种用途: 脚本,一个 ...

  2. C#多线程学习之如何操纵一个线程

    下面我们就动手来创建一个线程,使用Thread类创建线程时,只需提供线程入口即可.(线程入口使程序知道该让这个线程干什么事) 在C#中,线程入口是通过ThreadStart代理(delegate)来提 ...

  3. Java EE.JavaBean

    JavaBean是一组可移植.可重用.并可以组装到应用程序中的Java类.一个Model类(属性+构造函数).

  4. php curl问题汇总

    0. curl是个什么东西 复制代码代码如下: PHP supports libcurl, a library created by Daniel Stenberg, that allows you ...

  5. Java oop 多态

      1.多态指对象的多种形态:引用多态与方法多态   注意: A:继承是多态的实现基础 B:方法重写也是多态的体现   2.引用多态 A:父类的引用可以指向本类的对象:父类 对象名 = new 父类( ...

  6. Prometheus 整合 AlertManager

    简介 Alertmanager 主要用于接收 Prometheus 发送的告警信息,它很容易做到告警信息的去重,降噪,分组,策略路由,是一款前卫的告警通知系统.它支持丰富的告警通知渠道,可以将告警信息 ...

  7. centos6.5-7编译安装Ansible详细部署

    一.基础介绍==========================================================================================ansi ...

  8. CoreCLR Host源码分析(C++)

    废话不多说,直接上源码: 1.在托管程序集里面执行方法 HRESULT CorHost2::ExecuteAssembly(DWORD dwAppDomainId,//通过CreateAppDomai ...

  9. Apache ActiveMQ 实践 <二>

    一.订阅/发布模式 1.生产者 /** * 消息生产者 * */public class JMSProducer { private static final String USERNAME=Acti ...

  10. 【Java笔记】【Java核心技术卷1】chapter3 D3数据类型

    package chapter3; public class D3数据类型 { public static void main(String[] arg) { //Java 整型(字节数不会随硬件变化 ...