Codeforces 488D Strip【dp+RMQ--------ST】
2017-09-07 13:07
405 查看
B. Strip
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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.
Input
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
Output the minimal number of strip pieces.
If there are no ways to split the strip, output -1.
Examples
Input
Output
Input
Output
Note
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.
题目大意:
给你一个长度为N的序列,我们将其分成若干个子序列(必须连续),需要保证每个子序列中的最大值和最小值的差不超过S,并且子序列中的元素的个数不能少于l,问最少可以分成几个子序列出来,如果不可能,输出-1.
思路:
1、对应查询区间最大值和最小值,那么我们使用ST算法是最好不过的,预处理很快,查询O(1);
2、那么考虑dp,设定dp【i】表示前i个(包括第i个在内),最少能够分成几段。
那么不难推出其状态转移方程:
dp【i】=min(dp【j】+1,dp【i】),其中j是第一个合法的位子,使得从j+1到i保证有至少l个元素,且其区间值极差小于等于s、
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<map>
using namespace std;
int a[200005];
int maxn[200005][100];
int minn[200005][100];
int dp[200005];
int n,s,l;
void ST()
{
int len=floor(log10(double(n))/log10(double(2)));
for(int j=1;j<=len;j++)
{
for(int i=1;i<=n+1-(1<<j);i++)
{
maxn[i][j]=max(maxn[i][j-1],maxn[i+(1<<(j-1))][j-1]);
minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j-1]);
}
}
}
int query(int a,int b)
{
int len= floor(log10(double(b-a+1))/log10(double(2)));
return max(maxn[a][len], maxn[b-(1<<len)+1][len])-min(minn[a][len], minn[b-(1<<len)+1][len]);
}
int main()
{
while(~scanf("%d%d%d",&n,&s,&l))
{
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
maxn[i][0]=minn[i][0]=a[i];
}
ST();
memset(dp,0x3f3f3f3f,sizeof(dp));
int pre=0;
dp[0]=0;
for(int i=1;i<=n;i++)
{
while(i-pre>=l&&query(pre+1,i)>s||dp[pre]==0x3f3f3f3f)pre++;
if(i-pre>=l)dp[i]=min(dp[pre]+1,dp[i]);
}
if(dp
==0x3f3f3f3f)printf("-1\n");
else printf("%d\n",dp
);
}
}
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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.
Input
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
Output the minimal number of strip pieces.
If there are no ways to split the strip, output -1.
Examples
Input
7 2 2 1 3 1 2 4 1 2
Output
3
Input
7 2 2 1 100 1 100 1 100 1
Output
-1
Note
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.
题目大意:
给你一个长度为N的序列,我们将其分成若干个子序列(必须连续),需要保证每个子序列中的最大值和最小值的差不超过S,并且子序列中的元素的个数不能少于l,问最少可以分成几个子序列出来,如果不可能,输出-1.
思路:
1、对应查询区间最大值和最小值,那么我们使用ST算法是最好不过的,预处理很快,查询O(1);
2、那么考虑dp,设定dp【i】表示前i个(包括第i个在内),最少能够分成几段。
那么不难推出其状态转移方程:
dp【i】=min(dp【j】+1,dp【i】),其中j是第一个合法的位子,使得从j+1到i保证有至少l个元素,且其区间值极差小于等于s、
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<map>
using namespace std;
int a[200005];
int maxn[200005][100];
int minn[200005][100];
int dp[200005];
int n,s,l;
void ST()
{
int len=floor(log10(double(n))/log10(double(2)));
for(int j=1;j<=len;j++)
{
for(int i=1;i<=n+1-(1<<j);i++)
{
maxn[i][j]=max(maxn[i][j-1],maxn[i+(1<<(j-1))][j-1]);
minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j-1]);
}
}
}
int query(int a,int b)
{
int len= floor(log10(double(b-a+1))/log10(double(2)));
return max(maxn[a][len], maxn[b-(1<<len)+1][len])-min(minn[a][len], minn[b-(1<<len)+1][len]);
}
int main()
{
while(~scanf("%d%d%d",&n,&s,&l))
{
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
maxn[i][0]=minn[i][0]=a[i];
}
ST();
memset(dp,0x3f3f3f3f,sizeof(dp));
int pre=0;
dp[0]=0;
for(int i=1;i<=n;i++)
{
while(i-pre>=l&&query(pre+1,i)>s||dp[pre]==0x3f3f3f3f)pre++;
if(i-pre>=l)dp[i]=min(dp[pre]+1,dp[i]);
}
if(dp
==0x3f3f3f3f)printf("-1\n");
else printf("%d\n",dp
);
}
}
相关文章推荐
- Codeforces 488D Strip【dp+RMQ--------ST】
- poj4003 树形dp, rmq_st
- Codeforces 487b Strip, dp + RMQ(经典)
- Codeforces 390C Inna and Candy Boxes RMQ简单变化 或 前缀和dp一下
- Fzu 2177 ytaaa【dp+RMQ--------ST】
- 【Manthan, Codefest 16E】【DP 从右向左线性扫描不用ST-RMQ】Startup Funding 最大的min(取min,取max) + n值选k最小做权的期望
- Codeforces 652C Foe Pairs【二分+RMQ】好像这题Dp做法很多啊
- CodeForces 279C Ladder (RMQ + dp)
- codeforces 的一道题 RMQ_ST
- codeforces-487B Strip(dp+rmq+二分+水数据)
- codeforces 126D Fibonacci Sums 递推 DP
- CodeForces 599C Day at the Beach(RMQ)
- CodeForces 219D 树形DP
- codeforces-611D-New Year and Ancient Prophecy【lcp+dp】【好题】
- codeforces 182E Wooden Fence(方案数DP)【模板】
- 【Codeforces Round 333 (Div 2)D】【线段树 or ST-RMQ 初始化78msAC】Lipshitz Sequence 若干区间询问所有子区间的答案和
- Codeforces_789C_(dp)
- Cipher CodeForces - 156C(dp)
- hdu 4123--Bob’s Race(树形DP+RMQ)
- codeforces 543d Road Improvement 树形dp (★ )