码迷,mamicode.com
首页 > 其他好文 > 详细

uva 507 Jill Rides Again (分治)

时间:2015-03-04 22:52:44      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:acm   c语言   uva   

                               uva 507 Jill Rides Again


Jill likes to ride her bicycle, but since the pretty city of Greenhills where she lives has grown, Jill often uses the excellent public bus system for part of her journey. She has a folding bicycle which she carries with her when she uses the bus for the first part of her trip. When the bus reaches some pleasant part of the city, Jill gets off and rides her bicycle. She follows the bus route until she reaches her destination or she comes to a part of the city she does not like. In the latter event she will board the bus to finish her trip.


Through years of experience, Jill has rated each road on an integer scale of ``niceness.‘‘ Positive niceness values indicate roads Jill likes; negative values are used for roads she does not like. There are not zero values. Jill plans where to leave the bus and start bicycling, as well as where to stop bicycling and re-join the bus, so that the sum of niceness values of the roads she bicycles on is maximized. This means that she will sometimes cycle along a road she does not like, provided that it joins up two other parts of her journey involving roads she likes enough to compensate. It may be that no part of the route is suitable for cycling so that Jill takes the bus for its entire route. Conversely, it may be that the whole route is so nice Jill will not use the bus at all.


Since there are many different bus routes, each with several stops at which Jill could leave or enter the bus, she feels that a computer program could help her identify the best part to cycle for each bus route.

Input 

The input file contains information on several bus routes. The first line of the file is a single integer b representing the number of route descriptions in the file. The identifier for each route ( r) is the sequence number within the data file, 技术分享. Each route description begins with the number of stops on the route: an integer s, 技术分享 on a line by itself. The number of stops is followed by s - 1 lines, each line i ( 技术分享) is an integer ni representing Jill‘s assessment of the niceness of the road between the two stops i and i+1.

Output 

For each route r in the input file, your program should identify the beginning bus stop i and the ending bus stop j that identify the segment of the route which yields the maximal sum of niceness, m= n i+n i+1+...+n j-1. If more than one segment is maximally nice, choose the one with the longest cycle ride (largest j- i). To break ties in longest maximal segments, choose the segment that begins with the earliest stop (lowest i). For each route r in the input file, print a line in the form:


The nicest part of route r is between stops i and j


However, if the maximal sum is not positive, your program should print:


Route r has no nice parts

Sample Input 

3
3
  -1
   6
10
   4
  -5
   4
  -3
   4
   4
  -4
   4
  -5
4
  -2
  -3
  -4

Sample Output 

The nicest part of route 1 is between stops 2 and 3
The nicest part of route 2 is between stops 3 and 9
Route 3 has no nice parts


题目大意:每组样例包括两个部分:1)点的个数n  

                                                             2)n-1 个点与点间的值

                     求最长最大连续和。

解题思路:分治,在获取最大值的同时要记录边界(三种情况:最长--最大连续和可以在左边,右边或者起点在左区间终点在右区间)。注意,当和相等时,要求更长的序列。



#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int num[20005], n;
int cal(int x, int y, int &L, int &R) {
	int Max = 0;
	if (y - x == 1)	{
		L = x, R = y;
		return num[x];
	}
	int mid = x + (y - x) / 2; // 向下取整
	int l1, l2, r1, r2;
	int a = cal(x, mid, l1, r1), b = cal(mid, y, l2, r2);
	if (a > b) {
		Max = a;
		L = l1, R = r1;
	}
	else if (a < b) {
		Max = b;
		L = l2, R = r2;	
	}
	else { //若大小相同取更长的
		Max = a;
		if (r1 - l1 >= r2 - l2) {
			L = l1, R = r1;
		}
		else {
			L = l2, R = r2;
		}
	}
	int v = 0, l3 = mid - 1, vL = num[mid - 1];
	for (int i = mid - 1; i >= x; i--) { //检查起点在左区间,终点在右区间的序列
		v += num[i];
		if (v >= vL) {
			vL = v;
			l3 = i;
		}
	}
	int r3 = mid, vR = num[mid];
	v = 0;
	for (int i = mid; i < y; i++) {
		v += num[i];
		if (v >= vR) {
			vR = v;
			r3 = i;
		}
	}
	r3++;
	if (Max < vL + vR) {
		L = l3, R = r3;
		return vL + vR;
	}
	else if (Max > vL + vR) {
		return Max;
	}
	else { //相等时找出最长序列
		if (r3 - l3 > R - L) {
			L = l3, R = r3;
		}
		else if (r3 - l3 == R - L && l3 < L) {
			L = l3, R = r3;
		}
		return Max;
	}
}
int main() {
	int T, Case = 1;
	scanf("%d", &T);
	while (T--) {
		scanf("%d", &n);	
		memset(num, 0, sizeof(num));
		for (int i = 1; i < n; i++) {
			scanf("%d", &num[i]);
		}
		int f, e;
		int ans = cal(1, n, f, e);
		if (ans < 0) {
			printf("Route %d has no nice parts\n", Case++);
		}
		else {
			printf("The nicest part of route %d is between stops %d and %d\n", Case++, f, e);
		}
	}
	return 0;
}


uva 507 Jill Rides Again (分治)

标签:acm   c语言   uva   

原文地址:http://blog.csdn.net/llx523113241/article/details/44063117

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!