Strip
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.
我先说几句 我*************
不到四十行的sbdp写了两天。。。。。。。到现在还没想懂
不说了泪奔
#include<cstdio>
#include<cstring>
#include<set>
using namespace std;
#define N 200010
#define inf 1000000009
int n,S,l,last=;
int dp[N],mn[*N],a[N];
multiset<int> s;
set<int> v;
void update(int l,int r,int x,int pos,int num)
{
if(l==r)
{
mn[x]=num;
return;
}
int mid=(l+r)>>;
if(pos<=mid) update(l,mid,x<<,pos,num);
else update(mid+,r,x<<|,pos,num);
mn[x]=min(mn[x<<],mn[x<<|]);
}
int query(int l,int r,int x,int a,int b)
{
if(l>b||r<a) return inf;
if(l>=a&&r<=b) return mn[x];
int mid=(l+r)>>;
return min(query(l,mid,x<<,a,b),query(mid+,r,x<<|,a,b));
}
int main()
{
memset(mn,0x3f3f,sizeof(mn));
scanf("%d%d%d",&n,&S,&l);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
/*
dp[i]:到i位置能分多少段
*/
for(int i=;i<=n;i++)
{
s.insert(a[i]);
while(*s.rbegin()-*s.begin()>S)
{
s.erase(s.find(a[last]));
last++;
}
if(i-last+>=l&&last==) dp[i]=; else
dp[i]=query(,n,,last-,i-l)+;
update(,n,,i,dp[i]);
}
printf("%d\n",dp[n]>=inf?-:dp[n]);
return ;
}
Strip的更多相关文章
- python strip()函数 介绍
python strip()函数 介绍,需要的朋友可以参考一下 函数原型 声明:s为字符串,rm为要删除的字符序列 s.strip(rm) 删除s字符串中开头.结尾处,位于 rm删除 ...
- 4、Python:strip(),split()
1.strip()函数 strip()是删除'()'里面的字符,当()为空时,默认删除空白符(包括'\n','\r','\t','') (1)s.strip(rm) 删除s字符串中开头. ...
- Strip JS – 低侵入,响应式的 Lightbox 效果
Strip 是一个灯箱效果插件,显示的时候只会覆盖部分的页面,这使得侵扰程度较低,并留出了空间与页面上的大屏幕,同时给予小型移动设备上的经典灯箱体验.Strp JS 基于 jQuery 库实现,支持 ...
- strip的用法
函数原型 声明:s为字符串,rm为要删除的字符序列 s.strip(rm) 删除s字符串中开头.结尾处,位于 rm删除序列的字符 s.lstrip(rm) 删除s字符串中开头 ...
- python中strip,lstrip,rstrip简介
一.起因 今天在做角色控制中,有一个地方用到rstrip,判断用户请求的url是否与数据库对应可用权限中url相符. if request.path == x.url or request.path. ...
- 【C++实现python字符串函数库】strip、lstrip、rstrip方法
[C++实现python字符串函数库]strip.lstrip.rstrip方法 这三个方法用于删除字符串首尾处指定的字符,默认删除空白符(包括'\n', '\r', '\t', ' '). s.st ...
- pythong中字符串strip的用法
strip的用法是去除字符串中前后两端的xx字符,xx是一个字符数组,并不是去掉“”中的字符串, 数组中包含的字符都要在字符串中去除.默认去掉空格,lstrip则是去掉左边的,rstrip是右边的 见 ...
- Python strip函数用法小结
声明:s为字符串,rm为要删除的字符序列 s.strip(rm) 删除s字符串中开头.结尾处,位于 rm删除序列的字符 s.lstrip(rm) 删除s字符串中开头处,位于 ...
- python strip() lstrip() rstrip() 使用方法
Python中的strip用于去除字符串的首尾字符串,同理,lstrip用于去除最左边的字符,rstrip用于去除最右边的字符. 这三个函数都可传入一个参数,指定要去除的首尾字符. 需要注意的是,传入 ...
- Linux Strip
一.简介 strip经常用来去除目标文件中的一些符号表.调试符号表信息,以减小程序的大小. 二.语法 https://sourceware.org/binutils/docs/binutils/str ...
随机推荐
- 道路游戏(洛谷 P1070)
题目描述 小新正在玩一个简单的电脑游戏. 游戏中有一条环形马路,马路上有 n 个机器人工厂,两个相邻机器人工厂之间由一小段马路连接.小新以某个机器人工厂为起点,按顺时针顺序依次将这 n 个机器人工厂编 ...
- [NOIP1999] 提高组 洛谷P1016 旅行家的预算
题目描述 一个旅行家想驾驶汽车以最少的费用从一个城市到另一个城市(假设出发时油箱是空的).给定两个城市之间的距离D1.汽车油箱的容量C(以升为单位).每升汽油能行驶的距离D2.出发点每升汽油价格P和沿 ...
- PatentTips - Solid State Memory Wear Leveling
BACKGROUND OF THE INVENTION Solid-state memory devices encompass rewritable non-volatile memory devi ...
- mysql 时间类型datetime与timestamp区别比较
mysql 时间类型datetime与timestamp区别比较 相同点: 显示宽度和格式相同,显示宽度固定在19字符,格式为YYYY-MM-DD HH:MM:SS. 不同点: (1)时间范围不同: ...
- Tell me the area---hdu1798 (数学 几何)
http://acm.hdu.edu.cn/showproblem.php?pid=1798 给你两个圆求阴影部分的面积 求出两个扇形的面积减去四边形的面积 扇形的面积是度数(弧度制)*半径的平方 不 ...
- MongoDB学习day08--Mongoose索引、Mongoose内置方法、扩展Mongoose Model的静态方法和实例方法
一.Mongoose索引 索引是对数据库表中一列或多列的值进行排序的一种结构, 可以让我们查询数据库变得更快. MongoDB 的索引几乎与传统的关系型数据库一模一样, 这其中也包括一些基本的查询优化 ...
- 异步SOCKET分包和组包的一种通用算法
unit uPackage;// 应用协议// cxg 2016-9-23// 包=包头+包体 interface uses SysUtils, Classes, PeachCtrl.Net.Iocp ...
- pip命令自动补全功能;设置代理;使用国内源
这是pip自带的功能 执行的脚本 把脚本写入.zshrc或者profile等里面,执行source立即生效 设置代理: pip --proxy=http://username:password@pro ...
- 配置Python 2.7.1外加环境pywin32-216.win32-py2.7
python-2.7.1 安装包 下载地址:http://download.csdn.net/detail/baidu_14854543/7985187 pywin32-216.win32-py2. ...
- vue 自定义 移动端筛选条件
1.创建组件 components/FilterBar/FilterBar.vue <template> <div class="filterbar" :styl ...