hdu 5064 满足b2−b1≤b3−b2... 的最长子序列
http://acm.hdu.edu.cn/showproblem.php?pid=5064
要找出一个数组中满足b2−b1≤b3−b2≤⋯≤bt−bt−1 的最大的t
直接引题解:
1003 Find Sequence
首先考虑解的结构一定是C1,C1,…,C1,C2,C3,…,Cm这种形式,其中满足C1<C2<C3<…<Cm
所以对a1,a2,a3,…,an去重后从小到大排序得到c1,c2,c3,…,cx其中x是sqrt(M)级别的,用DP[i][j]表示以ci和cj结尾的满足条件的最长序列
首先初值化 DP[i][i]=count(ci) 即ci在原序列中的个数。
而dp[i][j]=max(dp[k][i] 其中k≤i还满足ci−ck≤cj−ci)+1
这样的复杂度是 O(x^3),在题中x最大为1000级别所以会超时,要使用下面优化
因为 dp[i][j]=max(dp[k][i] 其中k≤i还满足ci−ck≤cj−ci)+1
dp[i][j+1]=max(dp[k][i] 其中k≤i还满足ci−ck≤cj+1−ci)+1
注意到cj+1>cj 所以满足ci−ck≤cj−ci的dp[k][i]必然满足ci−ck≤cj+1−ci因而不必重复计算
即最后复杂度可以为O(x^2).
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define clr0(x) memset(x,0,sizeof(x))
typedef long long LL; int n,M;
const double pi = acos ( -1.0 ) ;
const LL modo = 1000000007;
int cnt[4200005],dp[2005][2005],c[2005];
void work()
{
int x;
clr0(cnt);
for(int i = 0;i < n;++i){
RD(x);
cnt[x]++;
}
int m = 0;
for(int i = 1;i <= M;++i)
if(cnt[i]){
c[m++] = i;
}
clr0(dp);
for(int i = 0;i < m;++i){
dp[i][i] = cnt[c[i]];
}
for(int i = 0;i < m;++i){
int k = i,mx = dp[i][i];
for(int j = i+1;j < m;++j){
for(;k >= 0;--k){
if(c[i] - c[k] <= c[j] - c[i]){
mx = max(mx,dp[k][i] + 1);
}
else break;
}
dp[i][j] = mx;
}
}
int ans = 0;
for(int i = 0;i < m;++i)
for(int j = i;j < m;++j)
ans = max(ans,dp[i][j]);
printf("%d\n",ans);
return;
}
int main () {
int T;
RD(T);
while(T--){
RD2(n,M);
work();
}
return 0 ;
}
hdu 5064 满足b2−b1≤b3−b2... 的最长子序列的更多相关文章
- Bestcoder13 1003.Find Sequence(hdu 5064) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5064 题目意思:给出n个数:a1, a2, ..., an,然后需要从中找出一个最长的序列 b1, b ...
- HDU 2254 奥运(矩阵高速幂+二分等比序列求和)
HDU 2254 奥运(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 2254 奥运 题意: 中问题不解释. 分析: 依据floyd的算法,矩阵的k次方表示这个矩阵走了k步. 所以k ...
- A和B是好友,他们经常在空闲时间聊天,A的空闲时间为[a1 ,b1 ],[a2 ,b2 ]..[ap ,bp ]。B的空闲时间是[c1 +t,d1 +t]..[cq +t,dq +t],这里t为B的起床时间。这些时间包括了边界点。B的起床时间为[l,r]的一个时刻。若一个起床时间能使两人在任意时刻聊天,那么这个时间就是合适的,问有多少个合适的起床时间?
// ConsoleApplication5.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<vector> ...
- Codeforces Round #599 (Div. 2) A,B1,B2,C 【待补 D】
排序+暴力 #include<bits/stdc++.h> using namespace std; #define int long long #define N 1005000 int ...
- HDU 1160 DP最长子序列
G - FatMouse's Speed Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- HDU 4604 Deque 最长子序列
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4604 Deque Time Limit: 4000/2000 MS (Java/Others) ...
- hdu 5909 Tree Cutting——点分治(树形DP转为序列DP)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5909 点分治的话,每次要做一次树形DP:但时间应该是 siz*m2 的.可以用 FWT 变成 siz*ml ...
- hdu 5087 Revenge of LIS II ( LIS ,第二长子序列)
链接:hdu 5087 题意:求第二大的最长升序子序列 分析:这里的第二大指的是,全部的递增子序列的长度(包含相等的), 从大到小排序后.排在第二的长度 cid=546" style=&qu ...
- HDU 1513 最长子序列
Palindrome Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
随机推荐
- 安装 neo4j 在 .../bin 目录下使用 ./neo4j 没反应 和 从csv 导入数据到neo4j
可以使用 /bin/sh ./neo4j start 如果提示:./neo4j: 28: set: Illegal option -o pipefail 那么 ubuntu”set Illegal o ...
- 基元线程同步构造之 Mutes(互斥体)
互斥体实现了“互相排斥”(mutual exclusion)同步的简单形式(所以名为互斥体(mutex)). 互斥体禁止多个线程同时进入受保护的代码“临界区”(critical section). 因 ...
- k8s 问题
kubelet: Orphaned pod "4db449f0-4eaf-11e8-94ab-90b8d042b91a" found, but volume paths are s ...
- linux 杂
set -e表示一旦脚本中有命令的返回值为非0,则脚本立即退出,后续命令不再执行; set -o pipefail表示在管道连接的命令序列中,只要有任何一个命令返回非0值,则整个管道返回非0值,即使最 ...
- linux 随笔
LINUX环境下的批处理文件的扩展名是.sh,而在windows环境的批处理文件名是.bat
- ubuntu下搭建testlink
环境配置: 1. 安装mysql 教程网上找 2. 安装apache sudo apt-get install apache2 重启apache服务 sudo /etc/init.d/apache2 ...
- reduction
reduction - 必应词典 美[rɪ'dʌkʃən]英[rɪ'dʌkʃ(ə)n] n.还原:降低:减少:缩小 网络缩减:减量:减小 变形复数:reductions:
- Distributing Ballot Boxes
Distributing Ballot Boxes http://acm.hdu.edu.cn/showproblem.php?pid=4190 Time Limit: 20000/10000 MS ...
- phpStudy5——php导入其他php文件(php文件的引入)
前言: 通过前边几个例子,相信大家都会有一个疑惑了,就是每个请求数据库的php页面,都要写一次连接数据库的代码,这个肯定是有违代码复用原则的.那么怎么解决这个问题呢? 在php中可以通过include ...
- 12-ssm中的description The request sent by the client was syntactically incorrect.
此问题一般是在前端的数据传回是封装成对象失败的情况: 1.对象名不一致: 2.对象的数据类型不一致: 特别注意日期类型的: 如果前端是date数据类型的话: 传入的日期有问题 在pojo类中限定 @D ...