【BZOJ】4430: [Nwerc2015]Guessing Camels赌骆驼

【题意】给定三个长度为n的排列,求在三个排列中顺序相同的数对个数。

【算法】逆序对

【题解】很容易联想到NOIP火柴排队,涉及顺序问题显然和逆序对息息相关。

一个数对如果在三个排列中顺序不同,一定是1+2或2+1,也就是只在两数列之间顺序相同。

所以对三个数列两两求逆序对总数num,则不满足要求的数对一定会产生且仅产生两个逆序对,ans=n*(n-1)/2-num/2。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#define lowbit(x) x&-x
#define ll long long
using namespace std;
int read(){
    char c;int s=0,t=1;
    while(!isdigit(c=getchar()))if(c==‘-‘)t=-1;
    do{s=s*10+c-‘0‘;}while(isdigit(c=getchar()));
    return s*t;
}
const int maxn=200010;
ll ans;
int d[maxn],n,A[maxn];
struct cyc{int num,id;}a[maxn],b[maxn],c[maxn];
bool cmp(cyc a,cyc b){return a.num<b.num;}
void modify(int x){for(int i=x;i<=n;i+=lowbit(i))d[i]++;}
int query(int x){int ans=0;for(int i=x;i>=1;i-=lowbit(i))ans+=d[i];return ans;}
void calc(cyc a[],cyc b[]){
    memset(d,0,sizeof(d));
    for(int i=1;i<=n;i++)A[a[i].id]=b[i].id;
    for(int i=1;i<=n;i++){
        modify(A[i]);
        ans+=i-query(A[i]);
    }
}
int main(){
    n=read();
    for(int i=1;i<=n;i++)a[i].num=read(),a[i].id=i;
    for(int i=1;i<=n;i++)b[i].num=read(),b[i].id=i;
    for(int i=1;i<=n;i++)c[i].num=read(),c[i].id=i;
    sort(a+1,a+n+1,cmp);sort(b+1,b+n+1,cmp);sort(c+1,c+n+1,cmp);
    ans=0;
    calc(a,b);calc(b,c);calc(a,c);
    printf("%lld",1ll*n*(n-1)/2-ans/2);
    return 0;
}

时间: 2025-01-23 01:55:09

【BZOJ】4430: [Nwerc2015]Guessing Camels赌骆驼的相关文章

bzoj 4430: [Nwerc2015]Guessing Camels赌骆驼

4430: [Nwerc2015]Guessing Camels赌骆 Description Jaap, Jan, and Thijs are on a trip to the desert after having attended the ACM ICPC World Finals 2015 in Morocco. The trip included a camel ride, and after returning from the ride, their guide invited th

bzoj4430 [Nwerc2015]Guessing Camels赌骆驼

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4430 [题解] 把每只骆驼在第一个人.第二个人.第三个人的位置找出来,然后做三维偏序即可. 排序+cdq分治+BIT # include <stdio.h> # include <string.h> # include <iostream> # include <algorithm> // # include <bits/stdc++.h>

BZOJ 4430 Guessing Camels赌骆驼

[题意概述] 给出三个n的排列,求有多少个数对在三个排列中顺序相同 [题解] 考虑用补集转化的方法,答案为总对数-不满足的对数 一对数不满足条件,当且仅当这对数在两个排列中顺序相同,在另一个排列中的顺序不同. 统计两两之间不满足偏序条件的数对的个数,那么每对数都被统计了两次 #include<cstdio> #include<algorithm> #include<cstring> #define LL long long using namespace std; co

Guessing Camels (***)

Guessing Camels Jaap, Jan, and Thijs are on a trip to the desert after having attended the ACM ICPC World Finals 2015 in Morocco. The trip included a camel ride, and after returning from the ride, their guide invited them to a big camel race in the e

BZOJ 4430 Guessing Camels

Description Jaap, Jan, and Thijs are on a trip to the desert after having attended the ACM ICPC World Finals 2015 in Morocco. The trip included a camel ride, and after returning from the ride, their guide invited them to a big camel race in the eveni

bzoj 4428: [Nwerc2015]Debugging调试

4428: [Nwerc2015]Debugging调试 Description Your fancy debugger will not help you in this matter. There are many ways in which code can produce different behavior between debug and release builds, and when this happens, one may have to resort to more pr

越狱Season 1-Episode 13: End of the Tunnel

Season 1, Episode 13: End of the Tunnel -Fernando: The name is John Abruzzi. 名字是John Abruzzi A b r u z Z... A b r u z Z... I don't care what protocol is. protocol: 规程 care: 关心,在乎 管你什么规定 I just want to know if he's okay. 我只想知道他情况怎样 Hello? Hello? 喂? 喂?

BZOJ 1013: [JSOI2008]球形空间产生器sphere

二次联通门 : BZOJ 1013: [JSOI2008]球形空间产生器sphere /* BZOJ 1013: [JSOI2008]球形空间产生器sphere 高斯消元 QAQ SB的我也能终于能秒题了啊 设球心的坐标为(x,y,z...) 那么就可以列n+1个方程,化化式子高斯消元即可 */ #include <cstdio> #include <iostream> #include <cstring> #define rg register #define Max

bzoj 3309 DZY Loves Math - 莫比乌斯反演 - 线性筛

对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007)=1, f(1)=0. 给定正整数a,b,求sigma(sigma(f(gcd(i,j)))) (i=1..a, j=1..b). Input 第一行一个数T,表示询问数. 接下来T行,每行两个数a,b,表示一个询问. Output 对于每一个询问,输出一行一个非负整数作为回答. Sample Input 4 7558588 9653114 6514903 445