题目链接:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=448
类型: 最大连续和
原题:
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.
The input file contains information on several bus routes. The first line of the file is a single integerbrepresenting
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 integers,on
a line by itself. The number of stops is followed bys- 1 lines, each linei()
is an integernirepresenting Jill's assessment of the niceness of the
road between the two stopsiandi+1.
For each routerin the input file, your program should identify the
beginning bus stopiand the ending bus stopjthat
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 (largestj-i).
To break ties in longest maximal segments, choose the segment that begins with the earliest stop (lowesti). For each routerin
the input file, print a line in the form:
The nicest part of routeris between stopsiandj
However, if the maximal sum is not positive, your program should print:
Routerhas no nice parts
3
3
-1
6
10
4
-5
4
-3
4
4
-4
4
-5
4
-2
-3
-4
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. 求一段连续的车站的好感度之和最大是多少。
分析与总结:
最大连续和问题,有好几种算法,其中最快的一种是O(n)的,只需要扫描一遍即可。
假设用数组arr存好感度 , 那么设sum[i]=arr[0]+arr[1]+..+arr[i]. 即sum[i]是从车站1到车站i的好感度之和。 这样的话,如果要求某一段车站的好感度之和, 假设车站3到车站5,只需要O(1)时间: sum[5]-sum[2]就是答案。
要使得sum[i]-sum[j]更大,就要使得sum[j]更小。
做题时需要注意的是,答案要求好感度最大的连续和区间长度越长越好, 起点越早越好。
代码:
/*
* UVa: 507 - Jill Rides Again
* Time: 0.120s
* Author: D_Double
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 20005
using namespace std;
int b,s;
int arr[MAXN];
int main(){
scanf("%d",&b);
for(int t=1; t<=b; ++t){
scanf("%d",&s);
arr[0]=0;
for(int i=1; i<=s-1; ++i){
scanf("%d",&arr[i]);
arr[i] += arr[i-1];
}
int min=0, maxSum=-2147483646, left=0, right=0;
for(int i=1; i<=s-1; ++i){
int t=arr[i]-arr[min];
if(t>maxSum) { maxSum=t; right=i; left=min; }
else if(t==maxSum) { // 这个特别要注意!!!因为题目要求区间越长越好
if(min==left)
right=i;
else if(i-min > right-left)
right=i, left=min;
}
if(arr[i]<arr[min]) { min=i;}
}
if(maxSum>0){
printf("The nicest part of route %d is between stops %d and %d\n", t, left+1, right+1);
}
else{
printf("Route %d has no nice parts\n", t);
}
}
return 0;
}
附:这个算法的升级版:UVa 108
- Maximum Sum
—— 生命的意义,在于赋予它意义。
分享到:
相关推荐
Create React App入门 该项目是通过引导的。 可用脚本 在项目目录中,可以运行: npm start 在开发模式下运行该应用程序。打开在浏览器中查看它。 如果您进行编辑,则页面将重新加载。您还将在控制台中看到任何棉绒...
Jack&Jill 只是一个在新的 Java8 和 Jack&Jill 环境中试用 Android Annotation Processor 的项目。 还要检查库和测试框架是否可以使用它。 首先,确保你知道什么是JACK&JILL。 检查这个。 支持的工具 即时运行 数据...
Jill 是一项开源计划,旨在为开发大规模多代理系统提供快速且轻量级的基于 Java 的 BDI 式(信念、愿望、意图)执行引擎。 吉尔是: BDI 执行引擎:支持 PRS 风格的复杂 BDI 目标计划行为层次结构(A Rao 和 M ...
TFS片段 稳定的 发展 TFSnippet是用于编写和测试TensorFlow模型的一组实用程序。 TFSnippet的设计理念是无干扰的。 它旨在提供一组有用的实用程序,可以与任何其他TensorFlow库和框架一起使用。...
资源来自pypi官网。 资源全名:jill-0.9.3.tar.gz
资源来自pypi官网。 资源全名:jill-0.6.10.tar.gz
高效算法作者: [法] Christoph Dürr / [法] Jill-Jênn Vie 出版社: 人民邮电出版社 副标题: 竞赛、应试与提高必修128例 译者: 史世强 出版年: 2018-5 页数: 204 定价: 55.00元 装帧: 平装 丛书: 图灵程序设计丛书 ...
account_jill
'H'代表Jack的家. 'S'代表Jack的学校 'h'及's'代表Jill有家和学校. ''代表公共的不可到达点 '.'代表空地输出一个数字,代表你的方案中
Jill-Jênn Vie,法国高等电力学院博士、算法讲师,担任法国高等师范学院Paris-Saclay团队在ACM竞赛中的算法导师;曾任法国国际编程大赛Prologin主席,并于2014年获Google RISE Award。 目录 第 1 章 引言 1 1 1 ...
吉尔 的增强型Python分支-适用于Linux(MacOS,Windows和FreeBSD)的Julia Installer-轻便 为什么是jill.py ? 发行版程序包管理器(例如apt , pac , chocho )可能会为破损的Julia提供不正确的二进制依赖性(例如...
jill:Julia语言的命令行安装程序
Java编写的Lua脚本引擎,可以用于JME
杰克和吉尔的例子使用 Jack&Jill 的示例 Android 项目
build-tools-26.0.2,android studio3.0使用,测试可用
这个是acm题库里面Jill's Tour Paths的问题,有两个源码,都可以在vc中运行通过
课程模板 该存储库是用于使用GitHub页面管理课程作业的模板。 它采用主题, 和呈现的材料。 对于课程的每个星期,创建一个子目录weekZZ ... 这两个存储库是通过协作开发的,作者身份由Jill Naiman和Matthew Turk共享。
有关许可信息,请联系南加州大学的 Jill McNitt-Gray 博士。 源代码版权 :copyright: 2014 Ryan Hewitt、Michael C. Hogan、Jill McNitt-Gray 博士、Ian Russell。 版权所有。 图片版权 :copyright: 2014 Ramiro ...
API端点 在本地运行API 所有API调用都是AJAX调用。 安装Django并提取最新的存储库以运行Django服务器并使用API调用后, $ python manage.py runserver 使用运行服务器提供的base_URL,然后在其末尾添加“ /...
自述文件 当您尝试解决人群之间的帐户问题时,请使用 minpay - minpay 会列出所有未付款项并将它们合并,以便您可以在个人之间以尽可能少的付款来解决您的债务。 ... 下载 minpay.rb ... Jill,40,Jill,Sue,Bob