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 ;
}
最新文章
- Web Js 按键事件……Enter提交事件 Enter Js事件
- Servlet高级
- SAP/SD - 做SD你要知道的透明表
- Linux: Set OR Change The Library Path
- 分布式消息系统Kafka初步
- RSA (cryptosystem)
- Chapter 2 Open Book——12
- 详解Java的自动装箱与拆箱(Autoboxing and unboxing)
- MYSQL数据库学习九 数据的操作
- java中的异常类型以及区别????
- ETCD相关介绍--整体概念及原理方面
- JavaScript 深拷贝(deep copy)和浅拷贝(shallow copy)
- excel合并
- Map集合转成json数据
- Java开发环境配置(4)--Maven安装 环境变量配置,本地仓库配置---插件安装
- freeswitch源码安装
- [转]jQuery选择器 (详解)
- SQLserver数据库连接问题
- oracle_存储过程小记
- Python Web学习笔记之TCP、UDP、ICMP、IGMP的解释和区别