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 evening. The camels they rode will also participate and it is customary to bet on the results of the race. One of the most interesting bets involves guessing the complete order in which the camels will finish the race. This bet offers the biggest return on your money, since it is also the one that is the hardest to get right.

Jaap, Jan, and Thijs have already placed their bets, but the race will
not start until an hour from now, so they are getting bored. They
started wondering how many pairs of camels they have put in the same
order. If camel c is before camel d on Jaap’s, Jan’s and Thijs’ bet, it
means that all three of them put c and d in the same order. Can you help
them to calculate the number of pairs of camels for which this
happened?

Input

The input consists of:

  • one line with an integer n (2 ≤ n ≤ 200 000), the number of camels;
  • one line with n integers a1,...,an (1≤ai≤n for all i), Jaap’s bet. Here a1 is the camel in the first position of Jaap’s bet, a2 is the camel in the second position, and so on;
  • one line with Jan’s bet, in the same format as Jaap’s bet;
  • one line with Thijs’ bet, in the same format as Jaap’s bet.

The camels are numbered 1, … , n. Each camel appears exactly once in each bet.

Output

Output the number of pairs of camels that appear in the same order in all 3 bets.

Sample Input

Sample input 1

3

3 2 1

1 2 3

1 2 3

Sample input 2

4

2 3 1 4

2 1 4 3

2 4 3 1

Sample Output

Sample output 1

0

Sample output 2

3

固定一个顺序,查找另一个顺序,贴代码主要是学习一下高效输入。

//(总对数减去不同的)/2;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
#define lowbit(x) x&(-x)
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define mem(a) (memset(a,0,sizeof(a)))
int a[4][200006],pos[200006],vis[200006],n;
inline int iread(){
    int f = 1, x = 0; char ch = getchar();
    for(; ch < ‘0‘ || ch > ‘9‘; ch=getchar())f = ch==‘-‘?-1:1;
    for(; ch <= ‘9‘ && ch >= ‘0‘; ch=getchar())x = x*10+ch-‘0‘;
    return f*x;
}
inline void add(int x)
{
    for(int i=x;i<=n;i+=lowbit(i))
    {
        vis[i]+=1;
    }
}
inline ll query(int x)
{
    ll ans=0;
    for(int i=x;i;i-=lowbit(i))
    {
        ans+=vis[i];
    }
    return ans;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        ll ans=0;
        for(int i=0;i<3;i++)
        {
            for(int j=1;j<=n;j++)
            {
                a[i][j]=iread();
                //scanf("%d",&a[i][j]);
            }
        }
        for(int i=0;i<3;i++)
        {
            memset(vis,0,sizeof(vis));
            for(int j=1;j<=n;j++)
            {
                pos[a[i][j]]=j;
            }
            for(int j=n;j;j--)
            {
                ans+=query(pos[a[(i+1)%3][j]]);
                add(pos[a[(i+1)%3][j]]);
            }
        }
        printf("%lld\n",(1ll*n*(n-1)-ans)>>1);
    }
    return 0;
}
时间: 2024-10-11 22:09:23

BZOJ 4430 Guessing Camels的相关文章

BZOJ 4430 Guessing Camels赌骆驼

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

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

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: [Nwerc2015]Guessing Camels赌骆驼

[题意]给定三个长度为n的排列,求在三个排列中顺序相同的数对个数. [算法]逆序对 [题解]很容易联想到NOIP火柴排队,涉及顺序问题显然和逆序对息息相关. 一个数对如果在三个排列中顺序不同,一定是1+2或2+1,也就是只在两数列之间顺序相同. 所以对三个数列两两求逆序对总数num,则不满足要求的数对一定会产生且仅产生两个逆序对,ans=n*(n-1)/2-num/2. #include<cstdio> #include<cstring> #include<algorithm

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 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礼物的解法. 看到网上清一色的素数筛+分解质因数解法,不解了好久,感觉写了假的礼物-- 后来觉得礼物的做法才比

洛谷 P2709 BZOJ 3781 小B的询问

题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重复次数.小B请你帮助他回答询问. 输入输出格式 输入格式: 第一行,三个整数N.M.K. 第二行,N个整数,表示小B的序列. 接下来的M行,每行两个整数L.R. 输出格式: M行,每行一个整数,其中第i行的整数表示第i个询问的答案. 输入输出样例 输入样例#1: 6 4 3 1 3 2 1 1 3