bjfu1253 最大上升子序列和
n^2的算法就行,很简单的动态规划。直接上代码
/*
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
/*
* 输入非负整数
* 支持short、int、long、long long等类型(修改typec即可)。
* 用法typec a = get_int();返回-1表示输入结束
*/
typedef int typec;
typec get_int() {
typec res = , ch;
while (!((ch = getchar()) >= '' && ch <= '')) {
if (ch == EOF)
return -;
}
res = ch - '';
while ((ch = getchar()) >= '' && ch <= '')
res = res * + (ch - '');
return res;
}
//输入整数(包括负整数,故不能通过返回值判断是否输入到EOF,本函数当输入到EOF时,返回-1),用法int a = get_int2();
int get_int2() {
int res = , ch, flag = ;
while (!((ch = getchar()) >= '' && ch <= '')) {
if (ch == '-')
flag = ;
if (ch == EOF)
return -;
}
res = ch - '';
while ((ch = getchar()) >= '' && ch <= '')
res = res * + (ch - '');
if (flag == )
res = -res;
return res;
}
/**
* 输入一个字符串到str中,与scanf("%s", str)类似,
* 会忽略掉缓冲区中的空白字符。返回值为输入字符串
* 的长度,返回-1表示输入结束。
*/
int get_str(char *str) {
char c;
while ((c = getchar()) <= ' ') {
if(c == EOF) {
return -;
}
}
int I = ;
while (c > ' ') {
str[I++] = c; c = getchar();
}
str[I] = ;
return I;
} const int MAXN = ;
int data[MAXN], f[MAXN];
int N; int main() {
while ((N = get_int()) > ) {
for (int i = ; i < N; i++) {
data[i] = get_int2();
}
f[] = data[];
for (int i = ; i < N; i++) {
int ma = ;
for (int j = i - ; j >= ; j--) {
if (data[j] < data[i] && f[j] > ma) {
ma = f[j];
}
}
f[i] = ma + data[i];
}
printf("%d\n", *max_element(f, f + N));
}
return ;
}
bjfu1253 最大上升子序列和的更多相关文章
- 用python实现最长公共子序列算法(找到所有最长公共子串)
软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...
- codevs 1576 最长上升子序列的线段树优化
题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [LeetCode] Is Subsequence 是子序列
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 动态规划之最长公共子序列(LCS)
转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
- [Data Structure] LCSs——最长公共子序列和最长公共子串
1. 什么是 LCSs? 什么是 LCSs? 好多博友看到这几个字母可能比较困惑,因为这是我自己对两个常见问题的统称,它们分别为最长公共子序列问题(Longest-Common-Subsequence ...
随机推荐
- 存储入门 – RAID技术(大图解释)
对于RAID,一直都知道个概念,但是对于细节没有去仔细的研究过.正好昨天Training的时候, 老师讲解了RAID的内容,所以顺便就整理一下.很多内容都是参考了ISMv2这本书. RAID中用到的技 ...
- YII2配置多语言
我的YII2版本是2.0.7, 设置多语言时和其他教程有不同的地方, 所以整理如下 1. 在一个controller里面写一个调用i18n的语句, 比如actionIndex echo \Yii::t ...
- iOS xcode设置
Xcode build search paths c/c++ 头文件引用问题include <> 引用编译器的类库路径下的头文件include “” 引用工程目录的相对路径的头文件 inc ...
- Linux服务器沦陷为肉鸡的全过程实录
1 从防火墙瘫痪说起 2015年3月10日,还没到公司就被电话告知办公室无法正常连接互联网了,网速非常慢,无法正常浏览网页.急急忙忙感到公司,开始查找问题. 首先排除了交换机故障,因为内部局域网正常. ...
- k序数组排序
这道题,已知一个数组a,a[i]排好序后位于a[i-k]跟a[i+k]之间,问说该怎么最快地排序. 可以转换成多路归并问题, a[0]<a[k+1]<a[2k+2]<a[3k+3]. ...
- http 服务
今天把一个功能模块做成了http服务,这还是第一次写http服务,纪录下来. package com.chuntent.itemsearch; import java.io.BufferedReade ...
- sleep和wait到底什么区别
wait是在当前线程持有wait对象锁的情况下,暂时放弃锁,并让出CPU资源,并积极等待其它线程调用同一对象的notify或者notifyAll方法.注意,即使只有一个线程在等待,并且有其它线程调用了 ...
- AE 栅格图分级渲染
ArcEngine对矢量数据进行风格化实在是得心应手,同样的对于栅格图像也能进行风格化!以前没接触过,今天正好需要,做出了栅格图像的渲染!下面实现的思路: 1.定义渲染的一系列接口 2.判断图像是否建 ...
- PL/SQL 如何导出INSERT语句
需要把查询出来的数据导出成Insert的语句.忽然发现不会用了. 上网查,找到一些,但都不尽如人意. 于是就写了这篇文章.助人助己. 在PL/SQL Developer左边的树状导航栏里,找到[Tab ...
- Android Touch(1)事件的传递流程(*)
1,Activity,ViewGroup,View的关系 2,触摸事件 3,传递事件时的重要函数 4,事件传递流程参考图 5,其它参考资料 1,Activity,ViewGroup,View的关系 本 ...