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 ;
}

最新文章

  1. 【emWin】例程四:显示文本
  2. 关于在DataGrid.RowDetailsTemplate中的控件查找不到的问题
  3. 利用Spring MVC搭建REST Service
  4. 执行HQL语句出现Remember that ordinal parameters are 1-based
  5. POJ 1014 Dividing(多重背包)
  6. 使用Redis SETNX 命令实现分布式锁
  7. umask设置导致的weblogic中的应用上传的文件没有权限打开
  8. shape的属性
  9. eclipse安装svn插件,在输入url后,一直卡在in progress界面不懂。
  10. 将其它图片格式转为.eps格式
  11. CAN通信帧ID如何设定?
  12. 教你正确打开async/await关键字的使用
  13. hdu1839(最小生成树)
  14. 力扣(LeetCode)69. x 的平方根
  15. Jmeter(四)_16个逻辑控制器详解
  16. dedecms的if标签、foreach标签
  17. 按str 存储和按 list 存储
  18. c# ajax从后台获取数据list数组 $.each再显示数据
  19. 并查集(模板&amp;典型例题整理)
  20. CF1012B Chemical table 题解【二分图】【构造】

热门文章

  1. python学习--导入自己的包
  2. C#之改变窗体icon图标、新建类文件、调用dll库
  3. 【BZOJ 1257】[CQOI2007]余数之和
  4. 【图灵杯 F】一道简单的递推题(矩阵快速幂,乘法模板)
  5. POJ——T3352 Road Construction
  6. light oj 1317
  7. pandas groupby 分组操作
  8. hdoj--1869--六度分离(floyd)
  9. hdoj--1301--Jungle Roads(克鲁斯卡尔)
  10. docker 命令合集