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 ...
随机推荐
- git 版本管理工具说明
$ git init (初始化本地仓库,会生成.git 文件夹 .git 文件夹里存储了所有的版本信息.标记等内容) $ git add . ...
- C# 返回分页查询的总页数
/// <summary> /// 返回分页查询操作的的总页数 /// </summary> /// <param name="count">总 ...
- 初学h5须知
9.41.浏览器是页面的环境(类似水是鱼的环境)2.浏览器结构:title 标题,题目 URL 网址 ...
- 玩转阿里云server——安装WebserverTomcat7
1. 以root用户身份登录阿里云server 2. 使用apt-get install安装Tomcat7 sudo apt-get install tomcat7 3.安装后.Tomcat在启动时报 ...
- badblocks 检查硬盘是否有坏道
硬盘是比較easy坏掉的设备,使用一段时间后可能会出现坏道等物理故障. 当硬盘出现坏道后,若不及时更换或者进行技术上的处理,磁盘的坏道就会越来越多,并会造成频繁死机和数据丢失. 最好的处理方法是更换新 ...
- AIX中经常使用的SMIT 的使用
AIX中经常使用的SMIT 的使用 1. smit 的日志文件 (1)$HOME/smit.log 记录了所訪问的全部菜单.对话内容,所运行的命令和输出结果 在 SMIT 会话中出现的全部 ...
- oracle 10g standby database 实时应用 redo 数据
-------physical standby database: real-time apply 须要配置 standby redo log: 启用实时应用, 日志应用服务会直接应用接收的redo ...
- leetcode -day29 Binary Tree Inorder Traversal & Restore IP Addresses
1. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' ...
- 使用框架的php假设使用定时服务Cronjob
工作须要用php开发了个监控的小程序,既然是监控就须要定时运行. 之前我用的是chrome加个定时刷新的小插件,放在server上执行.也能实现,就是别扭. 通用正规的做法应该是:linux上的Cro ...
- php课程 13-43 mysql的数据结构是什么
php课程 13-43 mysql的数据结构是什么 一.总结 一句话总结:cs结构,客户端,服务器 1.常用的比较出名的数据库有哪些? SQL数据库(关系型):1.收费:DB2SqlserverOra ...