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的更多相关文章

  1. BZOJ 1109: [POI2007]堆积木Klo

    1109: [POI2007]堆积木Klo Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 948  Solved: 341[Submit][Statu ...

  2. BZOJ 1109 [POI2007]堆积木Klo(树状数组)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1109 [题目大意] Mary在她的生日礼物中有一些积木.那些积木都是相同大小的立方体. ...

  3. bzoj 1109 [POI2007]堆积木Klo(LIS)

    [题意] n个数的序列,删除一个数后序列左移,求最后满足i==a[i]的最大个数. [思路] 设最终得到a[i]==i的序列为s,则s应满足: i<j,a[i]<a[j],i-a[i]&l ...

  4. 【BZOJ】1109: [POI2007]堆积木Klo

    题意 \(n(1 \le n \le 100000)\)个数放在一排,可以一走一些数(后面的数向前移),要求最大化\(a_i=i\)的数目. 分析 分析容易得到一个dp方程. 题解 \(d(i)\)表 ...

  5. BZOJ 1109 POI2007 堆积木Klo LIS

    题目大意:给定一个序列,能够多次将某个位置的数删掉并将后面全部数向左串一位,要求操作后a[i]=i的数最多 首先我们如果最后a[i]=i的数的序列为S 那么S满足随着i递增,a[i]递增(相对位置不变 ...

  6. BZOJ.1109.[POI2007]堆积木Klo(DP LIS)

    BZOJ 二维\(DP\)显然.尝试换成一维,令\(f[i]\)表示,强制把\(i\)放到\(a_i\)位置去,现在能匹配的最多数目. 那么\(f[i]=\max\{f[j]\}+1\),其中\(j& ...

  7. 【BZOJ1109】[POI2007]堆积木Klo 二维偏序

    [BZOJ1109][POI2007]堆积木Klo Description Mary在她的生日礼物中有一些积木.那些积木都是相同大小的立方体.每个积木上面都有一个数.Mary用他的所有积木垒了一个高塔 ...

  8. 【bzoj1109】[POI2007]堆积木Klo 动态规划+树状数组

    题目描述 Mary在她的生日礼物中有一些积木.那些积木都是相同大小的立方体.每个积木上面都有一个数.Mary用他的所有积木垒了一个高塔.妈妈告诉Mary游戏的目的是建一个塔,使得最多的积木在正确的位置 ...

  9. 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[ ...

随机推荐

  1. 巧用DNSlog实现无回显注入

    测试一些网站的时候,一些注入都是无回显的,我们可以写脚本来进行盲注,但有些网站会ban掉我们的ip,这样我们可以通过设置ip代理池解决, 但是盲注往往效率很低,所以产生了DNSlog注入.具体原理如下 ...

  2. 20145216史婧瑶《Java程序设计》第五次实验报告

    20145216 实验五<Java网络编程> 实验内容 1.掌握Socket程序的编写 2.掌握密码技术的使用 3.设计安全传输系统 实验要求 1.基于Java Socket实现安全传输 ...

  3. Unicode字符集和UTF-8, UTF-16, UTF-32编码

    ASCII 在过去的计算中,ASCII码被用来表示字符.英语只有26个字母和其他一些特殊字符和符号. 下表提供了ASCII字符及其相应的十进制和十六进制值. 可以从上面的表中推断,在十进制数系统中,A ...

  4. 【luogu P1850 换教室】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1850 难的不在状态上,难在转移方程. (话说方程写错居然还有84分= =) #include <cst ...

  5. RabbitMQ镜像模式双节点部署时故障转移过程中队列中消息的状态

    场景 现有节点Node1和Node2,建立Exchange:yu.exchange,创建队列yu1.queue镜像队列master位于Node1,yu2.queue镜像队列位于Node2,使用topi ...

  6. docker 导出导入

    容器导出 docker export -o myname.tar 容器id 容器导人 docker import myname.tar httpd:v1

  7. .net core 实践笔记(二)--EF连接Azure Sql

    ** 温馨提示:如需转载本文,请注明内容出处.** 本文链接:https://www.cnblogs.com/grom/p/9902098.html 笔者使用了常见的三层架构,Api展示层注入了Swa ...

  8. Mac 模拟慢速网络

    作为开发者,为了提升用户体验,有时需要模拟不同环境的网络.Mac环境下模拟慢速网络可以使用苹果官方提供的工具:Network Link Conditioner. 1.点击苹果开发者网站提供的下载页面, ...

  9. java环境变量配置(win7)

    JDK1.8 1.单击“计算机-属性-高级系统设置”,单击“环境变量”.在“系统变量”栏下单击“新建”,创建新的系统环境变量. 2.  (1)新建->变量名"JAVA_HOME&quo ...

  10. 【2017 ICPC亚洲区域赛北京站 J】Pangu and Stones(区间dp)

    In Chinese mythology, Pangu is the first living being and the creator of the sky and the earth. He w ...