[NOIP2017模拟]Problem
来源:互联网 发布:sql server access 编辑:程序博客网 时间:2024/05/22 01:42
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时(
因为每次最多向右移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就GGint 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;}
本题结。
- [NOIP2017模拟]Problem
- 【NOIP2017模拟赛】构造 A+B Problem(好题)
- NOIP2017模拟赛1
- NOIP2017模拟赛8
- NOIP2017模拟赛9
- [NOIP2017模拟]切蛋糕
- [NOIP2017模拟]随机图
- [NOIP2017模拟]能源
- [NOIP2017模拟]电影
- [NOIP2017模拟]鸭舌
- [NOIP2017模拟]permut
- [NOIP2017模拟]beautiful
- [NOIP2017模拟]路径
- [NOIP2017模拟]流
- [NOIP2017模拟]subset
- [NOIP2017模拟]hello
- [NOIP2017模拟]table
- [NOIP2017模拟]board
- leetcode
- 嵌入式socketiocpp lib交叉编译
- EL表达式和JSTL标签
- 文章标题
- 道路依旧
- [NOIP2017模拟]Problem
- 手机新软件指引实现步骤
- hdu2255 奔小康赚大钱(KM)
- gulp之向项目中添加 icon 图标
- Java——jdk1.5多线程 Lock接口及Condition接口
- jsonp的解决ajax跨域问题
- 操作系统实验一(进程管理)
- C++ 双向链表的实现
- 学习move _base包