답안 #888847

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
888847 2023-12-18T09:24:26 Z asdasdqwer Sirni (COCI17_sirni) C++14
98 / 140
5000 ms 439876 KB
#pragma GCC optimze("O3")
#pragma GCC target("avx,avx2,sse4")

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

#define int int64_t

struct DSU {
    vector<int32_t> p, r;
    DSU(int32_t n) {
        p.assign(n,0);
        r.assign(n,0);
        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;
        if (r[a]==r[b])r[a]++;
        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,3> pii;
    vector<pii> edg;
    for (int32_t x:b) {
        // cout<<x<<"\n";
        vector<pii> tmp;
        if (x != b.back()) {
            tmp.push_back({(clos[x+1] % x), 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) {
                    tmp.push_back({clos[j] % x, clos[j], x});
                }

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

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

        for (auto y:tmp) edg.push_back(y);
    }

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

    sort(edg.begin(),edg.end());

    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";
        x[1]=mp[x[1]];
        x[2]=mp[x[2]];
    }
    // cout<<"\n";

    DSU d(b.size());
    int cost=0;
    for (auto x:edg) {
        if (d.unite(x[1], x[2])) {
            // cout<<b[x[1]]<<" "<<b[x[2]]<<"\n";
            cost+=x[0];
        }
    }
    return cost;
}

bool brute() {
    int len=(rand() % 100000)+100000;
    vector<int> b;
    set<int> chose;
    for (int i=0;i<len;i++) {
        int j=(rand()%100000)+1;
        while (chose.find(j) != chose.end()) {
            j=(rand()%100000)+1;
        }

        chose.insert(j);
        b.push_back(j);
    }

    cout<<"gen numbers\n";

    sort(b.begin(),b.end());
    typedef array<int,3> pii;
    vector<pii> g;
    for (int i=0;i<len;i++) {
        for (int j=i+1;j<len;j++) {
            g.push_back({b[j] % b[i], j, i});
        }
    }

    sort(g.begin(),g.end());

    DSU d(len);
    int cost=0;
    for (auto x:g) {
        if (d.unite(x[1], x[2])) {
            // cout<<b[x[1]]<<" "<<b[x[2]]<<"\n";
            cost += x[0];
        }
    }

    cout<<"mst calc\n";

    // cout<<"\n";
    int cost2 = test(len, b);
    cout<<"mst fast\n";
    // cout<<"\n";
    if (cost != cost2) {
        cout<<cost<<" "<<cost2<<"\n";
    }
    // for (int x:b) {
    //     cout<<x<<" ";
    // }
    // cout<<"\n";
    cout<<"finished\n";
    cout.flush();
    return true;
}

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:1: warning: ignoring '#pragma GCC optimze' [-Wunknown-pragmas]
    1 | #pragma GCC optimze("O3")
      | 
In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from sirni.cpp:4:
sirni.cpp: In member function 'bool DSU::unite(int32_t, int32_t)':
sirni.cpp:23:18: warning: comparison of integer expressions of different signedness: 'int32_t' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |         assert(a < p.size() && b < p.size());
      |                ~~^~~~~~~~~~
sirni.cpp:23:34: warning: comparison of integer expressions of different signedness: 'int32_t' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |         assert(a < p.size() && b < p.size());
      |                                ~~^~~~~~~~~~
sirni.cpp: In function 'int test(int64_t, std::vector<long int>)':
sirni.cpp:84:19: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |     for (int i=0;i<b.size();i++) {
      |                  ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 39516 KB Output is correct
2 Correct 95 ms 43040 KB Output is correct
3 Correct 30 ms 39740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 604 KB Output is correct
2 Correct 278 ms 40332 KB Output is correct
3 Correct 30 ms 39772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 39768 KB Output is correct
2 Correct 27 ms 39552 KB Output is correct
3 Correct 30 ms 39880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 400 ms 33620 KB Output is correct
2 Correct 1313 ms 59544 KB Output is correct
3 Correct 509 ms 33580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 9036 KB Output is correct
2 Correct 729 ms 55796 KB Output is correct
3 Correct 413 ms 30296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 854 ms 58392 KB Output is correct
2 Correct 1762 ms 106824 KB Output is correct
3 Correct 489 ms 34212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 126 ms 10096 KB Output is correct
2 Correct 1787 ms 107276 KB Output is correct
3 Correct 473 ms 32636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 559 ms 68076 KB Output is correct
2 Execution timed out 5041 ms 439876 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 564 ms 68592 KB Output is correct
2 Execution timed out 5009 ms 438776 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 111 ms 44500 KB Output is correct
2 Execution timed out 5044 ms 436892 KB Time limit exceeded
3 Halted 0 ms 0 KB -