题意:

把一个数组分成若干组,保证每组的size >= k并且一组中任意两个数字的差的绝对值 <= d,问存不存在这样的分法。

思路:

线性dp。

用dp[i]表示前i个数是否有分法。

设j为满足a[i] - a[j] <= d的最小的a[j]的下标,那么dp[i]就可以从dp[j-1] ~ dp[i-k]转移,j可以二分得到。

首先一定得满足i - k,因为至少有k个数字;

假设前j-1个数字有分法,那么当j - 1 <= i - k的时候,说明第j到第i个数字至少有k个数字并且a[i] - a[j] <= d。

但是从j-1到i-k扫一遍要花费O(n)的时间,所以需要维护一个前缀和,判断的时候只需要j -1 <= i - k 并且pre[i-k] - pre[j-2] > 0,就说明从j - 1到i - k这个区间内有满足的分法。

代码:

 #include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 5e5 + ;
int a[N];
int dp[N];
int pre[N];
int main()
{
int n,k,d;
scanf("%d%d%d",&n,&k,&d);
for (int i = ;i <= n;i++)
{
scanf("%d",&a[i]);
}
sort(a+,a+n+);
if (a[k] - a[] <= d) dp[k] = ;
pre[k] = dp[k];
//dp[0] = 1;
//pre[0] = 1;
for (int i = k + ;i <= n;i++)
{
int en = i - k;
int l = ,r = i;
while (r - l > )
{
int mid = (l + r) >> ;
if (a[i] - a[mid] <= d) r = mid;
else l = mid + ;
}
while (r > && a[i] - a[r-] <= d) r--;
int st = r;
if (st == ) dp[i] = ;
else if (st- <= en) if (pre[en] - pre[st-] > ) dp[i] = ;
pre[i] = pre[i-] + dp[i];
}
//for (int i = 1;i <= n;i++) printf("dp[%d] = %d\n",i,dp[i]);
if (dp[n]) puts("Yes");
else puts("No");
return ;
}

codeforces 985E Pencils and Boxes的更多相关文章

  1. codeforces 985E Pencils and Boxes(dp+思维)

    E. Pencils and Boxes time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  2. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...

  3. Codeforces 985 E - Pencils and Boxes

    E - Pencils and Boxes 思路: dp 先排个序,放进一个袋子里的显然是一段区间 定义状态:pos[i]表示小于等于i的可以作为(放进一个袋子里的)一段区间起点的离i最近的位置 显然 ...

  4. codeforces 985 E. Pencils and Boxes (dp 树状数组)

    E. Pencils and Boxes time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  5. CodeForces 551C - GukiZ hates Boxes - [二分+贪心]

    题目链接:http://codeforces.com/problemset/problem/551/C time limit per test 2 seconds memory limit per t ...

  6. Codeforces 551C GukiZ hates Boxes(二分)

    Problem C. GukiZ hates Boxes Solution: 假设最后一个非零的位置为K,所有位置上的和为S 那么答案的范围在[K+1,K+S]. 二分这个答案ans,然后对每个人尽量 ...

  7. Codeforces 1053 C - Putting Boxes Together

    C - Putting Boxes Together 思路: 求带权中位数 用树状数组维护修改 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) ...

  8. Codeforces 821C - Okabe and Boxes

    821C - Okabe and Boxes 思路:模拟.因为只需要比较栈顶和当前要删除的值就可以了,所以如果栈顶和当前要删除的值不同时,栈就可以清空了(因为下一次的栈顶不可能出现在前面那些值中). ...

  9. Codeforces 260C - Balls and Boxes

    260C - Balls and Boxes 思路:模拟.在x前面找到最小值,如果没有,从0跳到n,继续找到最小值,边找最小值路过的点边减1.然后所有值都减去最小值,最小值那个点加上减去的值. 找到x ...

随机推荐

  1. 【Dubbo 源码解析】07_Dubbo 重试机制

    Dubbo 重试机制 通过前面 Dubbo 服务发现&引用 的分析,我们知道,Dubbo 的重试机制是通过 com.alibaba.dubbo.rpc.cluster.support.Fail ...

  2. Spark SQL configuration

    # export by: spark.sql("SET -v").show(n=200, truncate=False) key value meaning spark.sql.a ...

  3. 七、Sql Server 基础培训《进度7-笛卡尔积(知识点+实际操作)》

    知识点: 1.笛卡尔介绍 笛卡尔,近代法国著名哲学家.物理学家.数学家.神学家. 主要成就概述 笛卡尔在科学上的贡献是多方面的.笛卡尔不仅在哲学领域里开辟了一条新的道路,同时笛卡尔又是一勇于探索的科学 ...

  4. 排序算法--插入排序(Insertion Sort)_C#程序实现

    排序算法--插入排序(Insertion Sort)_C#程序实现 排序(Sort)是计算机程序设计中的一种重要操作,也是日常生活中经常遇到的问题.例如,字典中的单词是以字母的顺序排列,否则,使用起来 ...

  5. nuxt跨域

    根据nuxt官方文档提供的axios module 安装: npm install @nuxtjs/axios @nuxtjs/proxy --save nuxt.config.js modules: ...

  6. [Codeforces Round #507][Codeforces 1039C/1040E. Network Safety]

    题目链接:1039C - Network Safety/1040E - Network Safety 题目大意:不得不说这场比赛的题面真的是又臭又长...... 有n个点,m条边,每个点有对应的权值c ...

  7. 【谈谈IO】BIO、NIO和AIO

    BIO: BIO是阻塞IO,体现在一个线程调用IO的时候,会挂起等待,然后Thread会进入blocked状态:这样线程资源就会被闲置,造成资源浪费,通常一个系统线程数是有限的,而且,Thread进入 ...

  8. Eclipse 02: 安装SVN插件

    1.下载最新的Eclipse,我的版本是3.7.2 indigo(Eclipse IDE for Java EE Developers)版    如果没有安装的请到这里下载安装:http://ecli ...

  9. makefile编写规则

    cc = g++ -std=c++11 prom = calc deps = FtTest.h obj = FtTest.o newft.o LIBS = -lgtest_c11 $(prom): $ ...

  10. 为虚拟机配置NAT网络

    一.打开网络适配器 1.按图中所示进行配置 2.记得把网和VM8关联一下 3.点击网络设置 4.点击右下角小齿轮来配置ip 5.在IPv4下选择手动 6. 7. 这个就是Linux的NAT配置