답안 #906805

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
906805 2024-01-15T02:51:45 Z mathnetic Roller Coaster Railroad (IOI16_railroad) C++17
컴파일 오류
0 ms 0 KB
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;

ll plan_roller_coaster(const vector<int>& s, const vector<int>& t) {
    unordered_map<int, int> uf;

    auto fnd = [&](int x) {
        if (uf[x] == 0) uf[x] = x;
        return (x == uf[x]) ? x : (uf[x] = fnd(uf[x]));
    };

    auto un = [&](int x, int y) {
        uf[fnd(x)] = fnd(y);
    };

    vector<pair<int, int>> e, ed;
    int n = s.size();

    for (int i = 0; i < n; ++i) {
        un(s[i], t[i]);
        e.emplace_back(s[i], 1);
        e.emplace_back(t[i], -1);
    }

    sort(e.begin(), e.end());
    int b = 0;
    ll sol = 0;

    for (int i = 0, j = 0; i < e.size(); i = j) {
        for (; j < e.size() && e[j].first == e[i].first; ++j) {
            b += e[j].second;
        }

        if (j < e.size()) {
            if (b > 0) {
                sol += static_cast<ll>(b) * (e[j].first - e[i].first);
            }

            if (b == 0) {
                ed.emplace_back(e[i].first, e[j].first);
            } else {
                un(e[j].first, e[i].first);
            }
        }
    }

    sort(ed.begin(), ed.end(), [&](const auto& a, const auto& b) {
        return (a.second - a.first) < (b.second - b.first);
    });

    for (auto& i : ed) {
        if (fnd(i.first) != fnd(i.second)) {
            sol += i.second - i.first;
            un(i.first, i.second);
        }
    }

    return sol;
}

Compilation message

railroad.cpp: In lambda function:
railroad.cpp:13:44: error: use of 'fnd' before deduction of 'auto'
   13 |         return (x == uf[x]) ? x : (uf[x] = fnd(uf[x]));
      |                                            ^~~
railroad.cpp: In function 'll plan_roller_coaster(const std::vector<int>&, const std::vector<int>&)':
railroad.cpp:33:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |     for (int i = 0, j = 0; i < e.size(); i = j) {
      |                            ~~^~~~~~~~~~
railroad.cpp:34:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |         for (; j < e.size() && e[j].first == e[i].first; ++j) {
      |                ~~^~~~~~~~~~
railroad.cpp:38:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |         if (j < e.size()) {
      |             ~~^~~~~~~~~~