CSES - NOI 2024 - Results
Submission details
Task:Thin Ice
Sender:John Hedin
Submission time:2024-03-20 17:25:13 +0200
Language:C++17
Status:READY
Result:29
Feedback
groupverdictscore
#1ACCEPTED17
#2ACCEPTED12
#30
#40
#50
#60
Test results
testverdicttimegroup
#1ACCEPTED0.00 s1, 5, 6details
#2ACCEPTED0.00 s1, 5, 6details
#3ACCEPTED0.00 s1, 5, 6details
#4ACCEPTED0.00 s1, 5, 6details
#5ACCEPTED0.00 s1, 5, 6details
#6ACCEPTED0.00 s1, 5, 6details
#7ACCEPTED0.00 s1, 5, 6details
#8ACCEPTED0.00 s1, 5, 6details
#9ACCEPTED0.00 s1, 2, 5, 6details
#10ACCEPTED0.15 s2, 6details
#11ACCEPTED0.14 s2, 6details
#12ACCEPTED0.15 s2, 6details
#13ACCEPTED0.14 s2, 6details
#14ACCEPTED0.14 s2, 6details
#15ACCEPTED0.14 s2, 6details
#16ACCEPTED0.14 s2, 6details
#17ACCEPTED0.15 s2, 6details
#18ACCEPTED0.14 s2, 6details
#19ACCEPTED0.14 s2, 6details
#20ACCEPTED0.14 s2, 6details
#21ACCEPTED0.13 s2, 6details
#22--3, 4, 5, 6details
#23ACCEPTED0.00 s3, 4, 5, 6details
#24ACCEPTED0.00 s3, 4, 5, 6details
#25ACCEPTED0.00 s3, 4, 5, 6details
#26ACCEPTED0.00 s3, 4, 5, 6details
#27ACCEPTED0.00 s3, 4, 5, 6details
#28--4, 6details
#29ACCEPTED0.24 s4, 6details
#30ACCEPTED0.24 s4, 6details
#31--4, 6details
#32ACCEPTED0.29 s4, 6details
#33ACCEPTED0.20 s4, 6details
#34--4, 6details
#35--5, 6details
#360.01 s5, 6details
#37ACCEPTED0.01 s5, 6details
#38ACCEPTED0.01 s5, 6details
#39ACCEPTED0.01 s5, 6details
#40ACCEPTED0.01 s5, 6details
#41ACCEPTED0.01 s5, 6details
#420.27 s6details
#43--6details
#44ACCEPTED0.34 s6details
#450.36 s6details
#46ACCEPTED0.28 s6details
#47ACCEPTED0.35 s6details
#48--6details

Code

#include <iostream>
#include <vector>
#include <set>

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll,ll> pll;
typedef set<pll> sp;
typedef vector<sp> vsp;

#define rep(a,b,c) for(ll a = b; a < c; a++)
#define rrep(a,b,c) for(ll a = c-1; a >= b; a--)
#define itr_rep(a,b,_type) for(_type::iterator a = b.begin(); a != b.end(); a++)
#define pb push_back
#define mp make_pair
#define sz size
#define all(a) a.begin(),a.end()

ll uf_parent(ll a);
ll uf_merge(ll a, ll b);
bool try_D();

ll n;
ll m;
vll d;
ll D; // 2 * 10^5

ll coords(ll i, ll j) {return m*i + j;}

vll uf_par;
vll uf_size;
vsp uf_border; // <d,par_id> over all active components

sp best; // <d,par_id> over all active components
// best contains the id of where it is, not where it goes.
// being an active component is the same as having
//      an element with your par_id in this set.
sp edge; // <d,id> for all things on the edge.

int main() {
    cin >> n;
    cin >> m;
    d.resize(n*m,0);
    rep(i,0,n) {
        rep(j,0,m) {
            cin >> d[coords(i,j)];
        }
    }
    D = n*m;

    // initialize uf
    uf_par.resize(n*m,0);
    rep(i,0,n*m) {uf_par[i] = i;}
    uf_size.resize(n*m,1);
    uf_border.resize(n*m,sp());
    rep(i,0,n) {
        rep(j,0,m) {
            if (i != 0) {uf_border[coords(i,j)].insert(mp(-d[coords(i-1,j)],coords(i-1,j)));}
            if (i != n-1) {uf_border[coords(i,j)].insert(mp(-d[coords(i+1,j)],coords(i+1,j)));}
            if (j != 0) {uf_border[coords(i,j)].insert(mp(-d[coords(i,j-1)],coords(i,j-1)));}
            if (j != m-1) {uf_border[coords(i,j)].insert(mp(-d[coords(i,j+1)],coords(i,j+1)));}
        }
    }

    // create edge
    rep(i,0,n) {edge.insert(mp(-d[coords(i,0)],coords(i,0)));}
    rep(i,0,n) {edge.insert(mp(-d[coords(i,m-1)],coords(i,m-1)));}
    rep(j,0,m) {edge.insert(mp(-d[coords(0,j)],coords(0,j)));}
    rep(j,0,m) {edge.insert(mp(-d[coords(n-1,j)],coords(n-1,j)));}

    best.clear();

    while (!try_D()) {D--;} // D=1 must always work

    cout << D << endl;
}

