zoukankan      html  css  js  c++  java
  • P1966 火柴排队

    ( ext{Solution})

    首先有一个结论:(a) 序列的第 (k) 大和 (b) 序列的第 (k) 大在相同的位置上两列火柴的距离最小。

    证明要用排序不等式,这里不赘述。

    关键是接下来如何处理。对于 (a,b) 数组分别处理出 (pos) 数组表示第 (i) 小的数在原数组的下标为 (pos[i])。这时我们想要 (posa[i])(posb[i]) 相等。我们再搞个数组令 (c[posa[i]]=posb[i]),如果使这个数组单调递增就满足了要求。

    方便理解我们定义 (A={4,1,2,3},B={3,2,4,1})。则 (posa={2,3,4,1},posb={4,2,1,3})。那么 (C={3,4,2,1}),我们尝试理解 (C) 的第二项:(A) 数组的第一小在 (2) 位置,(B) 数组的第一小在 (4) 位置,那么 (C[2]=4) 就是需要把 (B) 数组 (4) 位置上的数换到 (B) 数组的 (2) 位置上以与 (A) 数组对应,而我们 (B) 数组原来编号相当于 (1,2,3,4)。所以上文的使这个数组单调递增实际是反向操作。

    还有一个坑了我挺久的点。先开始我没有用 (pos) 数组,直接令 (c[a[i]]=b[i]),得到了 (10pts) 的好成绩。。。实际上我们排的是权值并不是数组下标。

    ( ext{Code})

    #include <cstdio>
    
    #define rep(i,_l,_r) for(register signed i=(_l),_end=(_r);i<=_end;++i)
    #define fep(i,_l,_r) for(register signed i=(_l),_end=(_r);i>=_end;--i)
    #define erep(i,u) for(signed i=head[u],v=to[i];i;i=nxt[i],v=to[i])
    #define efep(i,u) for(signed i=Head[u],v=to[i];i;i=nxt[i],v=to[i])
    #define print(x,y) write(x),putchar(y)
    
    template <class T> inline T read(const T sample) {
    	T x=0; int f=1; char s;
    	while((s=getchar())>'9'||s<'0') if(s=='-') f=-1;
    	while(s>='0'&&s<='9') x=(x<<1)+(x<<3)+(s^48),s=getchar();
    	return x*f;
    }
    template <class T> inline void write(const T x) {
    	if(x<0) return (void) (putchar('-'),write(-x));
    	if(x>9) write(x/10);
    	putchar(x%10^48);
    }
    template <class T> inline T Max(const T x,const T y) {if(x>y) return x; return y;}
    template <class T> inline T Min(const T x,const T y) {if(x<y) return x; return y;}
    template <class T> inline T fab(const T x) {return x>0?x:-x;}
    template <class T> inline T Gcd(const T x,const T y) {return y?Gcd(y,x%y):x;}
    template <class T> inline T Swap(T &x,T &y) {x^=y^=x^=y;}
    
    #include <algorithm>
    using namespace std;
    
    const int maxn=1e5+5,mod=1e8-3;
    
    int n,a[maxn],b[maxn],c[maxn],t[maxn],d[maxn],e[maxn];
    
    int lowbit(int x) {return x&-x;}
    
    void add(int x,int k) {
    	while(x<=n) t[x]+=k,x+=lowbit(x);
    } 
    
    int ask(int x) {
    	int r=0;
    	while(x) (r+=t[x])%=mod,x-=lowbit(x);
    	return r;
    }
    
    bool cmp(int x,int y) {return a[x]<a[y];}
    
    bool Cmp(int x,int y) {return b[x]<b[y];}
    
    int main() {
    	int len,ans=0;
    	n=read(9);
    	rep(i,1,n) d[i]=i,a[i]=read(9);
    	sort(d+1,d+n+1,cmp);
    	rep(i,1,n) e[i]=i,b[i]=read(9);
    	sort(e+1,e+n+1,Cmp);
    	rep(i,1,n) c[d[i]]=e[i];
    	fep(i,n,1) (ans+=ask(c[i]-1))%=mod,add(c[i],1);
    	print(ans,'
    ');
    	return 0;
    }
    
  • 相关阅读:
    windows server 2008 R2 安装 sql server 2000 遇到的种种问题
    圆心的VPS记录信息
    无法访问.您可能没有权限使用网络资源.局域网无法访问共享,局域网无法访问打印机的一些方法
    win7 32位 fastcgi模式 运行php
    SpringBoot+JPA实例
    马斯洛的锤子论
    一道算法题
    MySQL源码解读之数据结构LF_HASH
    JS学习专辑外传(1)
    WPF 用DynamicResource动态样式切换
  • 原文地址:https://www.cnblogs.com/AWhiteWall/p/13556592.html
Copyright © 2011-2022 走看看