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

链接:https://codeforces.com/contest/1262/problem/B

题意:给定p数组的前缀最大值,问能否构造q数组,q数组必须是(1~n)输入n和p数组;

贪心一下,要么放当前p数组的值,要么放1~n中当前最小值;

code;

```

#include<bits/stdc++.h>
using namespace std;
vector<int> v;
void solve()
{
    v.clear();
    set<int> s;
    int n;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        int x;
        scanf("%d",&x);
        v.push_back(x);
        s.insert(i+1);
    }
    vector<int> ans;
    int mi=0;
    for(int i=0; i<n; i++)
    {
        if(v[i]>mi)
        {
            if(s.count(v[i]))
            {
                s.erase(v[i]);
                ans.push_back(v[i]);
            }
            else
            {
                printf("-1
");
                return ;
            }
            mi=v[i];
        }
        else
        {
            if(*s.begin()>mi)
            {
                printf("-1
");
                return ;
            }
            else
            {
               ans.push_back(*s.begin());
               s.erase(*s.begin());
            }
        }
    }
    for(int x : ans)
        printf("%d ",x);
    printf("
");

}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--) solve();
    return 0;
}

 ```

原文地址:https://www.cnblogs.com/sweetlittlebaby/p/12694071.html