题面: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. SqlBulkCopy批量插入数据神器

    1.简单例子 class Program { static void Main(string[] args) { Stopwatch sw = new Stopwatch(); DataTable d ...

  2. [转] createObjectURL方法 实现本地图片预览

    ie6 可以直接显示本本地路径的图片 如: <img src="file://c:/3.jpg" />  ~~~网上都说ie7就不支持这种文件系统路径的url,但测试 ...

  3. CTAP: Complementary Temporal Action Proposal Generation (ECCV2018)

    互补时域动作提名生成 这里的互补是指actionness score grouping 和 sliding window ranking这两种方法提proposal的结合,这两种方法各有利弊,形成互补 ...

  4. [BJOI2018]双人猜数游戏

    题解: 彻彻底底的思维题???还是挺难的.. 首先连样例解释都没给..没看题解搞了很久 大概就是 一个人要根据另一个人的决策来猜数 可以去看洛谷那篇题解的解释 然后我们用$f[A/B][i][j][k ...

  5. bzoj 松鼠的新家

    哈夫曼距离与切比雪夫距离的转化

  6. 【CF809D】Hitchhiking in the Baltic States

    题意: 给你n个区间[li,ri],让你选出从中一个子序列,然后在子序列的每个区间里都选择一个tj,满足t1<t2<...<tlent1<t2<...<tlen.最 ...

  7. keepalived高可用配置注意事项

    安装注意事项: 1.关闭防火墙或者打开防火墙vrrp协议的通过,centos6 和centos7打开方式不同 2.一定要关闭防火墙 3.配置完成后重启网卡和keepalived服务 4.日志:/var ...

  8. 解决 js setTimeout 传递带参数的函数无效果

    最近 js  用到 setTimeout 递归调用 刷新进度  setTimeout ("getProgress(name,type)", 3000) ; 发现getProgres ...

  9. P1052 过河 线性dp 路径压缩

    题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数 ...

  10. VMware下centos7安装

    VMware下centos7安装 转载地址:https://blog.csdn.net/hui_2016/article/details/68927487 一. 软件准备 二. Vmware12安装 ...