[USACO17FEB] Why Did the Cow Cross the Road I P (树状数组求逆序对 易错题)

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

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

本以为是一道逆序对水题,结果被卡了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=0; for(int i=x;i>0;i-=(i&(-i))) {ans+=s[i];} return ans;}

int main()
{
    //freopen("testdata.in","r",stdin);
    scanf("%d",&n);
    for(int i=1;i<=n;i++) {scanf("%d",&a[i]);}
    for(int i=1;i<=n;i++) {scanf("%d",&b[i]);}
    for(int i=1;i<=n;i++) {hx[a[i]]=i;}
    for(int i=1;i<=n;i++) {c[i]=hx[b[i]];}
    for(int i=1;i<=n;i++) {ans+=query(n)-query(c[i]);update(c[i],(ll)1);}ret1=ans;
    for(int i=1;i<=n;i++) {ans+=query(n)-query(c[i])-query(c[i]-1);ret1=min(ret1,ans);}
    memset(hx,0,sizeof(hx));memset(c,0,sizeof(c));memset(s,0,sizeof(s));ans=0;
    for(int i=1;i<=n;i++) {hx[b[i]]=i;}
    for(int i=1;i<=n;i++) {c[i]=hx[a[i]];}
    for(int i=1;i<=n;i++) {ans+=query(n)-query(c[i]);update(c[i],(ll)1);}ret2=ans;
    for(int i=1;i<=n;i++) {ans+=query(n)-query(c[i])-query(c[i]-1);ret2=min(ret2,ans);}
    printf("%lld",min(ret1,ret2));
    return 0;
}

原文地址:https://www.cnblogs.com/guapisolo/p/9696885.html

时间: 2024-08-01 05:44:02

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

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

题目大意:给你两个序列,你可以两个序列的点之间连边 要求:1.只能在点权差值不大于4的点之间连边 2.边和边不能相交 3.每个点只能连一次 设表示第一个序列进行到 i,第二个序列进行到 j,最多连的边数,容易得到方程: 不连边: 连边: 实际是这样的,每个位置如果想连边,就要从能连边的位置之前找最大值,即 直接转移不可取,由于最多只从9个位置转移,我们可以缩减一维,用记录b序列进行到位置 j 的最大连边数,再用树状数组维护的最大前缀和方便转移 1 #include <bits/stdc++.h>

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

Why Did the Cow Cross the Road III 时间限制: 1 Sec  内存限制: 128 MB提交: 65  解决: 28[提交][状态][讨论版] 题目描述 The layout of Farmer John's farm is quite peculiar, with a large circular road running around the perimeter of the main field on which his cows graze during

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

传送门 1.每个数的左右位置预处理出来,按照左端点排序,因为左端点是从小到大的,我们只需要知道每条线段包含了多少个前面线段的右端点即可,可以用树状数组 2.如果 ai < bj < bi, 也就是说在两个i之间只有一个j那么对答案的贡献为1,所以可以用树状数组,当第一次出现一个数的时候,那个位置+1,当第二次出现的时候,第一次出现的位置再-1,也就是把它对答案的贡献去掉,同样是树状数组统计答案 #include <cstdio> #include <iostream>

洛谷 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 has NN crosswalks across it, conveniently numbered 1 \ldots N1…N (1 \leq N \leq 100,0001≤N≤100,000). To allow cows to cross at these crosswalks, FJ in

洛谷 P3660 [USACO17FEB]Why Did the Cow Cross the Road III G(树状数组)

题目背景 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 题目描述 The layout of Farmer John's farm is quite peculiar, with a large circular road running around the perimeter of the main field on which his cows graze during the day. Every morn

hdu 2838 Cow Sorting 树状数组求所有比x小的数的个数

Cow Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4766    Accepted Submission(s): 1727 Problem Description Sherlock's N (1 ≤ N ≤ 100,000) cows are lined up to be milked in the evening.

[USACO17FEB]Why Did the Cow Cross the Road III P(CDQ分治)

题意 两列$n$的排列,相同的数连边,如果一对数有交叉且差的绝对值$>k$,则$++ans$,求$ans$ 题解 可以把每一个数字看成一个三元组$(x,y,z)$,其中$x$表示在第一列的位置,$y$表示在第二列的位置,$z$表示权值 两条线交叉,就是$x<x'$且$y>y'$,又要满足差值的绝对值小于等于$k$,就是$|z-z'|<=k$ 于是就转化为了一个三维偏序问题,直接上CDQ 具体细节请看代码 1 // luogu-judger-enable-o2 2 //minamot

HDU Cow Sorting (树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1 ≤ N ≤ 100,000) cows are lined up to be milked in the evening. Each cow has a unique "grumpiness" level in the range 1...100,000. Since grumpy cow

【POJ 3167】Cow Patterns (KMP+树状数组)

Cow Patterns Description A particular subgroup of K (1 <= K <= 25,000) of Farmer John's cows likes to make trouble. When placed in a line, these troublemakers stand together in a particular order. In order to locate these troublemakers, FJ has lined