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 ...
随机推荐
- 用命令行发布android程序
在开发android程序的过程中,我们使用ant debug和ant installd这两个命令就够了,不涉及到APK的签名. 但是在正式发布我们的Android程序时,需要对APK签名.ant re ...
- js数组的操作push,pop,shift,unshift
push(args)可以每次压入多个元素,并返回更新后的数组长度. var oldArr=[1,2,3]; alert(oldArr.push(4,[5,6]))–>5(这里只会将[5,6]当做 ...
- SpringBoot处理静态资源的两种方式
静态资源是指----> CSS.JS之类的文件 首先创建SpringBoot Web项目 添加Spring Boot Web Starter <dependency> <gro ...
- EXT3.3.1在IE9 IE10click事件 失效怎么解决
各位Ext君有福了. var treePanel = new Ext.tree.TreePanel({ id:'treePanel_'+(menuIndex++),//让菜单id可控 title: t ...
- Python基础-封装与扩展、静态方法和类方法
一.封装与扩展 封装在于明确区分内外,使得类实现者可以修改封装内的东西而不影响外部调用者的代码:而外部使用者只知道一个接口(函数),只要接口(函数)名.参数不变,使用者的代码永远无需改变.这就提供一个 ...
- SCons: 替代 make 和 makefile 及 javac 的极好用的c、c++、java 构建工具
http://scons.org/ https://www.ibm.com/developerworks/cn/linux/l-cn-scons/index.html 后附:另外,WAF是一个基于sc ...
- [转]VS2015 Git 源码管理工具简单入门
VS2015 Git 源码管理工具简单入门 1.VS Git插件 1.1 环境 VS2015+GitLab 1.2 Git操作过程图解 1.3 常见名词解释 拉取(Pull):将远程版本库合并到本 ...
- SharePoint 2013 Workflow Manager 1.0 远程服务器返回错误: (400) 错误的请求。 不支持查询字符串中的 api-version
环境: Windows Server 2012 R2 Standard SharePoint Server 2013 with sp1 通过Web 平台安装程序 5.0,已安装 Workflow Ma ...
- git免密配置
1.在git安装目录下双击bash.exeC:\DevTools\Git\bin 2.在弹出窗口内输入,回车,回车ssh-keygen -t rsa -C "542113457@qq.com ...
- PL/SQL第三章 基础查询语句
--查询所有列 select * from tab_name|view_name; SELECT * FROM emp; SELECT * FROM (SELECT * FROM emp); --查询 ...