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 
n
i
 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,2,3...s,  各个车站之间的景色值是不同的, 例如车站1到车站2的景色值是5, 车站3到车站4的景色值是-3.   求一段连续的车站的景色值之和最大是多少。

分析: 最大连续子序列,注意和相等时取长度大的;长度相同时,取起点最小的。
#include<stdio.h>
#include<string.h>
int l[20005];
int main()
{
int b,n,i,max,sum,st,ed,cas=1,bg;
scanf("%d",&b);
while(b--)
{
scanf("%d",&n);
for(i=1;i<n;i++)
scanf("%d",&l[i]);
max=sum=l[1];
st=bg=ed=1; /*开始时起点和终点都在第1站*/
for(i=2;i<n;i++)
{
if(sum<0)
{
sum=0;
st=i; /*更新起点*/
}
sum+=l[i];
if(sum>max||sum==max&&i-st>ed-bg)
{
max=sum;
bg=st; /*bg总是保存新的起点*/
ed=i;
}
}
if(max<=0)
printf("Route %d has no nice parts\n",cas++);
else
printf("The nicest part of route %d is between stops %d and %d\n",cas++,bg,ed+1);
}
return 0;
}

UVA 507 - Jill Rides Again 动态规划的更多相关文章

  1. UVa 507 - Jill Rides Again

    题目大意:最大和子序列问题.由于具有最大和的子序列具有一下性质:第一项不为负数,并且从第一项开始累加,中间不会有和出现负数,因为一旦有负数我们可以抛弃前边的部分以得到更大的子序列和,这将会产生矛盾. ...

  2. CJOJ 1070 【Uva】嵌套矩形(动态规划 图论)

    CJOJ 1070 [Uva]嵌套矩形(动态规划 图论) Description 有 n 个矩形,每个矩形可以用两个整数 a, b 描述,表示它的长和宽.矩形 X(a, b) 可以嵌套在矩形 Y(c, ...

  3. CJOJ 1071 【Uva】硬币问题(动态规划)

    CJOJ 1071 [Uva]硬币问题(动态规划) Description 有n种硬币,面值分别为v1, v2, ..., vn,每种都有无限多.给定非负整数S,可以选用多少个硬币,使得面值之和恰好为 ...

  4. uva507 - Jill Rides Again(最长连续和)

    option=com_onlinejudge&Itemid=8&page=show_problem&problem=448">题目:uva507 - Jill ...

  5. UVa 104 - Arbitrage(Floyd动态规划)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  6. UVa 10891 - Game of Sum 动态规划,博弈 难度: 0

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

  7. UVa 10891 Game of Sum - 动态规划

    因为数的总和一定,所以用一个人得分越高,那么另一个人的得分越低. 用$dp[i][j]$表示从$[i, j]$开始游戏,先手能够取得的最高分. 转移通过枚举取的数的个数$k$来转移.因为你希望先手得分 ...

  8. UVa 10635 Prince and Princess - 动态规划

    讲一下题目大意,就是有两个长度为p + 1和q + 1的序列,求它们的LCS. 如果用O(pq)的算法对于这道题来说还是太慢了.所以要另外想一些方法.注意到序列中的所有元素都不相同,所以两个序列中数对 ...

  9. uva 116 Unidirectional TSP(动态规划,多段图上的最短路)

    这道题目并不是很难理解,题目大意就是求从第一列到最后一列的一个字典序最小的最短路,要求不仅输出最短路长度,还要输出字典序最小的路径. 这道题可以利用动态规划求解.状态定义为: cost[i][j] = ...

随机推荐

  1. BarTender打印出来的条码与设计的不同如何处理

    今日有用户在使用BarTender设计打印条码时发现自己设计出来的条码与打印显示的条码有不一样的地方,也就是BarTender模板上的条码有显示警戒栏,但打印的条码警戒栏却没了,这一问题要如何解决呢? ...

  2. 对数组元素进行排序的方法总结(利用C++)

    首先,对数组元素进行排序方法总结为以下两类: 一.简单排序算法(时间复杂度O(n*n)) 1.插入排序 2.选择排序 3.交换排序,即冒泡排序 二.先进排序算法(时间复杂度O(n*logn)) 1.快 ...

  3. Swift的基础,操作符,字符串和集合类型

    这篇文章主要讲解苹果Swift官方指南的第二章前四节的要点内容,如果想看完整的英文文档可以去苹果开发者页面下载. Basic 声明常量let 声明变量var 注释依旧使用"//" ...

  4. 购买DigtalOcean VPS 以及 连接Linux

    1.DigtalOcean简介 digitalocean是一家成立于2012年的总部设置在纽约的云主机商家,眼下在荷兰的阿姆斯特丹(AMS1.AMS2).美国的纽约(NYC1.NYC2)和旧金山(SF ...

  5. Jquery Enter事件

    //IE或fireFox Event不同,所有要消除浏览器差异问题 <script type="text/javascript"> $('#<%=txtKeyWo ...

  6. Android图片编译报错

    一. AAPT err(1118615418): ERROR: 9-patch image icon_item_bottom_line.9.png malformed No marked region ...

  7. Stack的三种含义(转载--阮一峰)

    作者: 阮一峰 学习编程的时候,经常会看到stack这个词,它的中文名字叫做"栈". 理解这个概念,对于理解程序的运行至关重要.容易混淆的是,这个词其实有三种含义,适用于不同的场合 ...

  8. VMVare虚拟机的异常处理---内部错误

    由于昨天晚上关闭虚机时手快,直接把机器睡眠了.今天早上开机后,虚机显示启动状态,但是无法连接.搜了一下,网上说删掉.lk锁文件就可以了,删掉后,没有反应,强制关闭,再次启动VM,还是打不开,显示内部错 ...

  9. JSP基础之 C标签中的 varStatues属性

    变量状态参数,该属性有5个常用值count 表示当前遍历集合的元素个数index 表示当前遍历到集合的第几个元素current 表示当前的集合元素first 表示集合的第一个元素last 表示集合的最 ...

  10. theano安装

    theano安装有两类方法,一种是自己一步步安装,还有一种是借助其他软件安装.我是安装到一半发现第二种方法的...........所以就用的第一种麻烦的办法装的,但是过程也是一种学习. 电脑:win7 ...