codeforces 985E Pencils and Boxes
题意:
把一个数组分成若干组,保证每组的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的更多相关文章
- codeforces 985E Pencils and Boxes(dp+思维)
E. Pencils and Boxes time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 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 ...
- Codeforces 985 E - Pencils and Boxes
E - Pencils and Boxes 思路: dp 先排个序,放进一个袋子里的显然是一段区间 定义状态:pos[i]表示小于等于i的可以作为(放进一个袋子里的)一段区间起点的离i最近的位置 显然 ...
- 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 ...
- CodeForces 551C - GukiZ hates Boxes - [二分+贪心]
题目链接:http://codeforces.com/problemset/problem/551/C time limit per test 2 seconds memory limit per t ...
- Codeforces 551C GukiZ hates Boxes(二分)
Problem C. GukiZ hates Boxes Solution: 假设最后一个非零的位置为K,所有位置上的和为S 那么答案的范围在[K+1,K+S]. 二分这个答案ans,然后对每个人尽量 ...
- Codeforces 1053 C - Putting Boxes Together
C - Putting Boxes Together 思路: 求带权中位数 用树状数组维护修改 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) ...
- Codeforces 821C - Okabe and Boxes
821C - Okabe and Boxes 思路:模拟.因为只需要比较栈顶和当前要删除的值就可以了,所以如果栈顶和当前要删除的值不同时,栈就可以清空了(因为下一次的栈顶不可能出现在前面那些值中). ...
- Codeforces 260C - Balls and Boxes
260C - Balls and Boxes 思路:模拟.在x前面找到最小值,如果没有,从0跳到n,继续找到最小值,边找最小值路过的点边减1.然后所有值都减去最小值,最小值那个点加上减去的值. 找到x ...
随机推荐
- Web重温系列(一):利用寄宿于IIS的WCF序列化文件
这两年一直在做WinForm,对于Web已经比较生疏了,其实之前做的也不是很多. 这两天做了一个小工具,功能很简单,就是想有个地方存放办公室同事的机器名和IP的信息,包括附加的用户名和更新时间.比较之 ...
- SpringMVC+ Mybatis 配置多数据源 + 自动数据源切换 + 实现数据库读写分离
现在大型的电子商务系统,在数据库层面大都采用读写分离技术,就是一个Master数据库,多个Slave数据库.Master库负责数据更新和实时数据查询,Slave库当然负责非实时数据查询.因为在实际的应 ...
- yii2快速導出phpexcel
https://packagist.org/packages/moonlandsoft/yii2-phpexcel 安装方式:首先是已经安装过Composer,则通过 Composer 下载安装 Mo ...
- DOTween-Ease缓动函数
Ease.InQuad 不知道Quad代表什么意思 Ease.InQuart 有1/4的时间是没有缓动. Ease.InQuint, 是1/5时间没有缓动. Ease.InExpo 一直很平缓, ...
- pyCharm最新激活码(2018激活码)
首先输入新的License sever address 首先尝试处理方法是,针对过期会弹出激活框: 选择 Activate new license with License server (用lice ...
- LeetCode 51 N-Queens II
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- Adas术语简称
V2X: DSRC:专用短程通信(DSRC)的公用频谱信道交换数据
- java相对路径、绝对路径及类路径
import java.io.File; import java.net.URL; /** * java相对路径.绝对路径及类路径的测试 */ public class Test { /** * 测试 ...
- 《Spring实战》学习笔记-第五章:构建Spring web应用
之前一直在看<Spring实战>第三版,看到第五章时发现很多东西已经过时被废弃了,于是现在开始读<Spring实战>第四版了,章节安排与之前不同了,里面应用的应该是最新的技术. ...
- springboot2.X访问静态文件配置
config配置: @Configuration public class WebMvcConfig implements WebMvcConfigurer { /** * 跨域配置 * @retur ...