[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 MB
Submit: 153 Solved: 70
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
5
4
1
3
2
1
3
2
5
4
Sample Output
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 树状数组维护逆序对的更多相关文章
- BZOJ4989 [Usaco2017 Feb]Why Did the Cow Cross the Road 树状数组 逆序对
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4989 题意概括 一条马路的两边分别对应的序列A.B,长度为n,两序列为1到n的全排列.当Ai=Bj ...
- [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 ...
随机推荐
- Python3中文教程
搜索 此文档来源自网络 安装 PYTHON❝ Tempora mutantur nos et mutamur in illis. (时光流转,吾等亦随之而变.) ❞ — 古罗马谚语 深入欢迎来到 Py ...
- Python 推导式推导序列
推导式是从一个或多个迭代器快速创建序列的方法.它可以将循环和条件判断结合,从而避免冗长的代码. 一.列表推导式 语法: [表达式 for item in 可迭代对象] [表达式 for item in ...
- 每天一个Linux命令(8):chmod命令
chmod命令用来变更文件或目录的权限. 权限范围的表示法如下: u User,即文件或目录的拥有者:g Group,即文件或目录的所属群组:o Other,除了文件或目录拥有者或所属群组之 ...
- Python lambda介绍
在学习python的过程中,lambda的语法时常会使人感到困惑,lambda是什么,为什么要使用lambda,是不是必须使用lambda? 下面就上面的问题进行一下解答. 1.lambda是什么? ...
- Jmeter编码问题
问题现象:1.利用csv data set config参数化数据后,在beanshell中引用,能正常引用到,但是传给服务器时,还是报手机号格式不对 将jmeter日志级别打成debug(jmete ...
- 使用selenium监听每一步操作
1.创建类LogEventListener.java, 如下: package com.demo; import org.openqa.selenium.By; import org.openqa.s ...
- LeetCode(一)
Q&A ONE Given an array of integers, return indices of the two numbers such that they add up to a ...
- springmvc项目搭建三-添加前端框架
这几年前端框架发展可以说非常迅猛了...实际项目中也用到了几个,easyui相对来讲,算是我第一个接触的前端框架了,用的时候感觉很方便,省了很多代码量,一个好的前端框架可以为你省去很多精力在前端布局上 ...
- Json对象转json数组
var arr = []; arr.push(strData);
- 【bzoj1270】[BeijingWc2008]雷涛的小猫 dp
题目描述 输入 输出 样例输入 样例输出 8 题解 dp 设f[i][j]表示在第i棵树的j高度时最多吃到的柿子数. 那么只有两种可能能够到达这个位置:滑下来.跳下来. 滑下来直接用f[i][j+ ...