bool try_D() {
    while (-(*edge.begin()).first >= D) { // Introduce all edge components with d >= D
        ll tmp = (*edge.begin()).second;
        if (uf_par[tmp] == tmp) {best.insert(mp((*uf_border[tmp].begin()).first,tmp));}
        edge.erase(edge.begin()); // Adding item to best is the same as activating it?
    }

    while (!best.empty()) {
        ll cmp = uf_parent((*best.begin()).second);
        best.erase(best.begin());
        while (-(*uf_border[cmp].begin()).first >= D - uf_size[cmp]) {
            ll other = (*uf_border[cmp].begin()).second;
            uf_border[cmp].erase(uf_border[cmp].begin());
            cmp = uf_merge(cmp, other); // uf_merge deals with if the two are in the same component
            if (uf_size[cmp] == D) {return true;}
        } // backburner time!
        edge.insert(mp((*uf_border[cmp].begin()).first - uf_size[cmp],cmp)); // <-(d+uf_size), par_id>
    }
    return false;
}

ll uf_parent(ll a) {
    if (uf_par[a] != uf_par[uf_par[a]])  {uf_par[a] = uf_par[uf_par[a]];}
    return uf_par[a];
}

ll uf_merge(ll a, ll b) {
    ll ap = uf_parent(a);
    ll bp = uf_parent(b);
    if (ap == bp) {return ap;}
    if (uf_size[bp] > uf_size[ap]) {swap(ap,bp);}
    uf_par[bp] = ap;
    uf_size[ap] += uf_size[bp];
    if (uf_border[ap].sz() < uf_border[bp].sz()) {swap(uf_border[ap],uf_border[bp]);}
    uf_border[ap].insert(all(uf_border[bp]));
    uf_border[bp].clear();
    return ap;
}

// We build from larger to smaller, beginning at the edge.
// Hence we need to sort all the things at the edge to do
// stuff

// Consistency stuff:
// Everything within a compoent has the same parent
// This parent has a set with the cells on the border ordered by d
// It may contain cells already in the component, so this needs to be checked in every acess

// We want every component to be able to reach D.
// Hence it can add a cell iff the new call has d >= D-uf_size[cmp]. D is global. uf_size is consistent

// We always try to add the next cell with largest d over all possible merges.
// If this wasn't large enough, we put the component on the backburner.
// Backburner = components with no immediate possible merges.

// What if a new merge becomes possible for a component on the back-burner?
// This only happends when another component expands into it.
// Then the components will be merged and the non-backburner will keep being a non-backburner.

// What happends when all components have been put on the back-burner?
// Then we decrement D.
// How do we non-linearly get items back from the back-burner?
// Can we sort stuff on the back-burner such that some stuff get's back earlier?
// Then every thing goes back at most once per item.

// Something goes back when it can merge with the item. d_max >= D-uf_size, which happends when D =< d_max + uf_size.
// Hence we can use edge as the back_burner.

// After we have gone into a component and merged everything there is there,
// we add it to the back-burner as it cannot get high enough on the list in this iteration of D
// unless another compoennt merges into it, but then we only take over that spot in best.

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:

input
1 100
46 23 23 55 37 17 30 32 25 71 ...

correct output
30

user output
(empty)

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:

input
1 200000
138736 14949 12344 104147 1333...

correct output
1806

user output
(empty)

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:

input
1 200000
180410 160820 160820 180614 18...

correct output
160832

user output
(empty)

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:

input
1 200000
14737 162555 44228 170991 1340...

correct output
1902

user output
(empty)

Test 35

Group: 5, 6

Verdict:

input
31 32
669 792 226 189 860 737 291 83...

correct output
565

user output
(empty)

Test 36

Group: 5, 6

Verdict:

input
10 100
730 698 339 743 536 702 94 556...

correct output
529

user output
568

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:

input
447 447
6474 27185 108482 124481 16058...

correct output
88202

user output
88654

Test 43

Group: 6

Verdict:

input
1000 200
27722 57131 197677 184858 9285...

correct output
89324

user output
(empty)

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:

input
7 28571
193031 185883 171670 185794 17...

correct output
171680

user output
171715

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:

input
1000 200
198379 62425 88013 50967 49098...

correct output
89131

user output
(empty)