题目大意:给你两个序列,可以序列进行若干次旋转操作(两个都可以转),对两个序列相同权值的地方连边,求最少的交点数

记录某个值在第一个序列的位置,再记录第二个序列中某个值 在第一个序列出现的位置 ,求逆序对数量即可

本以为是一道逆序对水题,结果被卡了20分。看了题解才恍然大悟,实际上,序列可以旋转 ≠ 序列成环,由于逆序对的特殊性(并不适用于环),故不能把一个序列单独旋转看成它们的相对移动,正着旋转一个序列≠反着旋转另一个序列(更详细证明可以看洛谷)

所以我们要对两个序列再反着进行一次同样的操作

#include <bits/stdc++.h>
#define N 200100
#define ll long long
using namespace std; ll ans,ret1,ret2,s[N];
int n,a[N],b[N],hx[N],c[N];
void update(int x,ll w) {for(int i=x;i<=n;i+=(i&(-i))) {s[i]+=w;}}
ll query(int x) {ll ans=; for(int i=x;i>;i-=(i&(-i))) {ans+=s[i];} return ans;} int main()
{
//freopen("testdata.in","r",stdin);
scanf("%d",&n);
for(int i=;i<=n;i++) {scanf("%d",&a[i]);}
for(int i=;i<=n;i++) {scanf("%d",&b[i]);}
for(int i=;i<=n;i++) {hx[a[i]]=i;}
for(int i=;i<=n;i++) {c[i]=hx[b[i]];}
for(int i=;i<=n;i++) {ans+=query(n)-query(c[i]);update(c[i],(ll));}ret1=ans;
for(int i=;i<=n;i++) {ans+=query(n)-query(c[i])-query(c[i]-);ret1=min(ret1,ans);}
memset(hx,,sizeof(hx));memset(c,,sizeof(c));memset(s,,sizeof(s));ans=;
for(int i=;i<=n;i++) {hx[b[i]]=i;}
for(int i=;i<=n;i++) {c[i]=hx[a[i]];}
for(int i=;i<=n;i++) {ans+=query(n)-query(c[i]);update(c[i],(ll));}ret2=ans;
for(int i=;i<=n;i++) {ans+=query(n)-query(c[i])-query(c[i]-);ret2=min(ret2,ans);}
printf("%lld",min(ret1,ret2));
return ;
}

[USACO17FEB] Why Did the Cow Cross the Road I P (树状数组求逆序对 易错题)的更多相关文章

  1. BZOJ 4990 [USACO17FEB] Why Did the Cow Cross the Road II P (树状数组优化DP)

    题目大意:给你两个序列,你可以两个序列的点之间连边 要求:1.只能在点权差值不大于4的点之间连边 2.边和边不能相交 3.每个点只能连一次 设表示第一个序列进行到 i,第二个序列进行到 j,最多连的边 ...

  2. [USACO17FEB]Why Did the Cow Cross the Road III G (树状数组,排序)

    题目链接 Solution 二维偏序问题. 现将所有点按照左端点排序,如此以来从左至右便满足了 \(a_i<a_j\) . 接下来对于任意一个点 \(j\) ,其之前的所有节点都满足 \(a_i ...

  3. Why Did the Cow Cross the Road III(树状数组)

    Why Did the Cow Cross the Road III 时间限制: 1 Sec  内存限制: 128 MB提交: 65  解决: 28[提交][状态][讨论版] 题目描述 The lay ...

  4. BZOJ4990 [Usaco2017 Feb]Why Did the Cow Cross the Road II 动态规划 树状数组

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4990 题意概括 有上下两行长度为 n 的数字序列 A 和序列 B,都是 1 到 n 的排列,若 a ...

  5. BZOJ4993 [Usaco2017 Feb]Why Did the Cow Cross the Road II 动态规划 树状数组

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4993 题意概括 有上下两行长度为 n 的数字序列 A 和序列 B,都是 1 到 n 的排列,若 a ...

  6. [BZOJ4994] [Usaco2017 Feb]Why Did the Cow Cross the Road III(树状数组)

    传送门 1.每个数的左右位置预处理出来,按照左端点排序,因为左端点是从小到大的,我们只需要知道每条线段包含了多少个前面线段的右端点即可,可以用树状数组 2.如果 ai < bj < bi, ...

  7. hdu2838 cow sorting用树状数组求逆序对

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/2838/ 题目解法:题目给出一个1-n的排列,操作只有一种:交换相邻的元素,代价是两个元素之和,问将该序列变成升序 ...

  8. [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:  ...

  9. 洛谷 P3662 [USACO17FEB]Why Did the Cow Cross the Road II S

    P3662 [USACO17FEB]Why Did the Cow Cross the Road II S 题目描述 The long road through Farmer John's farm ...

随机推荐

  1. Tensorflow 之物体检测

    1)安装Protobuf TensorFlow内部使用Protocol Buffers,物体检测需要特别安装一下. # yum info protobuf protobuf-compiler 2.5. ...

  2. BA-水阀接线(图)

    220V水阀接线

  3. Shell编程入门(第二版)(上)

    简单的示例Shell程序 示例1. #!/bin/bash #This is to show what a shell script looks like echo "Our first e ...

  4. NEFU 84

    其实同POJ 1061 #include <iostream> #include <cstdio> #include <cstring> #include < ...

  5. 使用fatjar来实现将包括第三方jar包的项目到处成一个jar包供其它程序使用

    一.在线安装fat jar 在线安装步骤: eclipse菜单条 help >software updates >Search for new features to install> ...

  6. UVA 11825 Hackers’ Crackdown 状压DP枚举子集势

    Hackers’ Crackdown Miracle Corporations has a number of system services running in a distributed com ...

  7. UVA 11728 - Alternate Task 数学

    Little Hasan loves to play number games with his friends. One day they were playing a game whereone ...

  8. 堆排序(Swift版本)

    一:什么是堆? 堆可视为  "以数组方式存储的一棵完全二叉树" 堆又分为最大堆和最小堆, 最大堆就是对于整个二叉树中的每一个节点都满足:节点的键值比其左右子节点的键值都要大,对应的 ...

  9. USACO 1.4 Mother's Milk

    Mother's Milk Farmer John has three milking buckets of capacity A, B, and C liters. Each of the numb ...

  10. 运算符 and or ont

    运算符 逻辑运算: and 并且的意思. 左右两端的值必须都是真. 运算结果才是真 or 或者的意思. 左右两端有一个是真的. 结果就是真. 全部是假. 结果才能是假 not 非的意思. 原来是假. ...