Given a sequence of K integers { N​1​​, N​2​​, …, N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, …, N​j​​ } 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

思路

用DP的方法 动态更新答案 每次对每一个数求和 当和小于 0 的时候 就重置为0 然后 sum > ans 的时候 就更新

然后 有一个难点 就是 要输出 该子串的 首尾 元素

一共有几种情况

0.最大和序列中有负数

1.并列和对应i相同但是不同j,即 尾部有0

2.1个正数

3.全是负数

4.负数和0

5.最大和前面有一段0

6.最大N

我们只需要标记一下 起点就可以了

然后更新答案的那个 就是尾部

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 = 1e3 + 5;
const int MOD = 1e9 + 7; int arr[maxn]; int main()
{
int t;
cin >> t;
for (int l = 1; l <= t; l++)
{
int n, k;
scanf("%d%d", &n, &k);
ll ans = 0;
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
sort(arr, arr + n);
int len = n - k + 1;
for (int i = 0; i < len; i++)
ans -= arr[i];
for (int i = n - 1, j = 0; j < len; i--, j++)
ans += arr[i];
printf("Case #%d: %d\n", l, ans);
}
}

最新文章

  1. ExtJS获取父子、兄弟容器元素方法
  2. go语言之并发
  3. Android数据存储-文件操作
  4. struts 拦截器 Interceptor
  5. hdu1242 优先队列+bfs
  6. mysql 全文搜索的FULLTEXT
  7. 信息安全系统设计基础实验四:外设驱动程序设计 20135211李行之&amp;20135216刘蔚然
  8. 解决SourceGrid在某些系统上无法用鼠标滚轮滚动的问题
  9. IOS之表视图添加索引
  10. oracle之时间转换
  11. PHP移动互联网开发笔记(5)——文件的上传下载
  12. 你真的知道组件中的v-model吗?
  13. oracle游标的知识点
  14. NumPy 中的集合运算
  15. Netty高性能之Reactor线程模型
  16. AVAudioPlayer播放音频文件时没声音
  17. vs2010点调试,显示系统找不到指定的文件
  18. 查找Mysql慢查询Sql语句
  19. bzoj1907: 树的路径覆盖(树形DP)
  20. AtCoder Grand Contest 017 迟到记

热门文章

  1. supervisor开机自启动方法
  2. SilverLight-DataBinding-DataTemplates: 三、数据绑定 DataTemplates模板的使用(求助,没有到达实例效果,求高人指点迷津)
  3. C#里判断字符串是否为纯数字
  4. DIY树莓派之随身工具箱
  5. 基于django的网站开发一基础项目配置
  6. python logging模块学习(转)
  7. MetaQ简单实用demo
  8. winform程序公布后,client下载报错“您的 Web 浏览器设置不同意执行未签名的应用程序”
  9. 统一建模语言(UML,Unified Modeling Language)
  10. React学习之redux