Submission #248337

# Submission time Handle Problem Language Result Execution time Memory
248337 2020-07-12T09:11:32 Z VEGAnn Sirni (COCI17_sirni) C++14
0 / 140
5000 ms 162904 KB
#include <bits/stdc++.h>
#define sz(x) ((int)x.size())
#define all(x) x.begin(),x.end()
#define PB push_back
using namespace std;
typedef long long ll;
const int N = 100100;
const int BIG = int(1e7) + 10;
const int cn = int(1e4);
const int oo = 2e9;
bool del[BIG];
vector<int> vc, elms[N], nwv;
int mrk[BIG], n, best[BIG], id[BIG], nt[BIG];
ll ans = 0;

int get(int x) { return (x == nt[x] ? x : nt[x] = get(nt[x])); }

void calc(){
    for (int i = BIG - 2; i > 0; i--)
        if (mrk[i])
            nt[i] = i;
        else nt[i] = nt[i + 1];

    for (int cr : vc){
        int cm = mrk[cr];

        for (int x : elms[cm])
            nt[get(x)] = get(x + 1);

        for (int x : elms[cm]){
            for (int i = get(x); i < BIG - 1; ){
                int nw = i % x;

                if (best[cr] > nw){
                    best[cr] = nw;
                    id[cr] = i;
                }

                if (best[i] > nw){
                    best[i] = nw;
                    id[i] = cr;
                }

                if (nw == 0)
                    i += x;
                else i += x - nw;

                i = get(i);
            }
        }
    }
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

#ifdef _LOCAL
    freopen("in.txt","r",stdin);
#endif // _LOCAL

    cin >> n;

    for (int i = 1; i <= n; i++){
        int x; cin >> x;
        mrk[x] = i;
    }

    int siz = 0;

    for (int i = 1; i < BIG; i++)
        if (mrk[i]) {
            siz++;
            vc.PB(i);
            elms[mrk[i]].PB(i);
        }

    nt[BIG - 1] = BIG - 1;

    while (siz > 1){
        for (int cr : vc){
            id[cr] = -1;
            best[cr] = oo;
        }

        calc();

        reverse(all(vc));

        calc();

        for (int cr : vc){
            if (id[id[cr]] == cr && best[id[cr]] < 0) continue;

            ans += best[cr];

            siz--;

            int m1 = mrk[cr], m2 = mrk[id[cr]];
            int sz1 = sz(elms[m1]), sz2 = sz(elms[m2]);

            if (sz1 > sz2){
                swap(sz1, sz2);
                swap(m1, m2);
            }

            for (int x : elms[m1]) {
                mrk[x] = m2;
                elms[m2].PB(x);
            }

            del[m2] = 1;

            best[cr] = -1;
        }

        nwv.clear();

        for (int cr : vc)
            if (!del[cr])
                nwv.PB(cr);

        vc = nwv;
    }

    cout << ans << '\n';

    return 0;
}
# Verdict Execution time Memory Grader output
1 Execution timed out 5063 ms 52732 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 223 ms 85620 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5054 ms 53624 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5083 ms 58348 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5050 ms 54332 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5036 ms 58612 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5050 ms 46068 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5035 ms 162780 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5086 ms 162904 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5092 ms 144884 KB Time limit exceeded
2 Halted 0 ms 0 KB -