题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=448

典型的最大子段和问题。

#include<iostream>
using namespace std;
int main()
{
int testNumber; //测试的数目
cin>>testNumber; for(int index=1;index<=testNumber;index++) //循环测试每组数据
{
int max=0,sum=0,tempNumber=0;
int tempStart=0;
int start=0,end=0; int number;
cin>>number;
for(int i=0;i<number-1;i++)
{
cin>>tempNumber;
sum+=tempNumber;
if(sum<0) //小于0的话暂时放弃之前的值,将下一个值作为起点
{
tempStart=i+1;
sum=0;
}
if(sum>max||(sum==max&&start==tempStart))//只有在大于最大值的时候才会更新
{
//一定要注意第二种情况,
//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)
max=sum;
start=tempStart;
end=i;
}
}
if(max==0)
cout<<"Route "<<index<<" has no nice parts"<<endl;
else
cout<<"The nicest part of route "<<index<<" is between stops "<<start+1<<" and "<<end+2<<endl;
}
return 0;
}

UVA507-- Jill Rides Again的更多相关文章

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

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

  2. UVA 507 - Jill Rides Again 动态规划

      Jill Rides Again  Jill likes to ride her bicycle, but since the pretty city of Greenhills where sh ...

  3. UVa 507 - Jill Rides Again

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

  4. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  5. <算法竞赛入门经典> 第8章 贪心+递归+分治总结

    虽然都是算法基础,不过做了之后还是感觉有长进的,前期基础不打好后面学得很艰难的,现在才慢慢明白这个道理. 闲话少说,上VOJ上的专题训练吧:http://acm.hust.edu.cn/vjudge/ ...

  6. soj 1015 Jill's Tour Paths 解题报告

    题目描述: 1015. Jill's Tour Paths Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Every ...

  7. 常用rides命令

    rides使用步骤 1.源代码构建安装 1.下载,Linux下命令wget http://redis.io/download下载redis的包 2.解归档Linux下命令tar -xvf redis- ...

  8. uva-507

    题意:连续序列和最大,直接枚举..... 代码跑了2.4s.QAQ #include <string> #include<iostream> #include<map&g ...

  9. 分布式 +rides

    redis分布式部署 1.scrapy框架是否可以自己实现分布式? - 不可以.原因有二. 其一:因为多台机器上部署的scrapy会各自拥有各自的调度器,这样就使得多台机器无法分配start_urls ...

随机推荐

  1. 预定义异常 - PHP手册笔记

    Exception是所有异常的基类,类摘要如下: <?php class Exception { protected string $message; // 异常消息内容 protected i ...

  2. 正确决解Hibernate4.*中:Connection cannot be null when 'hibernate.dialect' not set

    <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hi ...

  3. jQuery创建节点

    注:摘自<锋利的jQuery(第二版)> 创建元素节点 例如要创建两个<li>元素节点,并且要把它们作为<ul>元素节点的子节点添加到DOM节点树上.完成这个任务需 ...

  4. 如何修改被编译后DLL文件

    原文 http://www.cnblogs.com/wujy/p/3275855.html 我们平时在工作中经常会遇到一些已经被编译后的DLL,而且更加麻烦是没有源代码可以进行修改,只能针对这个DLL ...

  5. qt介绍

    http://www.oschina.net/p/qt Qt 是一个跨平台的C++图形用户界面应用程序框架.它提供给开发者建立图形用户界面所需的功能,广泛用于开发GUI程序,也可用于开发非GUI程序. ...

  6. Spring、AOP详解

    如何配置AOP查看:Spring.Hello AOP 1.对于拦截规则@Pointcut的介绍: @Pointcut("execution (* cn.raffaello.service.. ...

  7. 全国计算机等级考试二级教程-C语言程序设计_第7章_函数

    函数执行,从右到左执行 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> main() ...

  8. JS计算两个日期时间之差之天数不正确

    做了一个时间倒计时,发现天数总是不正确. js代码: //定义目标日期 var targetTime = new Date(); //目标日期 targetTime.setFullYear(2015, ...

  9. 最短路(dijskra+SPFA+Bellman)

    最短路 Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissio ...

  10. UVA LA 7146 2014上海亚洲赛(贪心)

    option=com_onlinejudge&Itemid=8&page=show_problem&category=648&problem=5158&mosm ...