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 ...
随机推荐
- Scott Hanselman的问题-3
.Net程序员面试 中级篇 (回答Scott Hanselman的问题) 继<.Net 程序员面试 C# 语言篇 (回答Scott Hanselman的问题)>跟<.Net程序员 ...
- notepad++go语法高亮文件
notepad++go语法高亮文件 下载 右键另存为下载后在语言栏中的自定义面板中直接导入,重启即可
- Deep Networks : Overview
Overview In the previous sections, you constructed a 3-layer neural network comprising an input, hid ...
- 小程序block总结
小程序block总结 1.block并不是一个组件,它仅仅是一个包装元素,不会在页面中做任何渲染,只接受控制属性. 2.因为 wx:if 是一个控制属性,需要将它添加到一个标签上.如果要一次性判断多个 ...
- jersey+jetty实现文件上传
服务配置与启动类 import org.glassfish.jersey.servlet.ServletContainer; import javax.ws.rs.Path; import org.e ...
- [lougu1341]无序字母对
Description: 给定n个各不相同的无序字母对(区分大小写,无序即字母对中的两个字母可以位置颠倒).请构造一个有n+1个字母的字符串使得每个字母对都在这个字符串中出现. Solution: 欧 ...
- CodeForces 159c String Manipulation 1.0
String Manipulation 1.0 Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on Cod ...
- UVALive 5583 Dividing coins
Dividing coins Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Ori ...
- iOS - 系统经常使用框架(framework)的简介
系统框架(framework)的简介 ImageIO - 该框架的接口可用于导入或导出图像数据及图像元数据 CoreTelephony - 获取IMSI号,SIM卡背面的号码是SIM卡的电子串号, ...
- 使用框架的php假设使用定时服务Cronjob
工作须要用php开发了个监控的小程序,既然是监控就须要定时运行. 之前我用的是chrome加个定时刷新的小插件,放在server上执行.也能实现,就是别扭. 通用正规的做法应该是:linux上的Cro ...