hdu 5064 Find Sequence(单调性优化DP)
来源:互联网 发布:python isupper函数 编辑:程序博客网 时间:2024/05/29 19:16
hdu 5064 Find Sequence
一开始设想先排序然后两两相减并找出LIS来得到结果,显然没有考虑周全
根据题解,这题需要用到单调性DP。DP[i][j]表示序列以下标为i和j结尾,转移方程为
DP[i][j] = max(DP[k][i] + 1) { k<=i && num[j] - num[i] >= num[i] - num[k] }但是直接求解时间复杂度为O(n^3)。该状态方程具有特殊性质,即
若 k 满足 { k<=i && num[j] - num[i] >= num[i] - num[k] } , 则 k 在 j+1 的情况下也满足。所以当计算 j+1 时,k 可直接从上一状态下继续进行。#include <stdio.h>#include <vector>#include <string>#include <algorithm>#include <queue>#include <cstring>#include <map>#include <set>#include <iostream>#include <cmath>using namespace std;#ifdef __GNUC__typedef long long LL;inline void opt64(LL a){ printf("%lld", a);}#elsetypedef __int64 LL;inline void opt64(LL a){ printf("%I64d", a);}#endifconst int MAXN = (1<<22)+5;const int MAXM = 2050;const double eps = 1e-10;const double PI = acos(-1.0);inline int cmp(const double x, const double y){ return ((x-y)>eps) - ((x-y)<-eps);}inline int cmp(const double x){ return ((x)>eps) - ((x)<-eps);}int n, m, mp[MAXN], mq[MAXM], cnt;int dp[MAXM][MAXM];int solve(){ int res = -1; if (n < 3) return n; sort(mp, mp+n); cnt = 0; mp[n] = -1; for (int i = 0, tp = 0; i< n; ++i) { tp++; if (mp[i] != mp[i+1]) { mq[cnt] = mp[i], dp[cnt][cnt] = tp, res = max(res,tp), tp = 0, ++cnt; } } for (int i = 0; i< cnt; ++i) { int k = i, ans = dp[i][i]; for (int j = i+1; j< cnt; ++j) { for (; k>=0 && mq[j]-mq[i]>=mq[i]-mq[k]; --k) { ans = max(ans, dp[k][i]); } dp[i][j] = ans+1; res = max(res, ans+1); } } return res;}int main(){#ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin);#endif int t; scanf("%d", &t); while (t--) { scanf("%d%d", &n, &m); for (int i = 0; i< n; ++i) scanf("%d", mp+i); printf("%d\n", solve()); } return 0;}
0 0
- hdu 5064 Find Sequence(单调性优化DP)
- hdu 5064 Find Sequence(DP,单调性优化)
- hdu5064 Find Sequence 单调性dp
- hdu Lawrence(DP-单调性优化-斜率优化)
- HDU-5064-Find Sequence(DP)
- poj 3709 K-Anonymous Sequence(DP-单调性优化-斜率优化)
- Hdu 5064 Find Sequence(dp)
- poj 1180 Batch Scheduling(DP-单调性优化)
- [NOI2009]诗人小G(决策单调性优化dp)
- hdu 4412 Sky Soldiers (区间dp 单调性)
- bzoj4709 -- 决策单调性优化DP
- bzoj2369 && 2687 -- 决策单调性优化DP
- 【单调性dp】poj 3017 Cut the Sequence
- BZOJ1044 [HAOI2008]木棍分割(二分答案/单调性优化dp+递推优化)
- DP的单调性优化和斜率优化
- 【BZOJ1911】【Apio2010】特别行动队,斜率优化DP裸题(斜率有单调性)
- HNOI 2008 玩具装箱 (BZOJ 1010)进一步学习 →.→ DP 单调性优化
- [BZOJ1010][HNOI2008]玩具装箱(决策单调性/斜率优化DP)
- 代码的运行顺序总结
- PROC集计算
- WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping。请添加一个名为 jquery (区分大小写)的 ScriptRes
- 目标黑马,我的自学之路:第七章 字符串和字符串缓冲区
- 说说Python程序的执行过程
- hdu 5064 Find Sequence(单调性优化DP)
- 从TS流到PAT和PMT
- 数据科学家面试常见的77个问题
- 编程之美2.15 二维数组最大子数组的和(数组下标从(1,1)开始)
- C文件读写函数介绍
- LeetCode:Reverse Integer
- spring3 mvc与struts2比较
- Windows常用键
- 函数的末尾