UVa 507 - Jill Rides Again

动态规划

 

 

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5 
 6 #define MAXN 20002
 7 
 8 int routes,stopNum;
 9 int stops[MAXN];
10 int maxSum,st,en,newSt;
11 
12 int main()
13 {
14     #ifndef ONLINE_JUDGE
15         freopen("D:\\acm.txt","r",stdin);
16     #endif // ONLINE_JUDGE
17 
18     cin>>routes;
19     for(int i = 1;i <= routes;i++){
20         memset(stops,0,sizeof(stops));
21         maxSum = 0;newSt = st = en = 1;
22         cin >> stopNum;
23         for(int j = 1;j < stopNum;j++) cin>>stops[j];
24         ////////////////////////////
25         int m,sum = 0;
26         for(m = 1;m < stopNum;m++){
27             if(sum < 0){
28                 sum = 0;
29                 st = m;//更新起点
30             }
31             sum += stops[m];
32             if(sum > maxSum || (sum == maxSum && m - st > en - newSt))//和相等时取长度大的;长度相同时,取起点最小的。
33             {
34                 maxSum = sum;
35                 newSt = st;
36                 en = m;
37             }
38         }
39         if(maxSum > 0)
40             cout<<"The nicest part of route "<<i<<" is between stops "<<newSt<<" and "<<en + 1<<endl;
41         else
42             cout<<"Route "<< i <<" has no nice parts"<<endl;
43     }
44     return 0;
45 }

 

posted @ 2015-04-10 14:56  小白v  阅读(110)  评论(0编辑  收藏  举报