P2534 [AHOI2012]铁盘整理

题目描述

输入输出格式

输入格式:

共两行。第一行为铁盘个数N(1<=N<=50),第二行为N个不同的正整数,分别为从上到下的铁盘的半径R。(1<=R<=100)

输出格式:

一个正整数,表示使铁盘从小到大有序需要的最少翻转次数。

输入输出样例

输入样例#1:

5
2 4 3 5 1
输出样例#1:

5
//第一种:使用string.h中的strrev函数,只用于字符串
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char s[]="hello";
strrev(s);
cout<<s<<endl;
return ;
} //第二种:使用algorithm中的reverse函数,也适用于整型数组及其他存储结构
#include <iostream>
#include <string>
#include <algorithm>
using namespace std; int main()
{
string s="hello";
reverse(s.begin(),s.end());
cout<<s<<endl;
return ;
}

反转操作

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define mod1 2333333
#define mod2 1048577
#define maxn 51
using namespace std;
int n,t[maxn];
struct node{
int a[maxn],step;
}cur,nxt;
queue<node>q;
bool vis1[],vis2[];
int hash1(int x[]){
int res=;
for(int i=;i<=n;i++)res=(res*%mod1+x[i])*%mod1;
return res;
}
int hash2(int x[]){
int res=;
for(int i=;i<=n;i++)res=(res*%mod2+x[i])*%mod2;
return res;
}
int main(){
freopen("Cola.txt","r",stdin);
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%d",&cur.a[i]),t[i]=cur.a[i];
sort(t+,t+n+);
int t1=hash1(t),t2=hash2(t);
if(hash1(cur.a)==t1&&hash2(cur.a)==t2){puts("");return ;}
cur.step=;
q.push(cur);
while(!q.empty()){
cur=q.front();q.pop();
for(int i=;i<=n;i++){
nxt=cur;
reverse(nxt.a+,nxt.a+i+);
int h1=hash1(nxt.a),h2=hash2(nxt.a);
if(h1==t1&&h2==t2){
printf("%d",cur.step+);
return ;
}
if(!vis1[h1]||!vis2[h2]){
vis1[h1]=;vis2[h2]=;
nxt.step=cur.step+;
q.push(nxt);
}
}
}
return ;
}

20分 裸搜

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#define maxn 51
using namespace std;
int n,a[maxn];
struct node{
int w,id;
bool operator < (const node b)const{
return w<b.w;
}
}e[maxn];
int get(){
int res=;
for(int i=;i<=n;i++)
if(abs(a[i]-a[i-])!=)res++;
return res;
}
void dfs(int limit,int step){
int h=get();
if(!h&&a[]<a[]){
printf("%d",limit);
exit();
}
if(h+step>limit||step==limit)return;
for(int i=;i<=n;i++){
if(abs(a[i]-a[i+])!=){
reverse(a+,a+i+);
dfs(limit,step+);
reverse(a+,a+i+);
}
}
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%d",&e[i].w),e[i].id=i;
sort(e+,e+n+);
for(int i=;i<=n;i++)a[e[i].id]=i;
a[]=-0x7f7f7f7f;a[n+]=0x7f7f7f7f;
for(int k=;;k++)dfs(k,);
}

100分 迭代加深搜索

