Help!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

P2627 [USACO11OPEN] Mowing the Lawn G

与世无争 @ 2019-02-01 22:14:31

以下是本蒟蒻代码

#include<cstdio>
#include<iostream>
#define ll long long
using namespace std;
inline int read() {
    int n=0,w=1;
    register char c=getchar();
    while(c<'0'||c>'9') {
        if(c=='-') w=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9') n=n*10+c-'0',c=getchar();
    return n*w;
}
int n,m,a[100005],sum[100005],flag;//sum是已经连续取的个数 
ll f[100005],maxn;
int main() {
    n=read();
    m=read();
    for (int i=1; i<=n; i++) a[i]=read();//读入 
    f[n]=a[n];
    sum[n]=1;
    for (int i=n-1; i; i--) {
        maxn=0;
        for (int j=i+1; j<=n; j++)//枚举 i+1~n 的可行性最大解 
            if (((sum[j]<m&&j==i+1)||j!=i+1)&&f[j]>maxn) {
                maxn=f[j];
                flag=j;
            }
        f[i]=maxn+a[i];
        if (flag==i+1) sum[i]=sum[flag]+1;
        else sum[i]=1;
    }
    maxn=0;
    for (int i=1; i<n; i++) maxn=max(maxn,f[i]);//找所有的最大值 
    printf("%lld\n",maxn);
    return 0;
}

by 与世无争 @ 2019-02-01 22:15:00

求帮助


|