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 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 cc 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?
Jaap,Jan和Thijs在摩洛哥参加完ACMICPC2015全球总决赛后去沙漠进行了一次旅行。旅行包括了骑骆驼。在骑完回来之后,他们的向导邀请他们在晚上去看一场盛大的骆驼赛跑。他们骑的骆驼也会参加,而赌比赛的结果是一个习惯。其中一个最有趣的赌涉及猜测完成比赛的骆驼的完整名单。这个赌可以为你赢得最多的钱,因为它也是最难猜对的。
Jaap,Jan和Thijs已经下了注,但比赛要在一个小时后才开始,所以他们觉得很无聊。他们开始想知道有多少对骆驼他们赌了同样的顺序。如果在他们三人的猜测名单中,骆驼c在骆驼d前,就意味着c和d在他们的名单中有相同的顺序。你能帮他们计算有多少对骆驼是这样的吗?

Input

The input consists of:
one line with an integer n ( 2≤n≤200000), the number of camels;
one line with n integers a1,…,ana1,…,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.
输入包括:
第一行是一个整数n(2<=n<=200000),骆驼的数量。
第二行有n个整数a1…an(1<=ai<=n),是Jaap的下赌名单。a1是名单中第一位,a2是第二位,等等。
第三行是Jan的名单,格式同上。
第四行是Thijs的名单,格式同上。
骆驼从1到n编号,每头骆驼在一份名单中只出现一次。

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
题解:
先考虑怎么算两个序列不相同的(i,j)个数。
使用树状数组,用id[i]记录a[i]在a数组中的位置,然后倒着循环,在a数组中以b[i]结尾的个数就是1到i的前缀和,更新就是在id[b[i]]的位置加1,为什么是这样呢???
我们是从尾开始枚举的,这样假如这个数在a数组中有b数组之后位置的数,那么就是有问题的。
然后发现ans=(n*(n-1)-不相同数)/2
#include<stdio.h>
#include<iostream>
using namespace std;
const int N=;
int n,i,a[N],b[N],c[N],t[N],p[N];
long long ans;
void update(int x)
{
while(x<=n)
{
t[x]++;
x+=x&-x;
}
}
int solve(int x)
{
int ans=;
while(x>)
{
ans+=t[x];
x-=x&-x;
}
return ans;
}
int main()
{
scanf("%d",&n);
for(i=;i<=n;i++) scanf("%d",&a[i]);
for(i=;i<=n;i++) scanf("%d",&b[i]);
for(i=;i<=n;i++) scanf("%d",&c[i]);
for(i=;i<=n;i++) p[a[i]]=i;
for(i=n;i>=;i--)
{
ans+=solve(p[b[i]]);
update(p[b[i]]);
}
for(i=;i<=n;i++) p[b[i]]=i,t[i]=;
for(i=n;i>=;i--)
{
ans+=solve(p[c[i]]);
update(p[c[i]]);
}
for(i=;i<=n;i++) p[c[i]]=i,t[i]=;
for(i=n;i>=;i--)
{
ans+=solve(p[a[i]]);
update(p[a[i]]);
}
cout<<((1LL*n*(n-)-ans)>>);
return ;
}

bzoj 4430: [Nwerc2015]Guessing Camels赌骆驼的更多相关文章

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

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

  2. bzoj4430 [Nwerc2015]Guessing Camels赌骆驼

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

  3. BZOJ 4430 Guessing Camels赌骆驼

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

  4. BZOJ 4430 Guessing Camels

    Description Jaap, Jan, and Thijs are on a trip to the desert after having attended the ACM ICPC Worl ...

  5. bzoj 4428: [Nwerc2015]Debugging调试

    4428: [Nwerc2015]Debugging调试 Description Your fancy debugger will not help you in this matter. There ...

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

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

  7. BZOJ 4425: [Nwerc2015]Assigning Workstations分配工作站

    难度在于读题 #include<cstdio> #include<algorithm> #include<queue> using namespace std; p ...

  8. bzoj AC倒序

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

  9. 越狱Season 1-Episode 13: End of the Tunnel

    Season 1, Episode 13: End of the Tunnel -Fernando: The name is John Abruzzi. 名字是John Abruzzi A b r u ...

随机推荐

  1. AndroidStudio获得发布版安全码SHA1

    耗了一下午才搞定 在cmd中: 1.打开keytool的目录:即JDK的安装目录 2.输入口令: (E:\tenyears\tenyears\app是keystore文件的目录)

  2. CMD命令行下载文件

    远程执行sct的另一种姿势 cscript /b C:\Windows\System32\Printing_Admin_Scripts\zh-CN\pubprn.vbs 127.0.0.1 scrip ...

  3. Python参数输入模块-optparse

    废话: 模块名是optparse, 很多人打成optparser.以至于我一直导入导入不了.搞的不知所以. 模块的使用: import optparse #usage 定义的是使用方法,%prog 表 ...

  4. 一文看懂IC芯片生产流程:从设计到制造与封装

    http://blog.csdn.net/yazhouren/article/details/50810114 芯片制造的过程就如同用乐高盖房子一样,先有晶圆作为地基,再层层往上叠的芯片制造流程后,就 ...

  5. JS面试题第一弹

    1.javascript的typeof返回哪些数据类型  alert(typeof [1, 2]); //object     alert(typeof 'leipeng'); //string   ...

  6. MyBatis 模糊查询 防止Sql注入

    #{xxx},使用的是PreparedStatement,会有类型转换,所以比较安全: ${xxx},使用字符串拼接,可以SQL注入: like查询不小心会有漏洞,正确写法如下:   Mysql:   ...

  7. 《java并发编程实战》读书笔记5--任务执行, Executor框架

    第6章 任务执行 6.1 在线程中执行任务 第一步要找出清晰的任务边界.大多数服务器应用程序都提供了一种自然的任务边界选择方式:以独立的请求为边界. -6.6.1 串行地执行任务 最简单的任务调度策略 ...

  8. .net/c#常用框架/中间件简介(不定时更新)

    任务调度 Quartz.NET:Quartz.NET是一个开源的作业调度框架,非常适合在平时的工作中,定时轮询数据库同步,定时邮件通知,定时处理数据等. Quartz.NET允 许开发人员根据时间间隔 ...

  9. sql server 完整备份、差异备份、事务日志备份

    一. 理解: 完整备份为基础, 完整备份可以实物回滚还原,但是由于完整备份文件过大,对硬盘空间比较浪费这是就需要差异备份 或者 事务日志备份. 差异备份还原时,只能还原到备份的那个点, 日志备份还原时 ...

  10. Vue结合原生js实现自定义组件自动生成

    就目前三大前端主流数据驱动框架(vue,ng,react)而言,均具有创建自定义组件的api,但都是必须先做到事先写好挂载点,这个挂载点可以是原有静态元素标签也可以是自定义模板:对于多种组件通过同一数 ...