Skip to content

Instantly share code, notes, and snippets.

@unsuthee
Created November 16, 2012 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unsuthee/4085945 to your computer and use it in GitHub Desktop.
Save unsuthee/4085945 to your computer and use it in GitHub Desktop.
UVA 507 : Jill Rides Again
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int numRoutes;
cin >> numRoutes;
for (int r=1; r<=numRoutes; r++) {
int numStops;
cin >> numStops;
int sum_so_far = 0;
int max_sum = 0;
int start_edge = 1;
pair<int,int> best_range = make_pair(0,0);
for (int edge=1; edge<numStops; edge++) {
int val;
cin >> val;
sum_so_far += val;
if (sum_so_far < 0) {
sum_so_far = 0;
start_edge = edge+1;
}
if (sum_so_far >= max_sum) {
if (sum_so_far == max_sum) {
if (best_range.first == 0) {
best_range = make_pair(start_edge, edge); //initial route
}
else if (start_edge == best_range.first) {
best_range = make_pair(start_edge, edge); //extending the route
}
}
else {
best_range = make_pair(start_edge, edge);
}
max_sum = sum_so_far;
}
}
if (max_sum == 0) {
cout << "Route " << r << " has no nice parts" << endl;
}
else {
cout << "The nicest part of route " << r
<< " is between stops " << best_range.first
<< " and " << best_range.second+1 << endl;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment