Submission #954251

# Submission time Handle Problem Language Result Execution time Memory
954251 2024-03-27T14:05:40 Z GrindMachine Saveit (IOI10_saveit) C++17
100 / 100
169 ms 16676 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*

refs:
edi

in graph/tree encoding problems, think about:
spanning tree, virtual tree, shortest path tree, traversal orders (could be a combination of multiple things)

in this problem, thinking about spanning trees was the key
pick a random spanning tree of the graph
send the spanning tree using n*10 bits = 10000 bits

fix hub h
for 2 adj nodes (u,v) on the spanning tree,
|d(u,h)-d(v,h)| <= 1
encode the value that needs to be added when going from u to v in the spanning tree (only 3 possibilities)

naive encoding => 2 bits for each node
cost = n*10+h*n*2 = 10000+72000 = 82000

need to optimize
spending 2 bits to encode 3 possibilities is bad (1 more possibility could be encoded, so we are wasting a bit)
instead, spend log2(3^3)+1 = 5 bits to encode 3 guys
or better, log2(3^5)+1 = 8 bits to encode 5 guys

gives a ratio of 1.6 bits per guy
cost = n*10+h*n*1.6 = 10000+57600 = 67600
sufficient to pass all subtasks

note:
the costs are not exact, there could be slight differences when the code actually runs (like (n-1)*10 to send spanning tree instead of n*10)

*/

const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

#include "grader.h"
#include "encoder.h"

void encode(int n, int h, int m, int *U, int *V){
    vector<int> adj[n];
    rep(i,m){
        int u = U[i], v = V[i];
        adj[u].pb(v), adj[v].pb(u);
    }

    vector<int> par(n);
    vector<bool> vis(n);
    int dis[h][n];

    rep(i,h){
        queue<int> q;
        fill(all(vis),0);
        q.push(i);
        vis[i] = 1;
        dis[i][i] = 0;

        while(!q.empty()){
            int u = q.front();
            q.pop();

            trav(v,adj[u]){
                if(vis[v]) conts;
                if(i == 0){
                    par[v] = u;
                }
                q.push(v);
                vis[v] = 1;
                dis[i][v] = dis[i][u]+1;
            }
        }
    }

    rep1(u,n-1){
        int p = par[u];
        rep(bit,10){
            int f = 1<<(9-bit);
            int b = 0;
            if(p&f) b = 1;
            encode_bit(b);
        }
    }

    rep(i,h){
        vector<int> vals;
        vals.pb(0);
        rep1(u,n-1){
            int p = par[u];
            int d1 = dis[i][p], d2 = dis[i][u];
            int add = d2-d1;
            add++;
            vals.pb(add);
        }

        while(sz(vals)%5){
            vals.pb(0);
        }

        // encode 5 guys in 8 bits
        rep(block,sz(vals)/5){
            int mask = 0;
            for(int j = block*5; j < (block+1)*5; ++j){
                mask = mask*3+vals[j];
            }

            rep(bit,8){
                int f = 1<<(7-bit);
                int b = 0;
                if(mask&f) b = 1;
                encode_bit(b);
            }
        }
    }
}
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*

refs:
edi

in graph/tree encoding problems, think about:
spanning tree, virtual tree, shortest path tree, traversal orders (could be a combination of multiple things)

in this problem, thinking about spanning trees was the key
pick a random spanning tree of the graph
send the spanning tree using n*10 bits = 10000 bits

fix hub h
for 2 adj nodes (u,v) on the spanning tree,
|d(u,h)-d(v,h)| <= 1
encode the value that needs to be added when going from u to v in the spanning tree (only 3 possibilities)

naive encoding => 2 bits for each node
cost = n*10+h*n*2 = 10000+72000 = 82000

need to optimize
spending 2 bits to encode 3 possibilities is bad (1 more possibility could be encoded, so we are wasting a bit)
instead, spend log2(3^3)+1 = 5 bits to encode 3 guys
or better, log2(3^5)+1 = 8 bits to encode 5 guys

gives a ratio of 1.6 bits per guy
cost = n*10+h*n*1.6 = 10000+57600 = 67600
sufficient to pass all subtasks

note:
the costs are not exact, there could be slight differences when the code actually runs (like (n-1)*10 to send spanning tree instead of n*10)

*/

const int MOD = 1e9 + 7;
const int N = 1e3 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

#include "grader.h"
#include "decoder.h"

vector<pii> adj[N];
vector<int> dis(N);

void dfs(int u, int p){
    for(auto [v,w] : adj[u]){
        if(v == p) conts;
        dis[v] = dis[u]+w;
        dfs(v,u);
    }
}

