Railroad

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 10   Accepted Submission(s) : 3

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

A train yard is a complex series of railroad tracks for storing, sorting, or loading/unloading railroad cars. In this problem, the railroad tracks are much simpler, and we are only interested in combining two trains into one.

Figure 1: Merging railroad tracks.

The two trains each contain some railroad cars. Each railroad car contains a single type of products identified by a positive integer up to 1,000,000. The two trains come in from the right on separate tracks, as in the diagram above. To combine the two trains, we may choose to take the railroad car at the front of either train and attach it to the back of the train being formed on the left. Of course, if we have already moved all the railroad cars from one train, then all remaining cars from the other train will be moved to the left one at a time. All railroad cars must be moved to the left eventually. Depending on which train on the right is selected at each step, we will obtain different arrangements for the departing train on the left. For example, we may obtain the order 1,1,1,2,2,2 by always choosing the top train until all of its cars have been moved. We may also obtain the order 2,1,2,1,2,1 by alternately choosing railroad cars from the two trains.

To facilitate further processing at the other train yards later on in the trip (and also at the destination), the supervisor at the train yard has been given an ordering of the products desired for the departing train. In this problem, you must decide whether it is possible to obtain the desired ordering, given the orders of the products for the two trains arriving at the train yard.

Input

The input consists of a number of cases. The first line contains two positive integers N1 N2 which are the number of railroad cars in each train. There are at least 1 and at most 1000 railroad cars in each train. The second line contains N1 positive integers (up to 1,000,000) identifying the products on the first train from front of the train to the back of the train. The third line contains N2 positive integers identifying the products on the second train (same format as above). Finally, the fourth line contains N1+N2 positive integers giving the desired order for the departing train (same format as above).

The end of input is indicated by N1 = N2 = 0.

Output

For each case, print on a line possible if it is possible to produce the desired order, or not possible if not.

Sample Input

3 3
1 2 1
2 1 1
1 2 1 1 2 1
3 3
1 2 1
2 1 2
1 1 1 2 2 2
0 0

Sample Output

possible
not possible

Source

HDOJ开放式进阶训练(11)——全程测试
 
题解:记忆化搜索,记录状态
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; int n,m;
int a[],b[],c[];
bool vis[][];
bool dfs(int x,int y,int k)
{
if (k==n+m+) return ;
if(vis[x][y]) return ;
vis[x][y]=;
if (x<=n && a[x]==c[k] && dfs(x+,y,k+))
return ;
if (y<=m && b[y]==c[k] && dfs(x,y+,k+))
return ; return ;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
if (n== && m==) break;
for(int i=;i<=n;i++) scanf("%d",&a[i]);
for(int i=;i<=m;i++) scanf("%d",&b[i]);
for(int i=;i<=n+m;i++)scanf("%d",&c[i]);
memset(vis,,sizeof(vis));
if (dfs(,,)) printf("possible\n");
else printf("not possible\n");
}
return ;
}

HDU 3779 Railroad(记忆化搜索)的更多相关文章

  1. 不要62 hdu 2089 dfs记忆化搜索

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意: 给你两个数作为一个闭区间的端点,求出该区间中不包含数字4和62的数的个数 思路: 数位dp中 ...

  2. hdu 1078(dfs记忆化搜索)

    题意:容易理解... 思路:我开始是用dfs剪枝做的,968ms险过的,后来在网上学习了记忆化搜索=深搜形式+dp思想,时间复杂度大大降低,我个人理解,就是从某一个点出发,前面的点是由后面的点求出的, ...

  3. hdu 4826(dp + 记忆化搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4826 思路:dp[x][y][d]表示从方向到达点(x,y)所能得到的最大值,然后就是记忆化了. #i ...

  4. hdu---(3779)Railroad(记忆化搜索/dfs)

    Railroad Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  5. POJ 1198 / HDU 1401 Solitaire (记忆化搜索+meet in middle)

    题目大意:给你一个8*8的棋盘,上面有四个棋子,给你一个初始排布,一个目标排布,每次移动,可以把一个棋子移动到一个相邻的空位,或者跨过1个相邻的棋子,在保证棋子移动不超过8次的情况下,问能否把棋盘上的 ...

  6. hdu 4345 Permutation 记忆化搜索

    思路:实际上求的是和小于等于n的质数的种类数!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorit ...

  7. POJ1088 滑雪题解+HDU 1078(记忆化搜索DP)

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  8. hdu1078 记忆化搜索

    /* hdu 1078 QAQ记忆化搜索 其实还是搜索..因为里面开了一个数组这样可以省时间 (dp[x][y]大于0就不用算了直接返回值) */ #include<stdio.h> #i ...

  9. HDU 1429 (BFS+记忆化状压搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1429 题目大意:最短时间内出迷宫,可以走回头路,迷宫内有不同的门,对应不同的钥匙. 解题思路: 要是 ...

随机推荐

  1. tensorboard实现训练的可视化

    tensorboard是tensorflow自带的可视化工具 输入命令可以启动tensorboard服务. tensorboard --logdir=your log dir 通过浏览器localho ...

  2. Core ML 入门

    1.下载Xcode 9 2.下载模型,https://developer.apple.com/machine-learning/ 3.开动.. 4.待续 模拟器66的

  3. IPMI的几个问题

    IPMI针对大量监控.控制和自动回复服务器的作业,提供了智能型的管理方式.此标准适用于不同的服务器拓朴学,以及Windows.Linux. Solaris.Mac或是混合型的操作系统.此外,由于IPM ...

  4. kali 安装最新firefox的悲惨经历

    最新的的firefox用的是量子内核,在windows上面的确感觉相比之前的firefox快了好多 想把kali 2017虚拟机的也替换掉 按照步骤: 1 添加源: /etc/apt/sources. ...

  5. 20145310《Java程序设计》第5次实验报告

    20145310<Java程序设计>第5次实验报告 实验要求 掌握Socket程序的编写: 掌握密码技术的使用: 设计安全传输系统. 实验内容 根据所学内容,编写代码实现服务器与客户端 掌 ...

  6. COGS314. [NOI2004] 郁闷的出纳员

    ★★★   输入文件:cashier.in   输出文件:cashier.out   简单对比 时间限制:1 s   内存限制:128 MB [问题描述] OIER公司是一家大型专业化软件公司,有着数 ...

  7. MongoDB-与Python交互

    与python交互 点击查看官方文档 安装python包 进入虚拟环境 sudo pip install pymongo 或源码安装 python setup.py 引入包pymongo import ...

  8. [CF1051F]The Shortest Statement

    题目大意:给定一张$n$个点$m$条有权边的无向联通图,$q$次询问两点间的最短路 $n\le100000$,$m\le100000$,$1\le100000$,$m$-$n\le20$. 首先看到$ ...

  9. sbt安装与配置

    下载地址:http://www.scala-sbt.org/download.html 当前版本:sbt-0.13.13.tgz 安装 1.解压并赋予权限 [root@hidden util]# ta ...

  10. 一键安装 zabbix 2.0 版本 脚本

    原文地址: http://blog.csdn.net/u012449196/article/details/53859068 本文修改了原文中的部分错误,此脚本适用于zabbix 2.0 版本,以版本 ...