2017.10.11 T3 1969
样例数据
输入
5 6
1 2 1 1 5
1 2 3 4 5 6
输出
5 5 2 1 1 0
分析:又是一道O(n)的题orz,我也再一次没有想出正解,但是很接近了,想到40%暴力(千万不要多开数组,我忘了内存只能有68Mb!!!然后爆零orz)。
40%:某一段能够都>=k的前提是这一段的平均数至少是k,所以就算前缀和,更新不同长度的数串的总和,只要满足>=k*数串长度就可以了。
100%:存入所有前缀和。每读取一个k,就将前缀和改为所有数减去k后的,即sum[i]-sum[j]>=0时(
j<i
)就是平均数>=k的时候,所以要找距离最远的满足此条件的两个前缀和。在前缀和求出一个单调递减栈,然后从左往右扫一遍,在栈中找小于当前前缀和的最左边的前缀和。如果前缀和小于目前指针指向的栈中值,指针只右移一次(因为如果前面有满足条件的长度,而前缀和每次只向右移一个,所以栈中值最多向右移一个,不然长度就变短了,也就不用管了;如果到现在都还没有满足条件的,就说明这些前缀和都在栈中[因为两边都在移动,又一直没有满足条件的,说明栈中值和前缀和是平行],总不能让指针指向的栈中值位置都到枚举的前缀和的右边去了吧);如果前缀和大于指针指向的栈中值,就一直向左移到最大的一个满足小于当前前缀和的位置,算长度。
因为每次最多向右移n次,所以也最多向左移n次,最坏2n次运算,复杂度O(mN)。
其实如果以后还有类似的题的话,我可能依然不会orz
对于这样一道难想的题啊,即使出题人是神犇wuvin,我也想说一句:哔——
看看我考试时愤怒打出的代码:
代码
40%(数组改小后)
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<iomanip>
#include<queue>
#include<set>
using namespace std;
int getint()
{
int sum=0,f=1;
char ch;
for(ch=getchar();!isdigit(ch)&&ch!='-';ch=getchar());
if(ch=='-')
{
f=-1;
ch=getchar();
}
for(;isdigit(ch);ch=getchar())
sum=(sum<<3)+(sum<<1)+ch-48;
return sum*f;
}
long long sum[1010][1010],maxsum[1010],k;//千万不要贪心想试试能不能多对一组,sum多打一个0就GG
int N,m,a;
bool bj;
int main()
{
freopen("problem.in","r",stdin);
freopen("problem.out","w",stdout);
N=getint(),m=getint();
for(int i=1;i<=N;++i)
{
a=getint();
sum[1][i]=sum[1][i-1]+a;//其实我在想为什么要打二维数组欸,前缀和只要一维欸
maxsum[i]=sum[1][i];//记录长度为i的数串和最大为多少
}
for(int i=2;i<=N;++i)
for(int j=i;j<=N;++j)
{
sum[i][j]=sum[1][j]-sum[1][i-1];
if(sum[i][j]>maxsum[j-i+1])
maxsum[j-i+1]=sum[i][j];//记录长度为j-i+1的数串和最大为多少
}
while(m--)
{
bj=0;k=getint();
for(long long i=N;i>=1;--i)//从长到短找
if(k*i<=maxsum[i])//满足总和>=k*长度,就找到答案了
{
cout<<i<<" ";
bj=1;
break;
}
if(!bj)
cout<<0<<" ";//没找到,出0
}
return 0;
}
100%
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<iomanip>
#include<queue>
#include<set>
using namespace std;
int getint()
{
int sum=0,f=1;
char ch;
for(ch=getchar();!isdigit(ch)&&ch!='-';ch=getchar());
if(ch=='-')
{
f=-1;
ch=getchar();
}
for(;isdigit(ch);ch=getchar())
sum=(sum<<3)+(sum<<1)+ch-48;
return sum*f;
}
const int maxn=1000010;
struct node{
long long c;
int pos;
}zhan[maxn];
int n,m,k,a,zhizhen,cnt,ans;
long long sum[maxn],num[maxn];
int main()
{
freopen("problem.in","r",stdin);
freopen("problem.out","w",stdout);
n=getint(),m=getint();
for(int i=1;i<=n;++i)
{
a=getint();
sum[i]+=sum[i-1]+a;//算前缀和
}
while(m--)
{
ans=0;cnt=1;//栈的第一个位置是sum[0]也就是没有数的时候
k=getint();
for(int i=1;i<=n;++i)
{
num[i]=sum[i]-(long long)k*i;//把前缀和改成减去k后的
if(num[i]<zhan[cnt].c)//建立单调递减栈
{
zhan[++cnt].c=num[i];
zhan[cnt].pos=i;//记一下这个值的位置
}
}
zhizhen=1;//指针从栈中第一个开始
for(int i=1;i<=n;++i)
{
if(zhan[zhizhen].c>num[i]&&zhizhen<cnt)//如果不满足
zhizhen++;//只向右移一次
while(zhizhen&&num[i]>=zhan[zhizhen-1].c)//如果满足
zhizhen--;//移到最左边的满足条件的栈中位置
if(zhan[zhizhen].c<=num[i])
ans=max(ans,i-zhan[zhizhen].pos);//如果满足条件,看一下更不更新ans
}
cout<<ans<<" ";
}
return 0;
}
本题结。