[BZOJ4989] [Usaco2017 Feb]Why Did the Cow Cross the Road(树状数组)
发现就是逆序对
可以树状数组求出
对于旋转操作,把一个序列最后面一个数移到开头,假设另一个序列的这个数在位置x,那么对答案的贡献 - (n - x) + (x - 1)
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 200011
#define LL long long using namespace std; int n;
int a[N], b[N], pos1[N], pos2[N];
LL c[N], ans = 1ll * N * N; inline int read()
{
int x = 0, f = 1;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * f;
} inline void add(int x)
{
for(; x <= n; x += x & -x) c[x]++;
} inline LL query(int x)
{
LL ret = 0;
for(; x; x -= x & -x) ret += c[x];
return ret;
} inline void solve()
{
int i, x;
LL sum = 0;
memset(c, 0, sizeof(c));
for(i = 1; i <= n; i++) pos1[a[i]] = i;
for(i = 1; i <= n; i++) pos2[b[i]] = i;
for(i = 1; i <= n; i++)
{
x = pos1[b[i]];
sum += (LL)i - 1 - query(x);
add(x);
}
ans = min(ans, sum);
for(i = n; i > 1; i--)
{
x = pos2[a[i]];
sum -= n - x;
sum += x - 1;
ans = min(ans, sum);
}
} int main()
{
int i;
n = read();
for(i = 1; i <= n; i++) a[i] = read();
for(i = 1; i <= n; i++) b[i] = read();
solve();
swap(a, b);
solve();
printf("%lld\n", 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  树状数组 逆序对
		
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4989 题意概括 一条马路的两边分别对应的序列A.B,长度为n,两序列为1到n的全排列.当Ai=Bj ...
 - 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 ...
 
随机推荐
- Apache Kafka框架学习
			
背景介绍 消息队列的比较 kafka框架介绍 术语解释 文件存储 可靠性保证 高吞吐量实现 负载均衡 应用场景 背景介绍: kafka是由Apache软件基金会维护的一个开源流处理平台,由scala和 ...
 - 学习用5W1H来管理自己的项目/工作
			
学习用5W1H来管理自己的项目/工作 最近开始需要系统化的思维模型,这只是一个开始,一下用脑图的形式来简介5W1H的具体内容: 先写xmind思维树的文本导出,后面附上图片.^ _ ^ 5W1H ...
 - socket使用非阻塞connect
			
在使用tcp的connect调用时,默认是使用阻塞方式,当服务器当前不可用时,connect会等待(内部在重试?)直到超时时间到达,而这个超时时间是系统内核规定的,不能使用setSocketOpt来设 ...
 - http协议参数详解
			
整理一下http协议中的一些参数详解 截取了一个当前项目中的请求作为示例: Genaral:通用头 Request URL:当前请求的请求地址 Request Method:请求类型 get.post ...
 - vue.js学习总结
			
下面使用的命令工具为git bash 使用命令行工具搭建vue.js项目 vue.js官网命令行工具安装 为了提升安装速度,建议将 npm 的注册表源设置为国内的镜像 1.输入命令:npm insta ...
 - 如何使用Git Bash Here,将本地项目传到github上
			
申请一个github账号 安装git bash git与git bash的区别: git:版本控制工具,支持该工具的网站有Github.BitBucket.Gitorious.国内的osChina仓库 ...
 - iTOP-IMX6UL 实战项目:ssh 服务器移植到 arm 开发板
			
实验环境:迅为提供的Ubuntu12.04.2 以及虚拟机 编译器:arm-2009q3 编译器 开发板系统:QT系统 开发板使用手册中给Windows 系统安装了 ssh 客户端,给 Ubunt ...
 - 如何禁用Visual Studio的Browser Link功能
			
在Web.Config的AppSetting节点添加<add key="vs:EnableBrowserLink" value="false"/>
 - CS193p Lecture 6 - UINavigation, UITabBar
			
抽象类(Abstract):指的是这个类不能被实例化,只能被继承: OC中没有关键词来标明某个类是抽象类,只能在注释中标注一下: 抽象类中的抽象方法,必须是public的,使方法称为public的方法 ...
 - Greenplum/Deepgreen(单机/伪分布)安装文档
			
Greenplum/Deepgreen数据库安装(单机/伪分布) 首先去官网下载centos7:https://www.centos.org/download/,选择其中一个镜像下载即可,网上随意下载 ...