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[][],pos[],vis[],n;
inline int iread(){
int f = , x = ; char ch = getchar();
for(; ch < '' || ch > ''; ch=getchar())f = ch=='-'?-:;
for(; ch <= '' && ch >= ''; ch=getchar())x = x*+ch-'';
return f*x;
}
inline void add(int x)
{
for(int i=x;i<=n;i+=lowbit(i))
{
vis[i]+=;
}
}
inline ll query(int x)
{
ll ans=;
for(int i=x;i;i-=lowbit(i))
{
ans+=vis[i];
}
return ans;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
ll ans=;
for(int i=;i<;i++)
{
for(int j=;j<=n;j++)
{
a[i][j]=iread();
//scanf("%d",&a[i][j]);
}
}
for(int i=;i<;i++)
{
memset(vis,,sizeof(vis));
for(int j=;j<=n;j++)
{
pos[a[i][j]]=j;
}
for(int j=n;j;j--)
{
ans+=query(pos[a[(i+)%][j]]);
add(pos[a[(i+)%][j]]);
}
}
printf("%lld\n",(1ll*n*(n-)-ans)>>);
}
return ;
}

BZOJ 4430 Guessing Camels的更多相关文章

  1. BZOJ 4430 Guessing Camels赌骆驼

    [题意概述] 给出三个n的排列,求有多少个数对在三个排列中顺序相同 [题解] 考虑用补集转化的方法,答案为总对数-不满足的对数 一对数不满足条件,当且仅当这对数在两个排列中顺序相同,在另一个排列中的顺 ...

  2. bzoj 4430: [Nwerc2015]Guessing Camels赌骆驼

    4430: [Nwerc2015]Guessing Camels赌骆 Description Jaap, Jan, and Thijs are on a trip to the desert afte ...

  3. 【BZOJ】4430: [Nwerc2015]Guessing Camels赌骆驼

    [题意]给定三个长度为n的排列,求在三个排列中顺序相同的数对个数. [算法]逆序对 [题解]很容易联想到NOIP火柴排队,涉及顺序问题显然和逆序对息息相关. 一个数对如果在三个排列中顺序不同,一定是1 ...

  4. bzoj4430 [Nwerc2015]Guessing Camels赌骆驼

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4430 [题解] 把每只骆驼在第一个人.第二个人.第三个人的位置找出来,然后做三维偏序即可. ...

  5. 【容斥原理】【推导】【树状数组】Gym - 101485G - Guessing Camels

    题意:给你三个1~n的排列a,b,c,问你在 (i,j)(1<=i<=n,1<=j<=n,i≠j),有多少个有序实数对(i,j)满足在三个排列中,i都在j的前面. 暴力求的话是 ...

  6. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  7. Gym101485: NWERC 2015(队内第6次训练)

    A .Assigning Workstations 题意:给定N个人的工作时间和工作时长,我们可以假设有无数台工作机器,如果一台机器超过M时间未使用就会关闭,那么我们怎么安排机器的使用,使得需要开启机 ...

  8. 2015-2016 Northwestern European Regional Contest (NWERC 2015)

    训练时间:2019-04-05 一场读错三个题,队友恨不得手刃了我这个坑B. A I J 简单,不写了. C - Cleaning Pipes (Gym - 101485C) 对于有公共点的管道建边, ...

  9. bzoj 2632 [neerc2011]Gcd guessing game——贪心(存疑)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2632 官方题解:http://neerc.ifmo.ru/archive/2011/neer ...

随机推荐

  1. ormlite 中的onUpgrade

    public class DBHelper extends OrmLiteSqliteOpenHelper { public static final String DB_NAME = "y ...

  2. hdu 5269 ZYB loves Xor I &amp;&amp; BestCoder Round #44

    题意: ZYB喜欢研究Xor,如今他得到了一个长度为n的数组A. 于是他想知道:对于全部数对(i,j)(i∈[1,n],j∈[1,n]).lowbit(AixorAj)之和为多少.因为答案可能过大,你 ...

  3. [javase学习笔记]-6.7 封装

    这一节我们学习面向对象中的第一个特性,封装(encapsulation) 封装:是指隐藏对象的发生和实现细节,仅对外提供公共訪问方式. 那么什么是隐藏对象的实现细节? 我们来举例学习. 比方我们来定义 ...

  4. POJ 3187 全排列+杨辉三角(组合数)

    思路: next_permutation()加个递推组合数随便搞搞就A了- //By SiriusRen #include <cstdio> #include <algorithm& ...

  5. Boostrap零散

    12 row 是核心控件 class="form-control" 弹窗口<input data-toggle="modal" data-target=& ...

  6. Hive框架基础(二)

    * Hive框架基础(二) 我们继续讨论hive框架 * Hive的外部表与内部表 内部表:hive默认创建的是内部表 例如: create table table001 (name string , ...

  7. <Sicily>Greatest Common Divisors

    一.题目描述 A common divisor for two positive numbers is a number which both numbers are divisible by. It ...

  8. ubuntu重启网络报错

    执行:gw@ubuntu:/$ /etc/init.d/networking restart 报错:stop: Rejected send message, 1 matched rules; type ...

  9. js文字的无缝滚动(上下)

    使用scrolltop值的递增配合setInterval与setTimeout实现相关效果,左右无缝滚动使用scrollLeft即可 Dom内容 <div id="container& ...

  10. 运行npm start vue.js项目 出现 npm ERR! missing script: start 错误

    npm ERR! missing script: start 错误 有可能缺少依赖包,运行nmp install安装依赖(一般都依赖很多包,过程有点慢),安装完后发现多一个 node_modules文 ...