http://acm.hdu.edu.cn/showproblem.php?pid=1003
算法基本思路:
/*
* 此题是动态规划找到 a[1……n] 中 最大的字段和
* 令 b[j] 为 在 子段中 k->j 是最大值 k∈ (1,j)
* 那么 状态转移方程 为 b[j] = max{ b[j-1]+a[j] , a[j]};
*/
import java.util.Scanner;
/*
* 此题是动态规划找到 a[1……n] 中 最大的字段和
* 令 b[j] 为 在 子段中 k->j 是最大值 k∈ (1,j)
* 那么 状态转移方程 为 b[j] = max{ b[j-1]+a[j] , a[j]};
*/
public class HDU1003 {
private int sum=0, b=0;
private static int inf = (-1)*1<<30;
private int s,t;
public void dp( int a[],int n){
sum = inf ;b = inf ;s=0;t=0;
int stemp=0, ttemp = 0;
for(int i=0; i<n; i++){
if(b>=0){
b+= a[i];
ttemp = i;
}else{
b = a[i];
stemp = i;
ttemp = i;
}
if(sum < b){
sum = b;
s = stemp+1;
t = ttemp+1;
}
}
}
public void solve(){
Scanner sc = new Scanner(System.in);
int n,c;
int a[];
n = sc.nextInt();
for( int i=1; i<=n;i++){
c = sc.nextInt();
a = new int[c];
for(int j=0; j<c; j++ ){
a[j] = sc.nextInt();
}
dp(a,c);
System.out.println("Case "+i+":");
System.out.println(sum+" "+s+" "+t);
if( i <n){
System.out.println();
}
}
}
public static void main(String[] args) {
new HDU1003().solve();
}
}