POJ 1952 BUY LOW, BUY LOWER
$dp$。
一开始想了一个$dp$做法,$dp[i][j]$表示前$i$个数字,下降序列长度为$j$的方案数为$dp[i][j]$,这样做需要先离散化然后用树状数组优化,空间复杂度为${n^2}$,时间复杂度为$O({n^2}\log n)$,这样的做法被$POJ$卡了内存。既然是$MLE$,然后我去$discuss$测了一下数据,发现答案都是对的。
$MLE$:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
} const int maxn=;
int f[maxn][maxn],h[maxn][maxn],n,a[maxn],b[maxn];
int c[maxn][maxn];
int pre[(<<)+]; int get(int x)
{
int L=,R=n,res;
while(L<=R)
{
int mid=(L+R)/;
if(b[mid]<x) L=mid+;
else if(b[mid]==x) res=mid,L=mid+;
else R=mid-;
}
return res;
} int lowbit(int x){return x&(-x);} int sum(int p,int x)
{
int res=;
for(int i=x;i>;i=i-lowbit(i)) res=res+c[p][i];
return res;
} void update(int p,int x,int val)
{
for(int i=x;i<=;i=i+lowbit(i)) c[p][i]=c[p][i]+val;
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]),b[i]=a[i];
sort(b+,b++n); for(int i=;i<=n;i++) a[i]=get(a[i]); memset(h,,sizeof h); memset(c,,sizeof c);
memset(f,,sizeof f);
memset(pre,,sizeof pre); h[][]=; f[][]=; update(,,); for(int i=;i<=n;i++)
{
for(int j=;j<=i;j++) h[i][j]=sum(j-,)-sum(j-,a[i]);
int p=pre[a[i]];
for(int j=;j<=n;j++) f[i][j]=h[i][j]-h[p][j];
for(int j=;j<=n;j++) update(j,a[i],f[i][j]);
pre[a[i]]=i;
}
for(int j=n;j>=;j--)
{
int ans=;
for(int i=;i<=n;i++) ans=ans+f[i][j];
if(ans==) continue;
else
{
printf("%d %d\n",j,ans);
break;
}
} return ;
}
事实上,上述做法中很多信息都是冗余的,我们只需记录到$i$位置的最长下降序列的长度$f[i]$以及方案数$g[i]$就可以了。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
} const int maxn=;
int n,a[maxn],f[maxn],g[maxn]; int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]); for(int i=;i<=n;i++)
{
f[i]=; g[i]=; bool flag=;
for(int j=i-;j>=;j--)
{
if(a[j]<a[i]) continue;
if(a[j]==a[i])
{
if(flag==) g[i]=;
break;
}
else if(a[j]>a[i])
{
flag=;
if(f[j]+>f[i]) f[i]=f[j]+,g[i]=g[j];
else if(f[j]+==f[i]) g[i]=g[i]+g[j];
}
}
} int ans=; for(int i=;i<=n;i++) ans=max(ans,f[i]);
int ans2=; for(int i=;i<=n;i++) if(f[i]==ans) ans2=ans2+g[i];
printf("%d %d\n",ans,ans2); return ;
}
POJ 1952 BUY LOW, BUY LOWER的更多相关文章
- POJ 1952 BUY LOW, BUY LOWER 动态规划题解
Description The advice to "buy low" is half the formula to success in the bovine stock mar ...
- USACO Section 4.3 Buy low,Buy lower(LIS)
第一眼看到题目,感觉水水的,不就是最长下降子序列嘛!然后写……就呵呵了..要判重,还要高精度……判重我是在计算中加入各种判断.这道题比看上去麻烦一点,但其实还好吧.. #include<cstd ...
- POJ-1952 BUY LOW, BUY LOWER(线性DP)
BUY LOW, BUY LOWER Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9244 Accepted: 3226 De ...
- USACO 4.3 Buy Low, Buy Lower
Buy Low, Buy Lower The advice to "buy low" is half the formula to success in the stock mar ...
- poj1952 BUY LOW, BUY LOWER【线性DP】【输出方案数】
BUY LOW, BUY LOWER Time Limit: 1000MS Memory Limit: 30000K Total Submissions:11148 Accepted: 392 ...
- 洛谷P2687 [USACO4.3]逢低吸纳Buy Low, Buy Lower
P2687 [USACO4.3]逢低吸纳Buy Low, Buy Lower 题目描述 “逢低吸纳”是炒股的一条成功秘诀.如果你想成为一个成功的投资者,就要遵守这条秘诀: "逢低吸纳,越低越 ...
- [POJ1952]BUY LOW, BUY LOWER
题目描述 Description The advice to "buy low" is half the formula to success in the bovine stoc ...
- Buy Low, Buy Lower
Buy Low, Buy Lower 给出一个长度为N序列\(\{a_i\}\),询问最长的严格下降子序列,以及这样的序列的个数,\(1 <= N <= 5000\). 解 显然我们可以很 ...
- BUY LOW, BUY LOWER_最长下降子序列
Description The advice to "buy low" is half the formula to success in the bovine stock mar ...
随机推荐
- 玩下Javascript
玩下Javascript 前言 好久没有更新博客了,也蛮少捣弄javascript,今儿看到一个题目,关于给你一个面板,你可以随意的在上面画矩形,可以移动和删除任意一个你创建的矩形,心血来潮搞着玩哈, ...
- Python之FTP多线程下载文件之多线程分块下载文件
Python之FTP多线程下载文件之多线程分块下载文件 Python中的ftplib模块用于对FTP的相关操作,常见的如下载,上传等.使用python从FTP下载较大的文件时,往往比较耗时,如何提高从 ...
- node.js系列笔记之fs模块《二》
一:感触 最近工作比较忙,感觉也比较多,因为工作上的不顺利,再加上加班比较多,所以最近心情不是很好,再加上英语能力差到不行,所以最近半个月学习进度也比较慢, 但还是告诉自己每天都坚持学一点,即使今天心 ...
- vs2008 试用版评估到期 vs2008试用版 升级正式版
心得: 解决Vs2008 试用版升级正式版 1.在控制面板里面找到vs2008的程序. 2.点击 更改删除按钮, 3.出现Vs2008的维护模式. 4.在此维护模式下,如果没有出现填写正版密匙的地方, ...
- 在C#中模拟Javascript的setTimeout方法
在C#中模拟Javascript的setTimeout方法 背景 每种语言都有自己的定时器(Timer),很多人熟悉Javascript中的setInterval和setTimeout,在Javasc ...
- 三种不同实现初始化和销毁bean之前进行的操作的比较
Spring容器中的bean是有生命周期的,Spring 允许在 Bean 在初始化完成后以及 Bean 销毁前执行特定的操作,常用的设定方式有以下三种: 通过实现 InitializingBean/ ...
- python cookbook学习笔记 第一章 文本(1)
1.1每次处理一个字符(即每次处理一个字符的方式处理字符串) print list('theString') #方法一,转列表 结果:['t', 'h', 'e', 'S', 't', 'r', 'i ...
- 挖一下插件v1.3版本发布
Chrome图片下载插件,支持网页截屏 v.1.3更新说明: 新增屏蔽图片功能,可以将不想看到的图片隐藏 新增屏蔽图片管理选项,可以根据实际的需求取消屏蔽图片 优化操作界面 项目地址:https:// ...
- asp.net中的路由系统
ASP.NET MVC重写了ASP.NET管道HttpModule和处理程序HttpHandler.MVC自定义了MvcHandler实现了Controller的激活和Action的执行.但是在请求到 ...
- cocos2d-x protobuf; cocos2dx protocol buffer
昨天了解到项目要用到protocol buffer,今天晚上看了一下,了解protobuf本质上就是一个信息表达协议+编辑,解析库. linux开源软件都一个模式,先./configure --hel ...