-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuva507.cpp
118 lines (82 loc) · 1.31 KB
/
uva507.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
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
91
92
93
94
95
96
97
98
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
int N;
vector<int> distances;
int start, end1;
int s, e;
int Kadane()
{
int max_so_far = -1;
int max_ending_here = 0;
//int s, e;
s = 0;
start = s;
e = 0;
end1 = e;
for(int i = 0; i < distances.size(); i++)
{
max_ending_here += distances[i];
if(max_ending_here < 0)
{
max_ending_here = 0;
s = i+2;
e = i+2;
}
else
{
if(max_ending_here > max_so_far)
{
e = i+2;
start = s;
end1 = e;
}
if(max_ending_here == max_so_far)
{
//cout << "da " << endl;
e = i+2;
if(e-s > end1-start)
{
start = s;
end1 = e;
}
}
max_so_far = max(max_so_far, max_ending_here);
}
}
return max_so_far;
}
int main()
{
int tc;
cin >> tc;
for(int k = 1; k <= tc; k++)
{
cin >> N;
if(N <= 0)
printf("Route %d has no nice parts\n", k);
else
{
distances.clear();
distances.resize(N-1);
for(int i = 0; i < N-1; i++)
{
cin >> distances[i];
}
if(Kadane() == -1)
printf("Route %d has no nice parts\n", k);
else
{
if(start == 0)
start++;
printf("The nicest part of route %d is between stops %d and %d\n", k, start, end1);
}
}
}
return 0;
}