[HDU] [1003] Max Sum

Problem Description

Loading...

Problem Analysis

As 1003
At first glance it seemed very simple
And then…
I had no idea~

A Dynamic Programming problem

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<stdio.h>
int main() {
	int T,kase=0;
	scanf("%d",&T);
	while(T--) {
		printf("Case %d:\n",++kase);
		int n;
		scanf("%d",&n);
		int t,sum,tsum,s,ts,e;
		for(int i=1; i<=n; i++) {
			scanf("%d",&t);
			if(i==1) {
				sum=tsum=t;
				s=ts=e=1;
			} else {
				if(t>tsum+t) {
					tsum=t;
					ts=i;
				} else
					tsum+=t;
			}
			if(tsum>sum) {
				sum=tsum;
				s=ts;
				e=i;
			}
		}
		printf("%d %d %d\n",sum,s,e);
		if(T)
			printf("\n");
	}
	return 0;
}

Excellent blog post:
HDU 1003 Max Sum

Problem link: 【HDU】[1003]Max Sum

Loading...