CSES - NOI 2024 - Results
Submission details
Task:Thin Ice
Sender:Sofie Fu
Submission time:2024-03-17 16:36:55 +0200
Language:C++ (C++20)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
#40
#50
#60
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 5, 6details
#2ACCEPTED0.01 s1, 5, 6details
#3ACCEPTED0.01 s1, 5, 6details
#40.01 s1, 5, 6details
#5ACCEPTED0.01 s1, 5, 6details
#6ACCEPTED0.01 s1, 5, 6details
#7ACCEPTED0.01 s1, 5, 6details
#8ACCEPTED0.01 s1, 5, 6details
#9ACCEPTED0.01 s1, 2, 5, 6details
#10ACCEPTED0.37 s2, 6details
#110.15 s2, 6details
#12ACCEPTED0.32 s2, 6details
#13ACCEPTED0.12 s2, 6details
#140.14 s2, 6details
#150.22 s2, 6details
#16ACCEPTED0.21 s2, 6details
#170.31 s2, 6details
#18ACCEPTED0.30 s2, 6details
#19ACCEPTED0.10 s2, 6details
#20ACCEPTED0.12 s2, 6details
#21ACCEPTED0.05 s2, 6details
#220.01 s3, 4, 5, 6details
#23ACCEPTED0.01 s3, 4, 5, 6details
#240.01 s3, 4, 5, 6details
#25ACCEPTED0.01 s3, 4, 5, 6details
#26ACCEPTED0.01 s3, 4, 5, 6details
#270.01 s3, 4, 5, 6details
#280.44 s4, 6details
#290.26 s4, 6details
#300.26 s4, 6details
#310.33 s4, 6details
#32ACCEPTED0.06 s4, 6details
#33ACCEPTED0.22 s4, 6details
#340.46 s4, 6details
#350.01 s5, 6details
#360.01 s5, 6details
#370.01 s5, 6details
#380.01 s5, 6details
#39ACCEPTED0.01 s5, 6details
#40ACCEPTED0.01 s5, 6details
#410.01 s5, 6details
#420.53 s6details
#430.53 s6details
#440.39 s6details
#450.45 s6details
#46ACCEPTED0.06 s6details
#47ACCEPTED0.40 s6details
#480.58 s6details

Compiler report

input/code.cpp: In constructor 'dsu::dsu(long long int)':
input/code.cpp:46:18: warning: 'dsu::nei' will be initialized after [-Wreorder]
   46 |     vo<set<int>> nei;
      |                  ^~~
input/code.cpp:45:23: warning:   'vi dsu::conEdge' [-Wreorder]
   45 |     vi par, siz, val, conEdge, compind;
      |                       ^~~~~~~
