BZOJ3990 排序
题目:www.lydsy.com/JudgeOnline/problem.php?id=3990
这题很不错。
刚开始时无从下手,想了好多$O((2^n)log(2^n))$ 的idea,但是都不行。
后来去看题解发现操作序列是满足交换率的,然后竟然是搜索。
因为swap是swap的逆运算(歪歪的)
然后只要从小到大枚举操作序列就可以了。
这样类似分治下去,当你在计算长度为$2^i$的序列时已经保证了所有长度为$2^{i-1}$的序列的「连续且递增」。
注意是「连续且递增」,开始W了好多发,然后推掉重写(开抄) 呜呜。
好像可以证明是$O(n \cdot 2^{2n})$ ?
以后见到这种操作数很小的题目要想想搜索,就算是暴力也可以多拿分。
#include <cstdio>
#include <cstring>
#include <vector> #define LL long long
#define N 13 using namespace std; int n;
LL fact[],ans; void change(vector<int> &x,int l1,int l2,int len){
for(int i=;i<len;i++)
swap(x[l1+i],x[l2+i]);
} bool check(vector<int> x,int l,int len){
for(int i=;i<len;i++)
if(x[l+i]!=x[l+i-]+) return ;
return ;
} void dfs(vector<int> x,int t,int now){
if(t==n){
ans+=fact[now];
return;
}
int tot=,a[];
for(int i=;i<(<<n);i+=(<<(t+)))
if(!check(x,i,<<(t+))){
if(tot==) return;
a[++tot]=i; a[++tot]=i+(<<t);
}
vector<int> b;
if(!tot) dfs(x,t+,now);
if(tot==){
if(x[a[]]+(<<t)==x[a[]]){
b=x;
change(b,a[],a[],<<t);
dfs(b,t+,now+);
}
}
if(tot==){
if(x[a[]]+(<<t)==x[a[]] && x[a[]]+(<<t)==x[a[]]){
b=x;
change(b,a[],a[],<<t);
dfs(b,t+,now+);
}
if(x[a[]]+(<<t)==x[a[]] && x[a[]]+(<<t)==x[a[]]){
b=x;
change(b,a[],a[],<<t);
dfs(b,t+,now+);
}
if(x[a[]]+(<<t)==x[a[]] && x[a[]]+(<<t)==x[a[]]){
b=x;
change(b,a[],a[],<<t);
dfs(b,t+,now+);
}
if(x[a[]]+(<<t)==x[a[]] && x[a[]]+(<<t)==x[a[]]){
b=x;
change(b,a[],a[],<<t);
dfs(b,t+,now+);
}
}
} vector<int> a; int main(){
scanf("%d",&n);
a.resize(<<n);
for(int i=;i<(<<n);i++) scanf("%d",&a[i]);
fact[]=;
for(int i=;i<=;i++) fact[i]=fact[i-]*(LL)i;
dfs(a,,);
printf("%lld\n",ans);
return ;
}
BZOJ3990 排序的更多相关文章
- BZOJ3990 排序(sort)
排序(sort) 题目描述 小A有一个1~2N的排列A[1..2N],他希望将数组A从小到大排序.小A可以执行的操作有N种,每种操作最多可以执行一次.对于所有的i(1<=i<=N),第i种 ...
- 【SDOI2015】bzoj3990 排序
A. 排序 题目描述 输入格式 输出格式 一行,一个整数,表示可以将数组A从小到大排序的不同的操作序列的个数. 样例 样例输入 3 7 8 5 6 1 2 4 3 样例输出 6 数据范围与提示 对于3 ...
- [BZOJ3990][SDOI2015]排序(DFS)
3990: [SDOI2015]排序 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 902 Solved: 463[Submit][Status][ ...
- Bzoj3990 [SDOI2015]排序
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 651 Solved: 338 Description 小A有一个1-2^N的排列A[1..2^N], ...
- BZOJ3990:[SDOI2015]排序——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=3990 小A有一个1-2^N的排列A[1..2^N],他希望将A数组从小到大排序,小A可以执行的操作 ...
- [bzoj3990][SDOI2015]排序-搜索
Brief Description 小A有一个1-2^N的排列A[1..2^N],他希望将A数组从小到大排序,小A可以执行的操作有N种,每种操作最多可以执行一次,对于所有的i(1<=i<= ...
- BZOJ3990 [SDOI2015]排序 【搜索】
题目 小A有一个1-2^N的排列A[1..2^N],他希望将A数组从小到大排序,小A可以执行的操作有N种,每种操作最多可以执行一次,对于所有的i(1<=i<=N),第i中操作为将序列从左到 ...
- [BZOJ3990]:[SDOI2015]排序(搜索)
题目传送门 题目描述 小A有一个1-${2}^{N}$的排列A[1..${2}^{N}$],他希望将A数组从小到大排序,小A可以执行的操作有N种,每种操作最多可以执行一次,对于所有的i(1≤i≤N), ...
- [BZOJ3990][SDOI2015][LOJ#2181]-排序
说实话,这个题真好(?) <BZOJ题面> <LOJ题面> 看到这个题,一时没有思路 但是 我想到了一个错解:归并 这个题真的有一点把我们的思路往归并上引 于是WA10 诶?我 ...
随机推荐
- 【面试 JDK】【第一篇】Object类面试详解
1.Object类有哪些方法 1>clone()方法 保护方法,实现对象的浅复制,只有实现了Cloneable接口才可以调用该方法,否则抛出CloneNotSupportedException异 ...
- xgboost原理及并行实现
XGBoost训练: It is not easy to train all the trees at once. Instead, we use an additive strategy: fix ...
- ubuntu uninstall postgres
Steps that worked for me on Ubuntu 8.04.2 to remove postgres 8.3 List All Postgres related packages ...
- poj 2154 Color 欧拉函数优化的ploya计数
枚举位移肯定超时,对于一个位移i.我们须要的是它的循环个数,也就是gcd(i,n),gcd(i,n)个数肯定不会非常多,由于等价于n的约数的个数. 所以我们枚举n的约数.对于一个约数k,也就是循环个数 ...
- java开始到熟悉63-65
本次内容:java常用类 1.包装类 package array; public class wrapperclass { public static void main(String[] args) ...
- LeetCode232 Implement Queue using Stacks Java 题解
题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the bac ...
- ORA-07445 第一參数为:kkqljpmpr
在版本号11.2.0.1.0上,在pl/sql developer中运行一条SQL会导致连接中断,这样的错误要到trace文件夹下找到错误日志文件,再定位.查了一下资料,是这个版本号的bug. D ...
- Network Booting
http://en.wikipedia.org/wiki/Network_booting Network booting Network booting is the process of booti ...
- AliYunDun关闭
停止阿里云盾AliYunDun服务解决大量写磁盘问题-小内存ECS服务器 阿里云数据库在没备案,涉及大量IO操作时会自动启动阿里云盾这个服务,会导致服务器变得很卡 关闭服务: service aegi ...
- 最长公共上升子序列 (poj 2127) (Greatest Common Increasing Subsequence)
\(Greatest Common Increasing Subsequence\) 大致题意:给出两个长度不一定相等的数列,求其中最长的公共的且单调递增的子序列(需要具体方案) \(solution ...