洛谷P2534 [AHOI2012]铁盘整理的更多相关文章

  1. 洛谷 P2534 [AHOI2012]铁盘整理

    P2534 [AHOI2012]铁盘整理 题目描述 输入输出格式 输入格式: 共两行.第一行为铁盘个数N(1<=N<=50),第二行为N个不同的正整数,分别为从上到下的铁盘的半径R.(1& ...

  2. BZOJ2824 AHOI2012 铁盘整理 【IDA*】

    BZOJ2824 AHOI2012 铁盘整理 Description 在训练中,一些臂力训练器材是少不了的,小龙在练习的时候发现举重器械上的铁盘放置的非常混乱,并没有按照从轻到重的顺序摆放,这样非常不 ...

  3. BZOJ 2824: [AHOI2012]铁盘整理

    BZOJ 2824: [AHOI2012]铁盘整理 标签(空格分隔): OI-BZOJ OI-搜索 Time Limit: 10 Sec Memory Limit: 128 MB Descriptio ...

  4. [AHOI2012]铁盘整理

    题目描述 输入输出格式 输入格式: 共两行.第一行为铁盘个数N(1<=N<=50),第二行为N个不同的正整数,分别为从上到下的铁盘的半径R.(1<=R<=100) 输出格式: ...

  5. 【题解】洛谷P2532 [AHOI2012]树屋阶梯(卡特兰数+高精)

    洛谷P2532:https://www.luogu.org/problemnew/show/P2532 思路 来自Sooke大佬的推导: https://www.luogu.org/blog/Sook ...

  6. (bzoj1337 || 洛谷P1742 最小圆覆盖 )|| (bzoj2823 || 洛谷P2533 [AHOI2012]信号塔)

    bzoj1337 洛谷P1742 用随机增量法.讲解:https://blog.csdn.net/jokerwyt/article/details/79221345 设点集A的最小覆盖圆为g(A) 可 ...

  7. 洛谷p2370yyy2015c01的U盘题解

    没什么特殊的想法 就是看自己很久没有更新关于题解类的文章了而已 (其实这是我好久之前做的, 只是把它从洛谷博客搬到了这里而已) 题目 首先分析题目要二分 他长成这个亚子太二分了 所以就要二分 最好是先 ...

  8. 洛谷P2532 [AHOI2012]树屋阶梯(Catalan数)

    P2532 [AHOI2012]树屋阶梯 题目描述 输入输出格式 输入格式: 一个正整数N(1<=N<=500),表示阶梯的高度. 输出格式: 一个正整数,表示搭建方法的个数.(注:搭建方 ...

  9. 洛谷 P1103 书本整理(动规)

    洛谷 P1103 书本整理 题目描述 Frank是一个非常喜爱整洁的人.他有一大堆书和一个书架,想要把书放在书架上.书架可以放下所有的书,所以Frank首先将书按高度顺序排列在书架上.但是Frank发 ...

随机推荐

  1. WCF寄宿(Host)之自我寄宿(Self-Hosting)简单实例【Windows应用程序宿主】

     前言: 以各种应用程序做自我寄宿的宿主原理方法大同小异,故:这儿直接上案例! 步骤一:创建服务契约和服务 1.新建解决方案:添加WCF服务库项目. 2.为了演示,我把自动生成的接口以及实现接口的类删 ...

  2. Leetcode 1002. Find Common Characters

    python可重集合操作 class Solution(object): def commonChars(self, A): """ :type A: List[str] ...

  3. Wannafly #4 F 线路规划

    数据范围252501 劲啊 Q国的监察院是一个神秘的组织. 这个组织掌握了整个Q国的地下力量,监察着Q国的每一个人. 监察院一共有N个成员,每一个成员都有且仅有1个直接上司,而他只听从其上直接司的命令 ...

  4. CodeForces - 1017E :The Supersonic Rocket (几何+KMP,判定凸包是否同构)

    After the war, the supersonic rocket became the most common public transportation. Each supersonic r ...

  5. 1068 Find More Coins (30)(30 分)

    Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...

  6. cmd cvf war包

    1.进入要打包的目录下 --> cmd d: cd \路径 jar -cvf 包名.war * 2.解压 进入需要解压的目录 cd /depa123/webapps/css jar -xvf / ...

  7. ACM学习历程—HDU4675 GCD of Sequence(莫比乌斯)

    Description Alice is playing a game with Bob. Alice shows N integers a 1, a 2, …, a N, and M, K. She ...

  8. ACM学习历程—HDU 5446 Unknown Treasure(数论)(2015长春网赛1010题)

    Problem Description On the way to the next secret treasure hiding place, the mathematician discovere ...

  9. BZOJ4695:最假女选手

    浅谈区间最值操作和历史最值问题:https://www.cnblogs.com/AKMer/p/10225100.html 题目传送门:https://lydsy.com/JudgeOnline/pr ...

  10. 洛谷【P1358】扑克牌

    我对状态空间的理解:https://www.cnblogs.com/AKMer/p/9622590.html 题目传送门:https://www.luogu.org/problemnew/show/P ...