569B - Inventory - CodeForces Solution


greedy math *1200

Please click on ads to support us..

C++ Code:

#include <algorithm>
#include <bits/stdc++.h>
#include <cstring>
#include <functional>
#include <numeric>
using namespace std;
const int MOD = 1000000007;
const int mod = 998244353;
// #define LOCAL
#ifdef LOCAL
#define dbg(x...) do { cout << "[" << #x <<" -> "; err(x); } while (0)
void err() { cout << "]" << endl; }
template<class T, class... Ts> void err(const T& arg,const Ts&... args) { cout << arg << " "; err(args...); }
#else 
#define dbg(x...){}
#endif
#define int long long
#define pp pair<int, int>
class Comb {
    vector<int> Facs, Invs;
    void expand(size_t n) {
        while(Facs.size() < n + 1) Facs.push_back(1ll * Facs.back() * Facs.size() % MOD);
        if(Invs.size() < n + 1) { // 线性求阶乘的逆元
            Invs.resize(n + 1, 0);
            Invs.back() = 1;
            for(int a = Facs[n], p = MOD - 2; p; p >>= 1, a = 1ll * a * a % MOD)
                if(p & 1) Invs.back() = 1ll * Invs.back() * a % MOD; // 快速乘求 n! 的逆元
            for(int j = n-1; !Invs[j]; --j) Invs[j] = 1ll * Invs[j+1] * (j + 1) % MOD;
        }
    }
public:
    Comb() : Facs({1}), Invs({1}) {}
    Comb(int n) : Facs({1}), Invs({1}) { expand(n); }
    int operator() (int n, int k) {
        if(n == 0) return 1;
        if(k > n) return 0;
        expand(n);
        return (1ll * Facs[n] * Invs[n-k] % MOD) * Invs[k] % MOD;
    }
};

int fac[20];

int quickpow(int base, int exponent) {
    // return fac[exponent ];
    int result = 1;
    base %= mod;
    while (exponent > 0) {
        dbg(exponent);
        if (exponent & 1) {
            result = (result * base) % mod;
        }
        exponent >>= 1;
        base = (base * base) % mod;
    }
    // dbg(result);
    return (result + mod) % mod;
}


Comb comb;

using ll = long long;
int cnt = 0;
const int N = 1e3+10;
vector<int> primes;
vector<bool> is_prime(N + 1, true);
auto init =[](){
    for(int i = 2; i < N; i++) {
        if(is_prime[i]) primes.push_back(i);
        for (int& j: primes) {
            if (i * j > N) {
                break;
            }
            is_prime[i * j] = false;
            if (i % j == 0) break;
        }
    }
};


bool F;

void solve(){
    int n;
    cin >> n;
    vector<int> arr(n);
    for(auto& x: arr) cin >> x;
    set<int> st;
    for(int i = 1; i <= n; i++) {
        st.insert(i);
    }
    for(int i = 0; i < n; i++) {
        if (st.count(arr[i])) st.erase(arr[i]);
    }
    set<int> his;
    for(int i = 0; i < n; i++) {
        if(his.count(arr[i]) || arr[i] > n) {
            cout << *st.begin() << " \n"[i == n - 1];
            st.erase(st.begin());
        }else{
            cout << arr[i] << " \n"[i == n - 1];
            his.insert(arr[i]);
        }
    }

}

int T = 0;

signed main(){
#ifndef LOCAL
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
#endif
    int t;
    // init();
    // cin >> t;
    int c = 0;
    // while(c++ < t) {
        solve();
    // }
    // cout << "done" << endl;
    return 0;
}


Comments

Submit
0 Comments
More Questions

50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns
1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem
432D - Prefixes and Suffixes
486A - Calculating Function
1373B - 01 Game
1187A - Stickers and Toys
313B - Ilya and Queries
579A - Raising Bacteria
723A - The New Year Meeting Friends
302A - Eugeny and Array
1638B - Odd Swap Sort
1370C - Number Game
1206B - Make Product Equal One