http://pat.zju.edu.cn/contests/pat-practise/1007

 

 

1007. Maximum Subsequence Sum (25)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (<= 10000). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:
10
-10 1 2 3 4 -5 -23 3 7 -21
Sample Output:
10 1 4

一开始s忘记初始化了。。尼玛坑爹啊

[cpp]  view plain copy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<memory.h>  
  4. #include<algorithm>  
  5. #include<cstring>  
  6. #include<queue>  
  7. #include<cmath>  
  8. #include<cstdlib>  
  9. #include<string>  
  10. using namespace std;  
  11. #define MAX 0x6FFFFFFF  
  12. int a[10001];  
  13. int main(){  
  14.   
  15.     //freopen("in.txt", "r", stdin);  
  16.     int k;  
  17.     while(cin>>k){  
  18.         bool flag = true;  
  19.         for(int i=0;i<k;++i){  
  20.             scanf("%d", a+i);  
  21.             if(a[i]>=0)  
  22.                 flag = false;  
  23.         }  
  24.         if(flag){  
  25.             printf("0 %d %d\n", a[0], a[k-1]);  
  26.             continue;  
  27.         }  
  28.           
  29.         int sum = 0;  
  30.         int max = -MAX;  
  31.         int start, end;  
  32.         int s = 0;  
  33.           
  34.         for(int i=0;i<k;++i){  
  35.             sum += a[i];  
  36.             if(sum>max){  
  37.                 max = sum;  
  38.                 start = s;  
  39.                 end = i;  
  40.             }  
  41.             if(sum<0){  
  42.                 sum = 0;  
  43.                 s = i+1;  
  44.             }  
  45.         }  
  46.   
  47.         printf("%d %d %d\n", max, a[start], a[end]);  
  48.     }  
  49.     //fclose(stdin);  
  50.     return 0;  
  51. }  
  52.       
──(root㉿xcs)-[/home/xcs/桌面] └─# gpg --show-keys /etc/apt/trusted.gpg.d/kali-archive-keyring.gpg gpg: 目录‘/root/.gnupg’已创建 gpg: 钥匙箱‘/root/.gnupg/pubring.kbx’已创建 pub rsa4096 2025-04-17 [SC] [有效至:2028-04-17] 827C8569F2518CC677FECA1AED65462EC8D5E4C5 uid Kali Linux Archive Automatic Signing Key (2025) <devel@kali.org> ┌──(root㉿xcs)-[/home/xcs/桌面] └─# apt update 错误:1 http://mirrors.aliyun.com/kali kali-rolling InRelease 403 Forbidden [IP: 124.225.96.42 80] 命中:2 http://http.kali.org/kali kali-rolling InRelease 获取:3 http://http.kali.org/kali kali-rolling/main i386 Packages [20.4 MB] 获取:4 http://http.kali.org/kali kali-rolling/main i386 Contents (deb) [47.7 MB] 获取:5 http://http.kali.org/kali kali-rolling/contrib i386 Packages [97.1 kB] 获取:6 http://http.kali.org/kali kali-rolling/contrib i386 Contents (deb) [183 kB] 获取:7 http://http.kali.org/kali kali-rolling/non-free i386 Packages [147 kB] 获取:8 http://mirrors.neusoft.edu.cn/kali kali-rolling/non-free i386 Contents (deb) [859 kB] 错误: 无法下载 http://mirrors.aliyun.com/kali/dists/kali-rolling/InRelease 403 Forbidden [IP: 124.225.96.42 80] 错误: 仓库 “http://mirrors.aliyun.com/kali kali-rolling InRelease” 没有数字签名。 注意: 无法安全地用该源进行更新,所以默认禁用该源。 注意: 参见 apt-secure(8) 手册以了解仓库创建和用户配置方面的细节。 ┌──(root㉿xcs)-[/home/xcs/桌面] └─#
最新发布
07-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值