6 solutions

  • 0
    @ 2025-3-29 23:43:32

    queue or 数组模拟

    #include <bits/stdc++.h>
    #define endl '\n'
    using namespace std;
    typedef pair<int, int> PII;
    using ll = long long;
    using ULL = unsigned long long;
    const int N = 1e7 + 5;
    
    int n, k, a[N];
    //queue<int> q;
    int q[N], hh = 0, tt = -1;
    inline void solve() {
    /*
        cin >> n >> k;
        for (int i = 1; i <= n; i++) q.push(i);
        ll cnt = 0, id = 0;
        while (!q.empty()) {
            int hh = q.front();
            q.pop();
            cnt++;
            if (cnt == k) {
                id++;
                a[hh] = id;
                cnt = 0;
            }else q.push(hh);
        } 
        for (int i = 1; i <= n; i++) cout << a[i] << " \n"[i==n];
    */
        cin >> n >> k;
        for (int i = 1; i <= n; i++) q[++tt] = i;
        int cnt = 0, idx = 0;
        while (hh <= tt) {
            int aa = q[hh];
            ++hh;
            cnt++;
            if (cnt == k) {
                idx++;
                a[aa] = idx;
                cnt = 0;
            }else q[++tt] = aa;
        }
       for (int i = 1; i <= n; i++) cout << a[i] << " \n"[i==n];
    }
    
    int main() {
        ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
        int _ = 1;
        //int _; cin >> _;
        while (_--) solve();
        return 0;
    }
    
    

    Information

    ID
    79
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    6
    Tags
    # Submissions
    303
    Accepted
    103
    Uploaded By