[技术| 编程·课件·Linux] ACM题天天练 4 Max Sum

sunhongbo · 发布于 2012-08-23 12:05 · 6121 次阅读
41
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

源自http://acm.hdu.edu.cn/   1003题
欢迎大家踊跃参与。。。


共收到 29 条回复
hslx111 · #2 · 2012-8-23 13:34:51  回复 支持 反对
本帖最后由 hslx111 于 2012-8-23 13:36 编辑


[C++] 纯文本查看 复制代码
//hud 1003
#include <iostream>
using namespace std;

int main()
{
    int i,j,T,N;
    int num=1;
    cin>>T;
    while(T--)
    { 
        int start,end,pos,sum,now,temp;
        cin>>N>>temp;
        now=sum=temp;
        start=end=pos=1;       
        for(i=2;i<=N;i++)
        {
            cin>>temp;
            if(now+temp<temp)
                now=temp,pos=i;
            else 
                now+=temp;
            if(now>sum)
            sum=now,start=pos,end=i;
        }      
        cout<<"Case "<<num<<":"<<endl;
        cout<<sum<<" "<<start<<" "<<end<<endl;
        if(T!=0) cout<<endl;
        num++;
    }
    return 0;
}           


好长时间不写,手都生了啊....

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

点评

不用检测T的值和N的值吗,我就纳闷了,每次写完结果正确就是提交不了,求改,谢谢[mw_shl_code=c,true]#include int main() { int T; scanf("%d",&T); if(T20) { return 0; } int  详情 回复 发表于 2012-8-23 17:52
谢谢支持谢谢参与,真棒。。。  详情 回复 发表于 2012-8-23 14:55

评分

参与人数 1学分 +9 收起 理由
sunhongbo + 9 加分,虽然你的内么多了。。。ww

查看全部评分

yel_hb · #3 · 2012-8-23 13:55:00  回复 支持 反对
本帖最后由 yel_hb 于 2012-8-23 13:58 编辑

...看了楼上的贴自己代码有点心虚的说...
[C++] 纯文本查看 复制代码
#include <iostream>
using namespace std;

int main()
{
	int t,n,i,k,max,sum,x1,x2,y1;
	int *list;

	cin >> t;
	k = 1;

	while(t--)
	{
		cin >> n;

		list = new int[n];

		for(i = 0; i < n; ++i)
		{
			cin >> list[i];
		}

		x1 = 0;
		y1 = 0;
		x2 = 0;
		max = list[x1];
		sum = list[x1];

		for(i = x1 + 1; i < n; ++i)
		{
			if(sum >= 0)
			{
				sum += list[i];
			}
			else
			{
                                 x2 = i;
                                 sum = list[i];
			}
			if(max < sum)
			{
				max = sum;
				x1 = x2;
				y1 = i;
			}
		}
		cout << "Case " << k++ << ":" << endl;
		cout << max << " " << (x1 + 1) << " " << (y1 + 1) << endl;
                if(t != 0)
                {
                         cout << endl;
                }
	}

	return 0;
}

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

点评

。。。  详情 回复 发表于 2012-8-23 14:59

评分

参与人数 1学分 +9 收起 理由
sunhongbo + 9 条条大路通罗马,简单最好。。加分加分

查看全部评分

justcx · #4 · 2012-8-23 14:27:47  回复 支持 反对
本帖最后由 justcx 于 2012-8-23 14:41 编辑

[C++] 纯文本查看 复制代码
#include<iostream>
#include<iostream>
#define N 100000
static st,ed;
int b[N];
int MaxSumDP(int *a, int n);
using namespace std;
int main()
{
int n1,n2;
int a[N];
cin>>n1;
for(int j=0;j<n1;j++)
{
cin>>n2;
for(int i=0;i<n2;i++)
{
cin>>a[i];
}
int M=MaxSumDP(a,n2);
cout<<"Case :"<<j+1<<endl;
cout<<M<<' '<<st<<' '<<ed<<endl;
cout<<endl;
} 
return 0;
}
int MaxSumDP(int *a, int n) 
{
int i, maxSum = 0;
int count=0;
if(a[0] >=0)
{
maxSum=b[0]=a[0];
st=1;
ed=1;
count++;
}
for(i=1; i<n; ++i)
{
b = (b[i-1]+a > 0) ? (b[i-1]+a) : 0;
if(b[i-1]==0)
count++;
while(count==0)
{
if(b[i-1]==0&&b>0)
{
st=i+1;
}
}
if(maxSum < b[i])
{
maxSum = b[i];
ed=i+1;
}
} 
return maxSum;
}

