HDU ACM 3280 Equal Sum Partitions

分析:从前往后递推第一个和sum,然后往后依次划分,当第一次能够划分完,就得到最小的结果了。

#include<iostream>
using namespace std;

int a[10005];
int P,M,T,K;

void solve()
{
	int sum,cursum,i,j,j2;
	bool flag;

	flag=false;
	for(i=0,j2=0,sum=0;i<M;i++)
	{
		for(;j2<=i;j2++)
			sum+=a[j2];

		j=j2;
		cursum=0;
		while(j<M)
		{
			cursum+=a[j];
			if(cursum>sum)
				break;
			else if(cursum==sum)
			{
				j++;
				if(j==M)
				{
					cout<<K<<" "<<cursum<<endl;
					flag=true;
				}
				cursum=0;
			}
			else
				j++;
			if(flag) break;
		}
		if(flag) break;
	}
	if(i==M)
		cout<<K<<" "<<sum<<endl;
}

int main()
{
	int i;

	ios::sync_with_stdio(false);
	cin>>T;
	while(T--)
	{
		cin>>K>>M;
		for(i=0;i<M;i++)
			cin>>a[i];
		solve();
	}
    return 0;
}
时间: 2024-10-10 01:01:16

HDU ACM 3280 Equal Sum Partitions的相关文章

HDU 3280 Equal Sum Partitions(二分查找)

Equal Sum Partitions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 551    Accepted Submission(s): 409 Problem Description An equal sum partition of a sequence of numbers is a grouping of the

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

HDU acm 1003 Max Sum || 动态规划求最大子序列和详解

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 164592    Accepted Submission(s): 38540 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max su

杭电 HDU ACM 1003 Max Sum

 Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 164592    Accepted Submission(s): 38540 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the ma

HDU ACM 1003 Max Sum

注意只有一个负数时,负数也算最大值. #include<iostream> using namespace std; int main() { int a[100001]; int T,T2=0,n,i; int sum,maxsum,start,maxstart,maxend; cin>>T; while(T--) { cin>>n; for(i=0;i<n;i++) cin>>a[i]; maxstart=maxend=sum=0; maxsum=

HDU ACM 1024 Max Sum Plus Plus -&gt;最大连续子序列和的增强版

#include<iostream> #include<limits.h> using namespace std; #define N 1000005 int a[N],dp[N],maxpre[N]; int max(int a,int b) { return a>b?a:b; } int main() { int i,j,n,m,tmp; while(scanf("%d%d",&m,&n)==2) { for(i=1;i<=n;

hdu acm 1166 敌兵布阵 (线段树)

敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 37903    Accepted Submission(s): 15985 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务

HDU ACM 1009 FatMouse&#39; Trade

分析:贪心,每次优先取需要食物的重量和猫食的重量相比最大的,所以先要按比值进行排序,注意J[i]和B[i]要声明为实型,声明为整形就挂了. #include<iostream> #include<algorithm> using namespace std; struct FAT { double f,j; double avg; } fat[1005]; bool cmp(FAT a,FAT b) { if(a.avg<b.avg) return true; else re

杭电 HDU ACM 1397 Goldbach&#39;s Conjecture

Goldbach's Conjecture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4976    Accepted Submission(s): 1901 Problem Description Goldbach's Conjecture: For any even number n greater than or equal