51Nod 1927 动车调头 c/c++题解

题目描述

在动车站中,动车到达终点之后需要调头,这个时候需要利用调头车道来调头。
在这里插入图片描述
如上图,列车从A方向进去,然后从B方向出来,就调头了。
现在有N辆动车,依次编号1到N,按照编号从小到大从A方向进去,然后从B方向出来。会有人统计从B方向出来的车辆编号顺序。但是可能工作人员会统计错误,所以现在请帮忙检查一下工作人员是否统计错误。即给定一个出站的编号序列,判断一下这种情况有没有可能发生。
输入
多组测试数据。
第一行有一个整数T(1<=T<=1000)表示测试数据的数目。
接下来T个数据,每个占两行。
先给出一个N(1<=N<=1000),表示有N辆动车要从A方向进入调头。
接下来一行给出N个整数,表示1到N的一个排列,表示工作人员从B方向统计出来的出站编号序列,输入保证出站序列里面的数字一定是1到N的,且两两不同。
输出
对于每个数据,如果有可能输出Yes,否则输出No。
输入样例
样例输入1
2
5
1 2 3 4 5
5
5 4 1 2 3
输出样例
样例输出1
Yes
No

题解:

方法1:根据出栈的性质
如果入栈的顺序为1-N,则出栈的顺序必定满足:每个元素的后面 比该元素小的元素 是递减排列的
利用这一性质就能直接判断给定的出栈序列是否正确。
方法2:模拟入栈和出栈的过程
直接看代码
注意:如果使用c++的输入输出(cin、cout)会超时一个点,倒不是因为时间复杂度的原因,而是本身cin和cout就慢,所以这里必须打开输入输出优化。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <deque>
#include <list>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <iterator>
using namespace std;

typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll  INF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double E = exp(1.0);
const int MOD = 1e9+7;
const int MAX = 1e5+5;
int t;
int n;
vector <int> pop;

bool Judge1()
{
    // 1 2 3 4 5 6 7 8 9 10
    for(int i = 0; i < ((int)pop.size()-1); i++)
    {
        vector <int> tmp;
        for(int j = i+1; j < ((int)pop.size()); j++)
        {
            if(pop[j] < pop[i])
            {
                tmp.push_back(pop[j]);
            }
        }
        for(int j = 0; j < (int)tmp.size()-1; j++)
        {
            if(tmp[j] < tmp[j+1])
                return false;
        }
    }
    return true;
}

bool Judge2()
{
    vector <int> push;// 1-N的入栈顺序,pop为出栈顺序
    for(int i = 0; i < (int)pop.size(); i++)
    {
        push.push_back(i+1);
    }
    // 模拟入栈和出栈的过程,看是否给定的出栈序列满足要求
    int num = (int)pop.size();
    stack <int> s;
    int j = 0;
    for(int i = 0; i < num; i++)
    {
        s.push(push[i]);// 入栈
        if(s.top() != pop[j])
        {
            continue;
        }
        while(s.size() > 0)
        {
            if(s.top() == pop[j])
            {
                j++;
                s.pop();// 出栈
            }
            else
            {
                break;
            }
        }
    }
    if(s.size() != 0)
    {
        return false;
    }
    else
    {
        return true;
    }
}

int main()
{
    ios::sync_with_stdio(false);// 注意点1:这里如果使用C++,需要对输入输出优化,否则超时一个点
    cin.tie(0);
    cout.tie(0);

    /*
        题目大意:
        有确定的入栈序列1-N,给定一个出栈序列,判断是否符合要求。
        出栈序列:对于任何一个元素,保证后面小的元素一定递减的。
    */
    cin >> t;
    while(t--)
    {
        pop.clear();// 注意点2:每次都需要将pop清空
        cin >> n;
        int x;
        for(int i = 0; i < n; i++)
        {
            cin >> x; pop.push_back(x);
        }
        cout << (Judge1() ? "Yes" : "No") << endl;// 方法1
        //cout << (Judge2() ? "Yes" : "No") << endl; // 方法2
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值