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

代码跑了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. hdu 1518 BFS

    Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square? I ...

  2. Navicat 连接MySQL时出现1251错误的解决方案

    我用的MySQL版本是8.0.11,比较新的MySQL版本中采用的加密方式与旧的不同,从而导致1251错误. 解决方案:打开终端连接上数据库,执行以下语句,问题解决.(自己遇到过的坑,亲测有效) US ...

  3. HTML复习 2019-2-11

    HTML复习 2019-2-11 <!doctype html> <html> <!-- 常见问题答疑 Question 1:HTML标签可以大写吗? 大小写都可以,比如 ...

  4. 找出n之内的完全数, 并输出其因子

    定义: 完全数:所有的真因子(即除了自身以外的约数)的和,恰好等于它本身.例如:第一个完全数是6,它有约数1.2.3.6,除去它本身6外,其余3个数相加,1+2+3=6.第二个完全数是28,它有约数1 ...

  5. 使用Postfix与Dovecot部署邮件系统

  6. python中使用if __name__ == '__main__':

    引子 在python中,假设在一个test1.py的模块中定义了一个foo函数,然后调用函数foo进行测试的时候会产生一个内存空间.当你把这个模块导入到test2.py模块中,接下来如果在test2. ...

  7. 对www.518shengmao.com站资源打包,采用vue Node.js

    最近闲游时间比较多,于是想搞个网站练练手,首先选域名在godday里选了个518shengmao.com,买了个1元的阿里云服务器,接下来程序了. 采用vue+nodejs来开发的 一.NodeJs环 ...

  8. 星图智控推嵌入式家用AI摄像头

    1.星图智控推嵌入式家用AI摄像头 这款新产品核心卖点是使用intel movidius vpu,同时运行了5种AI神经网络,支持自定义AI拓展功能,全本地存储,以及本地AI处理. 与市面上同类产品相 ...

  9. fcn训练及预测tgs数据集

    一.背景 kaggle上有这样一个题目,关于盐份预测的语义分割题目.TGS Salt Identification Challenge | Kaggle  https://www.kaggle.com ...

  10. 导入tensorflow时DLL load failed: 找不到指定的模块

    简单暴力:卸载 重装 方法一: 先删除:pip uninstall tensorflow 再下载:pip install tensorflow 方法二: 也有可能是numpy版本不匹配的问题: 卸载: ...