1109: [POI2007]堆积木Klo
1109: [POI2007]堆积木Klo
https://lydsy.com/JudgeOnline/problem.php?id=1109
分析:
首先是dp,f[i]表示到第i个的最优值,f[i]=f[j]+1,(j<i,a[j]<a[i],j-a[j]<i-a[i]),三维偏序,可以cdq+线段树转移。实际上由a[j]<a[i]和j-a[j]<i-a[i]可以推出j<i所以二维偏序,直接LIS。
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ; struct Node{
int x, c;
bool operator < (const Node &A) const {
return x == A.x ? c > A.c : x < A.x;
}
}A[N];
int f[N]; int main() {
int n = read(), cnt = ;
for (int i = ; i <= n; ++i) {
int x = read();
if (i - x >= )
A[++cnt].x = x, A[cnt].c = i - x;
}
if (cnt == ) {
cout << ; return ;
}
int len = ;
sort(A + , A + cnt + );
f[] = A[].c;
for (int i = ; i <= cnt; ++i) {
if (A[i].x != A[i - ].x && A[i].c >= f[len]) f[++len] = A[i].c;
else {
int p = upper_bound(f + , f + len + , A[i].c) - f;
f[p] = A[i].c;
}
}
cout << len;
return ;
}
LIS
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ; struct Node{
int id, x, c;
bool operator < (const Node &A) const {
return c == A.c ? x < A.x : c < A.c;
}
}A[N], B[N];
int f[N], mx; #define Root 1, mx, 1
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
struct SegmentTree{
int mx[N << ];
SegmentTree() { for (int i = ; i <= ; ++i) mx[i] = -1e9; }
void update(int l,int r,int rt,int p,int v) {
if (l == r) { mx[rt] = v; return ; }
int mid = (l + r) >> ;
if (p <= mid) update(lson, p, v);
else update(rson, p, v);
mx[rt] = max(mx[rt << ], mx[rt << | ]);
}
int query(int l,int r,int rt,int p) {
if (p < ) return -1e9;
if (r <= p) return mx[rt];
int mid = (l + r) >> ;
if (p <= mid) return query(lson, p);
else return max(mx[rt << ], query(rson, p));
}
}T; void cdq(int l,int r) {
if (l >= r) return ;
int mid = (l + r) >> ;
cdq(l, mid);
for (int i = mid + ; i <= r; ++i) B[i] = A[i];
sort(B + mid + , B + r + );
for (int p = l, j = mid + ; j <= r; ++j) {
if (f[B[j].id] == -1e9) continue;
while (p <= mid && A[p].c <= B[j].c) T.update(Root, A[p].x, f[A[p].id]), p ++;
f[B[j].id] = max(f[B[j].id], T.query(Root, B[j].x - ) + );
}
for (int i = l; i <= mid; ++i) T.update(Root, A[i].x, ); // B[i].x!!!
cdq(mid + , r);
sort(A + l, A + r + );
} int main() {
int n = read();
for (int i = ; i <= n; ++i) {
A[i].x = read(), A[i].id = i, A[i].c = i - A[i].x;
if (A[i].x <= i) f[i] = ;
else f[i] = -1e9;
mx = max(mx, A[i].x);
}
cdq(, n);
int ans = ;
for (int i = ; i <= n; ++i) ans = max(ans, f[i]);
cout << ans;
return ;
}
CDQ
1109: [POI2007]堆积木Klo的更多相关文章
- BZOJ 1109: [POI2007]堆积木Klo
1109: [POI2007]堆积木Klo Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 948 Solved: 341[Submit][Statu ...
- BZOJ 1109 [POI2007]堆积木Klo(树状数组)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1109 [题目大意] Mary在她的生日礼物中有一些积木.那些积木都是相同大小的立方体. ...
- bzoj 1109 [POI2007]堆积木Klo(LIS)
[题意] n个数的序列,删除一个数后序列左移,求最后满足i==a[i]的最大个数. [思路] 设最终得到a[i]==i的序列为s,则s应满足: i<j,a[i]<a[j],i-a[i]&l ...
- 【BZOJ】1109: [POI2007]堆积木Klo
题意 \(n(1 \le n \le 100000)\)个数放在一排,可以一走一些数(后面的数向前移),要求最大化\(a_i=i\)的数目. 分析 分析容易得到一个dp方程. 题解 \(d(i)\)表 ...
- BZOJ 1109 POI2007 堆积木Klo LIS
题目大意:给定一个序列,能够多次将某个位置的数删掉并将后面全部数向左串一位,要求操作后a[i]=i的数最多 首先我们如果最后a[i]=i的数的序列为S 那么S满足随着i递增,a[i]递增(相对位置不变 ...
- BZOJ.1109.[POI2007]堆积木Klo(DP LIS)
BZOJ 二维\(DP\)显然.尝试换成一维,令\(f[i]\)表示,强制把\(i\)放到\(a_i\)位置去,现在能匹配的最多数目. 那么\(f[i]=\max\{f[j]\}+1\),其中\(j& ...
- 【BZOJ1109】[POI2007]堆积木Klo 二维偏序
[BZOJ1109][POI2007]堆积木Klo Description Mary在她的生日礼物中有一些积木.那些积木都是相同大小的立方体.每个积木上面都有一个数.Mary用他的所有积木垒了一个高塔 ...
- 【bzoj1109】[POI2007]堆积木Klo 动态规划+树状数组
题目描述 Mary在她的生日礼物中有一些积木.那些积木都是相同大小的立方体.每个积木上面都有一个数.Mary用他的所有积木垒了一个高塔.妈妈告诉Mary游戏的目的是建一个塔,使得最多的积木在正确的位置 ...
- BZOJ1109 : [POI2007]堆积木Klo
f[i]表示第i个在自己位置上的最大值 则f[i]=max(f[j])+1 其中 j<i a[j]<a[i] a[i]-a[j]<=i-j -> j-a[j]<=i-a[ ...
随机推荐
- JavaScript的DOM_动态加载脚本和样式
一.动态加载脚本 当网站需求变大,脚本的需求也逐步变大.我们就不得不引入太多的 JS 脚本而降低了整站的性能,所以就出现了动态脚本的概念,在适时的时候加载相应的脚本. 1.动态加载js文件 比如:我们 ...
- Codeforces Round #440 (Div. 2)【A、B、C、E】
Codeforces Round #440 (Div. 2) codeforces 870 A. Search for Pretty Integers(水题) 题意:给两个数组,求一个最小的数包含两个 ...
- 根据自增ID生成不重复序列号
网上看到一个例子,源地址:https://www.aliyun.com/jiaocheng/536419.html 借鉴修改一下 实现根据long类型的用户ID生成6位随机邀请码,并且根据邀请码能算出 ...
- 8、Web Service-IDEA-jaxws规范下的 spring整合CXF
前提:开发和之前eclipse的开发有很大的不同! 1.服务端的实现 1.新建项目 此时创建的是web项目 2.此时创建的项目是不完整的需要开发人员手动补充完整 3.对文件夹的设置(满满的软件使用方法 ...
- Java50道经典习题-程序33 杨辉三角
题目:打印出杨辉三角形(要求打印出10行如下图)分析: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 11 5 10 10 5 ...
- Apollo深度磁盘清理
摘要 在Apollo的使用过程中,会出现磁盘空间不足的情况,Apollo的官方提供的方法是删除apollo/data/log或者删除apollo/data/bag文件.但是即使删除了这些,磁盘空间并没 ...
- Linux mysql 5.5.10 二进制安装过程记录和 修改 密码 登录
1.useradd clouder2.解压缩mysql.tar.bz2到/home/clouder2.mv /etc/my.cnf /etc/my.cnf.bak3./home/clouder/mys ...
- Reading HPSRouter A High Performance Software Router
ICACT 2018 Background High speed traffic SDN NFV Hardware Advantages High performace Disadvantages C ...
- PDO介绍(16)
安装PDO PDO的数据选项 链接到数据库服务器并选择数据库 错误处理 获取和设置属性 查询执行 准备语句介绍 获取数据 设置绑定列 处理事务
- 获取并安装XWAF框架压缩包(2)
建议在Eclipse环境下使用XWAF框架来开发用户的Web项目,并遵循以下步骤和约定. 1.获取XWAF框架压缩包文件 程序员点击下列地址免费下载XWAF框架的压缩包文件:XWAF框架压缩文件 2. ...