分析:很显然这是一道搜索题,可能是由于我的搜索打的太不美观了,这道题又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. 【HNOI 2003】 激光炸弹

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1218 [算法] 二维前缀和 [代码] #include<bits/stdc++ ...

  2. Map类型介绍与遍历

    声明:本文非原创: 在程序员开发过程中,Map有着利用率占比是非常高:很多时间我们只知其用,不知其理:写这个随笔的目的也是希望对伙伴们对Map的理解有一点帮助. 类型介绍 java自带各种Map类.统 ...

  3. Rails 服务器架设失败问题

    更新: 2017/09/14 补充了简单的确认号码的方法 A server is already running. Check /Users/...../pids/server.pid. Exitin ...

  4. yield from (python生成器)

    #生成器中的yield from是干什么用的(一般多用于线程,协程那)def func(): # for i in 'AB': # yield i yield from 'AB' # 就相当于上面的f ...

  5. 大数据~说说Hadoop

    Hadoop是一个由Apache基金会所开发的分布式系统基础架构. 用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力进行高速运算和存储.  Hadoop实现了一个分布式文件系 ...

  6. MVC系列学习(七)-模板页

    1.新建一个MVC项目,选择基本 2.查看文件 看到VS为我们生成了一些东西 布局页面,Layout 指定了模板页 3.开始实例 首先控制器中的代码如下: 视图中代码如下: 1.在/Views/_Vi ...

  7. mysql索引初认识

    mysql> use mysql; Database changed mysql> show index from user; +-------+------------+-------- ...

  8. Android开发初体验

    本文通过开发一个应用来学习Android基本概念及构成应用的UI组件. 开发的应用名叫GeoQuiz,它能给出一道道地理知识问题.用户点击true或false按钮回答问题,应用即时做出反馈 第一步请先 ...

  9. HTML地理位置定位

    最近公司项目需要做一个类似微信朋友圈的互动交友功能,需要显示用户位置信息,因此在网上查了部分资料,记下demo供以后查看学习:(用到了百度api来实现定位功能) <!DOCTYPE html&g ...

  10. quartz 数据库表含义解释

    http://blog.csdn.net/tengdazhang770960436/article/details/51019291 一.表信息解析: 1.1.qrtz_blob_triggers : ...