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);
}
}

Maximum Subsequence Sum 【DP】的更多相关文章

  1. Maximum Subsequence Sum【最大连续子序列+树状数组解决】

    Problem Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i < ...

  2. HDU - 1003 Max Sum 【DP】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1003 题意 给出一个序列 要求找出一个和最大的子序列 思路 O(N)的做法 但是要标记 子序列的头部位 ...

  3. 【DP-最大子串和】PAT1007. Maximum Subsequence Sum

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

  4. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  5. HDOJ 1159 Common Subsequence【DP】

    HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  6. POJ_2533 Longest Ordered Subsequence【DP】【最长上升子序列】

    POJ_2533 Longest Ordered Subsequence[DP][最长递增子序列] Longest Ordered Subsequence Time Limit: 2000MS Mem ...

  7. PAT Maximum Subsequence Sum[最大子序列和,简单dp]

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

  8. HDOJ_1087_Super Jumping! Jumping! Jumping! 【DP】

    HDOJ_1087_Super Jumping! Jumping! Jumping! [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  9. PAT1007:Maximum Subsequence Sum

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

随机推荐

  1. The web application [/struts2_0100] created a ThreadLocal with key of type

    引用: 严重: The web application [/struts2_0100] created a ThreadLocal with key of type [com.opensymphony ...

  2. IIC设备驱动程序

    IIC设备是一种通过IIC总线连接的设备,由于其简单性,被广泛引用于电子系统中.在现代电子系统中,有很多的IIC设备需要进行相互之间通信 IIC总线是由PHILIPS公司开发的两线式串行总线,用于连接 ...

  3. 第1章 为什么创造WPF、第2章 XAML揭秘

    1.2 步入WPF 下面是WPF的一些亮点: 广泛整合:各种媒体类型都能组合起来并一起呈现 与分辨率无关:因为WPF使用矢量图形 硬件加速:WPF是基于Direct3D创建的,工作全部是由GPU完成的 ...

  4. AngularJS的稍复杂form验证

    代码下载:https://files.cnblogs.com/files/xiandedanteng/angularjsSoccerFormCheck.rar 代码: <!DOCTYPE HTM ...

  5. iOS开发数据持久化技术02——plist介绍

    有疑问的请加qq交流群:390438081 我的QQ:604886384(注明来意) 微信:niuting823 1. 简单介绍:属性列表是一种xml格式的文件.扩展名.plist: 2. 特性:pl ...

  6. C/C++内存管理详解(转)

    内存分配方式 简介 在C++中,内存分成5个区,他们分别是堆.栈.自由存储区.全局/静态存储区和常量存储区. 栈:在执行函数时,函数内局部变量的存储单元都可以在栈上创建,函数执行结束时这些存储单元自动 ...

  7. Android开发第一讲之目录结构和程序的执行流程

    1.如何在eclipse当中,修改字体 下面的这种办法,可以更改xml的字体 窗口--首选项--常规--外观--颜色和字体--基本--文本字体--编辑Window --> Preferences ...

  8. 如何修改eclipse的默认字符集和修改中文乱码

    转载,以供以后学习.谢谢 有时候 java代码,导入eclipse中会出现 乱码的问题,通过修改字符集就可以解决. 看下面图片演示过程. 发表在 使用教程 | 标签为 eclipse, 乱码 | 留下 ...

  9. (LeetCode)两个链表的第一个公共节点

    LeetCode上面的题目例如以下: Write a program to find the node at which the intersection of two singly linked l ...

  10. Spring Boot 从入门到实战汇总

    之前写过几篇spring boot入门到实战的博文,因为某些原因没能继续. 框架更新迭代很快,之前还是基于1.x,现在2.x都出来很久了.还是希望能从基于该框架项目开发的整体有一个比较系统的梳理,于是 ...