| Task: | Thin Ice |
| Sender: | Erik Hedin |
| Submission time: | 2024-03-06 20:00:03 +0200 |
| Language: | C++ (C++17) |
| Status: | READY |
| Result: | 100 |
| group | verdict | score |
|---|---|---|
| #1 | ACCEPTED | 17 |
| #2 | ACCEPTED | 12 |
| #3 | ACCEPTED | 11 |
| #4 | ACCEPTED | 19 |
| #5 | ACCEPTED | 14 |
| #6 | ACCEPTED | 27 |
| test | verdict | time | group | |
|---|---|---|---|---|
| #1 | ACCEPTED | 0.00 s | 1, 5, 6 | details |
| #2 | ACCEPTED | 0.00 s | 1, 5, 6 | details |
| #3 | ACCEPTED | 0.00 s | 1, 5, 6 | details |
| #4 | ACCEPTED | 0.00 s | 1, 5, 6 | details |
| #5 | ACCEPTED | 0.00 s | 1, 5, 6 | details |
| #6 | ACCEPTED | 0.00 s | 1, 5, 6 | details |
| #7 | ACCEPTED | 0.00 s | 1, 5, 6 | details |
| #8 | ACCEPTED | 0.00 s | 1, 5, 6 | details |
| #9 | ACCEPTED | 0.00 s | 1, 2, 5, 6 | details |
| #10 | ACCEPTED | 0.16 s | 2, 6 | details |
| #11 | ACCEPTED | 0.15 s | 2, 6 | details |
| #12 | ACCEPTED | 0.16 s | 2, 6 | details |
| #13 | ACCEPTED | 0.16 s | 2, 6 | details |
| #14 | ACCEPTED | 0.15 s | 2, 6 | details |
| #15 | ACCEPTED | 0.15 s | 2, 6 | details |
| #16 | ACCEPTED | 0.15 s | 2, 6 | details |
| #17 | ACCEPTED | 0.15 s | 2, 6 | details |
| #18 | ACCEPTED | 0.15 s | 2, 6 | details |
| #19 | ACCEPTED | 0.15 s | 2, 6 | details |
| #20 | ACCEPTED | 0.17 s | 2, 6 | details |
| #21 | ACCEPTED | 0.15 s | 2, 6 | details |
| #22 | ACCEPTED | 0.00 s | 3, 4, 5, 6 | details |
| #23 | ACCEPTED | 0.00 s | 3, 4, 5, 6 | details |
| #24 | ACCEPTED | 0.00 s | 3, 4, 5, 6 | details |
| #25 | ACCEPTED | 0.00 s | 3, 4, 5, 6 | details |
| #26 | ACCEPTED | 0.00 s | 3, 4, 5, 6 | details |
| #27 | ACCEPTED | 0.00 s | 3, 4, 5, 6 | details |
| #28 | ACCEPTED | 0.91 s | 4, 6 | details |
| #29 | ACCEPTED | 0.23 s | 4, 6 | details |
| #30 | ACCEPTED | 0.22 s | 4, 6 | details |
| #31 | ACCEPTED | 0.58 s | 4, 6 | details |
| #32 | ACCEPTED | 0.64 s | 4, 6 | details |
| #33 | ACCEPTED | 0.23 s | 4, 6 | details |
| #34 | ACCEPTED | 0.91 s | 4, 6 | details |
| #35 | ACCEPTED | 0.01 s | 5, 6 | details |
| #36 | ACCEPTED | 0.01 s | 5, 6 | details |
| #37 | ACCEPTED | 0.01 s | 5, 6 | details |
| #38 | ACCEPTED | 0.01 s | 5, 6 | details |
| #39 | ACCEPTED | 0.01 s | 5, 6 | details |
| #40 | ACCEPTED | 0.01 s | 5, 6 | details |
| #41 | ACCEPTED | 0.01 s | 5, 6 | details |
| #42 | ACCEPTED | 0.32 s | 6 | details |
| #43 | ACCEPTED | 0.34 s | 6 | details |
| #44 | ACCEPTED | 0.35 s | 6 | details |
| #45 | ACCEPTED | 0.47 s | 6 | details |
| #46 | ACCEPTED | 0.42 s | 6 | details |
| #47 | ACCEPTED | 0.36 s | 6 | details |
| #48 | ACCEPTED | 0.34 s | 6 | details |
Code
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <set>
#include <map>
#include <queue>
using namespace std;
typedef long long int ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef set<pll> spll;
typedef pair<pll, ll> ppll_ll;
typedef set<ppll_ll> sppll_ll;
typedef set<ll> sll;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define sz(a) (ll) a.size()
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define trav(itr, a, t) for (t::iterator itr = a.begin(); itr != a.end())
// coordinate convention (y, x) for (n, m)
const pll delta[4] = {mp(1, 0), mp(0, 1), mp(-1, 0), mp(0, -1)};
ll n, m, d;
vvll ice;
bool inline is_edge(const pll& pos) {return (pos.first == 0) || (pos.first == n - 1) || (pos.second == 0) || (pos.second == m - 1);}
ll inline coco(pll z) {return z.first + (z.second * n);}
pll inline rev_coco(ll z) {return mp(z % n, z / n);}
pll inline operator+(const pll& lhs, const pll& rhs) {return mp(lhs.first + rhs.first, lhs.second + rhs.second);}
pll inline operator+(const pll& lhs, const ll& rhs) {return mp(lhs.first + rhs, lhs.second);}
sppll_ll best_edges; // ((d[neigh] + cnt, neigh), src)
spll best_comp; // (cnt, src)
struct component;
vector<component> comps;
vll par;
ll find_par(ll x) {
if (x != par[x]) par[x] = find_par(par[x]);
return par[x];
}
struct component {
ll root;
ll cnt;
spll edges; // (d[neigh], neigh)
//sll members;
component(ll root) : root(root), cnt(1) {
par[root] = root;
//members.insert(root);
}
ppll_ll best_edge() {
return mp(*edges.rbegin() + cnt, root);
}
void add_best_edge() {
if (sz(edges)) best_edges.insert(best_edge());
}
void absorb(ll oth) {
//cout << "root " << root << endl;
if (sz(edges)) best_edges.erase(best_edge()); // remove previous best edge
best_comp.erase(mp(cnt, root));
//cout << oth << " -> " << find_par(oth) << endl;
oth = find_par(oth);
if (sz(comps[oth].edges)) best_edges.erase(comps[oth].best_edge()); // remove previous best edge from oth
best_comp.erase(mp(comps[oth].cnt, oth));
par[oth] = root;
cnt += comps[oth].cnt;
//cout << "cnt -> " << cnt << " (+" << comps[oth].cnt << ")" << endl;
edges.insert(comps[oth].edges.begin(), comps[oth].edges.end()); // merge edges
add_best_edge(); // make public best edge
best_comp.insert(mp(cnt, root));
}
void tick() {
best_edges.erase(best_edge());
edges.erase(prev(edges.end()));
add_best_edge();
}
};
spll e;
int main() {
cin.tie(NULL)->sync_with_stdio(false);
cin >> n >> m;
d = 1;
ice.resize(n);
rep(i, 0, n) {
ice[i].resize(m);
rep(j, 0, m) {
cin >> ice[i][j];
d = max<ll>(d, ice[i][j]);
}
}
par.resize(n * m);
//comps.reserve(n * m);
rep(j, 0, m) {
rep(i, 0, n) {
comps.pb(component(coco(mp(i, j))));
}
}
rep(i, 0, n) {
rep(j, 0, m) {
// initialize neighbors
rep(d, 0, 4) {
pll neigh = mp(i, j) + delta[d];
if ((neigh.first == -1) || (neigh.first == n) || (neigh.second == -1) || (neigh.second == m)) continue; // check oob
// add edge (i, j) -> neigh (with d = ice[neigh])
//cout << "edge: " << coco(mp(i, j)) << " -> (" << ice[neigh.first][neigh.second] << ", " << coco(neigh) << ")" << endl;
comps[coco(mp(i, j))].edges.insert(mp(ice[neigh.first][neigh.second], coco(neigh)));
}
if (is_edge(mp(i, j))) {
e.insert(mp(ice[i][j], coco(mp(i, j))));
}
/* if (is_edge(mp(i, j))) { // now we only consider edges from active...
// initialize presence in best_edges and best_comp
comps[coco(mp(i, j))].add_best_edge();
best_comp.insert(mp(1, coco(mp(i, j))));
} */
}
}
/* cout << "par:" << endl;
rep(i, 0, n) {
rep(j, 0, m) {
cout << par[coco(mp(i, j))] << " ";
}
cout << endl;
} */
d++;
while ((best_comp.empty()) || (best_comp.rbegin()->first < d)) {
//rep(tmp, 0, 20) {
//cout << "d = " << d << endl;
//if (!best_comp.empty()) cout << "comp: " << best_comp.rbegin()->first << " " << best_comp.rbegin()->second << endl;
//if (!best_edges.empty()) cout << "edge: " << best_edges.rbegin()->first.first << " " << best_edges.rbegin()->first.second << " " << best_edges.rbegin()->second << endl;
// check for edges to add
if ((!best_edges.empty()) && (best_edges.rbegin()->first.first >= d)) {
// do some pre-work
ll src = best_edges.rbegin()->second;
ll dest = best_edges.rbegin()->first.second;
// check if edge goes from comp to itself
if (find_par(dest) == src) {
//cout << "tick" << endl;
comps[src].tick(); // remove best edge, because it goes within itself
} else { // the edge merges two distinct components
//cout << "absorb" << endl;
comps[src].absorb(dest);
}
} else { // if no edges, then decrement d
d--;
while ((!e.empty()) && (e.rbegin()->first == d)) {
if (find_par(e.rbegin()->second) == e.rbegin()->second) {
comps[e.rbegin()->second].add_best_edge();
best_comp.insert(mp(1, e.rbegin()->second));
}
e.erase(prev(e.end()));
}
/* // activate all edges with ice = d
rep(i, 0, n) {
rep(j, 0, m) {
if (is_edge(mp(i, j)) && (find_par(coco(mp(i, j))) == coco(mp(i, j)))) {
if (ice[i][j] == d) {
// initialize presence in best_edges and best_comp
comps[coco(mp(i, j))].add_best_edge();
best_comp.insert(mp(1, coco(mp(i, j))));
}
}
}
} */
}
/* cout << "par:" << endl;
rep(i, 0, n) {
rep(j, 0, m) {
cout << par[coco(mp(i, j))] << " ";
}
cout << endl;
} */
}
cout << d << endl;
return 0;
}Test details
Test 1
Group: 1, 5, 6
Verdict: ACCEPTED
| input |
|---|
| 4 4 9 11 5 7 7 9 14 3 1 3 2 3 11 4 14 8 |
| correct output |
|---|
| 10 |
| user output |
|---|
| 10 |
Test 2
Group: 1, 5, 6
Verdict: ACCEPTED
| input |
|---|
| 5 3 10 7 11 8 5 5 12 9 10 3 13 9 ... |
| correct output |
|---|
| 10 |
| user output |
|---|
| 10 |
Test 3
Group: 1, 5, 6
Verdict: ACCEPTED
| input |
|---|
| 4 4 3 2 2 2 5 1 1 1 8 4 1 1 7 6 2 1 |
| correct output |
|---|
| 8 |
| user output |
|---|
| 8 |
Test 4
Group: 1, 5, 6
Verdict: ACCEPTED
| input |
|---|
| 3 5 1 11 1 3 5 4 12 7 8 7 13 14 14 9 4 |
| correct output |
|---|
| 14 |
| user output |
|---|
| 14 |
Test 5
Group: 1, 5, 6
Verdict: ACCEPTED
| input |
|---|
| 5 3 11 8 12 11 11 12 6 2 3 11 8 13 ... |
| correct output |
|---|
| 12 |
| user output |
|---|
| 12 |
Test 6
Group: 1, 5, 6
Verdict: ACCEPTED
| input |
|---|
| 3 5 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 |
| correct output |
|---|
| 14 |
| user output |
|---|
| 14 |
Test 7
Group: 1, 5, 6
Verdict: ACCEPTED
| input |
|---|
| 4 4 12 8 6 5 12 9 6 1 10 1 3 2 8 1 1 1 |
| correct output |
|---|
| 12 |
| user output |
|---|
| 12 |
Test 8
Group: 1, 5, 6
Verdict: ACCEPTED
| input |
|---|
| 4 4 8 3 15 14 10 12 2 4 5 16 9 6 7 13 1 11 |
| correct output |
|---|
| 13 |
| user output |
|---|
| 13 |
Test 9
Group: 1, 2, 5, 6
Verdict: ACCEPTED
| input |
|---|
| 4 4 4 1 1 2 3 5 4 2 2 2 1 2 1 4 3 4 |
| correct output |
|---|
| 4 |
| user output |
|---|
| 4 |
Test 10
Group: 2, 6
Verdict: ACCEPTED
| input |
|---|
| 10 20000 5 3 2 1 3 2 3 3 4 5 1 1 2 3 5 ... |
| correct output |
|---|
| 5 |
| user output |
|---|
| 5 |
Test 11
Group: 2, 6
Verdict: ACCEPTED
| input |
|---|
| 10 20000 1 2 1 2 1 2 1 2 1 1 1 1 2 1 1 ... |
| correct output |
|---|
| 3 |
| user output |
|---|
| 3 |
Test 12
Group: 2, 6
Verdict: ACCEPTED
| input |
|---|
| 10 20000 3 2 2 3 1 2 1 4 4 3 1 4 3 2 4 ... |
| correct output |
|---|
| 5 |
| user output |
|---|
| 5 |
Test 13
Group: 2, 6
Verdict: ACCEPTED
| input |
|---|
| 20000 10 1 1 3 1 2 1 1 1 1 1 1 2 2 1 1 1 1 2 1 1 2 1 1 1 2 2 1 1 1 2 1 1 1 1 1 1 1 1 1 1 ... |
| correct output |
|---|
| 4 |
| user output |
|---|
| 4 |
Test 14
Group: 2, 6
Verdict: ACCEPTED
| input |
|---|
| 10 20000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
| correct output |
|---|
| 3 |
| user output |
|---|
| 3 |
Test 15
Group: 2, 6
Verdict: ACCEPTED
| input |
|---|
| 10 20000 3 1 3 1 2 1 2 3 2 2 1 2 1 1 2 ... |
| correct output |
|---|
| 3 |
| user output |
|---|
| 3 |
Test 16
Group: 2, 6
Verdict: ACCEPTED
| input |
|---|
| 10 20000 1 2 2 2 1 2 3 1 2 2 2 1 2 2 2 ... |
| correct output |
|---|
| 4 |
| user output |
|---|
| 4 |
Test 17
Group: 2, 6
Verdict: ACCEPTED
| input |
|---|
| 10 20000 3 3 2 3 2 3 2 2 2 2 2 1 3 2 1 ... |
| correct output |
|---|
| 4 |
| user output |
|---|
| 4 |
Test 18
Group: 2, 6
Verdict: ACCEPTED
| input |
|---|
| 10 20000 1 3 3 1 1 4 3 3 3 1 2 2 1 3 1 ... |
| correct output |
|---|
| 5 |
| user output |
|---|
| 5 |
Test 19
Group: 2, 6
Verdict: ACCEPTED
| input |
|---|
| 7 28571 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ... |
| correct output |
|---|
| 3 |
| user output |
|---|
| 3 |
Test 20
Group: 2, 6
Verdict: ACCEPTED
| input |
|---|
| 28571 7 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 ... |
| correct output |
|---|
| 5 |
| user output |
|---|
| 5 |
Test 21
Group: 2, 6
Verdict: ACCEPTED
| input |
|---|
| 447 447 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 ... |
| correct output |
|---|
| 3 |
| user output |
|---|
| 3 |
Test 22
Group: 3, 4, 5, 6
Verdict: ACCEPTED
| input |
|---|
| 1 100 46 23 23 55 37 17 30 32 25 71 ... |
| correct output |
|---|
| 30 |
| user output |
|---|
| 30 |
Test 23
Group: 3, 4, 5, 6
Verdict: ACCEPTED
| input |
|---|
| 1 100 8 13 12 11 15 15 15 19 18 21 2... |
| correct output |
|---|
| 76 |
| user output |
|---|
| 76 |
Test 24
Group: 3, 4, 5, 6
Verdict: ACCEPTED
| input |
|---|
| 1 100 94 94 94 95 95 91 97 100 92 98... |
| correct output |
|---|
| 95 |
| user output |
|---|
| 95 |
Test 25
Group: 3, 4, 5, 6
Verdict: ACCEPTED
| input |
|---|
| 1 100 83 83 83 83 83 83 83 83 83 83 ... |
| correct output |
|---|
| 83 |
| user output |
|---|
| 83 |
Test 26
Group: 3, 4, 5, 6
Verdict: ACCEPTED
| input |
|---|
| 1 100 33 34 35 38 38 40 41 42 42 45 ... |
| correct output |
|---|
| 95 |
| user output |
|---|
| 95 |
Test 27
Group: 3, 4, 5, 6
Verdict: ACCEPTED
| input |
|---|
| 1 100 57 1 80 39 18 63 30 86 85 55 8... |
| correct output |
|---|
| 29 |
| user output |
|---|
| 29 |
Test 28
Group: 4, 6
Verdict: ACCEPTED
| input |
|---|
| 1 200000 138736 14949 12344 104147 1333... |
| correct output |
|---|
| 1806 |
| user output |
|---|
| 1806 |
Test 29
Group: 4, 6
Verdict: ACCEPTED
| input |
|---|
| 1 200000 141245 141090 141163 141286 14... |
| correct output |
|---|
| 155109 |
| user output |
|---|
| 155109 |
Test 30
Group: 4, 6
Verdict: ACCEPTED
| input |
|---|
| 1 200000 102358 102469 102440 102402 10... |
| correct output |
|---|
| 152388 |
| user output |
|---|
| 152388 |
Test 31
Group: 4, 6
Verdict: ACCEPTED
| input |
|---|
| 1 200000 180410 160820 160820 180614 18... |
| correct output |
|---|
| 160832 |
| user output |
|---|
| 160832 |
Test 32
Group: 4, 6
Verdict: ACCEPTED
| input |
|---|
| 1 200000 198270 198270 198270 198270 19... |
| correct output |
|---|
| 198270 |
| user output |
|---|
| 198270 |
Test 33
Group: 4, 6
Verdict: ACCEPTED
| input |
|---|
| 1 200000 1 1 3 2 1 1 2 3 6 6 6 7 8 9 10... |
| correct output |
|---|
| 199995 |
| user output |
|---|
| 199995 |
Test 34
Group: 4, 6
Verdict: ACCEPTED
| input |
|---|
| 1 200000 14737 162555 44228 170991 1340... |
| correct output |
|---|
| 1902 |
| user output |
|---|
| 1902 |
Test 35
Group: 5, 6
Verdict: ACCEPTED
| input |
|---|
| 31 32 669 792 226 189 860 737 291 83... |
| correct output |
|---|
| 565 |
| user output |
|---|
| 565 |
Test 36
Group: 5, 6
Verdict: ACCEPTED
| input |
|---|
| 10 100 730 698 339 743 536 702 94 556... |
| correct output |
|---|
| 529 |
| user output |
|---|
| 529 |
Test 37
Group: 5, 6
Verdict: ACCEPTED
| input |
|---|
| 32 31 633 613 618 605 635 638 668 67... |
| correct output |
|---|
| 678 |
| user output |
|---|
| 678 |
Test 38
Group: 5, 6
Verdict: ACCEPTED
| input |
|---|
| 142 7 983 930 963 926 979 962 962 966 930 963 924 928 928 926 926 929 929 922 931 931 978 929 929 929 922 959 928 964 ... |
| correct output |
|---|
| 934 |
| user output |
|---|
| 934 |
Test 39
Group: 5, 6
Verdict: ACCEPTED
| input |
|---|
| 31 32 977 977 977 977 977 977 977 97... |
| correct output |
|---|
| 977 |
| user output |
|---|
| 977 |
Test 40
Group: 5, 6
Verdict: ACCEPTED
| input |
|---|
| 50 20 1 27 14 23 38 48 56 3 12 9 6 2... |
| correct output |
|---|
| 997 |
| user output |
|---|
| 997 |
Test 41
Group: 5, 6
Verdict: ACCEPTED
| input |
|---|
| 20 50 481 532 624 290 965 58 448 872... |
| correct output |
|---|
| 504 |
| user output |
|---|
| 504 |
Test 42
Group: 6
Verdict: ACCEPTED
| input |
|---|
| 447 447 6474 27185 108482 124481 16058... |
| correct output |
|---|
| 88202 |
| user output |
|---|
| 88202 |
Test 43
Group: 6
Verdict: ACCEPTED
| input |
|---|
| 1000 200 27722 57131 197677 184858 9285... |
| correct output |
|---|
| 89324 |
| user output |
|---|
| 89324 |
Test 44
Group: 6
Verdict: ACCEPTED
| input |
|---|
| 447 447 70928 73154 72640 74764 75237 ... |
| correct output |
|---|
| 181096 |
| user output |
|---|
| 181096 |
Test 45
Group: 6
Verdict: ACCEPTED
| input |
|---|
| 7 28571 193031 185883 171670 185794 17... |
| correct output |
|---|
| 171680 |
| user output |
|---|
| 171680 |
Test 46
Group: 6
Verdict: ACCEPTED
| input |
|---|
| 10 20000 191628 191628 191628 191628 19... |
| correct output |
|---|
| 191628 |
| user output |
|---|
| 191628 |
Test 47
Group: 6
Verdict: ACCEPTED
| input |
|---|
| 200 1000 3550 2640 3791 4248 4257 4504 ... |
| correct output |
|---|
| 199997 |
| user output |
|---|
| 199997 |
Test 48
Group: 6
Verdict: ACCEPTED
| input |
|---|
| 1000 200 198379 62425 88013 50967 49098... |
| correct output |
|---|
| 89131 |
| user output |
|---|
| 89131 |
