数列两段的最大字段和

POJ2479

Maximum sum

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 41231 Accepted: 12879

Description

Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below:

Your task is to calculate d(A).

Input

The input consists of T(<=30) test cases. The number of test cases (T) is given in the first line of the input.

Each test case contains two lines. The first line is an integer n(2<=n<=50000). The second line contains n integers: a1, a2, ..., an. (|ai| <= 10000).There is an empty line after each case.

Output

Print exactly one line for each test case. The line should contain the integer d(A).

Sample Input

1

10
1 -1 2 2 3 -3 4 -4 5 -5

Sample Output

13

Hint

In the sample, we choose {2,2,3,-3,4} and {5}, then we can get the answer.

Huge input,scanf is recommended.

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm> using namespace std;
const int maxn = 50000+7; int t,n,arr[maxn],sum;
int a[maxn],b[maxn]; int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(int i = 1; i <= n ; i++ ) {
scanf("%d",&arr[i]);
}
a[1] = arr[1];
for(int i = 2; i <= n; i ++ ) {
if(a[i-1]<0)
a[i]=arr[i];
else
a[i]= a[i-1]+arr[i];
}
for(int i = 2; i <= n; i ++ ) {
a[i] = max(a[i-1],a[i]);
}
/*********************************/
b[n] = arr[n];
for(int i = n-1; i >= 1; i -- ) {
if(b[i+1]<0)
b[i]=arr[i];
else
b[i]= b[i+1]+arr[i];
}
for(int i = n-1; i >= 1; i -- ) {
b[i] = max(b[i+1],b[i]);
}
int ans = -999999999;
for(int i = 2; i <= n; i ++ ) {
ans = max(a[i-1]+b[i],ans);
}
printf("%d\n",ans);
} return 0;
}

最长公共上升子序列

POJ1458

Common Subsequence

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 53882 Accepted: 22384

Description

A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..., xm > another sequence Z = < z1, z2, ..., zk > is a subsequence of X if there exists a strictly increasing sequence < i1, i2, ..., ik > of indices of X such that for all j = 1,2,...,k, xij = zj. For example, Z = < a, b, f, c > is a subsequence of X = < a, b, c, f, b, c > with index sequence < 1, 2, 4, 6 >. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.

Input

The program input is from the std input. Each data set in the input contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.

Output

For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.

Sample Input

abcfbc         abfcab
programming contest
abcd mnp

Sample Output

4
2
0
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm> using namespace std;
const int maxn = 1e3+7; char str[maxn],ttr[maxn];
int dp[maxn][maxn]; int main()
{
while(~scanf("%s %s",str+1,ttr+1))
{
int n = strlen(str+1);
int m = strlen(ttr+1);
memset(dp,0,sizeof(dp));
dp[1][1] = str[1] == ttr[1];
for(int i = 1; i <= n; i ++ ) {
for(int j = 1; j <= m ; j ++ ) {
if(i == 1 && j == 1) {
continue;
}
if(str[i] == ttr[j]) {
dp[i][j] = dp[i-1][j-1]+1;
}
else {
dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
}
}
}
printf("%d\n",dp[n][m]);
}
return 0;
}

最长上升子序列

POJ2533

Longest Ordered Subsequence

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 53931 Accepted: 24094

Description

A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK <= N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).

Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.

Input

The first line of input file contains the length of sequence N. The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces. 1 <= N <= 1000

Output

Output file must contain a single integer - the length of the longest ordered subsequence of the given sequence.

Sample Input

7
1 7 3 5 9 4 8

Sample Output

4
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm> using namespace std;
const int maxn = 1005; int n,arr[maxn],dp[maxn]; int main()
{
while(~scanf("%d",&n))
{
for(int i =1 ; i <= n ; i ++ ) {
scanf("%d",&arr[i]);
dp[i] = 1;
}
for(int i = 1 ; i <= n ; i ++ ) {
for(int j = 1; j < i ; j ++ ) {
if(arr[i]>arr[j]) {
dp[i] = max(dp[i],dp[j]+1);
}
}
}
int ans = 0;
for(int i = 1; i <= n; i ++ ) {
ans = max(ans,dp[i]);
}
printf("%d\n",ans);
} return 0;
}
//二分版本
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm> using namespace std;
const int maxn = 1005; int n,arr[maxn],dp[maxn],top,data; int main()
{
while(~scanf("%d",&n))
{
top = 1;
scanf("%d",&dp[0]);
for(int i =1 ; i < n ; i ++ ) {
scanf("%d",&data);
if(data > dp[top-1]) {
dp[top++] = data;
}
else {
dp[lower_bound(dp,dp+top,data)-dp] = data;
}
}
printf("%d\n",top);
} return 0;
}

最长公共上升子序列

HDU1423

Greatest Common Increasing Subsequence

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8181 Accepted Submission(s): 2644

Problem Description

This is a problem from ZOJ 2432.To make it easyer,you just need output the length of the subsequence.

Input

Each sequence is described with M - its length (1 <= M <= 500) and M integer numbers Ai (-2^31 <= Ai < 2^31) - the sequence itself.

Output

output print L - the length of the greatest common increasing subsequence of both sequences.

Sample Input

1

5
1 4 2 5 -12
4
-12 1 2 4

Sample Output

2
#include <bits/stdc++.h>

