hdu 3779 Railroad (动态规划)

Railroad

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 554    Accepted Submission(s): 223

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

题意非常明白。

很容易想到用回溯的办法从左到右枚举选择看能不能枚举到最右端。但是很明显,要超时!

这种每种选择有两种选择方式又包含大量重叠子问题的类型,适合用动规的思想。(想想 数字三角形 的问题)

但是想到用动规的思想,也不算解决问题。。。因为对于这道题目中的问题,你得定义适合的状态,想到正确有效的状态转移方程,给出边界条件才行。

状态定义:d[i][j]=1表示第一道上的前i辆和第二道上的前j辆车恰好是最终目标道上的前i+j辆车;

状态转移:if( a1[i] == a[i+j] ) d[i][j]=dp(i-1,j);

if( a2[j] == a[i+j] ) d[i][j]=dp(i,j-1);

边界条件:好像这种布尔型一直延伸到最前端,所以不用特殊给出边界条件唉。。

我感觉这好像就是一个有向的动态规划,感觉像是线性的...

用记忆化搜索的方式。

其时间复杂度:总共最多可能有1000*1000个状态,其时间复杂度是O(n^2);

实现:

#include<cstdio>
#include<cstring>
#include <cctype>
#include <iostream>
using namespace std;
int N1,N2,num1[1010],num2[1010],num[2010],N,d[1010][1010];
int dp(int a,int b)
{
    if(a<0 || b<0) return 0;
    int &ans=d[a][b];
    if(ans!=-1) return ans;
    ans=0;
    if(num1[a]==num[a+b]&&!ans) ans=dp(a-1,b);
    if(num2[b]==num[a+b]&&!ans) ans=dp(a,b-1);
    return ans;
}
int main()
{
    while(scanf("%d%d",&N1,&N2)==2 && N1 && N2)
    {
        N=N1+N2;
        memset(d,-1,sizeof(d));
        d[0][0]=1;
        for(int i=1;i<=N1;i++) scanf("%d",&num1[i]);
        for(int i=1;i<=N2;i++) scanf("%d",&num2[i]);
        for(int i=1;i<=N;i++) scanf("%d",&num[i]);
        if(dp(N1,N2)) printf("possible\n");
        else printf("not possible\n");
    }
    return 0;
}

昨天组队赛的时候,最后几秒队友改完交上Ac 。。想想真是有点小激动...

时间: 2024-09-30 19:03:12

hdu 3779 Railroad (动态规划)的相关文章

HDU 3779 Railroad(记忆化搜索)

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 ser

hdu 2583 permutation 动态规划

Problem Description Permutation plays a very important role in Combinatorics. For example ,1 2 3 4  5 and 1 3 5 4 2 are both 5-permutations. As everyone's known, the number of n-permutations is n!. According to their magnitude relatives ,if we insert

hdu 3853 LOOPS 动态规划

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3853 迷宫类的动态规划 首先要作个数学推导 假设留在原地.右移.下移的概率分别是a, b, c 用dp[i][j]表示在第i行第j格能走出去的期望步数 则有: dp[i][j] = a * (dp[i][j] + 1) + b * (dp[i][j+1] + 1) + c * (dp[i+1][j] + 1) 整理一下可得: dp[i][j] = 1/(1-a) * (a +  b * (dp[i]

HDU 5481 Desiderium 动态规划

Desiderium Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5481 Description There is a set of intervals, the size of this set is n. If we select a subset of this set with equal probability, how many the expected

[HDU 1114] Piggy-Bank (动态规划)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 简单完全背包,不多说. 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <cmath> 5 #include <map> 6 #include <iterator> 7 #include <vector> 8 using

[HDU 3535] AreYouBusy (动态规划 混合背包)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3535 题意:有n个任务集合,需要在T个时间单位内完成.每个任务集合有属性,属性为0的代表至少要完成1个,属性为1的为至多完成1个,属性为2的为任意完成. 每个任务做完后都有个价值,问在T个时间单位内完成n个任务集合的任务获得的最大价值是多少?如果不能满足要求输出-1 首先先分析什么情况下输出-1: 因为属性为0的代表至少要完成1个,当遇到一个属性为0的任务集合里一个都无法完成的时候,输出-1. 其他

百度之星资格赛 hdu 4826 Labyrinth 动态规划

/********************* Problem Description 是一仅仅喜欢探险的熊.一次偶然落进了一个m*n矩阵的迷宫,该迷宫仅仅能从矩阵左上角第一个方格開始走,仅仅有走到右上角的第一个格子才算走出迷宫,每一次仅仅能走一格,且仅仅能向上向下向右走曾经没有走过的格子,每个格子中都有一些金币(或正或负,有可能遇到强盗拦路抢劫,度度熊身上金币能够为负,须要给强盗写欠条),度度熊刚開始时身上金币数为0.问度度熊走出迷宫时候身上最多有多少金币? Input 输入的第一行是一个整数T

H - Tickets HDU 1260 (动态规划)

H - Tickets Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1260 Description Jesus, what a great movie! Thousands of people are rushing to the cinema. However, this is really a tuff time for Joe who sel

[HDU 2955]Robberies (动态规划)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2955 题意是给你一个概率P,和N个银行 现在要去偷钱,在每个银行可以偷到m块钱,但是有p的概率被抓 问你被抓的概率在P以下,最多能偷多少钱. 刚开始我还在想,A银行被抓的概率是a,B银行被抓的概率是b,那么偷A和B被抓的概率是a*b.. 傻逼了- -..a*b是既被A银行抓又被B银行抓.. 所以用逃跑的概率计算 dp[i][j]代表从前i个银行里偷了j元逃跑的最大概率 代码: 1 #include