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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
#include <iostream> #include <cstdio> using namespace std; int arr[25000]; struct path{ int len,st,en; }; path p; int kadanesAlgo(int rd,int first) { int max_end_here=0,max_so_far=0; int begin=first,length; int finish=0,start=first; for(int i=first;i<rd;i++) { max_end_here+=arr[i]; if(max_end_here<0) { max_end_here=0; begin=i+1; } if(max_end_here>max_so_far) { max_so_far = max_end_here; start=begin; finish=i+1; length=finish-start+1; p.len=length; p.en=finish; p.st=start; } else if(max_end_here==max_so_far) { max_so_far = max_end_here; start=begin; finish=i+1; length=finish-start+1; if(length>p.len) { p.len=length; p.en=finish; p.st=start; } else if(length==p.len) { if(p.st>begin) { p.st=begin;p.en=finish; } } } } return max_so_far; } int main() { int t,kase=1; int rd,res; int first; cin>>t; bool f=0; while(t--) { cin>>rd; f=0; for(int i=1;i<rd;i++) { cin>>arr[i]; if(!f) { if(arr[i]>0) { first=i; f=1; } } } p.en=0;p.st=0;p.len=-1; res = kadanesAlgo(rd,first); if(res) cout<<"The nicest part of route "<<kase++<<" is between stops "<<p.st<<" and "<<p.en<<"\n"; else cout<<"Route "<<kase++<<" has no nice parts\n"; } return 0; } |