codeforces 487B B. 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 题意: 把n个数字分成尽量少的连续部分,每部分至少l个数,而且最大值和最小值得差不超过s,问最少能分成多少部分; 思路: dp[i]表示前i个数字最少能分成多少部分,因为差不能超过s,所以先求出RMQ,以方便后面询问,可以二分找出区间[l,i]l是满足要求的最左边的第一个差值不超过s的位置,然后看是否满足长度的要求,还有就是要转移最小的值; AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e6+3;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e5+10;
const int maxn=1e3+520;
const double eps=1e-12; int n,s,le,a[N],dp[N];
int mi[N][20],ma[N][20];
inline void RMQ()
{
for(int i=0;i<n;i++)ma[i][0]=mi[i][0]=a[i];
for(int j=1;(1<<j)<=n;j++)
{
for(int i=0;i+(1<<j)<=n;i++)
{
mi[i][j]=min(mi[i][j-1],mi[i+(1<<(j-1))][j-1]);
ma[i][j]=max(ma[i][j-1],ma[i+(1<<(j-1))][j-1]);
}
}
}
int query(int L,int R)
{
int k=0;
while((1<<(k+1))<=R-L+1)k++;
return max(ma[L][k],ma[R-(1<<k)+1][k])-min(mi[L][k],mi[R-(1<<k)+1][k]);
} int check(int x,int y)
{
int temp=query(x,y);
if(temp<=s)return 1;
return 0;
}
int main()
{
read(n);read(s);read(le);
For(i,0,n-1)read(a[i]),dp[i]=inf;
RMQ();
For(i,0,n-1)
{
int l=0,r=i;
while(l<=r)
{
int mid=(l+r)>>1;
if(check(mid,i))r=mid-1;
else l=mid+1;
}
r++; if(i-r+1<le)dp[i]=inf;
else
{
if(r==0)dp[i]=1;
else
{
for(int j=r-1;j<=i-le;j++)dp[i]=min(dp[i],dp[j]+1);
}
}
}
if(dp[n-1]==inf)dp[n-1]=-1;
cout<<dp[n-1]<<endl;
return 0;
}
codeforces 487B B. Strip(RMQ+二分+dp)的更多相关文章
- Codeforces 749E Gosha is hunting 二分+DP
很神奇的一题 看完题解不由惊叹 题意:$n$个神奇宝贝 $a$个普通球 $b$个高级球 普通球抓住$i$神奇宝贝的概率为$u[i]$ 高级球为$p[i]$ 一起用为$u[i]+p[i]-u[i]*p[ ...
- 【bzoj2500】幸福的道路 树形dp+倍增RMQ+二分
原文地址:http://www.cnblogs.com/GXZlegend/p/6825389.html 题目描述 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一 ...
- [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)
[Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...
- Codeforces 865C Gotta Go Fast 二分 + 期望dp (看题解)
第一次看到这种骚东西, 期望还能二分的啊??? 因为存在重置的操作, 所以我们再dp的过程中有环存在. 为了消除环的影响, 我们二分dp[ 0 ][ 0 ]的值, 与通过dp得出的dp[ 0 ][ 0 ...
- *HDU3486 RMQ+二分
Interviewe Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 「学习笔记」wqs二分/dp凸优化
[学习笔记]wqs二分/DP凸优化 从一个经典问题谈起: 有一个长度为 \(n\) 的序列 \(a\),要求找出恰好 \(k\) 个不相交的连续子序列,使得这 \(k\) 个序列的和最大 \(1 \l ...
- hdu 3486 Interviewe (RMQ+二分)
Interviewe Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 二分+DP HDU 3433 A Task Process
HDU 3433 A Task Process Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- hdu 3433 A Task Process 二分+dp
A Task Process Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
随机推荐
- hibernate初步4
JPA 1.JPA概述 JPA(Java Persistence API)是Sun官方提出的Java持久化规范.它为Java开发人员提供了一种对象/关系映射工具来管理Java应用中的关系数据.,而Hi ...
- 四、MyBatis主配置文件
//备注:该博客引自:http://limingnihao.iteye.com/blog/1060764 在定义sqlSessionFactory时需要指定MyBatis主配置文件: Xml代码 收藏 ...
- php正规则表达式学习笔记(几个常用函数的区别)
preg_mache()函数和 preg_mache_all()函数的区别: preg_mache()只会匹配规则中的字符一次, preg_mache_all()会匹配符合条件的所有字符! 例子对比: ...
- 【原创】使用.NET Core 1.0创建一个Self-Contained控制台应用
开发机器:win7-x64 .NET Core版本:1.0.0-preview2-003121 Visual Studio Code:1.2.1 至于什么是Self-Contained应用类型以及与P ...
- bootstrap dialog自行控制窗口的关闭
在使用dialog的时候,我们通常不希望点击btn的时候自动隐藏dialog,通常需要做一些清理或者ajax操作,在bootstrap dialog中,这是通过 data-dismiss=" ...
- mysql metadata lock锁
很多情况下,很多问题从理论上或者管理上而言都是可以避免或者说很好解决的,但是一旦涉及到现实由于管理或者协调或者规范执行的不够到位,就会出现各种各样本不该出现的问题,这些问题的通常在生产环境并不会出现, ...
- emulator: ERROR: x86 emulation currently requires hardware acceleration!
emulator: ERROR: x86 emulation currently requires hardware acceleration!Please ensure Intel is prop ...
- 胖AP(1602i)与苹果设备之间的问题总结
问题现象: 苹果设备(5GHz)连接不稳定,表现为时断时续,或者加入无线的时候一直加入不进去. 有些2.4GHz设备会在几个AP之间相互跳. 分析: 1. 先说苹果设备,它既支持2.4G 也支持5G, ...
- SharePoint 2013 图像呈现形式介绍
由于图像呈现形式依赖 SharePoint Server 2013 中的其他功能,因此需确保您满足本节中的先决条件,才能执行本文中的过程.先决条件包括: • 发布网站集 您要在其中添加图像呈现形式的网 ...
- IOS编程思想
从今天起想走进IOS架构的大门,一直不屑于学习第三方框架,觉得框架也是一点点代码给垒起来的,只要掌握了代码就可以了,殊不知垒代码的过程才是最重要的,而这个过程又岂是一朝一夕就能达到完美境界的,达到完美 ...