QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#660087 | #2955. Stable Table | enze114514# | WA | 0ms | 3752kb | C++20 | 2.2kb | 2024-10-20 05:23:43 | 2024-10-20 05:23:44 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 150 * 150, M = N * N;
vector<vector<int>> G;
map<int, int> in_s;
vector<int> s;
map<int, int> in_t;
vector<int> t;
int n, m;
int a[150][150];
int dis1[N], dis2[N];
void dij(int s, int dis[]) {
priority_queue<pair<int, int>> q;
fill(dis, dis + N, INT_MAX);
dis[s] = 0;
q.emplace(0, s);
while (!q.empty()) {
auto [d, u] = q.top();
q.pop();
d = -d;
if (d != dis[u]) continue;
for (int v : G[u]) {
if (dis[v] > dis[u] + 1) {
dis[v] = dis[u] + 1;
q.emplace(-dis[v], v);
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
G.resize(n * m);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> a[i][j];
}
}
for (int i = n; i > 1; --i) {
for (int j = 1; j <= m; ++j) {
if (a[i][j] != a[i - 1][j]) {
G[a[i - 1][j]].push_back(a[i][j]);
}
}
}
for (int i = 1; i <= m; ++i) {
if (!in_s[a[1][i]]) {
in_s[a[1][i]] = 1;
s.push_back(a[1][i]);
}
if (!in_t[a[n][i]]) {
in_t[a[n][i]] = 1;
t.push_back(a[n][i]);
}
}
if (s.size() == 1) {
dij(s[0], dis1);
int ans = INT_MAX;
for (int i = 0; i < t.size(); ++i) ans = min(ans, dis1[t[i]] + 1);
cout << ans << '\n';
} else {
dij(s[0], dis1);
dij(s[1], dis2);
int ans = INT_MAX;
for (int i = 0; i < t.size(); ++i) {
for (int j = 0; j < t.size(); ++j) {
// cout << "i: " << i << " j: " << j << '\n';
// cout << "t[i]: " << t[i] << " t[j]: " << t[j] << '\n';
// cout << dis1[t[i]] << ' ' << dis2[t[j]] << '\n';
// cout << "\n\n";
if (dis1[t[i]] == INT_MAX || dis2[t[j]] == INT_MAX) continue;
ans = min(ans, dis1[t[i]] + dis2[t[j]] + 2);
}
}
cout << ans << '\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3752kb
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:
6
result:
wrong answer 1st lines differ - expected: '5', found: '6'