评分

参与人数 1学分 +9 收起 理由
sunhongbo + 9 加分。。

查看全部评分

justcx · #5 · 2012-8-23 14:42:43  回复 支持 反对
代码复制上去咋格式老是变化掉了~就连部分字母和字符都有变化~

点评

是用“引用代码”添加上去的吗  详情 回复 发表于 2012-8-23 15:02
sunhongbo · #6 · 2012-8-23 14:55:23  回复 支持 反对
hslx111 发表于 2012-8-23 13:34
[mw_shl_code=cpp,true]
//hud 1003
#include

谢谢支持谢谢参与,真棒。。。
sunhongbo · #7 · 2012-8-23 14:59:09  回复 支持 反对
yel_hb 发表于 2012-8-23 13:55
...看了楼上的贴自己代码有点心虚的说...
[mw_shl_code=cpp,true]#include
using namespace st ...

。。。
sunhongbo · #8 · 2012-8-23 15:02:36  回复 支持 反对
justcx 发表于 2012-8-23 14:42
代码复制上去咋格式老是变化掉了~就连部分字母和字符都有变化~

是用“引用代码<>”添加上去的吗

点评

是的呢  详情 回复 发表于 2012-8-23 17:18
科尔沁猎人 · #9 · 2012-8-23 15:14:31  回复 支持 反对
Sample Output 的结果怎么得来的?

点评

亲,是根据Sample Input得出来的。。  详情 回复 发表于 2012-8-23 15:35
sunhongbo · #10 · 2012-8-23 15:35:05  回复 支持 反对
科尔沁猎人 发表于 2012-8-23 15:14
Sample Output 的结果怎么得来的?

亲,是根据Sample Input得出来的。。
fghhslk · #11 · 2012-8-23 15:52:06  回复 支持 反对
无语,调试没问题,谁能告诉我为何一直wa?
[Java] 纯文本查看 复制代码
package com.slk.MaxSumSubsequence;

import java.util.Scanner;

public class Main {
	public static void main(String[] args){ 
		Scanner sc = new Scanner(System.in);
		int t, n;
		int pos = 1;
		int spos = 1;
		int epos = 1;
		int caseNum = 1;
		t = Integer.parseInt(sc.nextLine());
		while (t-- > 0) {
			n = Integer.parseInt(sc.nextLine());
			int currentInput = Integer.parseInt(sc.nextLine());
			int tempSum = currentInput;
			int maxSum = tempSum;
			for (int i = 1; i < n; i++) {
				currentInput = Integer.parseInt(sc.nextLine());
				if (currentInput + tempSum < currentInput) {
					tempSum = currentInput;
					pos = i + 1;
				} else {
					tempSum += currentInput;
				}
				if (tempSum > maxSum) {
					maxSum = tempSum;
					spos = pos;
					epos = i + 1;
				}
			}
			System.out.println("Case " + caseNum++ + ":");
			System.out.println(maxSum + " " + spos + " " + epos + "\n");
		}
		sc.close();
	}
}

点评

最后一个case后面是没有空行的  详情 回复 发表于 2012-8-23 16:43
两个case之间有个空行...  详情 回复 发表于 2012-8-23 16:25
yel_hb · #12 · 2012-8-23 16:25:09  回复 支持 反对
fghhslk 发表于 2012-8-23 15:52
无语,调试没问题,谁能告诉我为何一直wa?
[mw_shl_code=java,true]package com.slk.MaxSumSubsequence;
...

两个case之间有个空行...

点评

