洛谷 1569 [USACO11FEB]属牛的抗议
【题解】
非常显然的DP,f[i]表示到第i个位置最多分成几组,f[i]=Max(f[i],f[j]+1) (j<i,sum[j]<=sum[i])
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define LL long long 5 #define N 200010 6 #define rg register 7 using namespace std; 8 int n,m,f[N]; 9 LL sum[N]; 10 inline int read(){ 11 int k=0,f=1; char c=getchar(); 12 while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar(); 13 while('0'<=c&&c<='9')k=k*10+c-'0',c=getchar(); 14 return k*f; 15 } 16 int main(){ 17 n=read(); 18 for(rg int i=1;i<=n;i++) sum[i]=sum[i-1]+read(); 19 for(rg int i=1;i<=n;i++) 20 for(rg int j=0;j<=i-1;j++)if(sum[i]-sum[j]>=0&&(f[j]>0||j==0)) 21 f[i]=max(f[i],f[j]+1); 22 if(f[n]) printf("%d\n",f[n]); 23 else puts("Impossible"); 24 // for(rg int i=1;i<=n;i++) printf("%d ",f[i]); puts(""); 25 return 0; 26 }