题意:连续序列和最大,直接枚举。。。。。

代码跑了2.4s.QAQ

#include <string>
#include<iostream>
#include<map>
#include<memory.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<math.h>
#include<iomanip>
#include<bitset>
#include"math.h"
namespace cc
{
using std::cout;
using std::endl;
using std::cin;
using std::map;
using std::vector;
using std::string;
using std::sort;
using std::priority_queue;
using std::greater;
using std::vector;
using std::swap;
using std::stack;
using std::queue;
using std::bitset; constexpr int N = ;
int seg[N] = { };
void solve()
{
int n;
cin >> n;
int t;
int cases = ;
while (n--)
{
cin >> t;
--t;
for (int i=;i<t;i++)
cin >> seg[i];
int as=-, ae=-;
int amax = -;
for (int s=;s<t;s++)
{
int curMax = ;
for (int e=s;e < t;e++)
{
curMax += seg[e];
if (curMax > amax)
{
amax = curMax;
as = s+;
ae = e+;
}
else if (curMax == amax&& as!=-)
{
if (ae - as- < e - s)
{
as = s+;
ae = e+;
}
}
} }
if (as==-)
{ cout << "Route "<<cases<<" has no nice parts" << endl;
}
else
{
cout<<"The nicest part of route "<<cases <<" is between stops "<<as<<" and "<<ae<<endl;
}
cases++; } }
}; int main()
{ #ifndef ONLINE_JUDGE
freopen("d://1.text", "r", stdin);
#endif // !ONLINE_JUDGE
cc::solve(); return ;
}

下面这个代码只花了120ms.

再次叙述题意:对于一个连续序列,寻找连续和最大的开始下标和结束下标,如果有多最大和相等,寻找最长的,如果最长的一样,寻找开始小标最小的.

以下代码只用了一层循环。使用as和ae标识我们期望的结果下标.sum表示当前的和,s表示当期和开始的计算下标.

#include"pch.h"
#include <string>
#include<iostream>
#include<map>
#include<memory.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<math.h>
#include<iomanip>
#include<bitset>
#include"math.h"
namespace cc
{
using std::cout;
using std::endl;
using std::cin;
using std::map;
using std::vector;
using std::string;
using std::sort;
using std::priority_queue;
using std::greater;
using std::vector;
using std::swap;
using std::stack;
using std::queue;
using std::bitset; void solve()
{
int n;
cin >> n;
int t;
int cases = ;
while (n--)
{
cin >> t;
int as = , ae = ;
int s = ;
int ans = -;
int sum = ;
for (int i = ;i < t;i++)
{
int k;
cin >> k;
sum += k;
if (sum > ans || (sum == ans && (ae - as) < (i - s)))
{
ans = sum;
as = s;
ae = i;
}
if (sum < )
{
sum = ;
s = i + ;
}
}
if (ans <= )
{
cout << "Route " << cases << " has no nice parts" << endl;
}
else
{
cout << "The nicest part of route " << cases
<< " is between stops " << as << " and " << ae+ << endl;
}
++cases;
} }
}; int main()
{ #ifndef ONLINE_JUDGE
freopen("d://1.text", "r", stdin);
#endif // !ONLINE_JUDGE
cc::solve(); return ;
}

uva-507的更多相关文章

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

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

  2. UVa 507 - Jill Rides Again

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

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

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

  4. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  5. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  6. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  7. UVA&&POJ离散概率与数学期望入门练习[4]

    POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...

  8. UVA计数方法练习[3]

    UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...

  9. UVA数学入门训练Round1[6]

    UVA - 11388 GCD LCM 题意:输入g和l,找到a和b,gcd(a,b)=g,lacm(a,b)=l,a<b且a最小 g不能整除l时无解,否则一定g,l最小 #include &l ...

  10. UVA - 1625 Color Length[序列DP 代价计算技巧]

    UVA - 1625 Color Length   白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束   和模拟赛那道环形DP很想,计算这 ...

随机推荐

  1. matplot画图kill问题,形成思路

    很多小伙伴刚学matplot的时候 看着代码就想敲  可是你应该现有概念啊 熟悉这两个再看下面的代码,下面的解决了一些人问中文字体的问题,满足了一般人的设置需求 代码注释很详细,我就不多哔哔了. 完全 ...

  2. 2019OO第二单元作业总结

    OO第二单元的作业主题是模拟电梯. ---------------------------------------------------------------------------------- ...

  3. CodeForce Educational round Div2 C - Vasya and Robot

    http://codeforces.com/contest/1073/problem/C   题意:给你长度为n的字符串,每个字符为L, R, U, D.给你终点位置(x, y).你每次出发的起点为( ...

  4. dll被设置为用记事本打开的解决方法

    dll被设置为用记事本打开的解决方法: 打开注册表编辑器 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Fi ...

  5. Oracle—通过操作系统进程查看数据库sql语句

    工作中遇到一个问题,某报表运行时间特别长,通过操作系统可以看到一个oracle进程消耗资源比较大,如何能够通过该操作系统进程找到具体SQL呢.记录如下: 1.查看Linux系统进程号 可以通过top动 ...

  6. JavaScript DOM&BOM

    1.DOM含义 D: Document 文档 一份文档就是一棵节点树,每个节点都是一个对象O:Object 对象 JavaScript语言里对象可以分为三种类型: (1)用户定义的对象(user-de ...

  7. 使用Python matplotlib做动态曲线

    今天看到“Python实时监控CPU使用率”的教程: https://www.w3cschool.cn/python3/python3-ja3d2z2g.html 自己也学习如何使用Python ma ...

  8. HashMap解读

    个人理解,欢迎提出问题

  9. 上传本地代码到gitHub过程详解

    1.注册Github账号 2.创建自己的GitHub仓库 3.创建自己的Repository(项目的名字等) 4.复制创建仓库的地址到Git命令窗口并执行命令行(Git clone 仓库的复制地址) ...

  10. Windows2008 R2 X64 PHP环境搭建步骤

    Windows2008 R2 X64 PHP环境搭建步骤: 下载:Mysql5.7.23.PHP5.6.Zend.XCahe 一.安装MYSQL.导入数据: 解压MYsql压缩包,并新建Data目录, ...