Submission details
Task:Hypyt
Sender:sharph2
Submission time:2025-10-18 15:47:08 +0300
Language:C++ (C++17)
Status:READY
Result:30
Feedback
groupverdictscore
#1ACCEPTED30
#20
Test results
testverdicttimegroup
#1ACCEPTED0.00 s1, 2details
#2ACCEPTED0.01 s1, 2details
#3ACCEPTED0.33 s2details
#4--2details
#5--2details

Code

#include <algorithm>
#include <iostream>
#include <limits>
#include <random>
#include <set>
#include <tuple>
#include <map>
#include <vector>

using namespace std;

using P = pair<int, int>;

P operator-(P a, P b) {
    return {a.first - b.first, a.second - b.second};
}
P operator+(P a, P b) {
    return {a.first + b.first, a.second + b.second};
}

vector<P> ratko(int n, int m) {
    int nm = n * m;

    vector<P> jarj;
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < m; ++j) {
            jarj.push_back({i, j});
        }
    }

    mt19937 rng(1234);

    shuffle(jarj.begin() + 1, jarj.end(), rng);

    map<P, P> yksiot;
    map<P, set<P>> monikot;

    const auto lisaa = [&](int i) {
        if(i >= nm - 1) {
            throw 5;
        }
        P hyppy = jarj[i + 1] - jarj[i];
        if(monikot.count(hyppy)) {
            monikot[hyppy].insert(jarj[i]);
        } else if(yksiot.count(hyppy)) {
            monikot[hyppy].insert(yksiot[hyppy]);
            monikot[hyppy].insert(jarj[i]);
            yksiot.erase(hyppy);
        } else {
            yksiot[hyppy] = jarj[i];
        }
    };
    const auto poista = [&](int i) {
        if(i >= nm - 1) {
            throw 5;
        }
        P hyppy = jarj[i + 1] - jarj[i];
        if(yksiot.count(hyppy)) {
            yksiot.erase(hyppy);
        } else if(monikot.count(hyppy)) {
            auto& l = monikot[hyppy];
            l.erase(jarj[i]);
            if(l.size() == 1) {
                yksiot[hyppy] = *l.begin();
                monikot.erase(hyppy);
            }
        } else {
            throw 5;
        }
    };

    for(int i = 1; i < nm; ++i) {
        lisaa(i - 1);
    }

    while(!monikot.empty()) {
        vector<pair<P, P>> monikkohypyt;
        for(auto& item : monikot) {
            for(auto& x : item.second) {
                monikkohypyt.push_back({item.first, x});
            }
        }
        auto [hyppy, pos1] = monikkohypyt[uniform_int_distribution<int>(0, (int)monikkohypyt.size() - 1)(rng)];

        int i1 = 0;
        while(jarj[i1] != pos1) {
            ++i1;
            if(i1 >= nm - 1) {
                throw 5;
            }
        }

        int i2 = i1;
        while(i1 == i2) {
            i2 = uniform_int_distribution<int>(0, nm - 1)(rng);
        }

        if(i1 > i2) {
            swap(i1, i2);
        }

        if(i1 < 0 || i2 < 0 || i1 > nm - 2 || i2 > nm - 1 || i1 == i2) {
            throw 5;
        }

        poista(i1);
        if(i2 != nm - 1) {
            poista(i2);
        }

        vector<P> tmp1(i2 - i1);
        vector<P> tmp2(nm - i2 - 1);
        copy(jarj.begin() + i1 + 1, jarj.begin() + i2 + 1, tmp1.begin());
        copy(jarj.begin() + i2 + 1, jarj.end(), tmp2.begin());
        copy(tmp2.begin(), tmp2.end(), jarj.begin() + i1 + 1);
        copy(tmp1.begin(), tmp1.end(), jarj.begin() + i1 + 1 + tmp2.size());

        lisaa(i1);
        if(i2 != nm - 1) {
            lisaa(i1 + tmp2.size());
        }
    }

    return jarj;
}

int main() {
    cin.sync_with_stdio(false);
    cin.tie(nullptr);

    // ratko(50, 50);
    // return 0;
    // for(int n = 1; n <= 50; ++n) {
    //     for(int m = 1; m <= 50; ++m) {
    //         ratko(n, m);
    //     }
    // }
    // return 0;

    int tc;
    cin >> tc;

    for(int ti = 0; ti < tc; ++ti) {
        int n, m;
        cin >> n >> m;
        vector<P> jarj = ratko(n, m);
        for(int i = 1; i < (int)jarj.size(); ++i) {
            P hyppy = jarj[i] - jarj[i - 1];
            cout << hyppy.first << " " << hyppy.second << "\n";
        }
    }

    return 0;
}

Test details

Test 1

Group: 1, 2

Verdict: ACCEPTED

input
25
1 1
1 2
1 3
1 4
...

correct output
0 1
0 2
0 -1
0 3
0 -2
...

user output
0 1
0 2
0 -1
0 2
0 1
...

Test 2

Group: 1, 2

Verdict: ACCEPTED

input
100
5 5
5 5
5 5
5 5
...

correct output
4 4
-4 -3
4 2
-4 -1
4 0
...

user output
1 1
-1 3
4 -1
-1 1
-3 -1
...

Test 3

Group: 2

Verdict: ACCEPTED

input
100
1 25
20 40
5 34
50 34
...

correct output
0 24
0 -23
0 22
0 -21
0 20
...

user output
0 3
0 20
0 -6
0 -1
0 -5
...

Test 4

Group: 2

Verdict:

input
100
46 47
41 39
46 36
46 30
...

correct output
45 46
-45 -45
45 44
-45 -43
45 42
...

user output
(empty)

Test 5

Group: 2

Verdict:

input
100
50 50
50 50
50 50
50 50
...

correct output
49 49
-49 -48
49 47
-49 -46
49 45
...

user output
(empty)