Submission #888856

# Submission time Handle Problem Language Result Execution time Memory
888856 2023-12-18T09:39:36 Z asdasdqwer Sirni (COCI17_sirni) C++14
70 / 140
5000 ms 577220 KB
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,sse4")

#include <bits/stdc++.h>
using namespace std;

struct DSU {
    vector<int32_t> p, r;
    DSU(int32_t n) {
        p.assign(n,0);
        r.assign(n,1);
        for (int i=0;i<n;i++)p[i]=i;
    }

    int get(int32_t i) {
        if (i != p[i]) p[i]=get(p[i]);
        return p[i];
    }

    bool unite(int32_t a,int32_t b) {
        // assert(a < p.size() && b < p.size());
        a=get(a);
        b=get(b);
        if (a==b)return false;
        if (r[a]<r[b])swap(a,b);
        p[b]=a;
        r[a]+=r[b];
        return true;
    }
};

signed test(int n, vector<int> b) {
    vector<int32_t> clos(b.back()+1,-1);
    for (int32_t x:b) {
        clos[x]=x;
    }

    for (int32_t i=clos.size()-2;i>=0;i--) {
        if (clos[i] == -1) {
            clos[i]=clos[i+1];
        }
    }

    // cout<<"here\n";

    typedef array<int32_t,2> pii;
    vector<vector<pii>> edg(b.back()+1);
    for (int32_t x:b) {
        // cout<<x<<"\n";
        vector<int> tmp;
        if (x != b.back()) {
            edg[clos[x+1]%x].push_back({x, clos[x+1]});
        }

        // cout<<"after\n";

        for (int32_t j=2*x;j<=b.back();j+=x) {
            // cout<<j<<" "<<clos[j]<<"\n";
            if (clos[j] != -1) {
                if (tmp.size() == 0) {
                    edg[clos[j]%x].push_back({clos[j], x});
                    tmp.push_back(clos[j]);
                }

                else if (tmp.back() != clos[j]) {
                    edg[clos[j]%x].push_back({clos[j], x});
                    tmp.push_back(clos[j]);
                }
            }
        }

        if (tmp.size() == 0 || tmp.back() != b.back()) {
            edg[b.back()%x].push_back({b.back(), x});
        }
    }

    // cout<<edg.size()<<"\n";

    map<int,int> mp;
    for (int i=0;i<b.size();i++) {
        mp[b[i]]=i;
    }

    for (auto &x:edg) {
        // cout<<x[1]<<" "<<x[2]<<"\n";
        for (auto &y:x) {
            y[0]=mp[y[0]];
            y[1]=mp[y[1]];
        }
    }
    // cout<<"\n";

    DSU d(b.size());
    int cost=0;
    int steps=0;
    bool term=false;
    for (int c=0;c<=b.size();c++) {
        for (auto &x:edg[c]) {
            if (d.unite(x[0], x[1])) {
                cost+=c;
                steps++;

                if (steps == n-1) {
                    term=true;
                    break;
                }
            }
        }

        if (term)break;
    }
    return cost;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;cin>>n;
    vector<int> a(n);
    for (int &x:a)cin>>x;
    if (n == 1) {
        cout<<0<<"\n";
        return 0;
    }
    sort(a.begin(),a.end());
    vector<int> b;
    for (int x:a) {
        if (b.size()==0||b.back()!=x)b.push_back(x);
    }

    cout<<test(n,b)<<"\n";
}

Compilation message

sirni.cpp: In function 'int test(int, std::vector<int>)':
sirni.cpp:80:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |     for (int i=0;i<b.size();i++) {
      |                  ~^~~~~~~~~
sirni.cpp:97:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |     for (int c=0;c<=b.size();c++) {
      |                  ~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 102 ms 274296 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 860 KB Output is correct
2 Correct 339 ms 274600 KB Output is correct
3 Correct 102 ms 274516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 102 ms 274468 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 312 ms 45268 KB Output is correct
2 Correct 859 ms 71440 KB Output is correct
3 Correct 353 ms 48164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 30652 KB Output is correct
2 Correct 374 ms 50584 KB Output is correct
3 Correct 267 ms 30312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 616 ms 56256 KB Output is correct
2 Correct 1082 ms 87636 KB Output is correct
3 Correct 348 ms 48420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 12108 KB Output is correct
2 Correct 955 ms 79016 KB Output is correct
3 Correct 343 ms 47120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 513 ms 295356 KB Output is correct
2 Execution timed out 5048 ms 501404 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 512 ms 295512 KB Output is correct
2 Execution timed out 5066 ms 577220 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 156 ms 278376 KB Output is correct
2 Execution timed out 5115 ms 571840 KB Time limit exceeded
3 Halted 0 ms 0 KB -