input/code.cpp:47:5: warning:   when initialized here [-Wreorder]
   47 |     dsu(int n): par(n), siz(n, 1), val(n), nei(n), conEdge(n, 0), compind(n){
      |     ^~~

Code

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vo vector
#define pb push_back
#define se second
#define fi first
#define sz(x) x.size()
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define umap unordered_map
#define uset unordered_set
 
#define rep(i, a, b) for(ll i=(a); i<b; i++)
#define pr1(x) cerr << #x << '=' << x << ' ';
//for google contests
#define all(v) v.begin(), v.end()
#define repd(i, a, b) for(ll i=(b-1); i >= a; i--)
void _pr(signed x) {cerr << x;}
void _pr(long long x) {cerr << x;}
void _pr(unsigned long long x) {cerr << x;}
void _pr(double x) {cerr << x;}
void _pr(char x) {cerr << '\'' << x << '\'';}
void _pr(const char* x) {cerr << x;}
void _pr(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V> void _pr(const pair<T, V> &x);
template<typename T, typename V> void _pr(const pair<T, V> &x) {cerr << "\e[95m" << "[ "; _pr(x.first); cerr << ", "; _pr(x.second); cerr << "\e[95m" << ']';}
template<typename T> void _pr(const T &x) {int F=0; cerr << '{'; for(auto &i: x) cerr << (F++ ? ", " : ""), _pr(i); cerr << "\e[91m" << '}';}
template <typename T, typename... V> void _pr(T t, V... v) {_pr(t); if(sizeof...(v)) cerr << ", "; _pr(v...);}
#define pr(x...) cerr << "\e[91m" << __func__ << ':' << __LINE__ << " [" << #x << "] = ["; _pr(x); cerr << "\e[91m" << ']' << "\033[0m"  << endl;
 
//go for outline with ;, then details
ll const inf = LLONG_MAX, mxn = 2e5+4;
int r, c, ans, val[mxn], siz[mxn], conEdge[mxn], add[mxn], solesiz[mxn];
set<int> dp[mxn];
vo<vi> tbl;
map<int, set<int>, greater<int>> order; //all comps of val=key;
 
int compress(int a, int b){
    return a*c + b;
}
 
struct dsu{
    vi par, siz, val, conEdge, compind;
    vo<set<int>> nei;
    dsu(int n): par(n), siz(n, 1), val(n), nei(n), conEdge(n, 0), compind(n){
        rep(i, 0, n) par[i] = i;
    }
    int findpar(int v){
        if(v==par[v]) return v;
        return par[v] = findpar(par[v]);
    }
    void unite(int a, int b){
        a = findpar(a), b = findpar(b);
        if(a!=b){
            if(siz[b] > siz[a]) swap(a, b);
            par[b] = a;
            siz[a] += siz[b]; siz[b] = 0;
            conEdge[a] |= conEdge[b];
            compind[a] = max(compind[a], compind[b]); //eeeh?
        }
    }
};
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
bool ir(int a, int b){
    return a>=0&&b>=0&&a<r&&b<c;
}
 
signed main(){
    cin.tie(0)->sync_with_stdio(0);
    cin>>r>>c; 
    tbl.assign(r, vi(c, 0));
    dsu g(r*c);
    rep(i, 0, r){
        rep(u, 0, c){
            cin>>tbl[i][u];
            if(i==0 || u==0 || i==r-1 || u==c-1) g.conEdge[compress(i, u)] = 1;
        }
    }
 
    rep(i, 0, r){
        rep(u, 0, c){
            for(auto [nr, nc]: dir){
                nr+=i, nc+=u;
                if(ir(nr, nc) && tbl[i][u] == tbl[nr][nc]){
                    g.unite(compress(nr, nc), compress(i, u));
                }
            }
        }
    }
    rep(i, 0, r){
        rep(u, 0, c){
            int b = compress(i, u); b=g.findpar(b); g.val[b] = tbl[i][u];
            order[tbl[i][u]].insert(b);
            for(auto [nr, nc]: dir){
                nr+=i, nc+=u;
                if(ir(nr, nc)){
                    int a = compress(nr, nc);
                    a = g.findpar(a);
                    if(tbl[i][u] >= tbl[nr][nc]){ //större grannar
                        g.nei[a].insert(b); 
 
                    }
                    if(tbl[nr][nc] >= tbl[i][u]){
                        g.nei[b].insert(a);
                    }
                }
            }
        }
    }

    //find all indirect and direct smaller comps neighbours
    int compind = 0;
    for(auto size: order){
        set<array<int, 3>> prevnei;
        //find compind to assign new dp
        for(auto x: size.se){ 
            prevnei.insert({x, -1, 0});
            for(auto neigh: g.nei[x]){
                neigh = g.findpar(neigh);
                prevnei.insert({neigh, g.compind[neigh], g.siz[neigh]});
            }
        }
        
        for(auto x: size.se){ //neigh is bigger
            for(auto neigh: g.nei[x]){
                g.unite(neigh, x);
            }
        }
        //need to add if x has no neighbours
        
        map<int, int> newcompind;
        for(auto [neigh, prevcompind, prevsiz]: prevnei){
            int nw = g.findpar(neigh);
            if(newcompind.count(nw)){
                if(prevcompind != -1) 
                    dp[prevcompind].insert(newcompind[nw]);
            }
            else{
                if(prevcompind != -1)
                    dp[prevcompind].insert(compind);
                newcompind[nw] = compind;
                g.compind[nw] = compind;
                val[compind] = size.fi;
                siz[compind] = g.siz[nw];
                conEdge[compind] = g.conEdge[nw];

                compind++;
            }
        }
    }

    //just go through comps increasing order
    // add saves each comps tmp val
    repd(ind, 0, compind){
        for(auto childind: dp[ind]){
            solesiz[childind] = max(solesiz[childind], siz[childind]-siz[ind]);
        }
    }
    repd(ind, 0, compind){
        // pr(ind, dp[ind])
        for(auto childind: dp[ind]){ //add is for best child
            add[ind] = max(add[ind], min(val[ind], add[childind] + solesiz[ind]));
            if(conEdge[ind]) ans = max(ans, min(val[ind], add[childind] + siz[ind]));
        }
        // pr(dp[ind], add[ind], siz[ind], val[ind])
    }
 
    cout << ans;
}
/*
4 4
9 11 5 7
7 9 14 3
1 3 2 3
11 4 14 8

om varja komponent innehåller rutor av samma nivå. 
aktivera en komponent och kolla efter komponenter av mindre nivå (som ska vara mergade) som
kan nås antingen direkt eller indirekt via rutor av högre nivå.

de mindre nivå komponterna har ett eget bästa svar : min(val[comp], compsize after merged with bigger/equal)
de mindre nivå komponenterna har ett tmp värde: min(val[comp], prevtmp + compsize) 

vi kan använda tmp värdet

för hitta dessa grannar: precomputa genom att merga större med mindre och skapa kanter mellan nya komponent och gamla komponents
information, information är allt som behövs. 

gör samma sak i början men spara barn i parcomps och ej rev
vid merge, prevcompind.insert(newcompind)
*/

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:

input
3 5
1 11 1 3 5
4 12 7 8 7
13 14 14 9 4

correct output
14

user output
13

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:

input
10 20000
1 2 1 2 1 2 1 2 1 1 1 1 2 1 1 ...

correct output
3

user output
4

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:

input
10 20000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
3

user output
2

Test 15

Group: 2, 6

Verdict:

input
10 20000
3 1 3 1 2 1 2 3 2 2 1 2 1 1 2 ...

correct output
3

user output
4

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:

input
10 20000
3 3 2 3 2 3 2 2 2 2 2 1 3 2 1 ...

correct output
4

user output
5

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
43

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:

input
1 100
94 94 94 95 95 91 97 100 92 98...

correct output
95

user output
62

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:

input
1 100
57 1 80 39 18 63 30 86 85 55 8...

correct output
29

user output
47

Test 28

Group: 4, 6

Verdict:

input
1 200000
138736 14949 12344 104147 1333...

correct output
1806

user output
3607

Test 29

Group: 4, 6

Verdict:

input
1 200000
141245 141090 141163 141286 14...

correct output
155109

user output
155314

Test 30

Group: 4, 6

Verdict:

input
1 200000
102358 102469 102440 102402 10...

correct output
152388

user output
152576

Test 31

Group: 4, 6

Verdict:

input
1 200000
180410 160820 160820 180614 18...

correct output
160832

user output
100060

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
4203

Test 35

Group: 5, 6

Verdict:

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

correct output
565

user output
643

Test 36

Group: 5, 6

Verdict:

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

correct output
529

user output
602

Test 37

Group: 5, 6

Verdict:

input
32 31
633 613 618 605 635 638 668 67...

correct output
678

user output
682

Test 38

Group: 5, 6

Verdict:

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
939

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:

input
20 50
481 532 624 290 965 58 448 872...

correct output
504

user output
541

Test 42

Group: 6

Verdict:

input
447 447
6474 27185 108482 124481 16058...

correct output
88202

user output
99107

Test 43

Group: 6

Verdict:

input
1000 200
27722 57131 197677 184858 9285...

correct output
89324

user output
99883

Test 44

Group: 6

Verdict:

input
447 447
70928 73154 72640 74764 75237 ...

correct output
181096

user output
181116

Test 45

Group: 6

Verdict:

input
7 28571
193031 185883 171670 185794 17...

correct output
171680

user output
171706

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
95904