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, $1 \le r \le b$. Each route description begins with the number of stops on the route: an integer s, $2 \le s \le 20,000$ on a line by itself. The number of stops is followed by s - 1 lines, each line i ( $1 \le 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= 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

思路:O(n)复杂度的贪心,求最大连续和的变形。
 1 /*************************************************************************
 2     > File Name: n.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com 
 5     > Created Time: Sat 26 Jul 2014 05:20:14 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 
16 int main(){
17     int Case,n,s[20005];
18     scanf("%d",&Case);
19     for(int t = 1; t <= Case; ++t){
20         scanf("%d",&n);
21         for(int i = 0; i < n - 1; ++i) scanf("%d",&s[i]);
22         int tmax = 0,sum = 0,st = 0,ed,ansst = 0;
23         for(int i = 0; i < n - 1; ++i){
24             sum += s[i];
25             if(sum < 0){
26                 sum = 0;
27                 st = i + 1;
28             }
29             if(sum > tmax || (sum == tmax && (i - st) > (ed - ansst))){
30                 tmax = sum;
31                 ed = i;
32                 ansst = st;
33             }
34         }
35         if(tmax)
36             printf("The nicest part of route %d is between stops %d and %d\n",t,ansst + 1,ed + 2);
37         else
38             printf("Route %d has no nice parts\n",t);
39     }
40     return 0;
41 
 

转载于:https://www.cnblogs.com/naturepengchen/articles/3871350.html

生信人的一天~HIFI数据+HIC数据组装基因组 HIFI+HIC数据组装基因组! 阅读详情

相关推荐

HTML5期末大作业:水果超市网站设计——水果超市(6页) HTML+CSS+JavaScript 学生DW网页设计作业成品 web课程设计网页规划与设计 网购商城设置网页

HTML5期末大作业:水果超市网站设计——水果超市(6页) HTML+CSS+JavaScript 学生DW网页设计作业成品 web课程设计网页规划与设计 网购商城设置网页 常见网页设计作业题材有 个人、 美食、 公司、 学校、 旅游、 电商、 宠物、 电器、 茶叶、 家居、 酒店、 舞蹈、 动漫、 明星、 服装、 体育、 化妆品、 物流、 环保、 书籍、 婚纱、 军事、 游戏、 节日、 戒烟、 电影、 摄影、 文化、 家乡、 鲜花、 礼品、 汽车、 其他 等网页设计题目, A+水平作业, 可满足大

1714

UVa 507 - Jill Rides Again

题目: 分析: 说明:

小白菜又菜 859

局域网ip冲突检测工具_python 小工具实现 windows笔记本与 ipad数据互传

宅在家没事的时候就开始琢磨ipad的进阶使用,由于没有mac无法隔空投送,ipad与windows的数据交互表现极差,所以设计了这个ftp小工具。当然,这个工具不仅仅能用于windows与ipad,还可以用于局域网任意两台设备之间程序源码与下载见文章末尾使用说明windows端:biubiubiu.exeipad端: appsotre下载 FE文件管理器windows端操作在c盘下创建ftp目录...

weixin_39866265的博客 4436

UVA - 507 Jill Rides Again

题意:求最大的连续子序列和,当最大相等的时候,取最长的长度#include #include #include using namespace std; int arr[20001],n,Max,begin,start,end; int main() { int t; scanf("%d",&t); for (int i = 1; i <= t; i++)

那些年 702

NCCL 源码深度解析:从初始化到通信链路构建全流程

本文深入解析了NVIDIA Collective Communication Library (NCCL) 的核心工作机制,从通信组初始化、硬件拓扑发现,到P2P、共享内存及网络传输链路的构建。详细剖析了源码中集合通信任务的执行流程与Proxy线程的协同机制,为开发者进行高性能分布式训练的性能调优与深度问题排查提供了清晰的底层视角。

hp777的博客 551

手把手教你用VL822-QFN88设计一个支持PD快充的Type-C四口HUB(附原理图要点)

本文详细介绍了基于VL822-QFN88芯片设计支持PD快充的Type-C四口HUB的完整流程,包括芯片选型、系统架构设计、原理图要点、PCB布局及固件配置。特别适合硬件工程师和电子设计爱好者,帮助实现高速数据传输与快速充电的多端口扩展方案。

weixin_30832405的博客 516

uva507 Jill Rides Again

简单的最大子序列问题,刘汝佳的书里有分治的

cgjiayou 438

[ATF]-smc指令详解

1、在ree/tee调用smc之前 2、调用smc之后,cpu触发同步异常,进入ATF的sync_exception_aarch64—>handle_sync_exception—>smc_handler64处理函数 在smc_handler64中又调用了smc_handler func smc_handler64 /* ---------------------------------------------- * Detect if this is a RUN_IMAGE or ot

baron-周贺贺-代码改变世界ctw 1万+

uva 507 Jill Rides Again(检索)

题目连接:507 - Jill Rides Again 题目大意:简单说就是求连续序列的最大和, 但是不同的是要输出从第几个开始到第几个结束。 解题思路:只要在分治的时候一起返回最大值的区间就可以了, 但是要注意下连续序列被分割成左右两边时考虑最大求解时候的下标问题。 #include #include const int N = 20005; int maxs

不慌不忙、不急不躁 1230

Debezium报错处理系列之第112篇:Cannot replicate because the master purged required binary logs. Replicate the

Debezium报错处理系列之第112篇:Cannot replicate because the master purged required binary logs.

zhengzaifeidelushang的博客 726

基于STM32、HAL库的IP5356M_DBZ_CA快充协议芯片简介及驱动程序设计

IP5356M_BATTERY_LEVEL_25 = 1, /* 电池电量25% */IP5356M_CHARGE_STATE_CHARGE_DONE = 4, /* 充电完成 */IP5356M_CHARGE_STATE_FAST = 2, /* 快速充电 */IP5356M_CHARGE_STATE_NONE = 0, /* 无充电 */IP5356M_CHARGE_STATE_PRE = 1, /* 预充电 *//* 延时1秒 */

corlin6688的博客 1418

UVA - 507 Jill Rides Again(连续子序列最大和)

题目大意:jill 骑行时每段路有一个好感值,可以先搭公交在任意站点下车再开始骑行,直到到达任意目的地,问骑哪段路得到的好感值最大。 解题思路:直觉就是暴力,O(n2^2)超时了,查题解,学习了最大子序列和问题 by 在hust快乐的学习,一步一步分析优化写的非常好。#include<iostream> #include<stdio.h> #include<algorithm> #include<

life kicks in right now 424

实测可复现|2026 Cloudflare盾v4.0反反爬全实战:从JS/WASM逆向到JA3指纹伪装,手把手突破拦截(附完整逆向代码+避坑手册)

本文以2026年Cloudflare盾v4.0(集成Shield Synapse v2.0)为核心,全程以实战复盘为视角,拆解了从JS/WASM混合逆向、全维度指纹伪装(JA3+浏览器指纹)、人类行为模拟,到代理池搭建的全流程,整合了完整可复现的代码,所有步骤和踩坑点均为实测所得,无任何AI套话,新手也能跟着复现,最终实现爬取成功率从12%提升至95%+。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 1686

UVa 507 Jill Rides Again (贪心&最大连续子串和)

507 - Jill Rides Again Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=448 Jill likes to ride her bicycle,

AC,∑ndless 1338

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

UVA 311 Packets 贪心+模拟

题意:有6种箱子,1x1 2x2 3x3 4x4 5x5 6x6,已知每种箱子的数量,要用6x6的箱子把全部箱子都装进去,问需要几个。 一开始以为能箱子套箱子,原来不是。。。 装箱规则:可以把箱子都看成正方体,装在6x6的盒子里。 典型的贪心题。 思路:(参考了Starginer大神的) ①每个6*6的都占一个箱子。 ②每个5*5的放在一个箱子里,同时里面还能装11个1*1的。 ③每

水果君の日常 2771

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

uva11134(贪心)

/* 按照区间长度排序,长度一样按照l从小到大排序 如此贪心,错误: 反例:[1,1],[2,3],[3,4],[1,3]; 因为有包含关系的区间,不能保证大区间尽量选择小区间外的点,从而使得小区间能选的点变少 正确的贪心思路是: 按r从小到大排序,一样按l从小到大排序,每次从左端点开始选,选出的点一定是对接下来区间影响最小的 因为接下来的区间都被当前区间包含,先满足大区间,小区间才能有更多机...

摆渡人的博客 764

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

[UVA12545]Bits Equalizer[贪心]

题目链接:[UVA12545]Bits Equalizer[贪心] 题意分析:可以进行三种操作。一、将0变成1;二、将问号变成0或者1;三、交换两个字符;用最少的步骤将S串变成T串。不行就输出-1。 解题思路:首先根据题意,既然0能变1,问号能变任何。那么不能变成T串的情况只能是S串中的1大于T串中1的个数。(因为减不掉) 然后考虑能够成为T串的情况。首先,不相同的0和不相同的1首先进行置换

闲停 1102

UVA12405 Scarecrow【贪心】

Taso owns a very long field. He plans to grow different types of crops in the upcoming growing season. The area, however, is full of crows and Taso fears that they might feed on most of the crops. For this reason, he has decided to place some scarecrows at

海岛Blog 524

uva 1316 - Supermarket(贪心)

题目:uva 1316 - Supermarket(贪心) 题目大意:给出n间商品,分别有商品的利润和股

1123

UVA 10714 Ants 蚂蚁 贪心+模拟 水题

题意:蚂蚁在木棍上爬,速度1cm/s,给出木棍长度和每只蚂蚁的位置,问蚂蚁全部下木棍的最长时间和最短时间。 模拟一下,发现其实灰常水的贪心。。。 不能直接求最大和最小的= =。只要求出每只蚂蚁都走长路下木棍时的最大值,和走短路时的就行了。 代码: /* * Author: illuz * Blog: http://blog.youkuaiyun.com/

水果君の日常 2204

uva1149(贪心入门)

/* 贪心的选最轻的物品i+最重的j,如果放不下就剩下的只能单独一个背包 证明:如果这种方法不是最优的 则i有两种情况:1.单独一个包,那么可以把j跟它放一起,这样可以得到一个可能更优的解 2.跟k(k<j)放一起,那么把j,k换位,交换后:k所在的船不会超重,因为原本放的是j;j所在的船也不会超重,因为贪心策略,所以能得到一个不比之前差的新解 综上:该策略为最优策略 */ #defi...

摆渡人的博客 390

uva 10037 - Bridge(贪心)

题目链接:uva 10037 - Bridge 题目大意:有n个人要过桥,每次可以过一个或两个人,但是过桥一定要有灯,现在找出过桥最短的方案。 解题思路:送灯肯定要让速度最快的来送,所以就有了两种过桥方式。假设现在要让a[i-1]和a[i]过桥,方案1:a0 a1,a0,ai-1 ai, a1.方案2:a0 ai,a0,ai-1 a0,a0。比较两种方案的时间,取较少的。注意n

不慌不忙、不急不躁 1637

(白书训练计划)UVa 11134 Fabled Rooks(贪心)

题目地址:UVa 11134 这题因为行与列是无关的,互无影响的。所以可以将行或列分开来计算。这就相当于转化成了在期间[1,n]内选择n个不同的整数,使得第i个整数在闭区间[Li,Ri]内。这就转换成了一个贪心问题了。但是注意不能先按照左端点排序,再按右端点排序,然后尽量往左边放,比如,(1,1),(1,3),(2,2),这样是不对的,应该按右端点为主关键字排序,再按左端点为次关键字排序。看到网

scf0920 3871

Watering Grass UVA - 10382 贪心

问题 https://vjudge.net/problem/UVA-10382 分析 将一个点的覆盖范围看作是一个长方形,舍弃弓形区域,变成区间覆盖问题,用贪心法 注意:bb-ww/4有可能小于0,要筛出 #include <iostream> #include <cstdio> #include <cmath> #include <cstring>...

zpf1998的博客 173

UVA - 10954 - Add All (贪心)

UVA - 10954 Add All Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem F Add All Input: standard input Output: stan

Never say never 1254

uva-993-贪心

uva-993-贪心   题意:给你一个数字y,生成另外一个最小的数字x,使得x里面的每一位相乘等于y 解题思路:直接贪心就是,x里面的每一位都小于等于9 #include <string> #include<iostream> #include<map> #include<memo...

weixin_34019144的博客 96
上一篇: Uva--11129(分治,数学)
下一篇: Uva--108(贪心)
dingdi3021
博客等级 码龄11年 1粉丝 230原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值