uva-507
题意:连续序列和最大,直接枚举。。。。。
代码跑了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的更多相关文章
- UVA 507 - Jill Rides Again 动态规划
Jill Rides Again Jill likes to ride her bicycle, but since the pretty city of Greenhills where sh ...
- UVa 507 - Jill Rides Again
题目大意:最大和子序列问题.由于具有最大和的子序列具有一下性质:第一项不为负数,并且从第一项开始累加,中间不会有和出现负数,因为一旦有负数我们可以抛弃前边的部分以得到更大的子序列和,这将会产生矛盾. ...
- <算法竞赛入门经典> 第8章 贪心+递归+分治总结
虽然都是算法基础,不过做了之后还是感觉有长进的,前期基础不打好后面学得很艰难的,现在才慢慢明白这个道理. 闲话少说,上VOJ上的专题训练吧:http://acm.hust.edu.cn/vjudge/ ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- UVA&&POJ离散概率与数学期望入门练习[4]
POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...
- UVA计数方法练习[3]
UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...
- 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 ...
- UVA - 1625 Color Length[序列DP 代价计算技巧]
UVA - 1625 Color Length 白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束 和模拟赛那道环形DP很想,计算这 ...
随机推荐
- centos 7 中安装 mysql 5.7
centos 7 中安装 mysql 5.7 环境说明: 查看centos的版本:cat /etc/redhat-release 安装和配置步骤: 下载 mysql 源安装包: sudo curl - ...
- mysql启动报错 mysql InnoDB: Error: could not open single-table tablespace file
mysql启动不成功,报错 mysql InnoDB: Error: could not open single-table tablespace file innodb_force_recovery ...
- MySQL 8.0常见问题
1.连接问题: 1.1:8.0的驱动地址更换由原来的com.mysql.jdbc.Driver改为com.mysql.cj.jdbc.Driver 1.2:8.0以后访问地址要加上时区.编码等属性jd ...
- sed常用操作命令
sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据进行替换.删除.新增.选取等特定工作. 命令格式: sed [OPTION]... {script-only-i ...
- 顺序栈代码实现&&stack库
#include<iostream> using namespace std; ; typedef int Elemtype; struct SqStack { Elemtype *bas ...
- 使用 AppScan 进行扫描
针对大型网站的扫描,我们按照戴明环 PDCA 的方法论来进行规划和讨论,建议 AppScan 使用步骤:计划(Plan).执行(Do).检查(check).分析(Analysis and Action ...
- java 运算符的优先级比较
口诀:淡云一笔安洛三福 单目>算数运算符>移位>比较>按位>逻辑>三目>赋值 单目运算符:+,-,++,-- 算数运算符:+,-,*,/,% 移位运算符:&l ...
- java web中分层MVC的意义
在web编程中,由于高内聚.低耦合的特点,需要将多个类实现多层,大致有以下几层:①entity,实体类,如user,role等,这些类里边包含了私有属性和公共的get.set方法这和数据库中的表相对应 ...
- 【网络】IP子网划分详解
1.IP地址组成 IP地址组成示意图 IP地址由32位二进制组成,32位二进制分成了4字节,每字节8位,字节之间用符.(点)分隔,为了方便 ...
- php使用insert语句动态添加用户
<html> <head> <title>Adding User</title> </head> <body> <h2&g ...