题面: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. 想要将我们的OSGi框架中的批量日志单独打印到文件中

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

  2. 彻底明确怎样设置minSdkVersion和targetSdkVersion

    minSdkVersion和targetSdkVersion相信非常多人都不太理解.我在网上也看了很多关于这两者差别的文章,感觉说的都非常模糊.直到我在stackOverFlow看到Android M ...

  3. ios线程和GCD和队列同步异步的关系

    1.什么是进程? 进程是指在系统中正在运行的一个应用程序.比如同时打开QQ.Xcode,系统就会分别启动2个进程.截图 2.什么是线程? 1).一个进程要想执行任务,必须得有线程(每一个进程至少要有一 ...

  4. python全栈开发day99-DRF序列化组件

    1.解释器组件源码分析 https://www.processon.com/view/link/5ba0a8e7e4b0534c9be0c968 2.基于CBV的接口设计 1).django循环que ...

  5. zabbix http服务监控实例

    1在被监控主机安装http服务 ,监听80端口 systemctl start httpd.service       启动服务  80端口已经启动 设定,监控80端口,当服务不当时先自动重启服务 2 ...

  6. 完成将 toChineseNum, 可以将数字转换成中文大写的表示,处理到万级别,例如 toChineseNum(12345),返回 一万二千三百四十五

    const toChineseNum = (num) => { const unit = ['', '十', '百', '千'] const counts = ['零', '一', '二', ' ...

  7. JavaSE| 流程控制

    程序流程控制 流程控制语句结构: .顺序结构 语句的关系是从上到下依次执行的顺序关系,中间没有任何判断和跳转: 它是最基本的结构,Java程序总体来说都是从main()依次执行 .分支结构: 根据条件 ...

  8. Python GUI - tkinter

    目录: Tkinter 组件 标准属性 几何管理 代码实例: 1. Label & Button 2. Entry & Text 3.Listbox列表 4.Radiobutton单选 ...

  9. vee-validate校验demo

    //太长了,把异步校验放到前面吧: VeeValidate.Validator.extend('checkLoginName',{ getMessage:function () { return &q ...

  10. Zookeeper的概述、安装部署及选举机制

    一.Zookeeper概述 1.Zookeeper是Hadoop生态的管理者,它致力于开发和维护开源服务器,实现高度可靠的分布式协调. 2.Zookeeper的两大功能: (1)存储数据 (2)监听 ...