加了"\n"了。。。  详情 回复 发表于 2012-8-23 16:29
fghhslk · #13 · 2012-8-23 16:29:48  回复 支持 反对
yel_hb 发表于 2012-8-23 16:25
两个case之间有个空行...

加了"\n"了。。。用println同样wa

点评

最后一个case不能有吧...  详情 回复 发表于 2012-8-23 16:37
yel_hb · #14 · 2012-8-23 16:37:38  回复 支持 反对
fghhslk 发表于 2012-8-23 16:29
加了"\n"了。。。用println同样wa

最后一个case不能有吧...

点评

System.out.println("Case " + caseNum++ + ":"); System.out.println(maxSum + " " + spos + " " + epos); if (t != 0) System.out.println(); 依旧wa  详情 回复 发表于 2012-8-23 16:44
hslx111 · #15 · 2012-8-23 16:43:51  回复 支持 反对
fghhslk 发表于 2012-8-23 15:52
无语,调试没问题,谁能告诉我为何一直wa?
[mw_shl_code=java,true]package com.slk.MaxSumSubsequence;
...

最后一个case后面是没有空行的

点评

问题依旧。。。真是无语  详情 回复 发表于 2012-8-23 16:47
fghhslk · #16 · 2012-8-23 16:44:49  回复 支持 反对
yel_hb 发表于 2012-8-23 16:37
最后一个case不能有吧...

System.out.println("Case " + caseNum++ + ":");
                        System.out.println(maxSum + " " + spos + " " + epos);
                        if (t != 0)
                                System.out.println();
依旧wa
fghhslk · #17 · 2012-8-23 16:47:15  回复 支持 反对
hslx111 发表于 2012-8-23 16:43
最后一个case后面是没有空行的

问题依旧。。。真是无语

点评

好不容易下了个Eclipse...发现不会用了 试试这组case: 1 5 -1 -1 -1 -1 -1  详情 回复 发表于 2012-8-23 17:26
justcx · #18 · 2012-8-23 17:18:14  回复 支持 反对
sunhongbo 发表于 2012-8-23 15:02
是用“引用代码”添加上去的吗

是的呢

点评

语言什么的都正确么,AC了没?  详情 回复 发表于 2012-8-23 22:23
yel_hb · #19 · 2012-8-23 17:26:00  回复 支持 反对
fghhslk 发表于 2012-8-23 16:47
问题依旧。。。真是无语

好不容易下了个Eclipse...发现不会用了
试试这组case:
1
5 -1 -1 -1 -1 -1

点评

Case 1: -1 1 1  详情 回复 发表于 2012-8-23 17:36
fghhslk · #20 · 2012-8-23 17:36:41  回复 支持 反对
yel_hb 发表于 2012-8-23 17:26
好不容易下了个Eclipse...发现不会用了
试试这组case:
1

Case 1:
-1 1 1

点评

额...发现结果不确定。不过 这个输入是读取一行的吧...应用nextInt()试试...  详情 回复 发表于 2012-8-23 18:57
maxOrder石 · #21 · 2012-8-23 17:52:08  回复 支持 反对
本帖最后由 maxOrder石 于 2012-8-23 17:55 编辑
hslx111 发表于 2012-8-23 13:34
[mw_shl_code=cpp,true]
//hud 1003
#include

不用检测T的值和N的值吗,我就纳闷了,每次写完结果正确就是提交不了,求改,谢谢
#include<stdio.h>
int main()
{
   int T;
   scanf("%d",&T);
   if(T<1||T>20)
   {
      return 0;
   }
   int N;
   int i;
   int j;
   int s,MaxSum[20]={0};
   int starP[20],endP[20]={0};
   for(j=0;j<T;j++)
   {
   scanf("%d",&N);
   if(N<1||N>100000)
    return 0;
     starP[j]=1;
     int *number=(int *)malloc(sizeof(int)*N);
     for(i=0;i<N;i++)
     {
        scanf("%d",number+i);
     }
     s=0;
     for(i=0;i<N;i++)
     {
       s=s+*(number+i);
       if(MaxSum[j]<s)
       {
          MaxSum[j]=s;
          endP[j]=i+1;
       }

     }

   }
for(i=0;i<T;i++)
{
    printf("Case %d:\n",i+1);
    printf("%d %d %d\n\n",MaxSum,starP,endP);
}
return 0;
}
字体变了,最后的printf(是MaxSum中括号i,后面都是一样的额)

