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. matlab 中语句 a(b)的意思

    a=[1 2; 3, 4; 5, 6]; b=[1 3 2 1; 1 2 4 2]; c=a(b) c =                 1     5     3     1 1     3   ...

  2. Win7+VS2010:mysql 源代码编译与调试

    win7+vs2010源代码编译mysql 近期因为在实习,工作重点在于一些数据库的开发,为了更好的理解数据库的实现机制.眼下萌生了要研究一下mysql数据库源代码的想法.那么好吧,说干就干.首先我们 ...

  3. leetcode笔记:Merge Sorted Array

    一.题目描写叙述 二.解题技巧 这道题不存在复杂的分析过程和边界条件.假设单纯得考虑从小到大地将两个数组进行合并的话.每次在num1中插入一个数的话,须要将后面的元素都向后移动一位.这样.整个处理过程 ...

  4. 在KVM中执行windows 10虚机(by quqi99)

    作者:张华  发表于:2015-12-22版权声明:能够随意转载.转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 ( http://blog.csdn.net/quqi99 ) KVM ...

  5. 拥抱PBO(基于项目的组织)聚焦核心价值创造

    近年来.PBO(Project-Based Organizations)作为一种新兴的整合各类专业智力资源和专业知识的组织结构,受到越来越多的关注,第五版PMBOK出现的新词汇.三种组织(职能型.矩阵 ...

  6. 在cncc的最后几天的笔记

    数据库范式:http://blog.csdn.net/fg2006/article/details/6936439 数据库事务隔离级别:http://blog.csdn.net/fg2006/arti ...

  7. centOS7下 安装nodejs+nginx+mongodb+pm2部署vue项目

    一.购买服务器并远程连接 1.购买服务器和域名 可以选择阿里云或者是其他的厂商的服务器.然后会获得服务器ip地址,用户名和密码. 购买域名,将域名绑定到ip地址上. 2.下载xshell,winscp ...

  8. 关联Anaconda和最新Pycharm2018.3.2

    在Anaconda和Pycharm 2018.3.2 x64都安装好之后,进行Anaconda 与Pycharm的关联操作 首先File -->New Project 打开以后切记要把Proje ...

  9. Linux下的压缩与解压缩

    1 .gz 1)压缩 root@xiaohuang-virtual-machine:/home/xiaohuang/桌面/hellow/hellow# gzip 2.txt 3.txt root@xi ...

  10. mySQL主从复制实战

    随着访问量的不断增加,单台MySQL数据库服务器压力不断增加,需要对MYSQL进行优化和架构改造,MYQSL优化如果不能明显改善压力情况,可以使用高可用.主从复制.读写分离来.拆分库.拆分表来进行优化 ...