void decode(int n, int h) {
    auto f = [&](int i){
        int mask = 0;
        rep1(iter,i){
            int b = decode_bit();
            mask = (mask<<1)|b;         
        }
        return mask;
    };

    vector<int> par(n);
    rep1(u,n-1){
        par[u] = f(10);
    }

    rep(i,h){
        int siz = ceil2(n,5)*5;
        vector<int> state(n+15);

        rep(block,siz/5){
            int mask = f(8);
            vector<int> curr;
            rep(bit,5){
                curr.pb(mask%3);
                mask /= 3;
            }

            reverse(all(curr));
            int ptr = 0;

            for(int u = block*5; u < (block+1)*5; ++u){
                state[u] = curr[ptr++];
            }
        }

        rep(u,n){
            adj[u].clear();
        }

        rep1(u,n-1){
            int p = par[u];
            int w = 0;
            if(state[u] == 0) w = -1;
            if(state[u] == 1) w = 0;
            if(state[u] == 2) w = 1;
            adj[p].pb({u,w}), adj[u].pb({p,-w});
        }

        dis[i] = 0;
        dfs(i,-1);

        rep(u,n){
            hops(i,u,dis[u]);
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 169 ms 16676 KB Output is correct - 67590 call(s) of encode_bit()
2 Correct 2 ms 11264 KB Output is correct - 64 call(s) of encode_bit()
3 Correct 19 ms 11516 KB Output is correct - 60830 call(s) of encode_bit()
4 Correct 2 ms 11244 KB Output is correct - 80 call(s) of encode_bit()
5 Correct 19 ms 12292 KB Output is correct - 60830 call(s) of encode_bit()
6 Correct 20 ms 11520 KB Output is correct - 67590 call(s) of encode_bit()
7 Correct 32 ms 12016 KB Output is correct - 67590 call(s) of encode_bit()
8 Correct 18 ms 11720 KB Output is correct - 65184 call(s) of encode_bit()
9 Correct 19 ms 11712 KB Output is correct - 67590 call(s) of encode_bit()
10 Correct 19 ms 11752 KB Output is correct - 67590 call(s) of encode_bit()
11 Correct 20 ms 11864 KB Output is correct - 67590 call(s) of encode_bit()
12 Correct 19 ms 11756 KB Output is correct - 67590 call(s) of encode_bit()
13 Correct 43 ms 12240 KB Output is correct - 67590 call(s) of encode_bit()
14 Correct 19 ms 11388 KB Output is correct - 67590 call(s) of encode_bit()
15 Correct 19 ms 11720 KB Output is correct - 67590 call(s) of encode_bit()
16 Correct 34 ms 12068 KB Output is correct - 67590 call(s) of encode_bit()
17 Correct 39 ms 12040 KB Output is correct - 67590 call(s) of encode_bit()
18 Correct 52 ms 12456 KB Output is correct - 67590 call(s) of encode_bit()
19 Correct 26 ms 12032 KB Output is correct - 67590 call(s) of encode_bit()
20 Correct 47 ms 14460 KB Output is correct - 67590 call(s) of encode_bit()
21 Correct 59 ms 14340 KB Output is correct - 67590 call(s) of encode_bit()
22 Correct 34 ms 12744 KB Output is correct - 67590 call(s) of encode_bit()
23 Correct 54 ms 14776 KB Output is correct - 67590 call(s) of encode_bit()
# Verdict Execution time Memory Grader output
1 Correct 169 ms 16676 KB Output is correct - 67590 call(s) of encode_bit()
2 Correct 2 ms 11264 KB Output is correct - 64 call(s) of encode_bit()
3 Correct 19 ms 11516 KB Output is correct - 60830 call(s) of encode_bit()
4 Correct 2 ms 11244 KB Output is correct - 80 call(s) of encode_bit()
5 Correct 19 ms 12292 KB Output is correct - 60830 call(s) of encode_bit()
6 Correct 20 ms 11520 KB Output is correct - 67590 call(s) of encode_bit()
7 Correct 32 ms 12016 KB Output is correct - 67590 call(s) of encode_bit()
8 Correct 18 ms 11720 KB Output is correct - 65184 call(s) of encode_bit()
9 Correct 19 ms 11712 KB Output is correct - 67590 call(s) of encode_bit()
10 Correct 19 ms 11752 KB Output is correct - 67590 call(s) of encode_bit()
11 Correct 20 ms 11864 KB Output is correct - 67590 call(s) of encode_bit()
12 Correct 19 ms 11756 KB Output is correct - 67590 call(s) of encode_bit()
13 Correct 43 ms 12240 KB Output is correct - 67590 call(s) of encode_bit()
14 Correct 19 ms 11388 KB Output is correct - 67590 call(s) of encode_bit()
15 Correct 19 ms 11720 KB Output is correct - 67590 call(s) of encode_bit()
16 Correct 34 ms 12068 KB Output is correct - 67590 call(s) of encode_bit()
17 Correct 39 ms 12040 KB Output is correct - 67590 call(s) of encode_bit()
18 Correct 52 ms 12456 KB Output is correct - 67590 call(s) of encode_bit()
19 Correct 26 ms 12032 KB Output is correct - 67590 call(s) of encode_bit()
20 Correct 47 ms 14460 KB Output is correct - 67590 call(s) of encode_bit()
21 Correct 59 ms 14340 KB Output is correct - 67590 call(s) of encode_bit()
22 Correct 34 ms 12744 KB Output is correct - 67590 call(s) of encode_bit()
23 Correct 54 ms 14776 KB Output is correct - 67590 call(s) of encode_bit()
# Verdict Execution time Memory Grader output
1 Correct 169 ms 16676 KB Output is correct - 67590 call(s) of encode_bit()
2 Correct 2 ms 11264 KB Output is correct - 64 call(s) of encode_bit()
3 Correct 19 ms 11516 KB Output is correct - 60830 call(s) of encode_bit()
4 Correct 2 ms 11244 KB Output is correct - 80 call(s) of encode_bit()
5 Correct 19 ms 12292 KB Output is correct - 60830 call(s) of encode_bit()
6 Correct 20 ms 11520 KB Output is correct - 67590 call(s) of encode_bit()
7 Correct 32 ms 12016 KB Output is correct - 67590 call(s) of encode_bit()
8 Correct 18 ms 11720 KB Output is correct - 65184 call(s) of encode_bit()
9 Correct 19 ms 11712 KB Output is correct - 67590 call(s) of encode_bit()
10 Correct 19 ms 11752 KB Output is correct - 67590 call(s) of encode_bit()
11 Correct 20 ms 11864 KB Output is correct - 67590 call(s) of encode_bit()
12 Correct 19 ms 11756 KB Output is correct - 67590 call(s) of encode_bit()
13 Correct 43 ms 12240 KB Output is correct - 67590 call(s) of encode_bit()
14 Correct 19 ms 11388 KB Output is correct - 67590 call(s) of encode_bit()
15 Correct 19 ms 11720 KB Output is correct - 67590 call(s) of encode_bit()
16 Correct 34 ms 12068 KB Output is correct - 67590 call(s) of encode_bit()
17 Correct 39 ms 12040 KB Output is correct - 67590 call(s) of encode_bit()
18 Correct 52 ms 12456 KB Output is correct - 67590 call(s) of encode_bit()
19 Correct 26 ms 12032 KB Output is correct - 67590 call(s) of encode_bit()
20 Correct 47 ms 14460 KB Output is correct - 67590 call(s) of encode_bit()
21 Correct 59 ms 14340 KB Output is correct - 67590 call(s) of encode_bit()
22 Correct 34 ms 12744 KB Output is correct - 67590 call(s) of encode_bit()
23 Correct 54 ms 14776 KB Output is correct - 67590 call(s) of encode_bit()
# Verdict Execution time Memory Grader output
1 Correct 169 ms 16676 KB Output is correct - 67590 call(s) of encode_bit()
2 Correct 2 ms 11264 KB Output is correct - 64 call(s) of encode_bit()
3 Correct 19 ms 11516 KB Output is correct - 60830 call(s) of encode_bit()
4 Correct 2 ms 11244 KB Output is correct - 80 call(s) of encode_bit()
5 Correct 19 ms 12292 KB Output is correct - 60830 call(s) of encode_bit()
6 Correct 20 ms 11520 KB Output is correct - 67590 call(s) of encode_bit()
7 Correct 32 ms 12016 KB Output is correct - 67590 call(s) of encode_bit()
8 Correct 18 ms 11720 KB Output is correct - 65184 call(s) of encode_bit()
9 Correct 19 ms 11712 KB Output is correct - 67590 call(s) of encode_bit()
10 Correct 19 ms 11752 KB Output is correct - 67590 call(s) of encode_bit()
11 Correct 20 ms 11864 KB Output is correct - 67590 call(s) of encode_bit()
12 Correct 19 ms 11756 KB Output is correct - 67590 call(s) of encode_bit()
13 Correct 43 ms 12240 KB Output is correct - 67590 call(s) of encode_bit()
14 Correct 19 ms 11388 KB Output is correct - 67590 call(s) of encode_bit()
15 Correct 19 ms 11720 KB Output is correct - 67590 call(s) of encode_bit()
16 Correct 34 ms 12068 KB Output is correct - 67590 call(s) of encode_bit()
17 Correct 39 ms 12040 KB Output is correct - 67590 call(s) of encode_bit()
18 Correct 52 ms 12456 KB Output is correct - 67590 call(s) of encode_bit()
19 Correct 26 ms 12032 KB Output is correct - 67590 call(s) of encode_bit()
20 Correct 47 ms 14460 KB Output is correct - 67590 call(s) of encode_bit()
21 Correct 59 ms 14340 KB Output is correct - 67590 call(s) of encode_bit()
22 Correct 34 ms 12744 KB Output is correct - 67590 call(s) of encode_bit()
23 Correct 54 ms 14776 KB Output is correct - 67590 call(s) of encode_bit()