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 ...
随机推荐
- numpy学习之创建数组
1.使用array函数创建数组 import numpy as np ndarray1 = np.array([1, 2, 3]) array([1, 2, 3]) ndarray2 = np.arr ...
- Zephyr学习(五)线程和调度
前面说过zephyr支持静态和动态两种方式创建线程,这里分析动态创建的方式.应用程序通过调用k_thread_create()函数创建一个线程,实际上是调用_impl_k_thread_create( ...
- 【Py-Github】根据条件筛选Github repo的例子
条件: language:python commits:>100 contributors:>2 stars:>5 fork:0 实现: from github import Git ...
- Python __all__变量用法
Python中一个py文件就是一个模块,“__all__”变量是一个特殊的变量,可以在py文件中,也可以在包的__init__.py中出现. 1.在普通模块中使用时,表示一个模块中允许哪些属性可以被导 ...
- RE:从零开始的莫比乌斯反演
炫酷反演魔术根本看不懂啊...也就看看PoPoQQQ的ppt了. 这个赛季结束了,一年可以学很多很多东西呢. 因为我是写给自己看的所以写的很垃圾. 公式: 按我的理解,反演就是 x可以表示成y,然后 ...
- css 背景图片虚化效果
转载地址:http://blog.csdn.net/ohehehou/article/details/51975539 需求:一个div设置了background: url,现在需要使图片背景模糊,d ...
- Win10上使用VS2015编译Caffe2
Caffe2的官网:https://caffe2.ai/ 1.下载.安装及相关准备 在Caffe2的官网点击"Get Started",即进入安装说明页面.官方还未提供编译好的bi ...
- devmapper: Thin Pool has 154464 free data blocks which is less than minimum required 163840 free dat
清理exited进程: docker rm $(docker ps -q -f status=exited) 清理dangling volumes: docker volume rm $(docker ...
- phpredis Redis集群 Redis Cluster
官方url: https://github.com/phpredis/phpredis/blob/develop/cluster.markdown#readme 2017年10月29日20:44:25 ...
- CH 3101 - 阶乘分解 - [埃筛]
题目链接:传送门 题解: $(1e6)!$ 这种数字,表示都表示不出来,想直接 $O(\sqrt{N})$ 分解质因数这种事情就不要想了. 考虑 $N!$ 的特殊性,这个数字的所有可能包含的质因子,就 ...