分析:很显然这是一道搜索题,可能是由于我的搜索打的太不美观了,这道题又WA又T......如果对每一个询问都做一次bfs是肯定会T的,注意到前70%的数据范围,N的值都相等,我们可以把给定N的所有情况给算出来,然后O(1)查询.从终点状态往起点状态BFS就可以了.

当最终状态很少,起始状态很多的时候,可以考虑倒着做.

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; int T, jian[], stu, shu[], cnt, top[], d[], cur[], vis[];
int n, a[], b[];
queue <int> q; bool cmp(int x, int y)
{
return a[x] < a[y];
} void bfs()
{
while (!q.empty())
{
int zhuangtai = q.front();
q.pop();
memset(top, , sizeof(top));
memset(shu, , sizeof(shu));
cnt = ;
int tt = zhuangtai;
while (tt)
{
shu[++cnt] = tt % ;
tt /= ;
}
reverse(shu + , shu + + cnt);
for (int i = cnt; i >= ; i--)
top[shu[i]] = i;
for (int i = ; i <= cnt; i++)
if (i == top[shu[i]])
{
int temp = shu[i];
if (temp != && (i < top[temp - ] || !top[temp - ]))
{
int newstu = zhuangtai - jian[cnt - i];
if (!vis[newstu])
{
q.push(newstu);
vis[newstu] = ;
d[newstu] = d[zhuangtai] + ;
}
} if (temp != cnt && (i < top[temp + ] || !top[temp + ]))
{
int newstu = zhuangtai + jian[cnt - i];
if (!vis[newstu])
{
q.push(newstu);
vis[newstu] = ;
d[newstu] = d[zhuangtai] + ;
}
}
}
}
} int main()
{
scanf("%d", &T);
jian[] = ;
stu = ;
for (int i = ; i <= ; i++)
{
jian[i] = jian[i - ] * ;
stu = stu * + i;
q.push(stu);
vis[stu] = ;
}
bfs();
//printf("flag\n");
while (T--)
{
scanf("%d", &n);
for (int i = ; i <= n; i++)
{
scanf("%d", &a[i]);
b[i] = i;
}
sort(b + , b + + n, cmp);
int stu = ;
for (int i = ; i <= n; i++)
stu = stu * + b[i];
if (!vis[stu])
printf("-1\n");
else
printf("%d\n", d[stu]);
} return ;
}

noip模拟赛 PA的更多相关文章

  1. 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程

    数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...

  2. 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...

  3. 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...

  4. 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...

  5. CH Round #58 - OrzCC杯noip模拟赛day2

    A:颜色问题 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2358%20-%20OrzCC杯noip模拟赛day2/颜色问题 题解:算一下每个仆人到它的目的地 ...

  6. CH Round #55 - Streaming #6 (NOIP模拟赛day2)

    A.九九归一 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2355%20-%20Streaming%20%236%20(NOIP模拟赛day2)/九九归一 题 ...

  7. CH Round #54 - Streaming #5 (NOIP模拟赛Day1)

    A.珠 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2354%20-%20Streaming%20%235%20(NOIP模拟赛Day1)/珠 题解:sb题, ...

  8. NOIP模拟赛20161022

    NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...

  9. contesthunter暑假NOIP模拟赛第一场题解

    contesthunter暑假NOIP模拟赛#1题解: 第一题:杯具大派送 水题.枚举A,B的公约数即可. #include <algorithm> #include <cmath& ...

随机推荐

  1. struts2里result类型Stream的参数配置

    转自:https://blog.csdn.net/q714699280/article/details/51756126 contentType 内容类型,和互联网MIME标准中的规定类型一致,例如t ...

  2. django入门与实践 3-1 环境搭建

    Python 2.7 .Django对2.7.3.5.3.4都是支持的. https://www.djangoproject.com/download/ django对python的兼容情况 pip安 ...

  3. linux下打开windows txt文件中文乱码问题 (转载)

    转自:http://blog.csdn.net/imyang2007/article/details/7448177 在linux操作系统下,我们有时打开在windows下的txt文件,发现在wind ...

  4. [Swift通天遁地]三、手势与图表-(8)制作股市中常用的蜡烛图表

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  5. JVM面试总结

    1.  Java虚拟机的内存布局(运行时数据区) 参考:https://www.cnblogs.com/lostyears/articles/8984171.html 2. GC算法及几种垃圾收集器 ...

  6. hdu2030

    http://acm.hdu.edu.cn/showproblem.php?pid=2030 #include<stdio.h> #include<math.h> #inclu ...

  7. const修饰规则 及其 用法

    const指针和指向const变量的指针,在写法上容易让人混淆,记住一个规则:从左至右,依次结合,const就近结合. 比如,int * const p: 1.int * (const p):变量p经 ...

  8. ansible 显示运行时间

    #独家秘诀cd /etc/ansible mkdir callback_plugins cd callback_plugins wget https://raw.githubusercontent.c ...

  9. Android开发高手课笔记 - 01 崩溃优化(上):关于“崩溃”那点事

    Android 的两种崩溃 Java 崩溃就是在 Java 代码中,出现了未捕获的异常,导致程序异常退出 Native 崩溃一般都是因为在 Native 代码中访问非法地址,也可能是地址对齐出了问题, ...

  10. 超经典~超全的jQuery插件大全

    海量的jQuery插件帖,很经典,不知道什么时候开始流传,很早以前就收藏过,为了工作方便还是发了一份放在日志里面. 其中有些已经无法访问,或许是文件移除,或许是被封锁.大家分享的东西,没什么特别的可说 ...