欢迎访问~原文出处——博客园-zhouzhendong

去博客园看该题解


题目传送门 - BZOJ4989


题意概括

  一条马路的两边分别对应的序列A、B,长度为n,两序列为1到n的全排列。当Ai=Bj时,两边之间会连一条边。你可以选择序列A或序列B进行旋转(只能使队尾或队头位置上的数字变成队头或队尾上的数字)任意K(0<=K<n)步,如123,可以变成 231 或 312。求旋转后,最少的边的交叉数。


题解

  两个都可以转,那么我们只需要分别转动两个并统计即可。
  旋转一个,那么我们只需要统计逆序对就可以了。对于任意一个情况,逆序对可以nlogn求出,但是如何统计这n种情况呢。我们发现,从一种情况转到另一种情况,改变的仅是与动的哪一个数字有关的,那么只需要加加减减就可以转移了。


代码

#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cmath>
using namespace std;
typedef long long LL;
const int N=100000+5;
int n,a[N],b[N],c[N],A[N],B[N];
LL tot,ans;
LL min(LL a,LL b){
return a<b?a:b;
}
int lowbit(int x){
return x&-x;
}
void add(int x,int d){
for (;x<=n;x+=lowbit(x))
c[x]+=d;
}
LL sum(int x){
int ans=0;
for (;x>0;x-=lowbit(x))
ans+=c[x];
return ans;
}
int main(){
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]),A[i]=a[i];
for (int i=1;i<=n;i++)
scanf("%d",&b[i]),B[i]=b[i];
for (int i=1;i<=n;i++)
c[b[i]]=i;
for (int i=1;i<=n;i++)
a[i]=c[a[i]];
memset(c,0,sizeof c);
tot=0;
for (int i=n;i>=1;i--){
tot+=sum(a[i]-1);
add(a[i],1);
}
ans=tot;
for (int i=n;i>=1;i--){
tot=tot-(LL)(n-a[i])+(LL)(a[i]-1);
ans=min(ans,tot);
}
for (int i=1;i<=n;i++)
a[i]=B[i];
for (int i=1;i<=n;i++)
b[i]=A[i];
for (int i=1;i<=n;i++)
c[b[i]]=i;
for (int i=1;i<=n;i++)
a[i]=c[a[i]];
memset(c,0,sizeof c);
tot=0;
for (int i=n;i>=1;i--){
tot+=sum(a[i]-1);
add(a[i],1);
}
for (int i=n;i>=1;i--){
tot=tot-(LL)(n-a[i])+(LL)(a[i]-1);
ans=min(ans,tot);
}
printf("%lld",ans);
return 0;
}

  

BZOJ4989 [Usaco2017 Feb]Why Did the Cow Cross the Road 树状数组 逆序对的更多相关文章

  1. [BZOJ4989][Usaco2017 Feb]Why Did the Cow Cross the Road 树状数组维护逆序对

    4989: [Usaco2017 Feb]Why Did the Cow Cross the Road Time Limit: 10 Sec  Memory Limit: 256 MBSubmit:  ...

  2. [BZOJ4989] [Usaco2017 Feb]Why Did the Cow Cross the Road(树状数组)

    传送门 发现就是逆序对 可以树状数组求出 对于旋转操作,把一个序列最后面一个数移到开头,假设另一个序列的这个数在位置x,那么对答案的贡献 - (n - x) + (x - 1) #include &l ...

  3. 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 线段树维护dp

    题目 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 链接 http://www.lydsy.com/JudgeOnline/proble ...

  4. 4989: [Usaco2017 Feb]Why Did the Cow Cross the Road

    题面:4989: [Usaco2017 Feb]Why Did the Cow Cross the Road 连接 http://www.lydsy.com/JudgeOnline/problem.p ...

  5. [BZOJ4990][Usaco2017 Feb]Why Did the Cow Cross the Road II dp

    4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II Time Limit: 10 Sec  Memory Limit: 128 MBSubmi ...

  6. [bzoj4994][Usaco2017 Feb]Why Did the Cow Cross the Road III_树状数组

    Why Did the Cow Cross the Road III bzoj-4994 Usaco-2017 Feb 题目大意:给定一个长度为$2n$的序列,$1$~$n$个出现过两次,$i$第一次 ...

  7. BZOJ4997 [Usaco2017 Feb]Why Did the Cow Cross the Road III

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4997 题意概括 在n*n的区域里,每一个1*1的块都是一个格子. 有k头牛在里面. 有r个篱笆把格 ...

  8. BZOJ4994 [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4994 题意概括 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi ...

  9. BZOJ4990 [Usaco2017 Feb]Why Did the Cow Cross the Road II 动态规划 树状数组

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4990 题意概括 有上下两行长度为 n 的数字序列 A 和序列 B,都是 1 到 n 的排列,若 a ...

随机推荐

  1. vue自学入门-2(vue创建项目)

    本人也是刚学习VUE,边找资料,边学习,边给大家分享.1.创建项目 2.启动项目 3.注意上面和下面全部用cnpm

  2. JS中的call()方法和apply()方法用法总结

    原文引自:https://blog.csdn.net/ganyingxie123456/article/details/70855586 最近又遇到了JacvaScript中的call()方法和app ...

  3. UDP网络程序,客户端和服务端交互原理

    创建一个udp客户端程序的流程是简单,具体步骤如下: 创建客户端套接字 发送/接收数据 关闭套接字 UDP是面向无连接的通讯协议,UDP数据包括目的端口号和源端口号信息,由于通讯不需要连接,所以可以实 ...

  4. 后端python基础

  5. Android关于RAM、ROM、SD卡以及各种内存的区别

    RAM:运行时内存.相当于PC机的内存存储,用于存储应用运行时的各种对象和变量常量等,主要作用在于提高运行速度.是唯一一种断电后数据会清除的存储器. 机身内存:相当于PC机硬盘.主要包括三块区域:RO ...

  6. 基于XML搭建Dubbo项目

    (1).新建一个普通Maven项目,用于存放一些公共服务接口及公共的Bean等. 公共Bean: package cn.coreqi.entities; import java.io.Serializ ...

  7. Jade教程

    Jade 是一个高性能的模板引擎,它深受 Haml 影响,它是用 JavaScript 实现的,并且可以供 Node 使用. 如何在jade模板上加业务逻辑 if res.length==5 h1= ...

  8. 扫AR

  9. MySQL日志——Undo | Redo【转】

    本文是介绍MySQL数据库InnoDB存储引擎重做日志漫游 00 – Undo LogUndo Log 是为了实现事务的原子性,在MySQL数据库InnoDB存储引擎中,还用Undo Log来实现多版 ...

  10. vue学习生命周期(created和mounted区别)

    created:在模板渲染成html前调用,即通常初始化某些属性值,然后再渲染成视图(例如ajax请求列表). mounted:在模板渲染成html后调用,通常是初始化页面完成后,再对html的dom ...