답안 #484635

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
484635 2021-11-04T23:10:03 Z SansPapyrus683 Sirni (COCI17_sirni) C++17
140 / 140
3847 ms 747000 KB
#include <iostream>
#include <cassert>
#include <vector>
#include <algorithm>

using std::cout;
using std::endl;
using std::vector;
using std::pair;

class DisjointSets {
    private:
        vector<int> parents;
        vector<int> sizes;
    public:
        DisjointSets(int size) : parents(size), sizes(size, 1) {
            for (int i = 0; i < size; i++) {
                parents[i] = i;
            }
        }

        int get_ultimate(int n) {
            return parents[n] == n ? n : (parents[n] = get_ultimate(parents[n]));
        }

        bool link(int n1, int n2) {
            n1 = get_ultimate(n1);
            n2 = get_ultimate(n2);
            if (n1 == n2) {
                return false;
            }
            if (sizes[n1] < sizes[n2]) {
                std::swap(n1, n2);
            }
            sizes[n1] += sizes[n2];
            parents[n2] = n1;
            return true;
        }
};

/**
 * https://oj.uz/problem/view/COCI17_sirni
 * 4
 * 2
 * 6
 * 3
 * 11 should output 1
 */
int main() {
    int card_num;
    std::cin >> card_num;
    vector<int> cards(card_num);
    for (int& c : cards) {
        std::cin >> c;
        assert(c >= 1);
    }
    std::sort(cards.begin(), cards.end());
    // we can erase the dupes bc modding them with the original one = 0
    cards.erase(std::unique(cards.begin(), cards.end()), cards.end());

    int largest = *std::max_element(cards.begin(), cards.end());
    vector<int> next_largest(largest + 1, -1);
    for (int i = 0; i < cards.size(); i++) {
        next_largest[cards[i]] = i;
    }
    for (int c = largest - 1; c >= 0; c--) {
        // if this isn't assigned yet, assign it the previous one
        if (next_largest[c] == -1) {
            next_largest[c] = next_largest[c + 1];
        }
    }

    vector<vector<pair<int, int>>> good_links(largest + 1);
    for (int i = 0; i < cards.size() - 1; i++) {
        good_links[cards[i + 1] % cards[i]].push_back({i, i + 1});
        for (int at = 2 * cards[i]; at <= largest; at += cards[i]) {
            int good_mod = next_largest[at];
            good_links[cards[good_mod] % cards[i]].push_back({i, good_mod});
        }
    }

    // cout << "alr" << endl;
    long long total_cost = 0;
    // int size = 0;
    DisjointSets linked_cards(cards.size());
    for (int c = 0; c <= largest; c++) {
        for (const pair<int, int>& link : good_links[c]) {
            bool result = linked_cards.link(link.first, link.second);
            total_cost += c * result;
        }
    }
    cout << total_cost << endl;
}

Compilation message

sirni.cpp: In function 'int main()':
sirni.cpp:63:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |     for (int i = 0; i < cards.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
sirni.cpp:74:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |     for (int i = 0; i < cards.size() - 1; i++) {
      |                     ~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 300 ms 274104 KB Output is correct
2 Correct 329 ms 302460 KB Output is correct
3 Correct 174 ms 273476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 588 KB Output is correct
2 Correct 1744 ms 668456 KB Output is correct
3 Correct 181 ms 274928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 190 ms 274228 KB Output is correct
2 Correct 172 ms 273740 KB Output is correct
3 Correct 195 ms 274388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 93 ms 38908 KB Output is correct
2 Correct 196 ms 66816 KB Output is correct
3 Correct 132 ms 49572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 29772 KB Output is correct
2 Correct 156 ms 51312 KB Output is correct
3 Correct 76 ms 25228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 159 ms 50404 KB Output is correct
2 Correct 263 ms 84124 KB Output is correct
3 Correct 118 ms 47332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 9688 KB Output is correct
2 Correct 271 ms 84740 KB Output is correct
3 Correct 141 ms 49084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 332 ms 289108 KB Output is correct
2 Correct 1939 ms 633236 KB Output is correct
3 Correct 375 ms 291688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 347 ms 293168 KB Output is correct
2 Correct 3810 ms 747000 KB Output is correct
3 Correct 552 ms 348356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 204 ms 276804 KB Output is correct
2 Correct 3847 ms 635088 KB Output is correct
3 Correct 135 ms 51116 KB Output is correct