using namespace std;
const int maxn = 1005; int f[maxn];
int a[maxn],n;
int b[maxn],m; int main()
{
int t;
while(~scanf("%d",&t))
{
while(t--)
{ memset(f,0,sizeof(f));
scanf("%d",&n);
for(int i = 1; i <= n; i ++ )
{
scanf("%d",&a[i]);
}
scanf("%d",&m);
for(int i = 1; i <= m; i ++ )
{
scanf("%d",&b[i]);
} for(int i = 1; i <= n; i ++ )
{
int MAX = 0;
for(int j = 1; j <= m; j ++ )
{
if(a[i] > b[j]) MAX = max(MAX,f[j]);
if(a[i] == b[j]) f[j] = MAX + 1;
}
} int ans = 0;
for(int j = 1; j <= m; j ++ )
{
ans = max(ans,f[j]);
}
printf("%d\n",ans);
if(t){
puts("");
}
}
} return 0;
}

跟着大佬重新入门DP的更多相关文章

  1. HDU 1231 最大连续子序列 --- 入门DP

    HDU 1231 题目大意以及解题思路见: HDU 1003题解,此题和HDU 1003只是记录的信息不同,处理完全相同. /* HDU 1231 最大连续子序列 --- 入门DP */ #inclu ...

  2. HDU 2571 命运 (入门dp)

    题目链接 题意:二维矩阵,左上角为起点,右下角为终点,如果当前格子是(x,y),下一步可以是(x+1,y),(x,y+1)或者(x,y*k) ,其中k>1.问最大路径和. 题解:入门dp,注意负 ...

  3. 【跟着大佬学JavaScript】之节流

    前言 js的典型的场景 监听页面的scroll事件 拖拽事件 监听鼠标的 mousemove 事件 ... 这些事件会频繁触发会影响性能,如果使用节流,降低频次,保留了用户体验,又提升了执行速度,节省 ...

  4. 【跟着大佬学JavaScript】之lodash防抖节流合并

    前言 前面已经对防抖和节流有了介绍,这篇主要看lodash是如何将防抖和节流合并成一个函数的. 初衷是深入lodash,学习它内部的好代码并应用,同时也加深节流防抖的理解.这里会先从防抖开始一步步往后 ...

  5. 【跟着大佬学JavaScript】之数组去重(结果对比)

    前言 数组去重在面试和工作中都是比较容易见到的问题. 这篇文章主要是来测试多个方法,对下面这个数组的去重结果进行分析讨论.如果有不对的地方,还请大家指出. const arr = [ 1, 1, &q ...

  6. 【笔记】入门DP

    复习一下近期练习的入门 \(DP\) .巨佬勿喷.\(qwq\) 重新写一遍练手,加深理解. 代码已经处理,虽然很明显,但请勿未理解就贺 \(qwq\) 0X00 P1057 [NOIP2008 普及 ...

  7. UVA 674 (入门DP, 14.07.09)

     Coin Change  Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We ...

  8. 洛谷P3724 大佬 [AH2017/HNOI2017] dp+bfs

    正解:dp+bfs 解题报告: 传送门! 这题看起来很复杂的样子其实真的很复杂 但是仔细看一下题目,会发现其实操作只有两个目的嘛,一个是保证自己不死,一个是让对手减血 而且保证自己不死只有一种操作 而 ...

  9. 入门dp总结

    写这篇博文主要是为了归纳总结一下dp的有关问题(不定期更新,暑假应该会更的快一些) 会大概讲一下思路,不会事无巨细地讲 另一篇是平时做过的一些dp题,这篇博客里面提到的题都有题解放在那边:https: ...

随机推荐

  1. linux 安装jdk以及nginx详细过程

    一.安装jdk 1:首先下载jdk到本地,然后通过git 上传到linux服务器上 2:进入目录usr,并创建目录java,将jdk的压缩文件移动到该目录下 cd /usr mkdir java mv ...

  2. Python 破解带密码保护的Zip文件

    今天发生了个有趣的事情,有个朋友发了一个带密码保护的Zip文件给我,却不给我密码,我就琢磨这怎么可以'猜'到密码呢? 经过一系列的尝试,最终使用python把这个密码给'猜'出来了.要想写出破解密码的 ...

  3. Spring Cloud学习笔记-005

    服务消费者 之前已经搭建好了微服务中的核心组件——服务注册中心(包括单节点模式和高可用模式).也有了服务提供者,接下来搭建一个服务消费者,它主要完成两个目标,发现服务以及消费服务.其中,服务发现的任务 ...

  4. ls-dyna基础教程

    刚刚开始使用ls-dyna,几天前还只知道点开dyna界面,然后就没有然后了,没人带,资料也没多少,但是科研还得继续往下做呀(手动滑稽),通过在仿真论坛上搜索相关的资料,并通过自己的一步步操作,做了大 ...

  5. 通过TCP实现显示屏截图请求及回传

    在很多业务场景下,需要监视显示屏画面.在实时性要求不高的情况下,可以通过定时对显示屏进行截图及回传实现. 本文通过C#中提供的TCP通信功能,对该功能的实现进行简单描述. 首先,该功能的实现分为客户端 ...

  6. swiper 应用

    swiper之PC端的广告页面[当前示例对应网站:http://shang.shuaishou.com/] plugins:[红线部分] html: <div class="banne ...

  7. 文件上传详解 (HTML FILE)

    FileUpload 对象 在 HTML 文档中 <input type="file"> 标签每出现一次,一个 FileUpload 对象就会被创建. 该元素包含一个文 ...

  8. [LeetCode] Reverse Words in a String III 翻转字符串中的单词之三

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

  9. 原生http请求封装

    满血复活,今天开始开始更新博客.随着es6的普遍应用,promise属性也随之用之普遍,我们在一些项目中,为了避免引入一些http库,节省空间,就简单将原生http请求做了封装处理,封装代码如下:(其 ...

  10. jQuery动画使用总结

    jQuery动画我用的比较多的仅仅只有show和hide,但是作为一个被我们大多数人所熟知的框架,相信他的动画功能还是比较多样的,这里做个小总结. 1.jQuery animate(),用于创建自定义 ...