UVA 507 Jill Rides Again 最大子序列和

很裸的最大子序列和。

具体算法分析见六种姿势拿下连续子序列最大和问题

代码:

 /*
 *   Author:        illuz <iilluzen[at]gmail.com>
 *   Blog:          http://blog.csdn.net/hcbbt
 *   File:          uva507.cpp
 *   Lauguage:      C/C++
 *   Create Date:   2013-09-05 00:09:13
 *   Descripton:    UVA 507	 Jill Rides Again 
 */
#include <cstdio>
#define rep(i, n) for (int i = 0; i < (n); i++)

const int MAXN = 20010;
int a[MAXN];

void maxSeq(int* a, int len, int &res, int &l, int &r) {
	res = a[0], l = 0, r = len - 1;
	int sum = 0, tl = 0;
	for (int i = 0; i < len; i++) {
		if (sum < 0) {
			sum = a[i];
			tl = i;
		}
		else sum += a[i];
		if (res < sum || (res == sum && l == tl))
		   	res = sum, l = tl, r = i;
	}
}

int main() {
	int n, t;
	scanf("%d", &t);
	rep(cas, t) {
		scanf("%d", &n);
		n--;
		rep(i, n) scanf("%d", &a[i]);
		int l, r, res;
		maxSeq(a, n, res, l, r);
		if (res <= 0) printf("Route %d has no nice parts\n", cas + 1);
		else printf("The nicest part of route %d is between stops %d and %d\n", cas + 1, l + 1, r + 2);
	}
	return 0;
}


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值