[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 ...
随机推荐
- nuget用法
Update-Package -reinstall -ProjectName Cardin.HeartCare.Service.ChatService
- Objective-C - NSString 和 NSDate 互相轉換
記錄一下在 Objective-C 由 NSString 轉換為 NSDate 或 NSDate 轉換為 NSString 的方法. 很簡單,使用 NSDateFormatter 就可以令 NSStr ...
- ABC3D创客项目:国旗
国旗是一个国家的象征,也是一个民族的骄傲,国旗带给人们的不仅是荣耀,更多的是爱国的情结.看一场天安门的升旗仪式一度成为广大游客去到北京的必有项目,看国旗仪仗队将五星红旗与太阳同时升起,象征着我国充满活 ...
- eclipse中Lombok注解无效
问题现象:eclipse中使用lombok的@Date,引用get方法时,报错. 解决方案: 在lombok官网(https://www.projectlombok.org/download)下载,或 ...
- WPF中播放视频音频
首先要在WPF中播放视频和音频,我们就需要用到MediaElement控件,下面我们示例播放音频和视频. 用MediaElement播放音频: 第一步:将你需要播放的音频(mp3)放在你WPF项目的D ...
- 前端性能优化:细说JavaScript的加载与执行
本文主要是从性能优化的角度来探讨JavaScript在加载与执行过程中的优化思路与实践方法,既是细说,文中在涉及原理性的地方,不免会多说几句,还望各位读者保持耐心,仔细理解,请相信,您的耐心付出一定会 ...
- python_111_异常处理
#1 name=['a','s'] try: print(name[3]) except: print('list index out of range')#list index out of ran ...
- java 去掉html/style/css等标签
//定义script的正则表达式 private static String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/sc ...
- 洛谷 P1126 机器人搬重物 (BFS)
题目链接:https://www.luogu.org/problemnew/show/P1126 吐槽:这题很阴险 一开始没把格子图转化成点图:30分 转化成点图,发现样例过不去,原来每步要判断vis ...
- Luogu P1080国王游戏(贪心)
国王游戏 题目链接:国王游戏 ps:题目数据说明了要写高精度. 这个题的答案是\(a.l * a.r < b.l * b.r\)按照这个进行排序 题解中大部分只是如何证明排序是: \(a.l * ...