답안 #948745

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
948745 2024-03-18T12:56:07 Z May27_th Sirni (COCI17_sirni) C++17
84 / 140
5000 ms 786432 KB
#include<bits/stdc++.h>
#define taskname "a"
using namespace std;

void World_Final();
void Solve();

int main(){
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    if (fopen(taskname".in", "r")) {
        freopen(taskname".in", "r", stdin);
        freopen(taskname".out", "w", stdout);
    }
    World_Final();
}

void World_Final(){
    int Tests = 1;
    //cin >> Tests;
    for (int i = 1; i <= Tests; i ++) {
        //cout << i << "\n";
        Solve();
    }
}

struct DSU{
    private:
        vector<int> par, sz;
    public:
        int ccp;
        DSU(int N) : par(N + 5), sz(N + 5) {
            ccp = N;
            for (int i = 1; i <= N; i ++) {
                par[i] = i;
                sz[i] = 1;
            }
        }
        /*void init(int N) {
            par.resize(N + 5); sz.resize(N + 5);
            for (int i = 1; i <= N; i ++) {
                par[i] = i; sz[i] = 1;
            }
        }*/

        int findPar(int u) {
            return (u == par[u] ? u : par[u] = findPar(par[u]));
        }

        //int getSize(int u) { return sz[findPar(u)]; }
        bool same(int u, int v) {
            return findPar(u) == findPar(v);
        }

        bool unite(int u, int v) {
            int paru = findPar(u);
            int parv = findPar(v);
            if (paru == parv) return false;
            if (sz[paru] < sz[parv]) swap(paru, parv);
            sz[paru] += sz[parv];
            par[parv] = paru;

            return true;
        }

};

const int MAXN = 1e5 + 10;
const int64_t INF = 1e18;

struct Edge{
    int u, v, c;
};
void Solve(){
    int N; cin >> N;
    vector<Edge> ve;
    int mx = 1e7 + 10;
    vector<int> P(N);
    map<int, int> pos;
    for (auto &x : P) cin >> x;
    sort (P.begin(), P.end());
    P.erase(unique(P.begin(), P.end()), P.end());
    N = P.size();
    for (int i = 0; i < N; i ++) pos[P[i]] = i;
    for (int i = 0; i < N; i ++) {
        if (i && P[i - 1] * 2 > P[i]) {
            ve.push_back({i, i - 1, min(P[i - 1], P[i] % P[i - 1])});
        }
        for (int j = P[i]; j <= mx - 10; j += P[i]) {
            auto it = lower_bound(P.begin(), P.end(), j);
            if (it != P.end()) {
                int w = min(P[i], (*it) % P[i]);
                ve.push_back({i, pos[*it], w});
            } else {
                break;
            }
        }
    } sort (ve.begin(), ve.end(), [&] (Edge &a, Edge &b){
        return a.c < b.c;
    });
    int64_t ans = 0; DSU d(N);
    for (auto [u, v, c] : ve) {
        if (d.unite(u, v)) {
            ans += c;
        }
    } cout << ans;


}






















Compilation message

sirni.cpp: In function 'int main()':
sirni.cpp:11:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 |         freopen(taskname".in", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
sirni.cpp:12:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 |         freopen(taskname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 174 ms 50600 KB Output is correct
3 Correct 3 ms 856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 600 KB Output is correct
2 Runtime error 1130 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 293 ms 30456 KB Output is correct
2 Correct 984 ms 104476 KB Output is correct
3 Correct 442 ms 55072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 4304 KB Output is correct
2 Correct 405 ms 51640 KB Output is correct
3 Correct 284 ms 30640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 645 ms 55096 KB Output is correct
2 Correct 1325 ms 104860 KB Output is correct
3 Correct 408 ms 30924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 99 ms 10436 KB Output is correct
2 Correct 1228 ms 103936 KB Output is correct
3 Correct 398 ms 55204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 376 ms 31408 KB Output is correct
2 Runtime error 3687 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 420 ms 31328 KB Output is correct
2 Runtime error 3575 ms 786432 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 6076 KB Output is correct
2 Execution timed out 5019 ms 448440 KB Time limit exceeded
3 Halted 0 ms 0 KB -