2 条题解
-
0AndyMo LV 5 @ 2018-09-15 13:55:13
#include <cstdio> #include <cstring> #include <algorithm> #define ll long long #define inf 1<<30 #define il inline #define in1(a) read(a) #define in2(a,b) in1(a),in1(b) #define in3(a,b,c) in2(a,b),in1(c) #define in4(a,b,c,d) in2(a,b),in2(c,d) il void readl(ll &x){ x=0;ll f=1;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-f;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} x*=f; } il void read(int &x){ x=0;int f=1;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-f;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} x*=f; } using namespace std; /*===================Header Template=====================*/ #define N 3005 int n,m; int a[N]; int main(){ int i,j,ans=0; in2(n,m); for(i=1;i<=n;i++) read(a[i]),a[i]+=a[i-1]; for(i=1;i<=n;i++){ for(j=max(1,i-m+1);j<=i;j++){ ans=max(ans,a[i]-a[j-1]); } } printf("%d",ans); return 0; }
-
02017-02-23 21:11:50@
#include<cstring> #include<algorithm> using namespace std; int data[300003],f[300003],w[300003],x[300003]; int main(){ // freopen("in.txt","r",stdin); int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%d",&data[i]); data[i]=data[i-1]+data[i]; } x[1]=0;w[1]=0; int head=1,tail=1; for(int i=1;i<=n;i++){ if(x[head]<i-m) head++; f[i]=data[i]-data[x[head]]; while(tail>=head&&data[i]<w[tail]) tail--; w[++tail]=data[i];x[tail]=i; } int res=-999999999; for(int i=1;i<=n;i++) res=max(f[i],res); printf("%d",res); return 0; }```
- 1