【BZOJ】4430: [Nwerc2015]Guessing Camels赌骆驼
【题意】给定三个长度为n的排列,求在三个排列中顺序相同的数对个数。
【算法】逆序对
【题解】很容易联想到NOIP火柴排队,涉及顺序问题显然和逆序对息息相关。
一个数对如果在三个排列中顺序不同,一定是1+2或2+1,也就是只在两数列之间顺序相同。
所以对三个数列两两求逆序对总数num,则不满足要求的数对一定会产生且仅产生两个逆序对,ans=n*(n-1)/2-num/2。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#define lowbit(x) x&-x
#define ll long long
using namespace std;
int read(){
char c;int s=,t=;
while(!isdigit(c=getchar()))if(c=='-')t=-;
do{s=s*+c-'';}while(isdigit(c=getchar()));
return s*t;
}
const int maxn=;
ll ans;
int d[maxn],n,A[maxn];
struct cyc{int num,id;}a[maxn],b[maxn],c[maxn];
bool cmp(cyc a,cyc b){return a.num<b.num;}
void modify(int x){for(int i=x;i<=n;i+=lowbit(i))d[i]++;}
int query(int x){int ans=;for(int i=x;i>=;i-=lowbit(i))ans+=d[i];return ans;}
void calc(cyc a[],cyc b[]){
memset(d,,sizeof(d));
for(int i=;i<=n;i++)A[a[i].id]=b[i].id;
for(int i=;i<=n;i++){
modify(A[i]);
ans+=i-query(A[i]);
}
}
int main(){
n=read();
for(int i=;i<=n;i++)a[i].num=read(),a[i].id=i;
for(int i=;i<=n;i++)b[i].num=read(),b[i].id=i;
for(int i=;i<=n;i++)c[i].num=read(),c[i].id=i;
sort(a+,a+n+,cmp);sort(b+,b+n+,cmp);sort(c+,c+n+,cmp);
ans=;
calc(a,b);calc(b,c);calc(a,c);
printf("%lld",1ll*n*(n-)/-ans/);
return ;
}
【BZOJ】4430: [Nwerc2015]Guessing Camels赌骆驼的更多相关文章
- bzoj 4430: [Nwerc2015]Guessing Camels赌骆驼
4430: [Nwerc2015]Guessing Camels赌骆 Description Jaap, Jan, and Thijs are on a trip to the desert afte ...
- bzoj4430 [Nwerc2015]Guessing Camels赌骆驼
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4430 [题解] 把每只骆驼在第一个人.第二个人.第三个人的位置找出来,然后做三维偏序即可. ...
- BZOJ 4430 Guessing Camels赌骆驼
[题意概述] 给出三个n的排列,求有多少个数对在三个排列中顺序相同 [题解] 考虑用补集转化的方法,答案为总对数-不满足的对数 一对数不满足条件,当且仅当这对数在两个排列中顺序相同,在另一个排列中的顺 ...
- BZOJ 4430 Guessing Camels
Description Jaap, Jan, and Thijs are on a trip to the desert after having attended the ACM ICPC Worl ...
- bzoj 4428: [Nwerc2015]Debugging调试
4428: [Nwerc2015]Debugging调试 Description Your fancy debugger will not help you in this matter. There ...
- 【容斥原理】【推导】【树状数组】Gym - 101485G - Guessing Camels
题意:给你三个1~n的排列a,b,c,问你在 (i,j)(1<=i<=n,1<=j<=n,i≠j),有多少个有序实数对(i,j)满足在三个排列中,i都在j的前面. 暴力求的话是 ...
- BZOJ 4425: [Nwerc2015]Assigning Workstations分配工作站
难度在于读题 #include<cstdio> #include<algorithm> #include<queue> using namespace std; p ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- 越狱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 ...
随机推荐
- unordered_map(hash_map)和map的比较
测试代码: #include <iostream> using namespace std; #include <string> #include <windows.h& ...
- 刚装的vs无法运行正确的程序
- linux svn apache
借助apache运行的svn服务器 一:安装Apache HTTP Server 1.安装Apache HTTP Server yum install httpd httpd-devel -y 2.修 ...
- [C/C++] 友元函数和友元类
A---友元函数: class Data{ public: ... friend int f(int &m);//友元函数 ... } 友元函数是可以直接访问类的私有成员的非成员函数.它是定义 ...
- 【Python】Python中的列表操作
Python的列表操作可谓是功能强大且方便(相对于Java)简单.常规的操作就不说了(这不是一个入门教程),介绍几个很有特点的例子 添加 # 追加到结尾(append) li = [1, 2, 3, ...
- BZOJ 2333 棘手的操作(离线+线段树+带权并查集)
这题搞了我一天啊...拍不出错原来是因为极限数据就RE了啊,竟然返回WA啊.我的线段树要开8倍才能过啊... 首先可以发现除了那个加边操作,其他的操作有点像线段树啊.如果我们把每次询问的联通块都放在一 ...
- bzoj 1103: [POI2007]大都市meg (dfs序)
dfs序,加个bit维护前缀和就行了 type arr=record toward,next:longint; end; const maxn=; var edge:..maxn]of arr; bi ...
- BZOJ1503:[NOI2004]郁闷的出纳员——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1503 (题面复制的洛谷的,因为洛谷好看) 题目描述 OIER公司是一家大型专业化软件公司,有着数以万 ...
- [BJOI2018]求和
link 其实可以用$sum(i,j)$表示从$i$到$1$的$k$次方的值,然后就是$lca$的基本操作 注意,能一起干的事情就一起搞,要不会超时 #include<iostream> ...
- mysql五补充部分:SQL逻辑查询语句执行顺序
一 SELECT语句关键字的定义顺序 SELECT DISTINCT <select_list> FROM <left_table> <join_type> JOI ...