zoukankan      html  css  js  c++  java
  • bzoj 3333: 排队计划 解决问题的方法

    【原标题】

    3333: 排队计划

    Time Limit: 20 Sec  Memory Limit: 128 MB
    Submit: 161  Solved: 71
    [Submit][Status]

    Description

    Input

    Output

    Sample Input

    6 2
    160 163 164 161 167 160
    2
    3

    Sample Output

    6
    3
    1

    HINT

    Source


    【分析】简述一下题目。N个数排成一列。每次指定一个位置P,然后把P~N中全部身高小于等于P的人都拎出来,排一遍序后再放进去。每次操作须要求一遍逆序对。

    首先非常easy想到:每次选定一个位置P后,降低的逆序对数量就是P~N中满足要求的人原来构成的逆序对——并且当操作完毕之后。这些人将永远不会产生逆序对了。

    考虑到这种性质,我们能够维护一个数的F[i],表示从I这个位置開始的逆序对数量。(我们并不关心某个位置某一个时刻的值是多少)然后我们用一个线段树维护i~N中的最小值。每次操作寻找P~N的最小值。假设是满足要求的且位置是X,那么我们就把f[x]置为0,然后能够把x位置的数置为无穷大。之后继续寻找,直到找不到为止。由于每一个数仅仅会最多被更新一次,所以效率均摊是NlogN的。

    【代码】

    #include<cstdio>
    #include<algorithm>
    #define N 500005
    #define INF 1000000005
    #define Lo(x) (x&-x)
    using namespace std;
    typedef long long LL;LL ans=0;
    struct Tree{int l,r,min,wh;}a[N*3];
    int tree[N];//pos P in array to the tree
    int pos[N*4];//pos P int tree to the array
    int f[N],data[N],c[N],n,cnt,x,i,opt,P,ord,size,now,L,R;
    struct array{int x,id;}uni[N];
    inline int cmp(const array &a,const array &b){return a.x<b.x;}
    inline int ask(int x){int s=0;for (;x;x-=Lo(x)) s+=c[x];return s;}
    inline void add(int x){for (;x<=n;x+=Lo(x)) c[x]++;}
    void build(int k,int l,int r)
    {
      a[k].l=l;a[k].r=r;
      if (l==r) {a[k].min=data[l];pos[k]=l;tree[l]=k;a[k].wh=k;return;}
      int mid=(l+r)>>1;
      if (l<=mid) build(k<<1,l,mid);
      if (r>mid) build(k<<1|1,mid+1,r);
      if (a[k<<1].min<a[k<<1|1].min) a[k].min=a[k<<1].min,a[k].wh=a[k<<1].wh;
      else a[k].min=a[k<<1|1].min,a[k].wh=a[k<<1|1].wh;
    }
    int query(int k)
    {
      if (L<=a[k].l&&a[k].r<=R) return a[k].wh;
      int mid=(a[k].l+a[k].r)>>1,resl=0,resr=0;
      if (L<=mid) resl=query(k<<1);
      if (R>mid) resr=query(k<<1|1);
      if (!resl) return resr;if (!resr) return resl;
      return (a[resl].min<a[resr].min)?

    resl:resr; } void update(int k) { if (a[k].l==a[k].r) {a[k].min=INF;a[k].wh=k;return;} int mid=(a[k].l+a[k].r)>>1; if (ord<=mid) update(k<<1);else update(k<<1|1); if (a[k<<1].min<a[k<<1|1].min) a[k].min=a[k<<1].min,a[k].wh=a[k<<1].wh; else a[k].min=a[k<<1|1].min,a[k].wh=a[k<<1|1].wh; } int main() { read(n);read(opt);//读入优化略去 for (i=1;i<=n;i++) read(x),uni[i]=(array){x,i}; sort(uni+1,uni+n+1,cmp); for (i=1;i<=n;i++) data[uni[i].id]=(uni[i].x==uni[i-1].x)?cnt:++cnt; for (i=n;i;i--) f[i]=ask(data[i]-1),add(data[i]),ans+=(LL)f[i]; build(1,1,n);printf("%lld ",ans); while (opt--) { read(P);now=a[tree[P]].min; while (now<INF) { L=P;R=n;ord=query(1);size=a[ord].min; if (size>now) break;ord=pos[ord]; ans-=(LL)f[ord];f[ord]=0;update(1); } printf("%lld ",ans); } return 0; }

    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    解决:ImportError: cannot import name 'login' from 'django.contrib.auth.views'
    报错:No module named 'django.contrib.staticfiles.templatetags'
    模块django.forms.forms的用法
    cannot import name 'python_2_unicode_compatible'
    解决ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'问题
    Django自学之 django基本命令,Django常用命令
    django使用cmd的基本命令-启动、新建
    解决 The repository located at pypi.doubanio.com is not a trusted or secure host and is being ignored.的问题
    解决ImportError: cannot import name 'six' from 'django.utils'
    设计模式--开篇
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4850964.html
Copyright © 2011-2022 走看看