2014/11/29
想了一天多,被它的标签吓住了。。
也不算dp,就是遍历求每个点包括在内的区间,中间加上判断(区间长度和 maxn - minn的限制)有一条件不满足就跳出。
最后输出记录的区间数ans
(真sb了,想了一天不敢写去搜题解,,)
#include<stdio.h>
#include<string.h>
int a[100000];
int Max(int a,int b)
{
return a>b?a:b;
}
int Min(int a,int b)
{
return a<b?a:b;
}
int main()
{
int n, s ,d;
while(~scanf("%d%d%d",&n,&s,&d))
{
int temp = 1,ans = 0;
int l , r, cnt;
int last = 0;
for(int i =0; i < n; i++)
scanf("%d",&a[i]);
for(int pos = 0;pos < n; pos++)
{
cnt = 1;
r = pos + 1;
int maxn = a[pos];
int minn = a[pos];
while(r < n)
{
maxn = Max(maxn,a[r]);
minn = Min(minn,a[r]);
if(maxn - minn <= s)
{
cnt++;
}
else break;
r++;
}
r--;
maxn = a[pos];
minn = a[pos];
l = pos - 1;
while(l >= last)
{
maxn = Max(maxn, a[l]);
minn = Min(minn, a[l]);
if(maxn - minn <= s)
cnt++;
else
break;
l--;
}
l++;
if(cnt < d)
{
temp = 0;
break;
}
ans++;
last = l + d;
pos = r;
}
if(!temp)
printf("-1\n");
else
printf("%d\n", ans);
}
return 0;
}