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 诶?我 ...
随机推荐
- SystemTap使用技巧 1 - 4 非常重要
http://blog.csdn.net/wangzuxi/article/details/42849053
- ReactNavtive框架教程(3)
原文:http://www.raywenderlich.com/99473/introducing-react-native-building-apps-javascript 注意:全部图片放在了百度 ...
- 使用azure send grid发送email
1. create a send grid account 2. remember the username/password of the send grid account watermark/2 ...
- 在 Edison 上自动启动 Arduino Sketch
前言 原创文章,转载引用务必注明链接,水平有限,如有疏漏,欢迎指正. 本文使用Markdown写成,为获得更好的阅读体验和正常的链接.图片显示,请访问我的博客原文: http://www.cnblog ...
- 多线程网页爬虫 python 实现(二)
#!/usr/bin/env python #coding=utf-8 import threading import urllib import re import time cur=0 last= ...
- 阿里云 ubuntu 14.04 模板上安装 docker
ubuntu 14.04 的内核是 3.13 ,所以内核不用升级. 安装过程例如以下: # apt-get update # apt-get install apt-transport-https # ...
- 使用 C# 开发智能手机软件:推箱子(四)
这是"使用 C# 开发智能手机软件:推箱子"系列文章的第四篇. 在这篇文章中,介绍 Common/FindPath.cs 源程序文件. using System; using Sy ...
- iOS设计模式 - (3)简单工厂模式
iOS设计模式 - (3)简单工厂模式 by Colin丶 转载请注明出处: http://blog.csdn.net/hitwhylz/article/ ...
- linux 命令之 watch
watch能够帮你监測一个命令的执行结果,省得你一遍遍的手动执行.在Linux下.watch是周期性的执行下个程序.并全屏显示执行结果.你能够拿他来监測你想要的一切命令的结果变化,比方 tail 一个 ...
- easyUI datagrid组件能否有display:none的隐藏效果
这个项目用了JQ easyUI datagrid 组件,我今天做了一个页面,页面有个div层,div里放了一个easyUI datagrid,页面初始化时div隐藏(display:none),通过点 ...