Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) B - Box

[Link title] B title link

[Title] analog type

[Title] effect you period sequence of length n, q1, q2, q3, q4 ... qn This sequence is generated by p1, p2 ... pn, there is only 1 to n numbers p1, p2 ... pn them, without will duplication, through the following operation obtained
Ql = P1,
Q2 = max (P1, P2),
Q3 = max (P1, P2, P3),
...
Qn = max (P1, P2, ..., PN).
now request you solved p1, p2 ... pn (p sequence that may exist many cases, you can either solving)

[Problem-solving ideas] First, we can start to perform some analysis of a sample, because the subject is given a sample solution process 1 of
the INPUT
5
1 3 5 5 4
q1 = p1 = 1;
q2 = max (p1, P2 ) =. 3;
Q3 = max (P1, P2, P3) =. 4;
Q4 = max (P1, P2, P3, P4) =. 5;
. Q5 = max (P1, P2, P3, P4, P5) =. 5
could solved

  • p1 = 1
  • Since max (p1, p2) = 3; So p2 = 3
  • Since max (p1, p2, p3) = 4 Therefore, p3 = 4
  • Since max (p1, p2, p3, p4) = 5 Therefore, p4 = 5
  • Since max (p1, p2, p3, p4, p5) = 5 and p4 = 5, and only 2 had not occurred in the sequence, it is only 2 p5
    solve for
    Output
    . 1 2. 5. 4. 3
    (this is the answer out case, the title is also the case when it comes to the existence of the answer does not come out, so look for the data output -1 is what
    the INPUT
    4
    1 1 3 4
    obviously this data will appear p1 and p2 are 1 case, then the sequence numbers do not satisfy the conditions do not overlap so that the output of the -1

The above data can obviously know, the larger the data needs to occur sooner (I expression may not be good, but please try to understand the meaning of this word)
like on
5
3 3 5 5 5
then I can sequence is
32541, the next three are because I 5, I want to ensure max values obtained after 5, so we need so arranged

ans is my answer array, it IS
(1) to ensure that the number that appears is not repeated, I declare a variable set to weed out the answers have emerged in the digital array
(2) q array is my title says q-array for receiving data input
(3) if I q [i]> p [i ] that it directly I q [I] into array I ans the answer, and determines whether the number of I there appeared p SET in the container to remove the p q [i] value, i.e. p.earse (q [i]);
(. 4) Finally, the number of remaining p ans put in it, because we has been met, a large number of possible forward row
(5) in wa can try this sample of 3 ~ 5case
5
. 3. 3 5 5 5

/**
 *    This code has been written by YueGuang, feel free to ask me question. Blog: http://www.yx.telstudy.xyz
 *    created:
 */
#include <cstdio>
#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#define rep(i, a, b) for(int i = a; i < b; i++)
#define rep_(i, a, b) for(int i = a; i <= b; i++)
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep1(i, n) for(int i = 1; i < n; i++)
#define rep_(i, n) for(int i = 0; i <= n; i++)
#define rep1_(i, n) for(int i = 1; i <= n; i++)
#define pb(x) push_back(x);
#define si(x) scanf("%d", &x);
#define sl(n) scanf("%lld", &n);
#define si(n) scanf("%d", &n);
#define RepAll(a) for(auto x: a)
#define cout(ans) cout << ans << endl;
typedef long long ll;

using namespace std;
const int maxn = 1e5+50;
set<int> p;
int q[maxn], ans[maxn];
int main(){
    int t; si(t);
    while(t--){
        bool isflag = true;
        int n; si(n);
        rep1_(i, n){ si(q[i]);}
        p.clear();
        rep1_(i, n){ p.insert(i); }
        rep1_(i, n){
            if(q[i] < q[i - 1]){ isflag = false; break;}
            if(q[i] > q[i - 1]){ ans[i] = q[i]; p.erase(q[i]);}
            else{
                if ((*p.begin()) < q[i]){
                    ans[i] = (*p.begin());
                    p.erase(*p.begin());
                }
                else{
                //如果set中剩余的数字比q[i]还大的话,那很明显是错的,max最后的值就会不符合要求
                    isflag = false;
                    break;
                }
            }
        }
        if(isflag == false){printf("-1\n"); continue;}
        rep1_(i, n){cout << ans[i] << " "; }
    }
}

The code is not on the time complexity and space complexity were there, and if there are better ideas and ideas welcome comments or send an email to my mailbox [email protected]

Published 20 original articles · won praise 3 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43382350/article/details/103229216