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

Problem Description
Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now.

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.

 
Input
Input contains multiple test cases. Each test case is described in a line as follow:
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.
 
Output
For each case, print the maximum according to rules, and one line one case.
 
Sample Input
3 1 3 2
4 1 2 3 4
4 3 3 2 1
0
 
Sample Output
4
10
3
 /*
时间复杂度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或线段树)的更多相关文章

  1. HDOJ/HDU 1087 Super Jumping! Jumping! Jumping!(经典DP~)

    Problem Description Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!&quo ...

  2. HDU 1087 最大上升子序列的和

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  3. HDU 4521 小明系列问题——小明序列 (线段树维护DP)

    题目地址:HDU 4521 基本思路是DP.找前面数的最大值时能够用线段树来维护节省时间. 因为间隔要大于d. 所以能够用一个队列来延迟更新,来保证每次询问到的都是d个之前的. 代码例如以下: #in ...

  4. hdu 4521 小明系列问题——小明序列(线段树+DP或扩展成经典的LIS)

    小明系列问题--小明序列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Tot ...

  5. AGC028E High Elements 贪心、DP、线段树

    传送门 看到要求"字典序最小"的方案,一个很直观的想法是按位贪心,那么我们需要check的就是当某一个数放在了第一个序列之后是否还存在方案. 假设当前两个序列的最大值和前缀最值数量 ...

  6. hdu 4521 小明系列问题——小明序列 线段树

    题意: 给你一个长度为n的序列v,你需要输出最长上升子序列,且要保证你选的两个相邻元素之间在原数组中的位置之差大于d 题解: 这个就是原来求最长上升子序列的加强版,这个思路和最长上升子序列的差不多   ...

  7. HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...

  8. Codeforces 675E Trains and Statistic(DP + 贪心 + 线段树)

    题目大概说有n(<=10W)个车站,每个车站i卖到车站i+1...a[i]的票,p[i][j]表示从车站i到车站j所需买的最少车票数,求所有的p[i][j](i<j)的和. 好难,不会写. ...

  9. 【HDU4630 No Pain No Game】 dp思想+线段树的离线操作

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4630 题意:给你n个数据范围在[1,n]中的数,m个操作,每个操作一个询问[L,R],让你求区间[L, ...

  10. HDU 4819 Mosaic(13年长春现场 二维线段树)

    HDU 4819 Mosaic 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4819 题意:给定一个n*n的矩阵,每次给定一个子矩阵区域(x,y,l) ...

随机推荐

  1. linux - 权限解析

    当你在linux下用命令ll 或者ls -la的时候会看到这些字眼,这些字眼表示为不同用户组的权限:r:read就是读权限 --数字4表示w:write就是写权限 --数字2表示 x:excute就是 ...

  2. c#和Java中的多态

    多态:让一个对象表现出多种类型,写出通用的代码,最大限度的屏蔽各个子类之间的差异性. c#举例: 将父类的方法标记为虚方法 ,使用关键字 virtual,这个函数可以被子类重新写一个遍. //真的鸭子 ...

  3. SSH程序框架之Spring与HIbernate整合

    spring整合hibernate 有两种方式 1.注解方式 2.xml方式实现 Spring整合Hibernate有什么好处? 1.由IOC容器来管理Hibernate的SessionFactory ...

  4. shell脚本,awk取中间列的方法。

    解释 1.$(int(NF/2)+1) 中int(NF/2)等于3,然后加1,就得到中间的4了. 2.$(NF/2+0.5) 相当于得出的是整数.NF/2是3.5,再由3.5+0.5,所以就是4了,也 ...

  5. Noip 训练指南

    目录 Noip 训练指南 图论 数据结构 位运算 期望 题解 Noip 训练指南 目前完成 \(4 / 72\) 图论 [ ] 跳楼机 [ ] 墨墨的等式 [ ] 最优贸易 [ ] 泥泞的道路 [ ] ...

  6. shell 练习 - 第七周

    1. 用shell实现传入进程pid, 查看对应进程/proc下CPU.内存指标 #!/bin/bash read -p "Input PID Value: " pid pid_e ...

  7. Flask-基本原理与核心知识

    虚拟环境 使用pipenv创建一个虚拟环境和项目绑定,安装:E:\py\qiyue\flask>python3 -m pip install pipenv 和项目绑定:到项目的目录中pipenv ...

  8. 基于网站地址URL传输session信息

    在php的学习中,会话是我们常常用到的,那今天我们就来详细讲讲会话中的session: 一.session的工作机制:当开启session后,服务器会在服务器中保存session文件,然后再浏览器保存 ...

  9. my购物车

    sum=0 a=input("请输入“水果”或“衣服”:") if a=="手机": while True: shop = { '蓝葡萄', '水蜜桃', '草 ...

  10. php三种方式操作mysql数据库

    php可以通过三种方式操作数据库,分别用mysql扩展库,mysqli扩展库,和mysqli的预处理模式分别举案例加以说明 1.通过mysql方式操作数据库 工具类核心代码: <?php cla ...