码迷,mamicode.com
首页 > 其他好文 > 详细

Guessing Camels (***)

时间:2017-07-13 10:36:53      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:ges   form   order   pat   lowbit   help   this   air   ipa   

 

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 (***)

标签:ges   form   order   pat   lowbit   help   this   air   ipa   

原文地址:http://www.cnblogs.com/xingkongyihao/p/7158659.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!