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 ...
随机推荐
- org.hibernate.InvalidMappingException: Could not parse mapping document from无法创建sessionFactory
把 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" 改为 "http://hibernate.sourc ...
- Appium Server
原理 我的是配置以下直接打开 报这个错是没按装apk { "platformName": "Android", "platformVersion&qu ...
- Javascript 运行上下文和作用域链
一.作用域Scope和上下文Context 在javascript中,作用域scope和上下文context是两个不同的概念.每个函数调用都会伴随着scope和context,从本质上来说,scope ...
- python数据分析及展示(一)
一.IDE选择 Anaconda软件:开源免费,https://www.anaconda.com下载,根据系统进行安装.由于下载速度慢,可以去清华大学开源软件镜像站下载. Spyder软件设置:Too ...
- 离线提取域控HASH的方法
1.注册表提取 提取文件,Windows Server 2003或者Win XP 及以前需要提升到system权限,以后只要Administrator权限即可. reg save hklm\sam s ...
- XML文档中的xmlns、xmlns:xsi和xsi:schemaLocation
文章转载自:https://yq.aliyun.com/articles/40353 相信很多人和我一样,在编写Spring或者Maven或者其他需要用到XML文档的程序时,通常都是将这些XML文档头 ...
- JavaScript 事件绑定函数
function panTest(m_onClickFun) { var This = this; This.onClickFun = m_onClickFun; /* This.onClickFun ...
- cmd返回上一级和根目录
https://jingyan.baidu.com/article/066074d6154cf4c3c21cb013.html
- java实现爬虫功能
/** * 爬取新闻信息,封装成实体bean */public class GetNews { public List<News> getNews() { // 存储新闻对象 List ...
- Wooden Sticks---(贪心)
Problem Description There is a pile of n wooden sticks. The length and weight of each stick are know ...