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. IntelliJ IDEA 与Eclipse Link with Editor等价功能设置

    Link With Editor是Eclipse内置功能中十分小巧,但却异常实用的一个功能. 这个开关按钮 (Toggle Button) 出现在各式导航器视图 ( 例如 Resource Explo ...

  2. ubuntu服务器下tomcat安装(不推荐使用apt-get)

    最近在阿里云服务器上装tomcat,一开始为了省事直接使用了apt-get安装,结果整个程序被拆开散到了好多地方,尤其是像网上说要把打包好了.war文件放到webapps文件夹下,但是开始并没有在/u ...

  3. 【[HAOI2011]向量】

    靠瞎猜的数学题 首先我们先对这些向量进行一顿组合,会发现\((a,b)(a,-b)\)可以组合成\((2a,0)\),\((b,-a)(b,a)\)可以组合成\((2b,0)\),同理\((0,2a) ...

  4. git提交代码到码云

    日常代码一般提交到github比较多,但我还是钟爱马爸爸,没错就是码云. 码云是中文版的代码托管的网站,不存在打开网速问题,使用也蛮方便的,日常自己保存托管代码已经足够,平时使用git提交代码到码云是 ...

  5. 图形剖析,当给 ul 设置padding=0, margin=0后 li前面的小黑点消失的现象原理!

  6. linux--yum源,源码包

    一.企业版 搜狐:http://mirrors.sohu.com/ 网易:http://mirrors.163.com/ 阿里云:http://mirrors.aliyun.com/ 腾讯:http: ...

  7. java.lang.IllegalStateException: Failed to load property source from location 'classpath:/application.yml'

    java.lang.IllegalStateException: Failed to load property source from location 'classpath:/applicatio ...

  8. 爬虫 - xpath 匹配

    例题 import lxml.html test_data = """ <div> <ul> <li class="item-0& ...

  9. 读取本地json文件另一种方式

    function getScenemapData(){ $.ajax({     url: "/js/currency.json",    type: "GET" ...

  10. 利用纯JS和HTML Canvas生成随机迷宫过程中产生的有趣的事情

    上效果图: #先看生成随机迷宫的代码吧↓ <html> <head> <title>生成随机迷宫v1.0</title> </head> & ...