BZOJ 4430 Guessing Camels赌骆驼

【题意概述】

给出三个n的排列,求有多少个数对在三个排列中顺序相同

【题解】

考虑用补集转化的方法,答案为总对数-不满足的对数

一对数不满足条件,当且仅当这对数在两个排列中顺序相同,在另一个排列中的顺序不同。

统计两两之间不满足偏序条件的数对的个数,那么每对数都被统计了两次

#include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
using namespace std;
const int maxn=200010;
int n,a[4][maxn],b[maxn],t[maxn];
LL ans,tmp;
void read(int &k){
	k=0; int f=1; char c=getchar();
	while(c<‘0‘||c>‘9‘)c==‘-‘&&(f=-1),c=getchar();
	while(‘0‘<=c&&c<=‘9‘)k=k*10+c-‘0‘,c=getchar();
	k*=f;
}
void add(int x){for(;x<=n;x+=(x&-x)) t[x]++;}
int query(int x){int ret=0; for(;x>0;x-=(x&-x)) ret+=t[x]; return ret;}
int main(){
	read(n);
	for (int j=1;j<=3;j++)
		for (int i=1;i<=n;i++) read(a[j][i]);
	for (int i=1;i<3;i++)
	for (int j=i+1;j<=3;j++){
		memset(t,0,sizeof(t)); tmp=0;
		for (int k=1;k<=n;k++) b[a[i][k]]=k;
		for (int k=1;k<=n;k++) a[j][k]=b[a[j][k]];
		for (int k=1;k<=n;k++) tmp+=query(a[j][k]),add(a[j][k]);
		ans+=1LL*n*(n-1)/2-tmp;
	}
	return printf("%lld\n",1LL*n*(n-1)/2-ans/2),0;
}

  

时间: 2024-10-16 02:39:39

BZOJ 4430 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

【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

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

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>

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

越狱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

【BZOJ】[HNOI2009]有趣的数列

[算法]Catalan数 [题解] 学了卡特兰数就会啦>_<! 因为奇偶各自递增,所以确定了奇偶各自的数字后排列唯一. 那么就是给2n个数分奇偶了,是不是有点像入栈出栈序呢. 将做偶数标为-1,做奇数标为+1,显然当偶数多于奇数时不合法,因为它压不住后面的奇数. 然后其实这种题目,打表就可知啦--QAQ 然后问题就是求1/(n+1)*C(2n,n)%p了,p不一定是素数. 参考bzoj礼物的解法. 看到网上清一色的素数筛+分解质因数解法,不解了好久,感觉写了假的礼物-- 后来觉得礼物的做法才比