版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CSDN___CSDN/article/details/87101665
http://codeforces.com/problemset/problem/427/B
从一串数字中选出连续的长度为c的子串,且子串中的每一个数都不能大于t,问这样的子串有多少个
TLE,看看n的范围就知道了,哎呀呀,有点chun
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
int a[200000+5];
int vis[200000+5];
int main()
{
ios_base::sync_with_stdio(false);
int n,t,c,i,j;
memset(vis,1,sizeof(vis));
cin >> n >> t >> c;
for(i=0;i<n;i++)
{
cin >> a[i];
if(a[i]>t)
vis[i] = 0;
}
int cnt = 0;
for(i=0;i<=n-c;i++)
{
int flag = 1;
for(j=i;j<i+c && i+c<=n;j++)
{
if(vis[j]==0)
{
flag = 0;
break;
}
}
cnt += flag;
}
cout << cnt << endl;
return 0;
}
这有点类似于一个滑动的窗口,设置两个变量,front和rear,当连续的c个数都满足的时候front和rear怎么移动,不满足的时候怎么移动
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
int a[200000+5];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n,t,c,i;
cin >> n >> t >> c;
for(i=0;i<n;i++)
{
cin >> a[i];
}
int cnt = 0,ans = 0;
int front = 0, rear = 0;
while(front<=n-c)
{
if(a[rear]<=t)
{
rear++;
ans++;
if(ans==c)
{
cnt++;
front++;
ans--;
}
}
else
{
front = rear+1;
rear = front;
ans = 0;
}
}
cout << cnt << endl;
return 0;
}