HDU-3280 Equal Sum Partitions
生活随笔
收集整理的這篇文章主要介紹了
HDU-3280 Equal Sum Partitions
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://acm.hdu.edu.cn/showproblem.php?pid=3280
用了簡單的枚舉。
Equal Sum Partitions
Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 453????Accepted Submission(s): 337
Problem Description An equal sum partition of a sequence of numbers is a grouping of the numbers (in the same order as the original sequence) in such a way that each group has the same sum. For example, the sequence: 2 5 1 3 3 7 may be grouped as: (2 5) (1 3 3) (7) to yield an equal sum of 7.Note: The partition that puts all the numbers in a single group is an equal sum partition with the sum equal to the sum of all the numbers in the sequence.
For this problem, you will write a program that takes as input a sequence of positive integers and returns the smallest sum for an equal sum partition of the sequence. Input The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. The first line of each data set contains the data set number, followed by a space, followed by a decimal integer M, (1 ≤ M ≤ 10000), giving the total number of integers in the sequence. The remaining line(s) in the dataset consist of the values, 10 per line, separated by a single space. The last line in the dataset may contain less than 10 values. Output For each data set, generate one line of output with the following values: The data set number as a decimal integer, a space, and the smallest sum for an equal sum partition of the sequence. Sample Input 3 1 6 2 5 1 3 3 7 2 6 1 2 3 4 5 6 3 20 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 Sample Output 1 7 2 21 3 2 #include<iostream> #include<cstring> #include<cstdio> using namespace std; int a[10005]; int main() {int i,j,t,n,m,sum,cursum,flag ,ans;scanf("%d",&t);while(t--){flag=0;memset(a,0,sizeof(a));scanf("%d%d",&n,&m);for(i=0;i<m;i++)scanf("%d",&a[i]);for(i=0;i<m;i++){sum=0;for(j=0;j<=i;j++)sum+=a[j];cursum=0;while(j<m){cursum+=a[j];if(cursum>sum)break;else if(cursum==sum){j++;if(j==m){printf("%d %d\n",n,sum);flag=1;}cursum=0;}elsej++;if(flag)break;}if(flag)break;}if(i==m)printf("%d %d\n",n,sum);}return 0; } /* 3 1 6 2 5 1 3 3 7 2 6 1 2 3 4 5 6 3 20 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 */
?區間dp
#include<iostream> #include<cstdio> using namespace std; int dp[10005][10005],ans[10005]; int main() {int t,n,m,i,j,k,g,a[10005];cin>>t;while(t--){cin>>n>>m;ans[0]=0;for(i=1;i<=m;i++){cin>>a[i];ans[i]=ans[i-1]+a[i];}for(k=0;k<m;k++)//k不能從1-m,雖然同樣個數相同,但是j=2開始,就會使區間減少了一層,{ //比如i=1,j=2就沒有這個區間。for(i=1;i<=m-k;i++){j=i+k;dp[i][j]=ans[j]-ans[i-1];//初始化dp,求出每個區間的和。for(g=i;g<j;g++){//三者的順序可以隨便調換。if((ans[g]-ans[i-1])==dp[g+1][j])dp[i][j]=min(dp[i][j],dp[g+1][j]);if(dp[i][g]==ans[j]-ans[g])dp[i][j]=min(dp[i][j],dp[i][g]); if(dp[i][g]==dp[g+1][j])dp[i][j]=min(dp[i][j],dp[i][g]);}}}printf("%d %d\n",n,dp[1][m]);}} /* 3 1 6 2 5 1 3 3 7 2 6 1 2 3 4 5 6 3 20 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 */?
轉載于:https://www.cnblogs.com/cancangood/p/3859488.html
總結
以上是生活随笔為你收集整理的HDU-3280 Equal Sum Partitions的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ganglia+nagiosrhel6.
- 下一篇: CSDN总结的面试中的十大算法