noip模拟赛 PA


分析:很显然这是一道搜索题,可能是由于我的搜索打的太不美观了,这道题又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的更多相关文章
- 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程
数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...
- 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...
- 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...
- 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...
- CH Round #58 - OrzCC杯noip模拟赛day2
A:颜色问题 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2358%20-%20OrzCC杯noip模拟赛day2/颜色问题 题解:算一下每个仆人到它的目的地 ...
- CH Round #55 - Streaming #6 (NOIP模拟赛day2)
A.九九归一 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2355%20-%20Streaming%20%236%20(NOIP模拟赛day2)/九九归一 题 ...
- 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题, ...
- NOIP模拟赛20161022
NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...
- contesthunter暑假NOIP模拟赛第一场题解
contesthunter暑假NOIP模拟赛#1题解: 第一题:杯具大派送 水题.枚举A,B的公约数即可. #include <algorithm> #include <cmath& ...
随机推荐
- LuoguP3261 [JLOI2015]城池攻占
题目描述 小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池.这 n 个城池用 1 到 n 的整数表示.除 1 号城池外,城池 i 会受到另一座城池 fi 的管辖,其中 fi &l ...
- tinymce 富文本编辑器 编写资料
tinymce官方文档: 粘贴图片插件 博客搬运地址 使用Blob获取图片并二进制显示实例页面 tinymce自动调整插件 是时候掌握一个富文本编辑器了——TinyMCE(1) XMLHttpRequ ...
- 通过类库ChineseChar实现将汉字转化为拼音
//封装dllusing Microsoft.International.Converters.PinYinConverter;using System.Text;namespace Utils{ p ...
- Java——Spring介绍
spring 是一个开源框架,是为了解决企业应用程序开发. 功能如下:1.目的:解决企业应用开发的复杂性.2.功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能.3.范围:任何Ja ...
- html用过标签记录
nowrap="nowrap" //用于列表中td不许换行 maxlength="100" //用于输入框的长度限制 colspan="2" ...
- js中添加node.js语法支持
File——>settings
- Recyclerview点击事件,更新item的UI+更新Recyclerview外的控件
项目中用到了Recyclerview,在第一行代码中学到了一种相对来说简单的点击事件方法,可是这种点击事件是在adapter中写的,没有教怎么更新item的ui和更新Recyclerview之外的控件 ...
- html5——动画案例(太阳系)
太阳系主要利用定位,伪元素 <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...
- php判断form数据是否为POST而来,判断数据提交方式
//判断form数据是否为POST而来,判断数据提交方式 if ($_SERVER['REQUEST_METHOD'] != 'POST') { // 非 POST 来路,做警告或你想做的事 retu ...
- Scroll / Jump to id without jQuery
<scripttype="text/javascript"> function scroll(element){var ele = document.getElemen ...