QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#699184#2955. Stable Tablenickbelov#WA 1ms4072kbC++203.8kb2024-11-02 03:22:532024-11-02 03:22:54

Judging History

你现在查看的是最新测评结果

  • [2024-11-02 03:22:54]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:4072kb
  • [2024-11-02 03:22:53]
  • 提交

answer

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T>
ostream_iterator<T> oit(const string &s = " "){ return ostream_iterator<T>(cout,s.c_str()); }
inline auto rep(int l, int r) { return views::iota(min(l, r), r); }
inline auto rep(int n) { return rep(0, n); }
inline auto rep1(int l, int r) { return rep(l, r + 1); }
inline auto rep1(int n) { return rep(1, n + 1); }
inline auto per(int l, int r) { return rep(l, r) | views::reverse; }
inline auto per(int n) { return per(0, n); }
inline auto per1(int l, int r) { return per(l, r + 1); }
inline auto per1(int n) { return per(1, n + 1); }
#define A(a) begin(a),end(a)
inline auto len = ranges::ssize;

struct chash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
template<typename T, typename U> using pb_map = gp_hash_table<T, U, chash>;
template<typename T> using pb_set = gp_hash_table<T, null_type, chash>;
#define K first
#define V second

using ll = long long;
using ld = long double;

using vi = vector<int>;
using vii = vector<vector<int>>;
typedef vector<ll> vll;
using pll = pair<ll,ll>;
using pii = pair<int,int>;

constexpr ll NN = 2e5, M = 1000000007, L = 20;

ll f1[NN], f2[NN];
ll inv(ll a, ll b=M) { return 1 < a ? b - inv(b % a, a) * b / a : 1; } // inv a mod b
ll choose(ll n, ll k) { return f1[n] * f2[k] % M * f2[n - k] % M; } // n choose k

void run()
{
    int h, w;
    cin >> h >> w;

    vii a(h, vi(w));

    for (auto &x : a) for (auto &x : x) cin >> x;

    int f = 9999;
    vii adj(1e4), radj(1e4);
    vi top;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (i == 0 && top.size() < 2 && (top.empty() || top.back() != a[i][j])) top.push_back(a[i][j]);

            if (i == h - 1) {
                adj[a[i][j]].push_back(f);
                radj[f].push_back(a[i][j]);
            } else {
                adj[a[i][j]].push_back(a[i + 1][j]);
                adj[a[i][j]].push_back(a[i][j + 1]);
            }
        }
    }

    vi da(1e4, 1e9);

    queue<int> nodes;
    nodes.push(top[0]);
    da[top[0]] = 0;
    while (nodes.size()) {
        int u = nodes.front();
        nodes.pop();

        for (int v : adj[u]) {
            if (da[v] == 1e9) {
                da[v] = da[u] + 1;
                nodes.push(v);
            }
        }
    }

    if (top.size() == 1) {
        cout << da[f];
        return;
    }

    vi db(1e4, 1e9);
    nodes.push(top[1]);
    db[top[1]] = 0;
    while (nodes.size()) {
        int u = nodes.front();
        nodes.pop();

        for (int v : adj[u]) {
            if (db[v] == 1e9) {
                db[v] = db[u] + 1;
                nodes.push(v);
            }
        }
    }

    vi df(1e4, 1e9); nodes.push(f);
    df[f] = 0;
    while (nodes.size()) {
        int u = nodes.front();
        nodes.pop();

        for (int v : radj[u]) {
            if (df[v] == 1e9) {
                df[v] = df[u] + 1;
                nodes.push(v);
            }
        }
    }

    ll ans = 1e9;
    for (int i = 0; i < 1e4; i++) {
        ans = min(ans, (ll)da[i] + db[i] + df[i]);
    }

    cout << ans;
}

int main()
{
    //KING OF THE WORLD...... U.W.T.B
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    run();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 4072kb

input:

7 8
3 3 3 3 10 10 10 10
2 14 3 7 7 10 4 11
2 14 3 1 1 10 4 11
2 14 8 8 8 8 4 11
9 14 5 5 5 5 4 13
9 14 12 12 12 12 4 13
9 6 6 6 6 6 6 13

output:

5

result:

ok single line: '5'

Test #2:

score: 0
Accepted
time: 1ms
memory: 3660kb

input:

8 8
1 1 1 1 1 1 1 1
2 3 3 4 5 6 6 7
2 2 3 4 5 6 7 7
2 3 3 3 6 6 6 7
2 2 3 8 8 8 6 7
9 2 9 10 10 11 11 12
9 9 9 9 10 11 12 12
13 9 14 14 10 10 15 15

output:

3

result:

ok single line: '3'

Test #3:

score: 0
Accepted
time: 1ms
memory: 3720kb

input:

10 10
1 1 1 1 1 1 1 1 1 1
1 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 2
3 4 5 6 7 8 9 10 11 2
12 13 5 6 7 7 7 7 2 2
14 15 5 6 6 6 6 7 16 16
14 15 15 15 6 16 16 16 16 16
14 14 14 15 16 16 17 16 18 19
15 15 15 15 20 20 20 20 18 19
20 20 20 20 20 21 21 21 21 19

output:

4

result:

ok single line: '4'

Test #4:

score: -100
Wrong Answer
time: 1ms
memory: 3932kb

input:

10 8
20 20 20 20 16 16 16 16
18 19 1 3 3 21 17 2
18 19 1 4 4 21 17 2
18 19 1 5 5 21 17 2
18 19 6 6 6 6 17 2
18 7 7 7 7 7 7 2
18 8 8 8 8 8 8 8
9 9 9 9 9 9 11 11
14 14 9 9 9 9 12 12
15 15 10 10 10 10 13 13

output:

8

result:

wrong answer 1st lines differ - expected: '7', found: '8'