Codeforces Round #278 (Div. 1) Strip (线段树 二分 RMQ DP)
1 second
256 megabytes
standard input
standard output
Alexandra has a paper strip with n numbers on it. Let's call them ai from left to right.
Now Alexandra wants to split it into some pieces (possibly 1). For each piece of strip, it must satisfy:
- Each piece should contain at least l numbers.
- The difference between the maximal and the minimal number on the piece should be at most s.
Please help Alexandra to find the minimal number of pieces meeting the condition above.
The first line contains three space-separated integers n, s, l (1 ≤ n ≤ 105, 0 ≤ s ≤ 109, 1 ≤ l ≤ 105).
The second line contains n integers ai separated by spaces ( - 109 ≤ ai ≤ 109).
Output the minimal number of strip pieces.
If there are no ways to split the strip, output -1.
7 2 2
1 3 1 2 4 1 2
3
7 2 2
1 100 1 100 1 100 1
-1
For the first sample, we can split the strip into 3 pieces: [1, 3, 1], [2, 4], [1, 2].
For the second sample, we can't let 1 and 100 be on the same piece, so no solution exists.
【题意】给你一个序列,让你分成若干份,对于每一份,要求最大值-最小值<=s,且每一份长度不小于L。求分得的最小份数,若不可分,输出-1.
【分析】考虑DP,dp[i]代表1~i分得的最小份数,则dp[i]=min(dp[j])+1,j当然得满足条件,首先i-j+1>=L,然后max[j,i]-min[j,i]<=s,考虑到区间最大值-最小值具有单调性,所以二分极限的j,然后判断一下j的可行性,可由j-1~i-L之间的最小dp值转移过来。区间最大最小值用RMQ,线段树维护区间dp最小值。
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 1e5+;;
const int M = ;
const int mod = 1e9+;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
int n,len,s;
int dp[N];
int a[N],mi[N*];
int mn[N][],mx[N][],mm[N];
void init() {
for(int j=; j<=mm[n]; ++j) {
for(int i=; i+(<<j)-<=n; ++i) {
mn[i][j]=min(mn[i][j-],mn[i+(<<(j-))][j-]);
mx[i][j]=max(mx[i][j-],mx[i+(<<(j-))][j-]);
}
}
}
int getmx(int l,int r) {
int k = mm[r-l+];
return max(mx[l][k],mx[r-(<<k)+][k]);
}
int getmn(int l,int r) {
int k = mm[r-l+];
return min(mn[l][k],mn[r-(<<k)+][k]);
}
void upd(int l,int r,int rt,int pos){
if(l==r){
mi[rt]=dp[l];
return;
}
int mid=(l+r)>>;
if(pos<=mid)upd(l,mid,rt<<,pos);
else upd(mid+,r,rt<<|,pos);
mi[rt]=min(mi[rt<<],mi[rt<<|]);
}
int qry(int L,int R,int l,int r,int rt){
if(L<=l&&r<=R){
return mi[rt];
}
int ret=inf,mid=(l+r)>>;
if(L<=mid)ret=min(ret,qry(L,R,l,mid,rt<<));
if(R>mid)ret=min(ret,qry(L,R,mid+,r,rt<<|));
return ret;
}
void solve(int pos){
int l=,r=pos,ans=-;
while(l<=r){
int mid=(l+r)>>;
int minn=getmn(mid,pos);
int maxn=getmx(mid,pos);
if(maxn-minn<=s)r=mid-,ans=mid;
else l=mid+;
}
if(ans==-||pos-ans+<len)dp[pos]=inf;
else if(ans==)dp[pos]=;
else dp[pos]=qry(max(ans-,),pos-len,,n,)+;
}
int main(){
mm[]=-;
for(int i=; i<N; ++i)mm[i]=(i&(i-))?mm[i-]:mm[i-]+;
scanf("%d%d%d",&n,&s,&len);
for(int i=;i<=n;i++)scanf("%d",&a[i]),mn[i][]=mx[i][]=a[i];
init();
met(dp,inf);
for(int i=;i<=n;i++){
if(i<len)dp[i]=inf;
else solve(i);
upd(,n,,i);
}
if(dp[n]>=inf)puts("-1");
else printf("%d\n",dp[n]);
return ;
}
Codeforces Round #278 (Div. 1) Strip (线段树 二分 RMQ DP)的更多相关文章
- Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)
https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...
- Codeforces Round #426 (Div. 2) D 线段树优化dp
D. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...
- Please, another Queries on Array?(Codeforces Round #538 (Div. 2)F+线段树+欧拉函数+bitset)
题目链接 传送门 题面 思路 设\(x=\prod\limits_{i=l}^{r}a_i\)=\(\prod\limits_{i=1}^{n}p_i^{c_i}\) 由欧拉函数是积性函数得: \[ ...
- Nastya Hasn't Written a Legend(Codeforces Round #546 (Div. 2)E+线段树)
题目链接 传送门 题面 题意 给你一个\(a\)数组和一个\(k\)数组,进行\(q\)次操作,操作分为两种: 将\(a_i\)增加\(x\),此时如果\(a_{i+1}<a_i+k_i\),那 ...
- Codeforces Round #422 (Div. 2) E. Liar 后缀数组+RMQ+DP
E. Liar The first semester ended. You know, after the end of the first semester the holidays beg ...
- Codeforces Round #267 (Div. 2) C. George and Job(DP)补题
Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently ...
- Codeforces Round #278 (Div. 1) B. Strip multiset维护DP
B. Strip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/487/problem/B De ...
- Educational Codeforces Round 6 E dfs序+线段树
题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...
- Codeforces Round #278 (Div. 2) D. Strip 线段树优化dp
D. Strip time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
随机推荐
- UVA129 暴力dfs,有许多值得学习的代码
紫书195 题目大意:给一个困难的串,困难的串的定义就是里面没有重复的串. 思路:不需要重新对之前的串进行判重,只需要对当前的加入的字符进行改变即可. 因为是判断字典序第k个的字符串,所以要多一个全局 ...
- 【BZOJ1975】【SDOI2010】魔法猪学院 [A*搜索]
魔法猪学院 Time Limit: 10 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description iPig在假期来到了传说中的魔法猪 ...
- java基础 运算符
算数运算符 加号:在操作数值.字符.字符串时其结果是不同的,当两个字符相加得到的是ASCII码表值, 当两个字符串相加时表示将两个字符串连接在一起,从而组成新的字符串. 除号:整数在使用除号操作时,得 ...
- [device tree] interrupt
Specifying interrupt information for devices ============================================ 1) Interru ...
- python3使用xlrd、xlwt、xlutils、openpyxl、xlsxwriter操作excel
特色简介 xlrd主要用来读excel,针对.xls格式: xlwt主要用来写excel,针对.xls格式,超出excel 的单格内容长度上限32767,就会报错: xlutils结合xlrd可以达到 ...
- 【bzoj1072】SCOI2007排列
状压dp,f[i][j]表示当前取了i,模数余j的状态. 然后向后推,枚举可能的数即可. 注意每个数存在重复,最后要除以相应出现次数的阶乘. #include<bits/stdc++.h> ...
- [New learn]SDWebImage框架的基本使用
代码:https://github.com/xufeng79x/SDWebImage 1.简介 SDWebImage是一个第三方框架,它能够帮助我们有效管理应用图片下载,沙盒保存和内存保存的任务.通过 ...
- [How to]基于本地镜像的yum镜像源搭建
1.简介 本文介绍如何在封闭环境(无外网)下安装离线安装本地镜像与基于本地镜像的yum镜像源. 2.环境版本交代: OS:CentOS-6.7-x86_64-minimal yum: yum-3.2. ...
- 调用微信JS-SDK接口上传图片
最近要在微信上做个问卷调查,有个上传图片功能,折腾找了半天资料,都不好弄,最终打算调用微信提供的上传图片接口,实现上传图片功能!此功能最大的好处是可以在微信服务器上暂存图片,减少本地服务器图片的缓存, ...
- sublime view_in_browser
今天安装了sublime的插件view in browser,发现ctrl+alt+V用不了,在preferences看了view in browser的配置,发现browser不是我电脑上的默认浏览 ...