【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 ...
随机推荐
- PHP 签到,与时间获取,数组长度获取
本文实例讲述了php实现签到功能的方法.分享给大家供大家参考,具体如下:首先我在数据库里建了两张表,一个是用户的积分表,一个是签到状态表,分来用来记录用户的积分数和先到状态 在用户签到状态表中我们有一 ...
- iOS-明杰解决字段冲突,及数组映射
/** 替换关键字的属性名 */ + (NSDictionary *)mj_replacedKeyFromPropertyName{ return @{@"UUID":@" ...
- python中装饰器的原理以及实现,
python版本 3.6 1.python的装饰器说白了就是闭包函数的一种应用场景,在运用的时候我们遵循 #开放封闭原则:对修改封闭,对拓展开放 2.什么是装饰器 #装饰他人的器具,本身可以是任意可调 ...
- 【linux】- nohup 和 &
&的意思是在后台运行, 什么意思呢? 意思是说,当你在执行 ./a.out & 的时候,即使你用ctrl C,那么a.out照样运行(因为对SIGINT信号免疫).但是要注意,如果你直 ...
- 错误 10 非静态的字段、方法或属性“Test10.Program.a”要求对象引用
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test ...
- getGeneratedKeys自动获取主键的方法
public class Demo { public static void main(String[] args) { try { String sql="insert into pers ...
- 【python】如何查看已经安装的python软件包和版本
pip 是一个安装和管理 Python 包的工具 , 是 easy_install 的一个替换品. pip freeze可以查看已经安装的python软件包和版本 pip list 也可以
- Lucene笔记二
lucene 的排序 package cn.itcast.lucene; import java.io.IOException; import org.apache.lucene.document.D ...
- Qt基本控件及三大布局
Qt基本控件及三大布局 来源: http://blog.csdn.net/a2604539133/article/details/73920696 Qt基本模块 一.Qt的三大布局 QHBoxLayo ...
- 【原创】宿主机远程登录虚拟机(windows server 2003系统)
转载请注明,谢谢合作 1.虚拟机网络设置为 “桥接模式”如图 2.系统装好并登陆后 右键点击我的电脑,点击属性,然后在弹出来的选择框中勾选远程桌面-->启用这台计算机的远程桌面 然后点添加-- ...