题目链接

https://www.patest.cn/contests/pat-a-practise/1007

思路

最大子列和

就是 一直往后加 如果 sum < 0 就重置为 0

然后每次 判断一下 sum 是否 > ans

如果是 就更新

然后 为什么这样是对的

就是 假设 当前数字是最大子串和 我们如何知道 前面的求和结果 要不要放入当前子串中 如果前面的求和结果 < 0 的话 那么对当前子串 是没有贡献的 所以就不要

然后 我们需要记录 子串的起始位置 就可以了

因为 更新答案的时候 当前位置 就是结束位置

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a) memset(a, 0, sizeof(a)) using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss; const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-6; const int INF = 0x3f3f3f3f;
const int maxn = 1e4 + 5;
const int MOD = 1e9 + 7; int arr[maxn]; int main()
{
int n;
cin >> n;
int l, r, ans = -1, num, temp = 0, vis, flag = 0;
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
if (i == 0 || flag)
{
vis = arr[i];
flag = 0;
}
temp += arr[i];
if (temp < 0)
{
temp = 0;
flag = 1;
continue;
}
if (temp > ans)
{
ans = temp;
l = vis;
r = arr[i];
}
}
if (ans != -1)
printf("%d %d %d\n", ans, l, r);
else
printf("%d %d %d\n", 0, arr[0], arr[n - 1]);
}

PAT 甲级 1007. Maximum Subsequence Sum (25) 【最大子串和】的更多相关文章

  1. PAT 甲级 1007 Maximum Subsequence Sum (25)(25 分)(0不是负数,水题)

    1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...

  2. PAT甲 1007. Maximum Subsequence Sum (25) 2016-09-09 22:56 41人阅读 评论(0) 收藏

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  3. PAT 甲级 1007 Maximum Subsequence Sum

    https://pintia.cn/problem-sets/994805342720868352/problems/994805514284679168 Given a sequence of K  ...

  4. PAT Advanced 1007 Maximum Subsequence Sum (25 分)

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  5. 1007 Maximum Subsequence Sum (25分) 求最大连续区间和

    1007 Maximum Subsequence Sum (25分)   Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A ...

  6. 1007 Maximum Subsequence Sum (25 分)

    1007 Maximum Subsequence Sum (25 分)   Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A ...

  7. PAT Advanced 1007 Maximum Subsequence Sum

    题目 1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., N**K }. A contin ...

  8. PAT 解题报告 1007. Maximum Subsequence Sum (25)

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...

  9. PAT (Advanced Level) 1007. Maximum Subsequence Sum (25) 经典题

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...

随机推荐

  1. Can''t find the channel handler for deviceType 工行 个人网银 错误

    背景描述:系统Win7,浏览器IE8.登录工商银行个人网银的时候,输入帐号密码和验证码后,出现空白页面,上面一句话  Can''t find the channel handler for devic ...

  2. 【好】Paxos以及分布式一致性的学习

    Paxos,一言以蔽之,我们需要一种提交协议来确保分布式系统中的全局操作即使是在发生故障的情况下也能保证正确性. 跟拜占庭将军问题是不同的问题,虽然拜占庭也是Lamport提出的.拜占庭里面有叛徒,有 ...

  3. c 数组做为形參时 该參数退化为指针

    当数组做为函数的形參的时候,该參数退化为指针,而且是无法直接求得数组的大小. 传数组给一个函数.数组类型自己主动转换为指针类型,因而传的实际是地址. void func(int array[10]) ...

  4. WinForm启动时接收参数

    1 默认的Main函数,修改如下: static class Program { /// <summary> /// 应用程序的主入口点. /// </summary> [ST ...

  5. npm run watch-poll 监控css、js 文件更新

    后台执行npm run watch-poll 你可以在执行命令的后面接一个&命令就会在后台运行了.完整命令:npm run watch-poll & 就是任务号 文章来源:刘俊涛的博客 ...

  6. jquery插件范例代码

    // 创建一个闭包 (function($) { // 插件的定义 $.fn.hilight = function(options) { debug(this); // build main opti ...

  7. 分享下多年积累的对JAVA程序员成长之路的总结

    http://blog.csdn.net/zhongzelin/article/details/8643269我也搞了几年JAVA了,由于一向懒惰,没有成为大牛,只是一普通程序猿,不爱玩社交网站,不爱 ...

  8. ORA-02050故障诊断一例

    http://czmmiao.iteye.com/blog/1474678昨天客户反映说在下午某时间段有几个事务失败了,让我查下当时数据库系统的负载是否正常,看了下CPU的历史负载,很正常,于是看了下 ...

  9. SQL存在一个表而不在还有一个表中的数据

    select a.id,a.oacode,a.custid,a.custname,a.xsz,a.salename,a.communicationtheme,a.communicationproper ...

  10. ie6中 object doesn’t support this property or method

    可能是由于方法或json中有注释,/**/或//删掉注释就可以了