HDU,杭电ACM1003题

MaxSumTimeLimit:2000/1000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others)TotalSub... Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16881 Accepted Submission(s): 3498

Problem Description
Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.

Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).

Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.

Sample Input
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5

Sample Output
Case 1:
14 1 4

Case 2:
7 1 6

我第一次在HDU上做题,不知道注意什么
我用DP写的,但是RUNTIME了
#include<iostream>
using namespace std;
int main()
{
int dp[500][500];
int n,i,j,a[500],max,s,e,ncase,t;
cin>>ncase;
for(t=1;t<=ncase;t++)
{
cin>>n;
max=-99999999;
if(n==0)
return 0;
memset(dp,0,sizeof(dp));
for(i=0;i<n;i++)
{
cin>>a[i];
dp[i][i]=a[i];
}
if(dp[0][0]>max)
max=dp[0][0];
for(j=1;j<n;j++)
for(i=0;i<=j;i++)
{
dp[i][j]=dp[i][j-1]+a[j];
//cout<<dp[i][j]<<" ";
if(dp[i][j]>max)
{
max=dp[i][j];
s=i;
e=j;
}

}
//cout<<endl;
cout<<"Case "<<t<<":"<<endl;
cout<<max<<" "<<s+1<<" "<<e+1<<endl;
cout<<endl;
}
return 0;
}
不知道该怎么修改,我数组开的大的话,又空间不够,小了又RUNTIME了,指点下,我一直在学校自己的OJ上做,没那么多注意!
展开
 我来答
chenliewangyan
2009-07-07
知道答主
回答量:6
采纳率:0%
帮助的人:0
展开全部
我用贪心做的 没有必要用数组

#include<stdio.h>
void main()
{
int c=1,b,a,s,n,i,j,k,m,l;
scanf("%d",&k);
while(k--){
m=-9999;s=0;j=0;
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%d",&a);
s=s+a;
j++;
if(s>m){
b=j;
m=s;
l=i;
}
if(s<0){
s=0;
j=0;
}
}
printf("Case %d:\n%d %d %d\n",c++,m,l-b+1,l);
if(k)printf("\n");
}
}
华芯测试
2024-09-01 广告
电学测试台是深圳市华芯测试科技有限公司的核心设备之一,它集成了高精度测量仪器与自动化控制系统,专为半导体芯片、电子元件及模块的电性能检测而设计。该测试台能够迅速、准确地完成电压、电流、电阻、电容及频率等关键参数的测试,确保产品质量符合行业标... 点击进入详情页
本回答由华芯测试提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式