读书人

请各位大神帮小弟我看一上为什么杭电A

发布时间: 2012-09-24 13:49:42 作者: rapoo

请各位大神帮我看一下为什么杭电ACM第三题提交后出现Wrong Answer
子序列最大和(杭电acm1003)
分类: ACM 2012-03-25 21:50 34人阅读 评论(1) 收藏 举报
Max Sum
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


C/C++ code
#include<iostream>using namespace std;void maxsubsum(int * num,int length){    int max,sum=0,i,j,start1=0,start2=0,end;    max=num[0];    for(i=0;i<length;i++)    {        sum+=num[i];        if(max<sum)        {            max=sum;            start1=start2;            end=i;        }        if(sum<0)        {            sum=0;            start2=i+1;        }        }    cout<<max<<" "<<start1+1<<" "<<end+1<<endl;}int main(){    int casenumber;    int number;    int integer;    cin>>casenumber;    if(casenumber<1||casenumber>20)    {        cout<<"There is something wrong for your input!!";        exit(0);    }    int ** num = new int *[casenumber];    int * length = new int[casenumber];    for(int i=0;i<casenumber;i++)    {        cin>>number;        if(number>=1&&number<=100000)        {            num[i]=new int[number];            length[i]=number;        }        for(int j=0;j<number;j++)        {            cin>>integer;            if(integer<-1000||integer>1000)            {                cout<<"the integer you input is wrong!!";                continue;            }            num[i][j]=integer;                }        cout<<endl;    }    for(int j=0;j<casenumber;j++)    {        cout<<"case"<<j+1<<":"<<endl;        maxsubsum(num[j],length[j]);        if(j!=casenumber-1)            cout<<endl;    }}

我不知道

[解决办法]
仅仅用一个循环就求出最大子列是不可能的.
例如数列: 6, -1, -7, 4.
你在算出6, -1, -7之后发现sum<0, 马上丢弃另辟新径, 结果得出4为最大子列.
殊不知在丢弃的那一串列中已然包含最大子列, 即6.
可以说, 你的算法是极度贪婪的, 缺不符合贪婪属性.
[解决办法]
这代码风格?。。。。。在oj上做题得悠着点
C/C++ code
#include<iostream>using namespace std;const int inf=0x3fffffff;int MaxSubSeq(const int*arr,int len,int& s,int& e){    int sum=0,max=-inf,b=0,f=0;    s=b;    e=f;    for(int i=0;i<len;i++){         sum+=arr[i];         f=i;         if(sum>max){             max=sum;             s=b;             e=f;         }         if(sum<0){             sum=0;             b=i+1;         }    }    return max;}int main(){    int t,len,arr[100005],s,e,max;    scanf("%d",&t);    for(int i=1;i<=t;i++){        scanf("%d",&len);        for(int j=0;j<len;j++)             scanf("%d",&arr[j]);        max=MaxSubSeq(arr,len,s,e);        if(i>1)             puts("");        printf("Case %d:\n",i);         printf("%d %d %d\n",max,s+1,e+1);    }    return 0;} 

读书人网 >软件架构设计

热点推荐