Guessing Camels (***)

Posted starry

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }

 

以上是关于Guessing Camels (***)的主要内容,如果未能解决你的问题,请参考以下文章

bzoj 4430: [Nwerc2015]Guessing Camels赌骆驼

BZOJ 4430 Guessing Camels赌骆驼

bzoj4430 [Nwerc2015]Guessing Camels赌骆驼

BZOJ4430: [Nwerc2015]Guessing Camels赌骆驼

[CF] E. Camels

codeforces 14E. Camels(多维dp)