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& ...
随机推荐
- Eclipse自定义HTML5,JSP模板
原文:http://blog.csdn.net/xz2585458279/article/details/78833893 我们知道在MyEclipse里面编写的html和jsp模板并不符合html5 ...
- 9.28NOIP模拟题
9.28NOIP模拟题 题目 哈 哈哈 哈哈哈 英文题目与子目录名 ha haha hahaha 单个测试点时间限制 1秒 1秒 1秒 内存限制 256M 128M 64M 测试点数目 10 10 1 ...
- [Swift通天遁地]九、拔剑吧-(4)使用开源类库创建可滑动的Segment分段控件
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- easyui datagrid 页面详细使用
//加载数据workflowName onloadmyCgxList: function (id) { if (id != null && id != "" ...
- python--9、进程及并发知识
进程 一个文件的正在执行.运行过程就成为一个进程.执行多个程序,把程序文件都加载到内存,并且多个程序的内存空间隔离--空间上的复用. 遇到IO等待,切CPU到别的程序,提升效率.没有IO,一个程序占用 ...
- 项目管理01--使用Maven构建项目(纯干货)
目录 1. Maven基础知识 2. Maven实战.开发.测试.打包.部署一个Web项目 一.Maven基础知识 Maven坐标 Maven提供了一个中央仓库,里面包含了大量的开源软件的jar包,只 ...
- 重现apache commons fileupload DOS漏洞
这个漏洞是2014年2月4日被发现的, 因为该组件试用范围非常广, 所以该漏洞的影响也非常巨大.通过特制的包含畸形header的http请求,可以导致使用该组件的应用程序进入无限循环从而耗尽CPU等资 ...
- C# 统计字符串出现的个数
string str1 = "123AAA456AAAA789AAAAAAA1011"; string str2 = "123456789AAA23456789AAAA3 ...
- Java中面向对象三大特性之——多态
多态的概述: 多态是继封装.继承之后,面向对象的第三大特性. 生活中,比如跑的动作,小猫.小狗和大象,跑起来是不一样的.再比如飞的动作,昆虫.鸟类和飞机,飞起来也是不一样的.可见,同一行为,通过不同 ...
- wpf绑定静态变量,模拟rem单位
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; usin ...