STL - arrangement of the column (pillar) 2019.8.2

Face questions

【Problem Description】

Dad came with a small S JZ Square, we found a small S side of the square from left to right with a row of pillars of different heights, very spectacular. Dad told small S, where a total of N columns, each column height is an integer between 1 and N, and any two columns is not as high.

Small S just learned to count and compare the size of two integers, and she would like to take this opportunity to show off J. She find a piece of paper, the write sequence number N, where the i-th from the left represents the number of i left columns higher than its column. For example, if arrangement is 3,1,2 column, a note will be so small S 0,1,1.

Now a small S would like to test you, she put the paper to you, you can know how the column is arranged it?

[Input Format]

The first row contains an integer \ (N \) ( \ (. 1 \ Le N \ 30000 Le \) ), represents the number of columns.

The next \ (N \) lines, each line an integer. The first \ (i + 1 \) line represents the paper of \ (I \) number.

[Output format]

The first line is YES, or NOindicating whether the number of the small S.

If the first line YES, the following output \ (N \) lines, each line an integer. The first \ (i + 1 \) line from the left represents the section \ (I \) height of the pillars.

Sample input [1]

7
0
0
1
0
2
0
4 

Sample output [1]

YES
1
5
2
6
4
7
3 

Sample input [2]

3
0
2
0

Sample output [2]

NO

solution

Not difficult to find, which is equivalent to seeking ranked \ (a_i \) number, and deleting it. We use a std::vectormaintenance unused height, then every time you can find this number and delete.

program

#define MAXN 30050
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
 
int n;
int a[MAXN], r[MAXN];
vector<int> ch;
 
void fail() {
    cout << "NO" << endl;
    exit(0);
}
 
int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        ch.push_back(i + 1);
    }
     
    for (int i = n - 1; i > -1; i--) {
        int ind = ch.size() - a[i] - 1;
//      cout << "ind " << ind << ", size " << ch.size() << ", no. " << a[i] << endl;
//      for (int j = 0; j < ch.size(); j++) cout << ch[j] << " "; cout << endl;
        if (ind < 0 || ind >= ch.size()) fail();
        r[i] = ch[ind];
        if (r[i] < 1) fail();
//        bitadd(r[i], 1);
        ch.erase(ch.begin() + ind); // error?
    }
     
    cout << "YES" << endl;
    for (int i = 0; i < n ; i++) {
        cout << r[i] << endl;
    }
     
    return 0;
}

Guess you like

Origin www.cnblogs.com/lrw04/p/11795103.html