hdu 1087 最大上升子序列的和(dp或线段树)
Super Jumping! Jumping! Jumping!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23328 Accepted Submission(s): 10266
The game can be played by two or more than two players. It consists of a chessboard(棋盘)and some chessmen(棋子), and all chessmen are marked by a positive integer or “start” or “end”. The player starts from start-point and must jumps into end-point finally. In the course of jumping, the player will visit the chessmen in the path, but everyone must jumps from one chessman to another absolutely bigger (you can assume start-point is a minimum and end-point is a maximum.). And all players cannot go backwards. One jumping can go from a chessman to next, also can go across many chessmen, and even you can straightly get to end-point from start-point. Of course you get zero point in this situation. A player is a winner if and only if he can get a bigger score according to his jumping solution. Note that your score comes from the sum of value on the chessmen in you jumping path.
Your task is to output the maximum value according to the given chessmen list.
N value_1 value_2 …value_N
It is guarantied that N is not more than 1000 and all value_i are in the range of 32-int.
A test case starting with 0 terminates the input and this test case is not to be processed.
/*
时间复杂度O(n^2)
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; typedef __int64 LL;
const int maxn=;
LL dp[maxn],f[maxn];
inline int max(int a,int b){return a>b?a:b;}
int main()
{
int n,i,j;
while(scanf("%d",&n),n)
{
for(i=;i<=n;i++)
{
scanf("%d",f+i);dp[i]=f[i];
}
for(i=;i<=n;i++)
{
for(j=i-;j>;j--)
if(f[i]>f[j])
dp[i]=max(dp[i],dp[j]+f[i]);
}
LL ans=;
for(i=;i<=n;i++)
ans=max(ans,dp[i]);
printf("%I64d\n",ans);
}
return ;
}
/*
时间复杂度O(nlogn)
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int maxn = ;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
int sum[maxn<<];
int a[maxn],b[maxn],c[maxn];
inline int max(int a,int b){return a> b?a:b;} void swap(int &a,int &b){int t=a;a=b;b=t;} void qsort(int l,int r)
{
if(l<r)
{
int t=b[l],i=l,j=r;
while(i!=j)
{
while(b[j]>=t && i<j) j--;
while(b[i]<=t && i<j) i++;
if(i<j) swap(b[i],b[j]);
}
b[l]=b[i];b[i]=t;
qsort(l,i-);
qsort(i+,r);
}
}
int binarysearch(int l,int r,int val)//二分查找,未找到返回-1
{
int mid,ans=-;
while(l<=r)
{
mid=(l+r)>>;
if(val>c[mid]) l=mid+;
else if(val==c[mid]) return mid;
else r=mid-;
}
return ans;
} void build(int l,int r,int rt)
{
sum[rt] = ;
if(l == r)
return ;
int mid = (l + r)>>;
build(lson);
build(rson);
}
void updata(int pos,int v,int l,int r,int rt)
{
if(l == r)
{
sum[rt]=v;
return ;
}
int mid = (l + r)>>;
if(pos <= mid)
updata(pos,v,lson);
else
updata(pos,v,rson);
sum[rt]=(sum[rt<<]>sum[rt<<|]?sum[rt<<]:sum[rt<<|]);
}
int query(int L,int R,int l,int r,int rt)
{
if(L <= l && R >= r)
{
return sum[rt];
}
int mid = (l + r)>>;
int ans;
if(L <= mid)
ans = query(L,R,lson);
if(R > mid)
ans = max(ans,query(L,R,rson));
return ans;
} int main()
{
int n,i;
while(scanf("%d",&n),n)
{
for(i=;i<=n;i++)
{
scanf("%d",a+i);b[i]=a[i];
}
qsort(,n);
int cnt=;c[]=b[];
for(i=;i<=n;i++) if(b[i]!=b[i-])//离散化去重
c[++cnt]=b[i];
build(,cnt,);
int x,maxv,ans=;
for(i=;i<=n;i++)
{
x=binarysearch(,cnt,a[i]);
if(x>)
{
maxv=query(,x-,,cnt,);
updata(x,maxv+a[i],,cnt,);
}
else
{
maxv=;
updata(x,maxv+a[i],,cnt,);
}
if(maxv+a[i]>ans) ans=maxv+a[i];
}
printf("%d\n",ans);
}
return ;
}
hdu 1087 最大上升子序列的和(dp或线段树)的更多相关文章
- HDOJ/HDU 1087 Super Jumping! Jumping! Jumping!(经典DP~)
Problem Description Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!&quo ...
- HDU 1087 最大上升子序列的和
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- HDU 4521 小明系列问题——小明序列 (线段树维护DP)
题目地址:HDU 4521 基本思路是DP.找前面数的最大值时能够用线段树来维护节省时间. 因为间隔要大于d. 所以能够用一个队列来延迟更新,来保证每次询问到的都是d个之前的. 代码例如以下: #in ...
- hdu 4521 小明系列问题——小明序列(线段树+DP或扩展成经典的LIS)
小明系列问题--小明序列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Tot ...
- AGC028E High Elements 贪心、DP、线段树
传送门 看到要求"字典序最小"的方案,一个很直观的想法是按位贪心,那么我们需要check的就是当某一个数放在了第一个序列之后是否还存在方案. 假设当前两个序列的最大值和前缀最值数量 ...
- hdu 4521 小明系列问题——小明序列 线段树
题意: 给你一个长度为n的序列v,你需要输出最长上升子序列,且要保证你选的两个相邻元素之间在原数组中的位置之差大于d 题解: 这个就是原来求最长上升子序列的加强版,这个思路和最长上升子序列的差不多 ...
- HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...
- Codeforces 675E Trains and Statistic(DP + 贪心 + 线段树)
题目大概说有n(<=10W)个车站,每个车站i卖到车站i+1...a[i]的票,p[i][j]表示从车站i到车站j所需买的最少车票数,求所有的p[i][j](i<j)的和. 好难,不会写. ...
- 【HDU4630 No Pain No Game】 dp思想+线段树的离线操作
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4630 题意:给你n个数据范围在[1,n]中的数,m个操作,每个操作一个询问[L,R],让你求区间[L, ...
- HDU 4819 Mosaic(13年长春现场 二维线段树)
HDU 4819 Mosaic 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4819 题意:给定一个n*n的矩阵,每次给定一个子矩阵区域(x,y,l) ...
随机推荐
- LeetCode 53题 最大子序和 -- JavaScript
解题思路分析: 该题是在一个整数数组中找到一个和最大的连续子数组,并返回和值.那么如何找到一个和最大的连续子数组呢?我们知道,这肯定需要遍历数组才行:好,那我们就开始遍历数组.首先,我们初始化最大和 ...
- ps基础入门快捷方法总结
1. 快速打开文件 双击Photoshop的背景空白处(默认为灰色显示区域)即可打开选择文件的浏览窗口. 2. 随意更换画布颜色 选择油漆桶工具并按住Shift点击画布边缘,即可设置画布底色为当前选择 ...
- 在无TNS配置时,登录到数据库。
sqlplus user/pw@ip:port/servicename sqlplus user/pwd@tnsname sqlplus user/pwd---aix sqlplus /nolog&g ...
- IOS中将颜色转换为image
- (UIImage *)createImageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f ...
- zabbix3.2安装手册
Alexei Vladishev创建了Zabbix项目,当前处于活跃开发状态,Zabbix SIA提供支持. Zabbix是一个企业级的.开源的.分布式的监控套件 Zabbix可以监控网络和服务的监控 ...
- Java-JFrame-swing嵌套浏览器步骤
Java-JFrame-swing嵌套浏览器步骤 一.使用swing嵌套浏览器要实现的功能: 通过java的swing实现在一个窗体中嵌套一个浏览器,可以在这个浏览器中将另一个项目的内容显示出来,只需 ...
- java的面向对象 (2013-09-30-163写的日志迁移
1)面向对象的特征 1. 抽象:(从java方面来说抽象大多数人还是把它作为java中的一种特征来对待) 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象包括 ...
- 18.Yii2.0框架模型修改记录 和 修改点击量
目录 修改数据 修改点击量 修改数据 上面要 use app\models\Article; //修改 //http://yii.com/?r=home/Edit public function ac ...
- Python基础——异常
捕捉所有异常 for i in range(10): try: input_number=input('write a number') if input_number=='q': break res ...
- Python: simple drawings
import cv2; # OpenCV Python import numbers; import numpy as np; import math; import matplotlib; impo ...