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 ...
随机推荐
- thinkphp---定义前台视图模板
具体可以参考: http://document.thinkphp.cn/manual_3_2.html#template_define 方法一:在入口文件中定义 // 定义模板路径 define(&q ...
- Jenkins之pipeline流水线配置
使用gitlab监听事件一旦git push自动部署 使用构建后操作 配置完用户构建前一步会自动构建下一个项目 pipeline插件 新建视图 点击run运行
- Oracle数据库PL/SQL Developer查询结果显示问号乱码的解决方法
PL SQL Developer,查询结果中的中文变成了一堆问号,SQL语句中的中文被提示invalid character,不能识别. 解决方法: 执行,select userenv('langua ...
- 利用Dockerfile构建一个基于CentOS 7镜像
利用Dockerfile构建一个基于CentOS 7,包括java 8, tomcat 7,php ,mysql+mycat的镜像. Dockerfile内容如下: FROM centosMAINTA ...
- Android中的Apk的加固(加壳)原理解析和实现(转)
一.前言 今天又到周末了,憋了好久又要出博客了,今天来介绍一下Android中的如何对Apk进行加固的原理.现阶段.我们知道Android中的反编译工作越来越让人操作熟练,我们辛苦的开发出一个apk, ...
- Python知识总汇
一.python基础 python基础 python编码问题 逻辑运算 二.python数据类型 二.python数据类型 三.IO(文件处理) 三.IO(文件处理) 四.函数 函数基础 名称空间与作 ...
- (4.20)sql server性能指标、性能计数器
(4.20)sql server性能指标.性能计数器 常规计数器 收集操作系统服务器的服务器性能信息,包括Processor.磁盘.网络.内存 Processor 处理器 1.1 % Processo ...
- linux route命令的使用详解 添加永久静态路由 tracert traceroute
linux route命令的使用详解 添加永久静态路由 tracert traceroute route -n Linuxroute print Windows traceroute ...
- Java基础教程:注解
Java基础教程:注解 本篇文章参考的相关资料链接: 维基百科:https://zh.wikipedia.org/wiki/Java%E6%B3%A8%E8%A7%A3 注解基础与高级应用:http: ...
- hdu5057 分块处理,当数值大于数据范围时树状数组 真是巧 将大数据分为小数据来处理
这题说的给了100000个数有100000次操作 询问 L和R 区间内 在D位上为P的个数,用树状数组存 要开[10][10][100000]的int 开不了但是能开 这么大的unsign short ...