OCAC summer second game sequence queue problem explanations M

Sequence queue
[title] Description
tell you now have a queue, a start it is empty, the first thing you'll 1,2,3, ... m m this number in turn push into the queue.
Next, operation will be performed n times, each operation you need the following steps:
First, a head of the queue element, the output element of the first team, the head of the queue and the queue element pop out; you can then optionally selected two of the following actions:
1. the first element of this team will never abandon;
2. the head of the queue element to rejoin the team in the end.
Now tell your team first element n and n times the output of the operation, you need to determine whether there is a number m such that this operation is feasible.
[Input format
of the first line of a positive integer n, the number of operations for representing. (1≤n≤100000)
a second row of n positive integer, separated by a space, for the first team represents the n-th element output operation.
[Output Format]
If there is a number such that n times m feasible operation, output "YES"; otherwise, the output "NO".
Sample input [1]
. 5
1 2 2 1. 3
[Sample 1] output
YES
[Sample 2] input
. 5
1 2 2 1. 3
[Sample 2] output
NO
[explain] Sample
For the sample 1, and when m == 3, we can do the following to obtain a target sequence [1,2,3,2,1]
initially, que = [1,2,3]
step.1: Output 1 = que.front (), que.pop ( ), que.push (1), this time = que [2,3,1]
Step.2: Output 2 = que.front (), que.pop (), que.push (2) , this time = que [3,1,2]
Step.3: output 3 = que.front (), que.pop ( ), que.push (3), At this time = que [l, 2,3]
Step.4: output 1 = que.front (), que.pop ( ), this time = que [2,3]
step.5: output 2 = que.fornt ( ), que.pop (), this time que = [3]
we output sequence is [1,2,3,1,2], so that m == 3 exist. This sequence legitimate, we output "YES".
For the sample 2, we have no way to get m.
Because we output after 1, 2, 3 2, that I started to abandon the 1, and m == 3, then this output I 2 is legitimate,
but I after 2 and 1 output, but analog here we can clearly see certainly before 1 is abandoned. So you do not legally sequences, output "NO".
[Analysis Problem
This question is a question about the simulation queue.
Simulation title it, it is the simulation of a class of things.
If it is a simulation questions on the elevator, we must first understand the principle of the elevator, then we will start the simulation questions handy.
After then for simulation title queue of it, we also want to understand the operation of the queue, and then go to law.
First, we find that m is very good looking, because a legitimate sequence of words, it's the first m data is certainly 1,2,3, ..., m.
So if this property is not satisfied, then the sequence is not legal.
Or, if we find m, but then there exists a sequence element is larger than m, that is not legitimate.
Then we begin to see the sequence of m + 1 to n-th element,
they can be decomposed into a number of contiguous sequences increased, and if the front of one of these sequences does not contain a, then it is not legal.
For example, a sequence as follows:
1, 2, 3, 4, 5, 2, 3, 5, 3, 4
according to the first five elements, we can determine m == 5
and then we will start the sequence of the 5th element divided into several consecutive rising sequence, then the sequence group after dividing (the first five elements) as follows:
    [1, 2, 3, 4, 5]
    [2, 3, 5]
    [3, 4]
according to the first and a second sub-sequence, we can determine the first wheel 4 and weed out 1;
then we go to the third subsequences, wherein there are four, but the first one 4 has been removed, so that presence is not legitimate. Output "NO".
Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int n, a[maxn], m;
bool cut[maxn];

void get_m() {
    m = -1;
    if (a[1] != 1) return;
    for (int i = 2; i <= n; i ++) {
        if (a[i] > a[i-1] + 1) return;
        if (a[i] <= a[i-1]) { m = a[i-1]; return; }
    }
    m = n;
}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i ++) cin >> a[i];
    get_m();    // 得到 m
    if (m == -1) {
        puts("NO");
        return 0;
    }
    for (int i = m+1; i <= n;i ++) {// there is determined number is greater than m 
        if (a[i] > m) {
            the puts ( "NO");
            return 0;
        }
    }
    queue<int> que1, que2;
    for (int i = 1; i <= m; i ++) que1.push(i);
    for (int i = m+1; i <= n; i ++) {
        while (!que1.empty() && que1.front() != a[i]) que1.pop();
        if (!que1.empty() && que1.front() == a[i]) {
            que2.push(que1.front());
            que1.pop();
        }
        else {
            while (!que2.empty()) {
                que1.push(que2.front());
                que2.pop();
            }
            while (!que1.empty() && que1.front() != a[i]) que1.pop();
            if (!que1.empty() && que1.front() == a[i]) {
                que2.push(que1.front());
                que1.pop();
            }
            else {
                puts("NO");
                return 0;
            }
        }
    }
    puts("YES");
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/ocac/p/11131692.html