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) ...
随机推荐
- k8s1.13.0二进制部署-master节点(三)
部署apiserver 创建生成CSR的JSON配置文件 [root@k8s-master1 ssl]# vim kubernetes-csr.json { "CN": " ...
- 作业题:输出单个字符 输入单个字符 scanf printf
输出单个字符用putchar() #include <iostream> using namespace std; int main(){ char x='B'; char y='O'; ...
- centOS下lnamp安装
首先安装apache,mysql ,最后安装php 1>apache安装 安装:yum install -y httpd 运行:/bin/systemctl start httpd.servic ...
- jenkins 全局工具配置
- MySQL中CONCAT()的用法
MySQL中CONCAT()的用法 在日常开发过程中,特别是在书写接口的时候,经常会遇到字符串拼接的情况,比如在返回图片数据时,数据库里往往存储的是相对路径,而接口里一般是存放绝对地址,这就需要字符串 ...
- PHP做ERP, CRM, CMS系统需要注意哪些地方
php作为二次开发弱类型语言, 可读性, 可视度都是比较高的. 在很多人眼里, 也许php只能做一些web应用开发, 比如某个公司的网站, 某个公司的网站后台, 其实,我可以告诉大家, php不比任何 ...
- Python基础——时间
导入时间模块 import time 时间戳 print(time.time()) 获取本地时间 print(time.localtime(time.time())) 时间显示格式化 print(ti ...
- AJAX小练习
/index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pa ...
- 笔记-python-centos环境下安装配置
笔记-python-centos环境下安装配置 1. 准备工作 环境准备 centos6.5 mini,已有python 2.6.6 下载源码包 Python官网下载Gzipped sour ...
- 《鸟哥的Linux私房菜》学习笔记(1)——文件与目录
在Linux中,任何设备都是文件,不仅如此,连数据通信的接口也有专门的文件负责.可以说,一切皆文件,目录也是一种文件,是路径映射.因此,文件系统是Linux的基础. 一.文件与目录管理命令 1.ls( ...