POJ 1952 BUY LOW, BUY LOWER DP记录数据
最长递减子序列。加记录有多少个最长递减子序列。然后须要去重。
最麻烦的就是去重了。
主要的思路就是:全面出现反复的值,然后还是同样长度的子序列。这里的DP记录的子序列是以当前值为结尾的时候,而且一定选择这个值的最长递减子序列。 那么就须要减去前面已经出现过了的子序列。
有点绕口。
举例就是9 8 9 8 2 和 10 5 12 5 3;这些样例去重。
本类型的题目假设不用记录数据是能够使用O(nlgn)的算法的,只是临时不知道怎样记录数据。故此这里仅仅使用DP了。
#include <stdio.h>
#include <vector>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <limits.h>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
const int MAX_N = 5001;
int arr[MAX_N], N, tbl[MAX_N], C[MAX_N]; void getLongest(int &len, int &n)
{
memset(tbl, 0, sizeof(int) * (N+1));
memset(C, 0, sizeof(int) * (N+1));
tbl[0] = 1; C[0] = 1;
for (int i = 1; i < N; i++)
{
tbl[i] = 1;
for (int j = 0; j < i; j++)
{
if (tbl[j] == -1) continue;
if (arr[j] > arr[i] && tbl[i] < tbl[j]+1)
{
tbl[i] = tbl[j]+1;
}
}
for (int j = 0; j < i; j++)
{
if (arr[j] > arr[i] && tbl[i] == tbl[j]+1)
{
C[i] += C[j];
}
}
if (C[i] == 0) C[i] = 1;//递增的时候
/*能够不用以下这段代码
for (int j = 0; j < i; j++)
{
if (arr[i] == arr[j] && tbl[i] == tbl[j] && C[i] == C[j])
{
tbl[i] = -1;
break;
}//去掉同样的数据 9 8 9 8
}
if (tbl[i] == -1) continue;*/ for (int j = 0; j < i; j++)
{
if (arr[j] == arr[i] && tbl[j] == tbl[i]) C[i] -= C[j];
}//特例:6 5 7 5 3 须要去掉前后5反复的地方
}
len = INT_MIN;
for (int i = 0; i < N; i++)
{
len = max(len, tbl[i]);
}
n = 0;
for (int i = 0; i < N; i++)
{
if (tbl[i] == len) n += C[i];
}
} int main()
{
while (scanf("%d", &N) != EOF)
{
for (int i = 0; i < N; i++)
{
scanf("%d", arr + i);
}
int len, n;
getLongest(len, n);
printf("%d %d\n", len, n);
}
return 0;
}
POJ 1952 BUY LOW, BUY LOWER DP记录数据的更多相关文章
- POJ 1952 BUY LOW, BUY LOWER 动态规划题解
Description The advice to "buy low" is half the formula to success in the bovine stock mar ...
- POJ-1952 BUY LOW, BUY LOWER(线性DP)
BUY LOW, BUY LOWER Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9244 Accepted: 3226 De ...
- poj1952 BUY LOW, BUY LOWER【线性DP】【输出方案数】
BUY LOW, BUY LOWER Time Limit: 1000MS Memory Limit: 30000K Total Submissions:11148 Accepted: 392 ...
- USACO Section 4.3 Buy low,Buy lower(LIS)
第一眼看到题目,感觉水水的,不就是最长下降子序列嘛!然后写……就呵呵了..要判重,还要高精度……判重我是在计算中加入各种判断.这道题比看上去麻烦一点,但其实还好吧.. #include<cstd ...
- 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 ...
- 洛谷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 ...
随机推荐
- 52. nodejs报错:Cannot find module 'ejs'
转自:https://blog.csdn.net/u010142437/article/details/79012605 错误显示: Error: Cannot find module 'ejs' ...
- web api 特点
webapi有很多特点(我不想用优点这个词),比如说restful,支持路由,简单,类似mvc controller/action的代码编写方式,灵活的托管方式,和web的集成等等. Web API的 ...
- 亲测有效的解决在vue cli@3 create 命令执行后 node-sass无法安装上的问题
在使用Vue cli@3 搭建工程手脚架的过程中.当我们选择了采用 sass 处理 css 编译.在使用vue create test 命令行执行到最后.会到以下这步:然后开始报错: Download ...
- [Angular & Unit Testing] Testing Component with Store
When using Ngrx, we need to know how to test the component which has Router injected. Component: imp ...
- 缩放文本框ExpandTextView
效果图: 代码: import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import ...
- node----ajax请求太大报错------解决方法
//----分析主体程序var bodyParser = require(‘body-parser‘); app.use(bodyParser.json({limit: ‘50mb‘})); app. ...
- HASH Partitioning--转载
原文地址:https://dev.mysql.com/doc/refman/5.1/en/partitioning-hash.html HASH Partitioning [+/-] 18.2.3.1 ...
- 如何使用Linux套接字?
我们知道许多应用程序,例如E-mail.Web和即时通信都依靠网络才能实现.这些应用程序中的每一个都依赖一种特定的网络协议,但每个协议都使用相同的常规网络传输方法.许多人都没有意识到网络协 ...
- Spring异步执行(@Async)2点注意事项
Spring中可以异步执行代码,注解方式是使用@Async注解. 原理.怎么使用,就不说了. 写2点自己遇到过的问题. 1.方法是公有的 // 通知归属人 @Async public void not ...
- Stack switching mechanism in a computer system
A method and mechanism for performing an unconditional stack switch in a processor. A processor incl ...