点评

这要看出题风格了,hdu的题一般对T和N这样的已知值没有严格的限制,不过写上是对的,我是懒= = 请问你的结果是WA了吗?  详情 回复 发表于 2012-8-23 21:01
yel_hb · #22 · 2012-8-23 18:57:31  回复 支持 反对
fghhslk 发表于 2012-8-23 17:36
Case 1:
-1 1 1

额...发现结果不确定。不过
[Java] 纯文本查看 复制代码
currentInput = Integer.parseInt(sc.nextLine());

这个输入是读取一行的吧...应用nextInt()试试...

点评

nextInt已经试过,妥妥的不行,放弃了。。。多谢帮忙debug  详情 回复 发表于 2012-8-23 19:34
fghhslk · #23 · 2012-8-23 19:34:47  回复 支持 反对
yel_hb 发表于 2012-8-23 18:57
额...发现结果不确定。不过[mw_shl_code=java,true]currentInput = Integer.parseInt(sc.nextLine());[/m ...

nextInt已经试过,妥妥的不行,放弃了。。。多谢帮忙debug
hslx111 · #24 · 2012-8-23 21:01:32  回复 支持 反对
maxOrder石 发表于 2012-8-23 17:52
不用检测T的值和N的值吗,我就纳闷了,每次写完结果正确就是提交不了,求改,谢谢
#include
int main() ...

这要看出题风格了,hdu的题一般对T和N这样的已知值没有严格的限制,不过写上是对的,我是懒= =
请问你的结果是WA了吗?

点评

恩,是的,你怎么知道,哈  详情 回复 发表于 2012-8-23 23:20
sunhongbo · #25 · 2012-8-23 22:23:05  回复 支持 反对
justcx 发表于 2012-8-23 17:18
是的呢

语言什么的都正确么,AC了没?

点评

都正确的,运行过了~前几次复制还没问题的,今天就出了这个问题~把符号[]变成或者就没有~  详情 回复 发表于 2012-8-23 23:20
maxOrder石 · #26 · 2012-8-23 23:20:40  回复 支持 反对
hslx111 发表于 2012-8-23 21:01
这要看出题风格了,hdu的题一般对T和N这样的已知值没有严格的限制,不过写上是对的,我是懒= =
请问你的结果 ...

恩,是的,你怎么知道,哈

点评

首先starP是会变的,你这里默认都是1了,还有最后一个case后面只有一个\n的. 而且那个判断最大子序列的过程好像有点问题哦,你可以看看前面别人写的代码或者自己再好好想想  详情 回复 发表于 2012-8-24 08:38
justcx · #27 · 2012-8-23 23:20:55  回复 支持 反对
sunhongbo 发表于 2012-8-23 22:23
语言什么的都正确么,AC了没?

都正确的,运行过了~前几次复制还没问题的,今天就出了这个问题~把符号[]变成<>或者就没有~

点评

AC了就好呀 亲。  详情 回复 发表于 2012-8-24 13:02
hslx111 · #28 · 2012-8-24 08:38:47  回复 支持 反对
maxOrder石 发表于 2012-8-23 23:20
恩,是的,你怎么知道,哈

首先starP是会变的,你这里默认都是1了,还有最后一个case后面只有一个\n的.
而且那个判断最大子序列的过程好像有点问题哦,你可以看看前面别人写的代码或者自己再好好想想
sunhongbo · #29 · 2012-8-24 13:02:53  回复 支持 反对
justcx 发表于 2012-8-23 23:20
都正确的,运行过了~前几次复制还没问题的,今天就出了这个问题~把符号[]变成或者就没有~

AC了就好呀 亲。
justcx · #30 · 2012-8-27 23:59:51  回复 支持 反对
怎么天天练不更新了?
回帖
B Color Image Link Quote Code Smilies
Command + Enter
快速回复 返回顶部 返回列表