题意略。

思路:

这个题目开始想的有点暴力,后来发现有搜索的性质,因此转而用动态规划。首先,我们要把这些数排个序。

定义状态:dp[i]为排序后i~n能否成功打包,1表示可以,0表示不能打包。

状态转移方程:dp[i] = max{dp[j]} (i + k <= j <= upper)upper为在有序数组中,第一个比当前值store[i] + d大的数的下标。

为了防止它卡数据,这里可以用单调队列优化一下,由于确定upper要使用二分搜索,所以总的复杂度是O(nlogn)。

详见代码:

#include<bits/stdc++.h>
#define maxn 500005
using namespace std; int dp[maxn],que[maxn],head,tail,store[maxn];
int n,k,d; int main(){
scanf("%d%d%d",&n,&k,&d);
for(int i = ;i <= n;++i) scanf("%d",&store[i]);
sort(store + ,store + + n);
dp[n + ] = ;
for(int i = n;i >= ;--i){
int temp = store[i] + d;
int r = upper_bound(store + ,store + + n,temp) - store;
int l = i + k;
if(l > r){
dp[i] = ;
}
else{
while(que[head] > r && head < tail) ++head;
while(head < tail && dp[que[tail - ]] < dp[l]) --tail;
que[tail++] = l;
dp[i] = dp[que[head]];
}
}
printf("%s\n",dp[] ? "YES" : "NO");
return ;
} /*
18 3 1
1 1 1 2 2 3 5 5 5 6 6 7 9 9 9 10 10 11 YES
*/

Codeforces 985E的更多相关文章

  1. codeforces 985E Pencils and Boxes

    题意: 把一个数组分成若干组,保证每组的size >= k并且一组中任意两个数字的差的绝对值 <= d,问存不存在这样的分法. 思路: 线性dp. 用dp[i]表示前i个数是否有分法. 设 ...

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

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

  3. Magolor的数据结构作业

    \(CodeForces 706E ~Working routine\) 给出一个矩阵,每次操作交换两个子矩阵,求最后状态. 使用链表存储,每次交换后,影响到的之后矩阵边缘的指针,暴力修改. \(~~ ...

  4. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  5. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  6. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  7. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  8. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  9. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

随机推荐

  1. PHP中的$_GET变量

    定义 在 PHP 中,预定义的 $_GET 变量用于收集来自 method="get" 的表单中的值. $_GET 变量 预定义的 $_GET 变量用于收集来自 method=&q ...

  2. python课堂整理1

      1.变量 变量只能由字母.数字.下划线组成 特例:1.变量不能用数字开头    2.不能是python的关键字 3.最好不要和python内置的东西重复 让变量名有意义 些 python3的关键字 ...

  3. 搭建python环境

    参考文章:https://blog.csdn.net/qq_33855133/article/details/73106176 对于配置环境变量,懂些技术的人来说,都是很简单. 变量是在操作系统中一个 ...

  4. spark 源码分析之十一--Spark RPC剖析之TransportClient、TransportServer剖析

    TransportClient类说明 先来看,官方文档给出的说明: Client for fetching consecutive chunks of a pre-negotiated stream. ...

  5. SSM-员工管理系统Demo---带分页和校验(含源码)

    页面展示: 前端JSP: <%@ page language="java" contentType="text/html; charset=UTF-8" ...

  6. $.ajax()在IE9下的兼容性问题

    最近在主导一个项目,遇到了一点问题,跟大家分享一下. 最终bug解决方案的链接地址:http://stackoverflow.com/questions/5241088/jquery-call-to- ...

  7. 从深处去掌握数据校验@Valid的作用(级联校验)

    每篇一句 NBA里有两大笑话:一是科比没天赋,二是詹姆斯没技术 相关阅读 [小家Java]深入了解数据校验:Java Bean Validation 2.0(JSR303.JSR349.JSR380) ...

  8. 如何创建Github创库

    重点:利用Markdown语言写简单的日常使用的文本 基础写作和语法格式 本篇文章的内容来源于Github的基础写作帮助.如果在观看时有什么问题,可以直接查阅源文件.另外需要说明的是Git对Markd ...

  9. 使用富文本编辑器Kindeditor

    今天在做需求的时候,遇到有一个字段,需要保存带有格式的内容,决定使用富文本框编辑器Kindeditor来实现,解决方法如下: 登录官网下载控件包: http://kindeditor.net/down ...

  10. The philosophy of ranking

    In the book Decision Quality, one will be trained to have three decision making system; one of them ...