zoukankan      html  css  js  c++  java
  • bzoj1109[POI2007]堆积木Klo*

    bzoj1109[POI2007]堆积木Klo

    题意:

    n个数,第i个数为ai,现在要移走一些数,使得移走后有最多的数位于它对应的位置上。求移走的数。n≤100000。

    题解:

    dp方程:f[i]=f[j]+1(i>j,a[i]>a[j],a[i]-a[j]>=i-j即a[i]-i>=a[j]-j),而第一个限制条件是可以由后两个限制条件所推出,因此只要在满足第二个条件的前提下求满足第三个条件的即可,而这可以在对ai排序后用关于a[i]-i]的最长上升子序列解决。本弱用的是树状数组(将数从小到大插入,每次求其左边的f最大值+1),复杂度O(nlog2n)。(听说这叫二维偏序)

    代码:

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <queue>
     5 #define inc(i,j,k) for(int i=j;i<=k;i++)
     6 #define maxn 100010
     7 #define lb(x) x&-x
     8 using namespace std;
     9 
    10 inline int read(){
    11     char ch=getchar(); int f=1,x=0;
    12     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    13     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    14     return f*x;
    15 }
    16 int n,c[maxn],ans,tot; pair<int,int>nds[maxn];
    17 inline void add(int x,int y){while(x<=n)c[x]=max(c[x],y),x+=lb(x);}
    18 inline int query(int x){int q=0; while(x>=1)q=max(q,c[x]),x-=lb(x); return q;}
    19 int main(){
    20     n=read(); inc(i,1,n){int x=read(); if(i-x>=0)nds[++tot]=make_pair(i-x,x);}
    21     sort(nds+1,nds+1+tot);
    22     inc(i,1,tot){int x=query(nds[i].second-1)+1; ans=max(ans,x); add(nds[i].second,x);}
    23     printf("%d",ans); return 0;
    24 }

    20161031

  • 相关阅读:
    浅析 MySQL Replication(转)
    mysql优化案例
    create index 与 alter table add index 区别
    /etc/sysctl.conf参数解释(转)
    Linux内核 TCP/IP参数调优
    OneProxy常用参数说明
    转载:如何在面试中写出好的代码
    F面经:painting house
    Lintcode: Merge Sorted Array II
    Lintcode: Median
  • 原文地址:https://www.cnblogs.com/YuanZiming/p/6038662.html
Copyright © 2011-2022 走看看