[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 ...
随机推荐
- linux 下node升级
npm install -g n n stable 安装的路径: cd /usr/local/n/versions/node/10.15.3 修改环境变量 cd /etc sudo vim profi ...
- .NET 通过 NPOI 操作 Excel
目录 .NET 通过 NPOI 操作 Excel 第一步:通过 NuGet 获取 NPOI 包并引入程序集 第二步:引入 NPOI 帮助类 第三步:在程序中调用相应的方法对数据进行导出导入操作 将 D ...
- ubuntu16.0 安装 openstack
主要参考官方文档:https://docs.openstack.org/liberty/zh_CN/install-guide-ubuntu/environment-nosql-database.ht ...
- python打飞机pro版
# -*- coding: utf-8 -*- import pygame from sys import exit import random pygame.init() screen = pyga ...
- Ace 在HTML中使用方法
<!DOCTYPE html> <html> <head> <title>Demo of ACE Editor</title> <!- ...
- 第八篇:cx_Oracle出现的问题
1.cx_Oracle.DatabaseError: ORA-24315: illegal attribute type 2.cx_Oracle.InterfaceError: Unable to a ...
- 在Terminal中,如何打开Finder,并显示当前的目录
这是一个非常方便实用的小技巧,在Terminal中输入如下命令: $ open . 有图有真相: 参考: Open Finder in Current Folder from Terminal
- js获得本季度的开始日期 结束日期
var now = new Date(); //当前日期var nowMonth = now.getMonth()+1; //当前月var nowYear = now.getFullYear(); / ...
- BXS入门赛部分writeup
pwn1 盲打(笑) 前言:没有听鱼哥的话,事先没有装好环境,于是开始没做出来,然后全程在装pwntools,经过一番努力,失败了0.0 最终在网上搜了一段python socket连接脚本,终于可 ...
- 学习C语言库函数
使用C语言功能强大的函数,我们需要包含头文件 #include<math.h>. 1)取两个数的较大值或较小值函数: double a = 9.9; double b = 6.6; pri ...