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

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 153  Solved: 70
[Submit][Status][Discuss]

Description

Why did the cow cross the road? We may never know the full reason, but it is certain that Farmer Joh
n's cows do end up crossing the road quite frequently. In fact, they end up crossing the road so oft
en that they often bump into each-other when their paths cross, a situation Farmer John would like t
o remedy.Farmer John raises N breeds of cows (1≤N≤100,000), and each of his fields is dedicated to
 grazing for one specific breed; for example, a field dedicated to breed 12 can only be used for cow
s of breed 12 and not of any other breed. A long road runs through his farm. There is a sequence of 
NN fields on one side of the road (one for each breed), and a sequence of N fields on the other side
 of the road (also one for each breed). When a cow crosses the road, she therefore crosses between t
he two fields designated for her specific breed.Had Farmer John planned more carefully, he would hav
e ordered the fields by breed the same way on both sides of the road, so the two fields for each bre
ed would be directly across the road from each-other. This would have allowed cows to cross the road
 without any cows from different breeds bumping into one-another. Alas, the orderings on both sides 
of the road might be different, so Farmer John observes that there might be pairs of breeds that cro
ss. A pair of different breeds (a,b) is "crossing" if any path across the road for breed aa must int
ersect any path across the road for breed bb.Farmer John would like to minimize the number of crossi
ng pairs of breeds. For logistical reasons, he figures he can move cows around on one side of the ro
ad so the fields on that side undergo a "cyclic shift". That is, for some 0≤k<N, every cow re-locat
es to the field kk fields ahead of it, with the cows in the last kk fields moving so they now popula
te the first kk fields. For example, if the fields on one side of the road start out ordered by bree
d as 3, 7, 1, 2, 5, 4, 6 and undergo a cyclic shift by k=2, the new order will be 4, 6, 3, 7, 1, 2, 
5. Please determine the minimum possible number of crossing pairs of breeds that can exist after an 
appropriate cyclic shift of the fields on one side of the road.上下有两个位置分别对应的序列A、B,长度为n,
两序列为n的一个排列。当Ai == Bj时,上下会连一条边。
你可以选择序列A或者序列B进行旋转任意K步,
如 3 4 1 5 2 旋转两步为 5 2 3 4 1。
求旋转后最小的相交的线段的对数。
 
 

Input

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.
 

Output

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).
 

Sample Input

5
5
4
1
3
2
1
3
2
5
4

Sample Output

0

HINT

 

Source

 
显然题目要求求逆序对个数。
对于每一次旋转,相当于将最后一位放到第一位。
只有与最后一位有关的逆序对数受到影响。
由于他从最后一位变到了第一位,原先比大的数旋转之前与他构成逆序对,旋转后不构成逆序对。反之之前比他的的数构成逆序对。统计答案即可。
 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define maxn 100005
#define LL long long
using namespace std;
int n;
LL a[maxn],b[maxn];
LL sum[maxn];
LL pos[maxn];
int lowbit(int x){return x&(-x);}
void update(int x,int val) {for(int i=x;i<=n;i+=lowbit(i)) sum[i]+=val;}
LL query(int x){
LL ans=;
for(int i=x;i>;i-=lowbit(i)) ans+=sum[i];
return ans;
}
int main() {
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%lld",&a[i]);
for(int i=;i<=n;i++) scanf("%lld",&b[i]);
for(int i=;i<=n;i++) pos[a[i]]=i;
LL cnt=;
for(int i=;i<=n;i++) {
cnt+=i--query(pos[b[i]]-);
update(pos[b[i]],);
}
LL ans=cnt;
for(int i=n;i>=;i--) {
cnt=cnt+(pos[b[i]]-)-(n-pos[b[i]]);
ans=min(ans,cnt);
}
memset(sum,,sizeof(sum));
for(int i=;i<=n;i++) pos[b[i]]=i;
cnt=;
for(int i=;i<=n;i++) {
cnt+=i--query(pos[a[i]]-);
update(pos[a[i]],);
}
ans=min(cnt,ans);
for(int i=n;i>=;i--) {
cnt=cnt+(pos[a[i]]-)-(n-pos[a[i]]);
ans=min(ans,cnt);
}
printf("%lld",ans);
}

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

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

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

  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. mybatis 关联查询实现一对多

    场景:最近接到一个项目是查询管理人集合  同时每一个管理人还存在多个出资人   要查询一个管理人列表  每个管理人又包含了出资人列表 采用mybatis关联查询实现返回数据. 实现方式: 1 .在实体 ...

  2. Trident学习笔记(二)

    aggregator ------------------ 聚合动作:聚合操作可以是基于batch.stream.partiton [聚合方式-分区聚合] partitionAggregate 分区聚 ...

  3. Windows系统搭建Mysql Cluster集群

    简单介绍一下MySQL集群涉及的三种节点:     管理节点(也可以称管理服务器)是整个集群环境的核心,类似于集群中起调度作用的枢纽,由它来负责管理其它节点(数据节点和SQL节点)的开启.关闭或重启某 ...

  4. 后端接口迁移(从 webapi 到 openapi)前端经验总结

    此文已由作者张磊授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 前情提要 以前用的是 webapi 现在统一切成 openapi,字段结构统统都变了 接入接口 20+,涉及模 ...

  5. Android学习记录(5)—在java中学习多线程下载之断点续传②

    在上一节中我们学习了在java中学习多线程下载的基本原理和基本用法,我们并没有讲多线程的断点续传,那么这一节我们就接着上一节来讲断点续传,断点续传的重要性不言而喻,可以不用重复下载,也可以节省时间,实 ...

  6. 23、php知识点总结基础教程--part-1

    1.基本语法 PHP 脚本可放置于文档中的任何位置. PHP 脚本以 <?php 开头,以 ?> 结尾 <?php // 此处是 PHP 代码 ?> PHP 文件的默认文件扩展 ...

  7. 安装LoadRunner11报缺少vc2005_sp1_with_atl_fix_redist的错误

    找到安装程序自带的lrunner\Chs\prerequisites\vc2005_sp1_redist,双击运行vcredist_x86.exe,再重新安装LoadRunner即可成功. 参考资料: ...

  8. python基础实践(二)

    -*-越简单越快乐-*-# -*- coding:utf-8 -*-# Author:sweeping-monkQuestion_1 = "python中的整数运算"Method_ ...

  9. 先立一个书单【flag】,敦促自己温故知新

    书单来源david mimno副教授给ML新生的建议博文,外加一部分搜罗的书籍 学习方式:以书籍查看,习题为辅,代码为最终实现方式,分主题进行今年的学习笔记,立此旗为证. 线代 --> 概率统计 ...

  10. CI框架入门

    本人最近在学习CI框架,网上找到一些个人觉得入门比较好的资料,记录一下: 兄弟连的CI框架入门系类: [军哥谈CI框架]之入门教程之第一讲:codeigniter的介绍和安装配置:http://bbs ...