题目
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 ® is the sequence number within the data file, 1 ≤ r ≤ b. Each route description begins with the number of stops on the route: an integer s, 2 ≤ s ≤ 20,000 on a line by itself. The number of stops is followed by s−1 lines, each line i (1 ≤ i < s) 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 = ni + ni+1 + … + nj−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

思路
经典动态规划(也是贪心)问题——求一个序列中最大子序列和,这里简述一下算法:
1:初始化结果ans = 0,累加0到n-1个元素,每一步得到一个和sum;
2:如果某一步中sum > ans,更新ans;
3:如果sum < 0,重置sum为0;
最终ans中储存的即最大子序列和。相关证明需要自行查阅资料。

本体的额外要求是要记录这个和最大的最大子序列的起始点,尽可能的包含更多的元素

代码

#include <iostream>
#include <algorithm>
using namespace std;
int n[20010];
int main()
{int TC, r, cot = 1;scanf("%d", &TC);while(TC--){scanf("%d", &r);int ans = 0, sum = 0, r1 = 1, r2 = 0, ans_r1 = 1, ans_r2 = 0;for(int i = 1; i < r; i++) scanf("%d", &n[i]);for(int i = 1; i < r; i++){sum += n[i]; r2 = i + 1;if(sum < 0){ r1 = i + 1; sum = 0; }else if(sum > ans || (sum == ans && r2 - r1 > ans_r2 - ans_r1)){ ans = sum; ans_r2 = r2; ans_r1 = r1; }}if(ans > 0) printf("The nicest part of route %d is between stops %d and %d\n", cot++, ans_r1, ans_r2);else printf("Route %d has no nice parts\n", cot++);}
}

Jill Rides Again UVA - 507(求最大子序列和)相关推荐

  1. LeetCode算法题3:求最大子序列和

    文章目录 前言 一.递归 二.求最大子序列和 1,最朴素的解法 2,较朴素的解法(更进一步) 3,分治和递归 4,精妙的解法 总结 前言 本文简单介绍递归的使用(依次打印出一个 int 整数的每一位) ...

  2. 算法探讨——再议经典算法问题:求最大子序列和、绝对值最大子序列和以及其区间...

    算法探讨--再议经典算法问题:求最大子序列和.绝对值最大子序列和以及其区间 给定任一数字序列,如{-5,4,-20,16,-2,-3},求出其最大子序列和,绝对值最大子序列和以及对应的区间,在这个例子 ...

  3. 求连续子序列平均最大权值的问题

    原题链接 求长度为n.m(<=1e5)的a,b两组数组一段长度不小于x.y的连续子序列平均最大权值. 分析: 要求一个数组a长度不小于len的连续子序列平均最大权值. 二分答案x,令b[i]=a ...

  4. Uva 507 - Jill Rides Again(最大子数组求和问题)

    题目:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  5. 求最大子序列和的四种方法

    求一个给定序列的连续子序列中和最大的那个子序列的和,下边方法只求和,没有找出最大子序列. 用到的头文件和宏定义如下 #include "stdafx.h" #include< ...

  6. Maximum Sum UVA - 108(连续子序列最大和—变形之子矩阵最大和)

    题目大意:给出 n*n 的矩阵,找每隔数字之和最大的子矩阵,输出最大和.  解题思路:枚举矩阵左上和右下的坐标,分别合并子矩阵的每列,使得二维转化为一维,然后利用连续子序列最大和去做就行. Time ...

  7. 求最大子序列和(动态规划)

    最大子序列和 描述: 给定一个序列a[1],a[2],a[3]......a[n],你的工作是计算一个子序列的最大和.例如,给定(6,-1,5,4,-7),这个序列的最大和是6+(-1)+5+4=14 ...

  8. 动态规划1:状态转移方程-求最大子序列和

    时间复杂度O(n) 序列:-2 11 -4 13 -5 -2 //最大连续子序列和 //使用到状态转移方程 #include <cstdio> #include "algorit ...

  9. ICPC训练联盟2021寒假冬令营(4)_2021.01.21_笔记

    文章目录 试题链接 学习笔记-排序算法( O(n^2^)时间复杂度 ) 选择排序程序段(C++) 冒泡排序程序段(C++) 插入排序程序段 A - Necklace (UVA 11001) 中文释义 ...

  10. 动态规划法求最大字段和时间复杂度_九章算法 | 动态规划:最长上升子序列

    给定一个整数序列,找到最长上升子序列(LIS),返回LIS的长度. 在线评测地址:LintCode 领扣 说明 最长上升子序列的定义: 最长上升子序列问题是在一个无序的给定序列中找到一个尽可能长的由低 ...

最新文章

  1. POJ 3630 Phone List
  2. Linux系统运维工程师PDF文档精选
  3. keras 导出onnx并使用
  4. 导入表编程-枚举导入表
  5. A. Many Equal Substrings(自己很水)
  6. 第一讲 ODE几何方法
  7. 从TCP协议的原理来谈谈rst复位攻击
  8. linux内核4.0,新闻|Linux内核4.0功能:实时内核补丁,支持PS3
  9. linux centos8下载,在Centos8上安装和使用curl
  10. printf 指针地址_c语言对指针的理解
  11. python手机安装模块_python如何安装模块 模块安装操作教程
  12. Tips--更改Jupyter Notebook的默认工作路径
  13. [Android Pro] 内容提供者ContentProvider的基本使用
  14. tcp服务端无故死掉了linux,TCP服务端socket会丢连接的诡异问题及思考
  15. android源码百度网盘下载(AOSP源码集合)
  16. unixbench分析_unixbench测试CPU性能工具
  17. Lattice Diamond 3.12下载与安装(免费获取license.dat)
  18. 傲腾内存 可以用ghost系统_windows xp sp3安装包用傲腾技术打造专业修图电脑配置...
  19. MySQL之Innodb引擎的4大特性
  20. ESP系统——ABS、TCS、VDC及VAF功能介绍

热门文章

  1. python文件操作(1)
  2. linux常用命令-part2
  3. Android qq 登录 界面 圆头像
  4. 配对交易之统计套利配对:介绍
  5. 文件夹访问被拒绝,您需要权限来执行操作
  6. 浅谈网游服务器的承载
  7. JavaSE(9)-细节狂魔:OOP之继承多态?20K字长篇看完,有手就行
  8. 用计算机弹课间进行曲,课间进行曲(修改稿)
  9. 魅族16 USB连接计算机,在魅族16x中连接电脑的方法分享
  10. 【有机】镍催化非活化烯烃的不对称氢烷基化构建全烷基取代的饱和三级碳手性中心...