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 ...
随机推荐
- 微信开发创业交流QQ群列表
方倍工作室参与或主导的微信相关的QQ社群列表,欢迎对照加入. QQ群号 群名称 说明 推荐 518924126 微信平台开发有问必答 知识付费 ★★★★★ 188280503 微信公众平台开发最佳实 ...
- SNMP收集
http://velep.com/archives/416.html 协议基本格式
- 【BZOJ4826】【HNOI2017】影魔(扫描线,单调栈)
[BZOJ4826][HNOI2017]影魔(扫描线,单调栈) 题面 BZOJ 洛谷 Description 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万.千百年来,他 ...
- pygame(class类)调用视图的方法
以下将介绍pygame精灵动画的基础知识,希望对大家有帮助:1.在此,精灵类必须继承pygame.sprite.Sprite并初始化pygame.sprite.Sprite.__init__(self ...
- VS2012 安装番茄插件
1.visual assist x 1929_大番茄.rar 下载地址: http://pan.baidu.com/s/1qXOUuJE 网盘提取密码: 3mka 文件解压密码: eg5p 2.安装完 ...
- solr学习
入门文档 http://www.cnblogs.com/edwinchen/p/3972904.html 中文分词 https://github.com/EugenePig/ik-analyzer-s ...
- IDEA常用快捷键和常用插件集成,持续更新......
用习惯了eclipse,不容易转过来,记一下! 快捷键 psvm: main 方法快捷键 sout :syso快捷键 CTRL+O: 重写父类方法 Ctrl+Alt+V :自动补全返回值 Ctrl+S ...
- java工程添加类库
在属性中添加自定义类库 在工程中引入自定义类库
- Linux下常见音频格式之间的转换方法
Linux下常见音频格式之间的转换方法[转] 下面简单介绍下Linux环境常见音频格式之间的转换方法: MP3 相关工具: lameOGG 相关工具: vorbis-toolsAPE 相关工具: ma ...
- Nodejs 实现ESL内联FreeSWITCH设定说明
一.背景说明: SIP Server IP (Centos):192.168.11.61 ,服务器IP(Windows):192.168.11.19 二.目的: 能够从192.168.11.19上通 ...