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 ...
随机推荐
- MySql快速插入以及批量更新
MySql快速插入以及批量更新 插入: MySql提供了可以一次插入多条数据的用法: [sql] INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6), ...
- 通用 正则表达式 C# (.NET)Regex 总结
[参考]C#正则表达式Regex类的用法 语法: 1. new System.Text.RegularExpressions.Regex("\\$\\d{1,2}\\}"). ...
- centos7.3安装nvidia驱动和cuda9
一, 挂载本地源镜像 1) 下载操作系统镜像 所有服务器操作系统必须统一,本平台只支持 CentOS 7.3 1611,镜像下载地址. 2) 上传镜像到服务器 ,假设上传在 root 下 3) 建立挂 ...
- ifconfig 中的 eth0 eth0:1 eth0.1 与 lo
1. eth0 eth0:1 eth0.1 eth0 eth0:1 和eth0.1三者的关系对应于物理网卡.子网卡.虚拟VLAN网卡的关系:物理网卡:物理网卡这里指的是服务器上实际的网络接口设备,这里 ...
- 【Excel】读取CSV文本
Option Explicit ' CSV形式テキストファイル(5カラム)読み込みサンプル Sub READ_TextFile() Const cnsTITLE = "テキストファイル読み込 ...
- 入围T00ls 2018风云人物
今天早上打开T00ls,发现成功入围了<T00ls第六届(2018)年度人物风云榜>,共34名年度人物,每个id可投10票,34选10. T00ls是当前国内为数不多的民间网络信息安全研究 ...
- Elasticsearch学习之有用博客
推荐阅读:1.阿里:https://elasticsearch.cn/article/61712.滴滴:http://t.cn/EUNLkNU3.腾讯:http://t.cn/E4y9ylL4.携程: ...
- ubuntu中python3安装package
1.实验环境 Ubuntu16.04x86 + python3.5 ubuntu中同时存在python2.7 和 python3.5 2.pip使用说明 sudo pip install packag ...
- Codeforces Round 504
(交互题真神奇,,,我自己瞎写了一发目测样例都没过去就AC了...) (只出了两题的竟然没掉下蓝名真是可怕) A:我的代码太不美观了,放个同学的(因为我是c++63分的蒟蒻所以根本不知道那些函数怎么用 ...
- 24、设计模式、webpack
利用静态属性:长驻内存 (一) 单例模式 概念:单个实例,只有一个对象,多次创建,返回同一个对象. 单例模式的核心:==确保只有一个实例==,并提供全局访问. //利用了静态属性:长驻内存 funct ...