首页 技术 正文
技术 2022年11月6日
0 收藏 468 点赞 959 浏览 1604 个字

题目请见:传送门
以下为题解,直接从洛谷上搬过来的,还专门改了markdown,(汗)

宽搜 with 一些技巧

  • 由于查询量很大,所以要预先处理所有答案
  • 预处理当然是用BFS,并同时进行delete,swap,add操作。注意,不能在x为队首元素时才更新答案,这样会使效率大打折扣(不更新的话,dist[x]任为-1,相当于少了判重)
  • 对于以上三种操作也有要求,一下进行一些优化(炒鸡模拟应该也能过,因为swap(),嗯嗯):
  • 法一:见一楼的题解,不过有局限性
  • 法二:针对add(),和delete(),可以然枚举的状态有序化以达到优化效果,适用范围更广泛;
  • 以下为我的代码,其中结构体部分可以省去,u.step可以直接用dist[x]代替。
#include<queue>#include<cstdio>#include<cstring>main() {}int dist[10000000];struct in{int x,s;}u;std::queue<in>que;void tie(int *a,int &lth,int x) {    lth=0;//解码    while(x) a[lth++]=x%10,x/=10;}int dis(int *a,int lth) {    int x=0;//还原    while(lth) x*=10,x+=a[--lth];    return x;}void add(int *a,int lth,int step) {    int tmp;//o(n)的add操作    a[lth]=a[lth-1];    for(int i=lth-1;i>=1;i--) {        for(int j=a[i+1]+1;j<a[i-1];j++) {            a[i]=j;            tmp=dis(a,lth+1);            if(dist[tmp]==-1) dist[tmp]=step,que.push((in){tmp,step});        }        a[i]=a[i-1];    }}void del(int *a,int lth,int step) {    int tmp,out=0;//o(n)的delete操作    for(int i=lth-1;i>=0;i--) {        out^=a[i]^=out^=a[i];        tmp=dis(a,lth-1);        if(dist[tmp]==-1) dist[tmp]=step,que.push((in){tmp,step});    }}void swa(int *a,int lth,int step) {    int tmp;//o(n*(n-1)/2)的swap操作    for(int i=0;i<lth;i++) {        for(int j=i+1;j<lth;j++) {            if(a[i]==a[j]) continue;            a[i]^=a[j]^=a[i]^=a[j];            tmp=dis(a,lth);            if(dist[tmp]==-1) dist[tmp]=step,que.push((in){tmp,step});            a[i]^=a[j]^=a[i]^=a[j];        }    }}int entry() {    memset(dist,-1,sizeof dist);    int a[10],lth,lmt,x;    scanf("%d",&x);    tie(a,lmt,x),dist[x]=0;    que.push((in){x,0});    while(!que.empty()) {        u=que.front();        que.pop();        memset(a,0,sizeof a);        tie(a,lth,u.x);        if(lth>1) del(a,lth,u.s+1),tie(a,lth,u.x);        if(lth>1) swa(a,lth,u.s+1),tie(a,lth,u.x);        if(lth<lmt) add(a,lth,u.s+1);    }    scanf("%d",&lmt);    while(lmt--) {        scanf("%d",&x);        printf("%d\n",dist[x]);    }    return 0;}int aptal=entry();
上一篇: RocketMQ快速入门
下一篇: Minecraft
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,893