题面:4989: [Usaco2017 Feb]Why Did the Cow Cross the Road

连接

http://www.lydsy.com/JudgeOnline/problem.php?id=4989

题面

上下有两个位置分别对应的序列A、B,长度为n,

两序列为n的一个排列。当Ai == Bj时,上下会连一条边。

你可以选择序列A或者序列B进行旋转任意K步,

如 3 4 1 5 2 旋转两步为 5 2 3 4 1。

求旋转后最小的相交的线段的对数。

输入

The first line of input contains N.

The next N lines describe the order, by breed ID, of fields on one side of the road;

each breed ID is an integer in the range 1…N.

The last N lines describe the order, by breed ID, of the fields on the other side of the road.

输出

Please output the minimum number of crossing pairs of breeds after a cyclic shift of the

fields on one side of the road (either side can be shifted).

样例输入

5

5

4

1

3

2

1

3

2

5

4

样例输出

0

题解

树状数组,然后推一下移动一个数的代价就好了。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7; int d[maxn],c[maxn];
int a[maxn],b[maxn],a2[maxn],b2[maxn];
int n;
int lowbit(int x){
return x&(-x);
}
void update(int x,int v){
for(int i=x;i<maxn;i+=lowbit(i)){
d[i]+=v;
}
}
long long get(int x){
long long ans = 0;
for(int i=x;i;i-=lowbit(i)){
ans+=d[i];
}
return ans;
}
long long solve(int a[],int b[]){
memset(d,0,sizeof(d));
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++){
c[b[i]]=i;
}
for(int i=1;i<=n;i++){
a[i]=c[a[i]];
}
long long ans = 0;
for(int i=1;i<=n;i++){
ans+=(n-a[i])-get(a[i]);
update(a[i],1);
}
long long ans2 = ans;
for(int i=n;i>=1;i--){
ans=ans-(n-a[i])+(a[i]-1);
ans2=min(ans2,ans);
}
return ans2;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
a2[i]=a[i];
}
for(int i=1;i<=n;i++){
scanf("%d",&b[i]);
b2[i]=b[i];
}
cout<<min(solve(a,b),solve(b2,a2))<<endl;
}

4989: [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. 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 ...

  3. [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 ...

  4. [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$第一次 ...

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

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

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

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

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

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4989 题意概括 一条马路的两边分别对应的序列A.B,长度为n,两序列为1到n的全排列.当Ai=Bj ...

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

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

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

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

随机推荐

  1. Android常用框架和控件使用

    Router框架 https://github.com/iqiyi/Andromeda/blob/master/CHINESE_README.md https://github.com/alibaba ...

  2. XmlSerializer 实现序列化CDATA

    [XmlIgnore] public string GuestRemarks { get; set; } [XmlElement("GuestRemarks")] public X ...

  3. 不显示TensorFlow加速指令警告

    vim ~/.bashrc 在打开的文件中追加: export TF_CPP_MIN_LOG_LEVEL= 最后再执行 source ~/.bashrc

  4. 小改造gotty,使之适合接收经过一层加密的URL

    当然,比起密码设置,这个安全性也不是特别高, 但比起直接传参数. 还是要专业一些的. 其实现思路分两种: 第一种是在gotty.js之内加入解url的函数. 第二种在是go源代码时,加入同等的解密函数 ...

  5. 想要将我们的OSGi框架中的批量日志单独打印到文件中

    我们的日志虽然没有直接依赖logback,但遗憾的是也没有使用slf4j,而是使用了Apache Common-Logging slf4j 和 common-logging有什么区别呢 common- ...

  6. [转] 设置div的overflow:scroll,但是在手机上滑动的时候有点卡顿

    设置div的overflow:scroll,但是在手机上滑动的时候有点卡顿,所以在这个div上加一个css: -webkit-overflow-scrolling : touch; 在苹果手机上使用- ...

  7. (转载)Memcached和Redis简介

    转载自: Memcached和Redis简介 博主的Redis资料列表.http://www.cnblogs.com/programlearning/category/1003158.html 前言: ...

  8. 使用jquery.more.js上滑加载更多

    html: <div id="more"> <div class="single_item"> <div class=" ...

  9. 求自然数幂和 B - The Sum of the k-th Powers CodeForces - 622F

    题解: 很多方法 斯特林数推导略麻烦但是不依赖于模数 代码: 拉格朗日插值 由于可以证明这是个K+1次多项式于是可以直接用插值 #include <bits/stdc++.h> using ...

  10. 【Android】AndroidManifest 中original-package标签

    Manifest.xml 中   <manifest>标签中package属性用于设置应 用程序的进程名,即在运行时使用ddms查看到的进程名.   <original-packag ...