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 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 file contains several test cases, each of them as described below. Each test case consists of:

    ? one line with an integer n (2 ≤ n ≤ 200000), 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

   For each test case, output the number of pairs of camels that appear in the same order in all 3 bets on a line by itself.

Sample Input

   3

  3 2 1

  1 2 3

  1 2 3

  4

  2 3 1 4

  2 1 4 3

  2 4 3 1

Sample Output

  0

  3

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #define ll long long
 5 #define lowbit(x) x&(-x)
 6 using namespace std;
 7 const int MAX = 200006;
 8 int n,tree[MAX],a[MAX],b[MAX],c[MAX],pos[MAX];
 9 inline int iread(){
10     int f = 1, x = 0; char ch = getchar();
11     for(; ch < ‘0‘ || ch > ‘9‘; ch=getchar())f = ch==‘-‘?-1:1;
12     for(; ch <= ‘9‘ && ch >= ‘0‘; ch=getchar())x = x*10+ch-‘0‘;
13     return f*x;
14 }
15 inline void add(int x){
16     while(x < MAX){
17         tree[x] ++;
18         x += lowbit(x);
19     }
20 }
21 inline ll query(int x){
22     ll sum = 0;
23     while(x > 0){
24         sum += (ll)tree[x];
25         x -= lowbit(x);
26     }
27     return sum;
28 }
29 inline ll solve(int *x, int *y){
30     ll res = 0;
31     memset(tree,0,sizeof(tree));
32     for(int i = 1; i <= n; i ++) pos[x[i]] = i;
33     for(int i = n; i; i--) res += query(pos[y[i]]), add(pos[y[i]]);
34     return res;
35 }
36 int main(){
37     while(scanf("%d",&n)!=EOF){
38         for(int i = 1; i <= n; i ++)a[i] = iread();
39         for(int i = 1; i <= n; i ++)b[i] = iread();
40         for(int i = 1; i <= n; i ++)c[i] = iread();
41         ll ans = 1ll*n*(n-1);
42         ans -= solve(a,b)+solve(c,a)+solve(b,c);
43         cout << ans/2 << endl;
44     }
45     return 0;
46 }
时间: 2024-10-01 02:37:04

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

BZOJ 4430 Guessing Camels赌骆驼

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

【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

uva 1521 - GCD Guessing Game(贪心)

题目链接:uva 1521 - GCD Guessing Game 题目大意:给定一个数N,现在又一个数x,在1~N之间,现在每次可以猜一个数a,返回gcd(x,a),问说最少猜几次可以确定x. 解题思路:其实就将1~N里面的素数都要考虑一遍,因为有一个N的限制,所以每次选出来的素数的积不大于N即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const

Haybale Guessing

Time Limit: 1000MS   Memory Limit: 65536K       Description The cows, who always have an inferiority complex about their intelligence, have a new guessing game to sharpen their brains. A designated 'Hay Cow' hides behind the barn and creates N (1 ≤ N

Spoj-NPC2015A Eefun Guessing Words

Eefun Guessing Words Eefun is currently learning to read. His way of learning  is unique, by trying to make every possible substring from a given string. However, when the string becomes longer, he can no longer remember all the substring. His friend

2632: [neerc2011]Gcd guessing game

2632: [neerc2011]Gcd guessing game Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 144  Solved: 84[Submit][Status][Discuss] Description 给定一个数n ,有一个数x , ( 1<=x<=n ) 每次你可以猜在[1,n]中的数,假设是y,如果x==y,游戏结束,如果没猜中,则告诉你gcd(x,y),然后继续猜,直到猜中为止. 现在问给定n的情况下,最坏情况下最少要