-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJillRidesAgain_UVA507.cpp
80 lines (67 loc) · 1.7 KB
/
JillRidesAgain_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
#include <bits/stdc++.h>
using namespace std;
ifstream fin("JillRidesAgain_UVA507.in");
ofstream fout("JillRidesAgain_UVA507.out");
struct solution
{
int l;
int r;
int v;
};
int main()
{
int testCase; fin >> testCase;
for (int r = 1; r <= testCase; ++r)
{
int s; fin >> s;
vector<int> a(s, 0);
for (int i = 1; i <= s - 1; ++i)
{
fin >> a[i];
}
solution ans{1, 2, a[1]}, _null{0, 0, 0};
vector<solution> dp(s, _null);
dp[1] = ans;
for (int i = 2; i <= s - 1; ++i)
{
dp[i].r = i + 1;
if (dp[i - 1].v >= 0)
{
dp[i].v = dp[i - 1].v + a[i];
dp[i].l = dp[i - 1].l;
}
else
{
dp[i].v = a[i];
dp[i].l = i;
}
if (dp[i].v > ans.v)
{
ans = dp[i];
}
else if (dp[i].v == ans.v)
{
if (dp[i].r - dp[i].l > ans.r - ans.l)
{
ans = dp[i];
}
else if (dp[i].r - dp[i].l == ans.r - ans.l)
{
if (dp[i].l < ans.l)
{
ans = dp[i];
}
}
}
}
if (ans.v < 0)
{
fout << "Route " << r << " has no nice parts\n";
}
else
{
fout << "The nicest part of route " << r << " is between stops " << ans.l << " and " << ans.r << '\n';
}
}
return 0;
}