日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

洛谷P1569 [USACO11FEB]属牛的抗议Generic Cow Prote…

發布時間:2023/12/20 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 洛谷P1569 [USACO11FEB]属牛的抗议Generic Cow Prote… 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目描述

Farmer John's N (1 <= N <= 100,000) cows are lined up in a row and numbered 1..N. The cows are conducting another one of their strange protests, so each cow i is holding up a sign with an integer A_i (-10,000 <= A_i <= 10,000).

FJ knows the mob of cows will behave if they are properly grouped and thus would like to arrange the cows into one or more contiguous groups so that every cow is in exactly one group and that every group has a nonnegative sum.

Help him count the number of ways he can do this, modulo 1,000,000,009.

By way of example, if N = 4 and the cows' signs are 2, 3, -3, and 1, then the following are the only four valid ways of arranging the cows:

(2 3 -3 1) (2 3 -3) (1) (2) (3 -3 1) (2) (3 -3) (1) Note that this example demonstrates the rule for counting different orders of the arrangements.

約翰家的N頭奶牛聚集在一起,排成一列,正在進行一項抗議活動。第i頭奶牛的理智度 為Ai,Ai可能是負數。約翰希望奶牛在抗議時保持理性,為此,他打算將所有的奶牛隔離成 若干個小組,每個小組內的奶牛的理智度總和都要大于零。由于奶牛是按直線排列的,所以 一個小組內的奶牛位置必須是連續的。 請幫助約翰計算一下,最多分成幾組。

輸入輸出格式

輸入格式:

第1行包含1個數N,代表奶牛的數目。

第2至N+1行每行1個整數Ai。

輸出格式:

輸出文件有且僅有一行,包含1個整數即為最多組數。

若無法滿足分組條件,則輸出Impossible。

輸入輸出樣例

輸入樣例#1:?4 2 3 -3 1 輸出樣例#1:?3

說明

【數據規模和約定】

30%的數據滿足N≤20。

100%的數據滿足N≤1000,|Ai|≤100000。


區間DP入門題。。。

用 sum 記錄 前綴和 。

設 dp[i] 表示到第i個數時,能分成的最大組數。

則:枚舉j:1~i,dp[i]=max{ dp[j]+1,dp[i] }

注意:sum[n]<0時可以直接輸出Impossible

附代碼:

#include<iostream> #include<algorithm> #include<cstdio> #define MAXN 1010 using namespace std; int n,a[MAXN],sum[MAXN],dp[MAXN]; inline int read(){int date=0,w=1;char c=0;while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();}return date*w; } int main(){n=read();sum[0]=0;for(int i=1;i<=n;i++){a[i]=read();sum[i]=sum[i-1]+a[i];if(sum[i]>=0)dp[i]=1;}if(sum[n]<0){printf("Impossible\n");return 0;}for(int i=1;i<=n;i++)for(int j=1;j<i;j++)if(dp[j]>0&&sum[i]-sum[j]>=0)dp[i]=max(dp[i],dp[j]+1);if(!dp[n])printf("Impossible\n");else printf("%d\n",dp[n]);return 0; }

總結

以上是生活随笔為你收集整理的洛谷P1569 [USACO11FEB]属牛的抗议Generic Cow Prote…的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。