Codeforces Round #278 (Div. 2) D. Strip 线段树优化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.
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=1e6+,inf=2e9;
const ll INF=1e18+,mod=;
int a[N];
struct linetree
{
int maxx[N<<],minn[N<<];
void pushup(int pos)
{
maxx[pos]=max(maxx[pos<<],maxx[pos<<|]);
minn[pos]=min(minn[pos<<],minn[pos<<|]);
}
void build(int l,int r,int pos)
{
if(l==r)
{
maxx[pos]=a[l];
minn[pos]=a[l];
return;
}
int mid=(l+r)>>;
build(l,mid,pos<<);
build(mid+,r,pos<<|);
pushup(pos);
}
void update(int p,int c,int l,int r,int pos)
{
if(l==r)
{
maxx[pos]=c;
minn[pos]=c;
return;
}
int mid=(l+r)>>;
if(p<=mid)
update(p,c,l,mid,pos<<);
else
update(p,c,mid+,r,pos<<|);
pushup(pos);
}
int query(int L,int R,int l,int r,int pos,int flag)
{
if(L<=l&&r<=R)
{
if(flag)
return maxx[pos];
else
return minn[pos];
}
int mid=(l+r)>>;
int ans=-inf;
if(!flag)
ans=inf;
if(L<=mid)
if(flag)
ans=max(ans,query(L,R,l,mid,pos<<,flag));
else
ans=min(ans,query(L,R,l,mid,pos<<,flag));
if(R>mid)
if(flag)
ans=max(ans,query(L,R,mid+,r,pos<<|,flag));
else
ans=min(ans,query(L,R,mid+,r,pos<<|,flag));
return ans;
}
};
linetree tree,dp;
int main()
{
int n,l,s;
scanf("%d%d%d",&n,&s,&l);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
tree.build(,n+,);
dp.build(,n+,);
dp.update(,,,n+,);
for(int i=;i<=n;i++)
{
int st=,en=i-l,ans=-;
while(st<=en)
{
int mid=(st+en)>>;
if(tree.query(mid+,i,,n+,,)-tree.query(mid+,i,,n+,,)<=s)
{
ans=mid;
en=mid-;
}
else
st=mid+;
}
//cout<<ans<<endl;
if(ans==-||ans+>i-l+)
dp.update(i+,inf,,n+,);
else
{
int minn=dp.query(ans+,i-l+,,n+,,);
//cout<<ans+1<<" "<<i-l+1<<" "<<i<<" "<<minn<<endl;
dp.update(i+,minn+,,n+,);
} }
if(dp.query(n+,n+,,n+,,)>=inf)
printf("-1");
else
printf("%d\n",dp.query(n+,n+,,n+,,));
return ;
}
/// dp[i]=min(dp[mid-1]-dp[i-l])+1
Codeforces Round #278 (Div. 2) D. Strip 线段树优化dp的更多相关文章
- Codeforces Round #271 (Div. 2) E. Pillars 线段树优化dp
E. Pillars time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Codeforces 1603D - Artistic Partition(莫反+线段树优化 dp)
Codeforces 题面传送门 & 洛谷题面传送门 学 whk 时比较无聊开了道题做做发现是道神题( 介绍一种不太一样的做法,不观察出决策单调性也可以做. 首先一个很 trivial 的 o ...
- 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 ...
- Codeforces Round #278 (Div. 1) B - Strip dp+st表+单调队列
B - Strip 思路:简单dp,用st表+单调队列维护一下. #include<bits/stdc++.h> #define LL long long #define fi first ...
- Codeforces Round #603 (Div. 2) E. Editor 线段树
E. Editor The development of a text editor is a hard problem. You need to implement an extra module ...
- Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树
C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...
- Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
- Codeforces Round #765 Div.1 F. Souvenirs 线段树
题目链接:http://codeforces.com/contest/765/problem/F 题意概述: 给出一个序列,若干组询问,问给出下标区间中两数作差的最小绝对值. 分析: 这个题揭示着数据 ...
- 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
随机推荐
- mysql导入数据失败:mysql max_allowed_packet 设置过小
mysql根据配置文件会限制server接受的数据包大小. 有时候大的插入和更新会受max_allowed_packet 参数限制,导致写入或者更新失败. 查看目前配置 show VARIABLES ...
- UUID的定义以及作用
UUID含义是通用唯一识别码 (Universally Unique Identifier),这 是一个软件建构的标准,也是被开源软件基金会 (Open Software Foundation, OS ...
- yii2GridView的简单使用
GridView::widget([ 'dataProvider' => $dataProvider,// 你传过来的ActiveDataProvider // 'filterModel' =& ...
- Oracle数据类型clob和blob
clob(size)变长 字符型大对象,最大8tb 应用场景:如果varchar2不够用了使用clob blob(size)变长 二进制数据,可以存放图片声音 8tb 但是,实际工作中很少把图片和声音 ...
- 怎么将Android的API demo导入到Eclipse工作区中
File ->New Android Project 选择Create project from existing sample (不同Android版本有对应的ApiDemo示例).
- stark - filter、pop、总结
一.filter 效果图 知识点 1.配置得显示Filter,不配置就不显示了 list_filter = ['title','publish', 'authors'] 2.前端显示 后端返回 字典 ...
- FPN(feature pyramid networks)
多尺度的object detection算法:FPN(feature pyramid networks). 原来多数的object detection算法都是只采用顶层特征做预测,但我们知道低层的特征 ...
- 解决SpringMVC中文乱码
第一种:表单提交后controller获得中文参数后乱码解决方案 注意: 1: form表单提交方式为必须为post,get方式下面spring编码过滤器不起效果 2: jsp页面编码设置为UTF-8 ...
- Day22 文件上传下载和javaMail
day22总结 文件上传概述 1 文件上传的作用 例如网络硬盘!就是用来上传下载文件的. 在智联招聘上填写一个完整的简历还需要上传照片呢. 2 文件上传对页面的要求 上传文件的要求比较多,需要 ...
- sql server 碎片整理——DBCC SHOWCONTIG
转自: 1.http://blog.sina.com.cn/s/blog_6d2675450101ks6i.html 2.http://www.cnblogs.com/CareySon/archive ...