标签:des style class blog code java
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 138410 Accepted Submission(s): 32144
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 //#define LOCAL 5 using namespace std; 6 7 8 int main() 9 { 10 #ifdef LOCAL 11 freopen("d:datain.txt","r",stdin); 12 freopen("d:dataout.txt","w",stdout); 13 #endif 14 int n; 15 while(scanf("%d",&n)!=EOF) 16 { 17 int i,m; 18 for(i =0 ; i< n;i++) 19 { 20 scanf("%d",&m); 21 int dp[100000],a[100000]; 22 scanf("%d",&a[0]); 23 dp[0] = a[0]; //当前最大 24 for(int j = 1; j<m;j++) //生成了dp状态数组了 25 { 26 scanf("%d",&a[j]); 27 if(dp[j-1]+a[j]<a[j]) //状态转移方程 28 dp[j]=a[j]; 29 else 30 dp[j]=dp[j-1]+a[j]; 31 } 32 int Max,End; 33 Max = dp[0]; 34 End = 0; 35 for(int j = 1 ;j<m;j++) //寻找区间 36 if(Max<dp[j]) 37 { 38 End = j; 39 Max = dp[j]; 40 } 41 int Begin = End; 42 int temp = 0; 43 for(int j = End;j>=0;j--) 44 { 45 temp +=a[j]; 46 if(temp==dp[End]) 47 Begin = j; 48 } 49 cout<<"Case "<<i+1<<":"<<endl<<Max<<" "<<Begin+1<<" "<<End+1<<endl; 50 if(i<n-1) 51 cout<<endl; 52 } 53 } 54 return 0; 55 }
简化后不带dp数组的,因为这题在dp问题中是比较简单的。
1 //hdu 1003 2 3 #include<stdio.h> 4 int main() 5 { 6 7 int n; 8 while(scanf("%d",&n)!=EOF) 9 { 10 for(int i = 0;i<n;i++) 11 { 12 int a; 13 int Max = -9999; 14 int sum = 0,m; 15 int Begin=0,End=0,flag=0; 16 scanf("%d",&m); 17 scanf("%d",&a); 18 Max = sum = a; 19 for(int j = 1 ;j<m ;j++) 20 { 21 scanf("%d",&a); 22 if(sum<0) 23 { 24 sum=a; 25 flag=j; 26 } 27 else 28 { 29 30 sum=sum+a; 31 } 32 if(Max<sum) 33 { 34 Max = sum ; 35 Begin =flag; 36 End = j; 37 } 38 } 39 printf("Case %d:\n%d %d %d\n",i+1,Max,Begin+1,End+1); 40 if(i<n-1) 41 printf("\n"); 42 } 43 44 } 45 return 0; 46 }
标签:des style class blog code java
原文地址:http://www.cnblogs.com/jhldreams/p/3786478.html