$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的更多相关文章

  1. POJ 1952 BUY LOW, BUY LOWER 动态规划题解

    Description The advice to "buy low" is half the formula to success in the bovine stock mar ...

  2. USACO Section 4.3 Buy low,Buy lower(LIS)

    第一眼看到题目,感觉水水的,不就是最长下降子序列嘛!然后写……就呵呵了..要判重,还要高精度……判重我是在计算中加入各种判断.这道题比看上去麻烦一点,但其实还好吧.. #include<cstd ...

  3. POJ-1952 BUY LOW, BUY LOWER(线性DP)

    BUY LOW, BUY LOWER Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9244 Accepted: 3226 De ...

  4. 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 ...

  5. poj1952 BUY LOW, BUY LOWER【线性DP】【输出方案数】

    BUY LOW, BUY LOWER Time Limit: 1000MS   Memory Limit: 30000K Total Submissions:11148   Accepted: 392 ...

  6. 洛谷P2687 [USACO4.3]逢低吸纳Buy Low, Buy Lower

    P2687 [USACO4.3]逢低吸纳Buy Low, Buy Lower 题目描述 “逢低吸纳”是炒股的一条成功秘诀.如果你想成为一个成功的投资者,就要遵守这条秘诀: "逢低吸纳,越低越 ...

  7. [POJ1952]BUY LOW, BUY LOWER

    题目描述 Description The advice to "buy low" is half the formula to success in the bovine stoc ...

  8. Buy Low, Buy Lower

    Buy Low, Buy Lower 给出一个长度为N序列\(\{a_i\}\),询问最长的严格下降子序列,以及这样的序列的个数,\(1 <= N <= 5000\). 解 显然我们可以很 ...

  9. BUY LOW, BUY LOWER_最长下降子序列

    Description The advice to "buy low" is half the formula to success in the bovine stock mar ...

随机推荐

  1. UIWebView的探索

    UIWebView 说到iOS的UIWebView,应该会很快回忆起常用委托方法,异步loadRequest.stopLoading.reload方法等. 在此我总结一些容易忽略的属性和方法: 1.  ...

  2. jquery上传控件uploadify使用备忘

    我简单修改了js和样式,效果如下 使用起来也是超简单,将文件下载并解压到你网站目录下,然后 .在使用位置插入代码 ============================= <iframe wi ...

  3. Bootstrap3.0学习第六轮(表单)

    Bootstrap3.0学习第六轮(表单) 前言 阅读之前您也可以到Bootstrap3.0入门学习系列导航中进行查看http://www.cnblogs.com/aehyok/p/3404867.h ...

  4. JVM内存划分

    JVM内存划分吗? 前言: 大家都知道虚拟机,都知道JVM,其实这些都是基于sun公司[oracle公司]的HotSpot虚拟机,当然本篇博文也是以sun公司为基础.还有其他的虚拟机,常见的就有JRo ...

  5. Private和Protected方法

    .NET中如何测试Private和Protected方法   TDD是1)写测试2)写通过这些测试的代码,3)然后重构的实践.在,NET社区中, 这个概念逐渐变得非常流行,这归功于它所增加的质量保证. ...

  6. SharePoint开发

    做SharePoint开发有时候是一件比较痛苦的事情,毕竟庞大的框架总是笨重的~~ 往往如果采取传统的方式开发SharePoint的话,更改一个代码需要有以下操作: 1)更改代码 2)VS编译——&g ...

  7. [笔记] OS X and iOS 内核开发

    一.KEXT包的安全性说明 KEXT 程序包及其包含的所有文件及文件夹必须属于 root 用户(用户 id 是 0) KEXT 程序包及其包含的所有文件及文件夹必须属于 wheel 组(组 id 是 ...

  8. 运用Unity结合PolicyInjection实现拦截器

    运用Unity结合PolicyInjection实现拦截器[结合操作日志实例] 上一篇文章我们通过Unity自身Unity.InterceptionExtension.IInterceptionBeh ...

  9. Linux Wine with *.bat *.exe ( Photoshop and etc.. )

    Firtly all you need is to install wine on your computer. Mine is ubuntu 12.04 which is running KDE o ...

  10. TensorFlow 入门之手写识别(MNIST) softmax算法

    TensorFlow 入门之手写识别(MNIST) softmax算法 MNIST flyu6 softmax回归 softmax回归算法 TensorFlow实现softmax softmax回归算 ...