BZOJ4989 [Usaco2017 Feb]Why Did the Cow Cross the Road 树状数组 逆序对
欢迎访问~原文出处——博客园-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 树状数组 逆序对的更多相关文章
- [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: ...
- [BZOJ4989] [Usaco2017 Feb]Why Did the Cow Cross the Road(树状数组)
传送门 发现就是逆序对 可以树状数组求出 对于旋转操作,把一个序列最后面一个数移到开头,假设另一个序列的这个数在位置x,那么对答案的贡献 - (n - x) + (x - 1) #include &l ...
- 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 ...
- 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 ...
- [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 ...
- [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$第一次 ...
- BZOJ4997 [Usaco2017 Feb]Why Did the Cow Cross the Road III
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4997 题意概括 在n*n的区域里,每一个1*1的块都是一个格子. 有k头牛在里面. 有r个篱笆把格 ...
- BZOJ4994 [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4994 题意概括 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi ...
- BZOJ4990 [Usaco2017 Feb]Why Did the Cow Cross the Road II 动态规划 树状数组
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4990 题意概括 有上下两行长度为 n 的数字序列 A 和序列 B,都是 1 到 n 的排列,若 a ...
随机推荐
- CF875F Royal Questions
传送门 似乎可以按边权排序后二分图匹配 这里给一个复杂度稳定的算法 把一个公主能匹配的两个点连边,然后依次加边,每当加到一个大小为\(n\)的连通块中有\(n\)条边之后,这时形成了基环树,将这些边定 ...
- 【Elasticsearch】Elasticsearch在windows下的安装方法
版本 elasticsearch-2.4.4 2.4.4版本下载地址 下载地址1 下载地址2 启动 进入bin目录,双击elasticsearch.bat: 在浏览器中输入http://localho ...
- tomcat杂记
组成 Server –> Service –> Connector & Container( Engine –> Host –> Context( Wrapper( S ...
- js数组的操作push,pop,shift,unshift
push(args)可以每次压入多个元素,并返回更新后的数组长度. var oldArr=[1,2,3]; alert(oldArr.push(4,[5,6]))–>5(这里只会将[5,6]当做 ...
- 【apache tika】apache tika获取文件内容(与FileUtils的对比)
Tika支持多种功能: 文档类型检测 内容提取 元数据提取 语言检测 重要特点: 统一解析器接口:Tika封装在一个单一的解析器接口的第三方解析器库.由于这个特征,用户逸出从选择合适的解析器库的负担, ...
- linux 中的 open() read() write() close() 函数
1. open()函数 功能描述:用于打开或创建文件,在打开或创建文件时可以指定文件的属性及用户的权限等各种参数. 所需头文件:#include <sys/types.h>,#includ ...
- Netty通信原理
Netty是一个异步事件驱动的网络应用程序框架,用于快速开发可维护的高性能协议服务器和客户端,它极大的简化了TCP和UDP套接字服务器等网络编程. BIO(Blocking IO):每一个请求,一个S ...
- SpringBoot整合Druid(阿里巴巴)数据源
(1).添加相关依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId ...
- Linux mmc framework1:软件架构
[部分内容来自] http://www.wowotech.net/comm/mmc_framework_arch.html 1. 前言 由eMMC基础技术1:MMC简介中MMC.SD.SDIO的介绍可 ...
- emmc基础技术8:操作模式4-data transfer mode
1.前言 eMMC总线操作包含: boot mode, device identification mode interrupt mode data transfer mode 本文主要描述data ...