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. C++STL 常用 函数 用法(转)

    http://www.cnblogs.com/duoduo369/archive/2012/04/12/2439118.html 迭代器(iterator) 个人理解就是把所有和迭代有关的东西给抽象出 ...

  2. linux-常用指令2

    命令链接符                                                                                               ...

  3. 【遍历二叉树】11把二叉树转换成前序遍历的链表【Flatten Binary Tree to Linked List】

    本质上是二叉树的root->right->left遍历. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...

  4. 本地未安装Oracle数据库,如何连接远程Oracle数据库

    方法一:用Navicat Premium连接 注意,这里用的要是黄色的版本,而不是只针对Mysql的绿色版本 工具栏选择[工具]-[选项],点击[其他-OCI]    你会发现有个OCI librar ...

  5. poj 1845 Sumdiv(约数和,乘法逆元)

    题目: 求AB的正约数之和. 输入: A,B(0<=A,B<=5*107) 输出: 一个整数,AB的正约数之和 mod 9901. 思路: 根据正整数唯一分解定理,若一个正整数表示为:A= ...

  6. 对存在JavaScript隐式类型转换的四种情况的总结

    一般存在四种情况,JavaScript会对变量的数据类型进行转换. 目录 * if中的条件会被自动转为Boolean类型 * 会被转为false的数据 * 会被转为true的数据 * 参与+运算都会被 ...

  7. findBug 错误修改指南

      1. EC_UNRELATED_TYPESBug: Call to equals() comparing different types Pattern id: EC_UNRELATED_TYPE ...

  8. HDU1698(线段树入门题)

    Just a Hook Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descrip ...

  9. k8s 基础 k8s架构和组件

    k8s 的总架构图

  10. winform 客户端 HTTP协议与服务端通信以及解决中文乱码

    本来从来没有仔细研究过Http协议,今天因为公司业务需求,调试了半天,终于现在会Winform用Http协议与服务端通信了,其中常用的有POST和Get方式: 仔细看了人人网和新浪等大部分都是采用GE ...