Submission #55020028


Source Code Expand

//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,sse4a,avx,avx2,popcnt,tune=native")
#include <bits/stdc++.h>
 
using namespace std;
 
#define all(v) (v).begin(), (v).end()
#define sz(a) ((long long)(a).size())
#define X first
#define Y second
 
using ll = long long;
using ull = unsigned long long;
using dbl = long double;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll myRandMod) {
    return (ull)rng() % myRandMod;
}
 
const ll INF = 1e9;
const ll LINF = 1e18;
const int MOD = (int)1e9 + 7;
const int MAXN = 505;

bool t[MAXN][MAXN];
 
void solve() {
    memset(t, 0, sizeof t);
    int n, k;
    cin >> n >> k;
    vector<int> p(n);
    vector<int> pos(n);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
        --p[i];
        pos[p[i]] = i;
    }
    vector<pair<int, int>> ans;


    // for (int i = 1; i < n; ++i) {
    //     bool ok = true;
    //     while (ok) {
    //         ok = false;
    //         for (int x = n - 1; x >= 0; --x) {
    //             int y = x - i;
    //             if (y < 0) {
    //                 continue;
    //             }
    //             int px = pos[x];
    //             int py = pos[y];
    //             if (k > py - px || t[px][py]) {
    //                 continue;
    //             }
    //             ok = true;
    //             t[px][py] = 1;
    //             swap(pos[x], pos[y]);
    //             swap(p[px], p[py]);
    //             ans.push_back({px, py});
    //             break;
    //         }
    //     }
    // }



    bool ok0 = true;
    while (ok0) {
        ok0 = false;
        for (int i = 1; i < n && !ok0; ++i) {
            // bool ok = true;
            // while (ok) {
                // ok = false;
                for (int x = 0; x < n; ++x) {
                // for (int x = n - 1; x >= 0; --x) {
                    int y = x - i;
                    if (y < 0) {
                        continue;
                    }
                    int px = pos[x];
                    int py = pos[y];
                    if (k > py - px || t[px][py]) {
                        continue;
                    }
                    // ok = true;
                    ok0 = true;
                    t[px][py] = 1;
                    swap(pos[x], pos[y]);
                    swap(p[px], p[py]);
                    ans.push_back({px, py});
                    break;
                }
            // }
        }
    }

    cout << ans.size() << "\n";
    for (auto [x, y] : ans) {
        cout << x + 1 << " " << y + 1 << "\n";
    }
}
 
signed main() {
#ifdef LOCAL
    assert(freopen("in.txt", "r", stdin));
    assert(freopen("out.txt", "w", stdout));
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(20);
 
    int T = 1;
    // cin >> T;
    for (int numt = 0; numt < T; ++numt) {
        solve();
    }
 
#ifdef LOCAL
    cout << endl << endl << "time = " << clock() / (double)CLOCKS_PER_SEC << endl;
#endif
}

Submission Info

Submission Time
Task B - Improve Inversions
User mHuman
Language C++ 20 (gcc 12.2)
Score 600
Code Size 3207 Byte
Status AC
Exec Time 40 ms
Memory 4448 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 4
AC × 23
Set Name Test Cases
Sample 00-sample-001.txt, 00-sample-002.txt, 00-sample-003.txt, 00-sample-004.txt
All 00-sample-001.txt, 00-sample-002.txt, 00-sample-003.txt, 00-sample-004.txt, 01-001.txt, 01-002.txt, 01-003.txt, 01-004.txt, 01-005.txt, 01-006.txt, 01-007.txt, 01-008.txt, 01-009.txt, 01-010.txt, 01-011.txt, 01-012.txt, 01-013.txt, 01-014.txt, 01-015.txt, 01-016.txt, 01-017.txt, 01-018.txt, 01-019.txt
Case Name Status Exec Time Memory
00-sample-001.txt AC 1 ms 3788 KiB
00-sample-002.txt AC 1 ms 3572 KiB
00-sample-003.txt AC 1 ms 3792 KiB
00-sample-004.txt AC 1 ms 3728 KiB
01-001.txt AC 1 ms 3704 KiB
01-002.txt AC 2 ms 3744 KiB
01-003.txt AC 4 ms 3640 KiB
01-004.txt AC 1 ms 3724 KiB
01-005.txt AC 1 ms 3780 KiB
01-006.txt AC 7 ms 3844 KiB
01-007.txt AC 1 ms 3720 KiB
01-008.txt AC 2 ms 3728 KiB
01-009.txt AC 20 ms 3988 KiB
01-010.txt AC 20 ms 4040 KiB
01-011.txt AC 19 ms 3980 KiB
01-012.txt AC 19 ms 3860 KiB
01-013.txt AC 15 ms 3984 KiB
01-014.txt AC 24 ms 4012 KiB
01-015.txt AC 2 ms 3712 KiB
01-016.txt AC 1 ms 3776 KiB
01-017.txt AC 2 ms 3732 KiB
01-018.txt AC 40 ms 4448 KiB
01-019.txt AC 38 ms 4392 KiB