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 ...
随机推荐
- elasticsearch index 功能源码概述
从本篇开始,对elasticsearch的介绍将进入数据功能部分(index),这一部分包括索引的创建,管理,数据索引及搜索等相关功能.对于这一部分的介绍,首先对各个功能模块的分析,然后详细分析数据索 ...
- Lua 是一个小巧的脚本语言
Redis进阶实践之七Redis和Lua初步整合使用 一.引言 Redis学了一段时间了,基本的东西都没问题了.从今天开始讲写一些redis和lua脚本的相关的东西,lua这个脚本是一个好东西,可以运 ...
- window cmd 命令大全 (order)
Windows CMD命令大全 命令简介 cmd是command的缩写.即命令行 . 运行操作 CMD命令:开始->运行->键入cmd或command(在命令行里可以看到系统版本.文件系统 ...
- mount 命令
命令格式:mount [-t vfstype] [-o options] device dir 嵌入式设备挂载命令mount -o nolock -t nfs 192.168.1.24:/home/t ...
- vue 遇到的报错
[Vue warn]: Invalid default value for prop "dataParams": Props with type Object/Array must ...
- Android Cordova 插件开发之编写自己定义插件
前言 本文适合Android+web的复合型人才,由于cordova本身就是混合开发,所以在Android开发的基础上,还要懂web相关技术(HTML+CSS+JS).可是也有例外,比方我.仅仅需负责 ...
- jquery js解析函数、函数直接调用
----------------------------------------------------------------- cc = function(){alert(345)}, pushS ...
- Android学习笔记进阶17之LinearGradient
具体的看一下博文:Android学习笔记进阶15之Shader渲染 package xiaosi.BitmapShader; import android.app.Activity; import a ...
- (转)修改 ubuntu 默认启动项
转自: http://jingyan.baidu.com/article/afd8f4de58959134e386e969.html 当我们安装windows和ubuntu双系统以后,默认启动变成ub ...
- 洛谷 P1781 宇宙总统
P1781 宇宙总统 题目背景 宇宙总统竞选 题目描述 地球历公元6036年,全宇宙准备竞选一个最贤能的人当总统,共有n个非凡拔尖的人竞选总统,现在票数已经统计完毕,请你算出谁能够当上总统. 输入输出 ...