답안 #1101093

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1101093 2024-10-15T13:24:59 Z Zero_OP Palembang Bridges (APIO15_bridge) C++14
0 / 100
2 ms 592 KB
#include <bits/stdc++.h>

using namespace std;

#define rep(i, l, r) for(int i = (l), _r = (r); i < _r; ++i)
#define FOR(i, l, r) for(int i = (l), _r = (r); i <= _r; ++i)
#define ROF(i, r, l) for(int i = (r), _l = (l); i >= _l; --i)
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define sz(v) (int)v.size()
#define dbg(x) "[" #x " = " << (x) << "]"

template<typename T>
bool minimize(T& a, const T& b){
    if(a > b) return a = b, true; return false;
}

template<typename T>
bool maximize(T& a, const T& b){
    if(a < b) return a = b, true; return false;
}

using ll = long long;
using ld = long double;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T> T random_int(T l, T r){ return uniform_int_distribution<T>(l, r)(rng); }
template<typename T> T random_real(T l, T r){ return uniform_real_distribution<T>(l, r)(rng); }

void testcase(){
    int k, n;
    cin >> k >> n;
    vector<pair<int, int>> opposite;
    vector<int> values;

    ll answer = 0;
    rep(i, 0, n){
        char b1, b2;
        int x1, x2;

        cin >> b1 >> x1 >> b2 >> x2;

        if(b1 == b2) answer += abs(x1 - x2);
        else {
            opposite.push_back({x1, x2});
            values.push_back(x1);
            values.push_back(x2);
        }
    }

    n = sz(opposite);
    answer += n;

    if(k == 1){
        sort(all(values));
        int median = values[sz(values) / 2];
        for(int i = 0; i < n; ++i){
            answer += abs(opposite[i].first - median) + abs(opposite[i].second - median);
        }
    } else{
        sort(all(opposite), [&](const pair<int, int>& p1, const pair<int, int>& p2){
            return p1.first + p1.second < p2.first + p2.second;
        });

        priority_queue<int> left;
        priority_queue<int, vector<int>, greater<int>> right;

        vector<ll> pref(n);
        ll left_sum = 0, right_sum = 0, cur = 0;
        int median = -1;

        auto add = [&](int x) -> void{
            left.push(x);
            left_sum += x;

            while(sz(left) > sz(right)){
                int y = left.top();
                left_sum -= y;
                left.pop();
                right.push(y);
                right_sum += y;
            }

            while(!left.empty() && !right.empty() && left.top() > right.top()){
                int u = left.top();
                int v = right.top();

                left_sum -= u;
                right_sum -= v;

                left.pop();
                right.pop();

                left.push(v);
                right.push(u);

                left_sum += v;
                right_sum += u;
            }

            median = right.top();
        };

        for(int i = 0; i < n; ++i){
            add(opposite[i].first);
            add(opposite[i].second);

            cur = 1ll * sz(left) * median - left_sum + right_sum - 1ll * sz(right) * median;
            pref[i] = cur;
        }

        priority_queue<int>().swap(left);
        priority_queue<int, vector<int>, greater<int>>().swap(right);
        left_sum = 0;
        right_sum = 0;
        cur = 0;

        ll extra = pref[n - 1];

        for(int i = n - 1; i >= 0; --i){
            add(opposite[i].first);
            add(opposite[i].second);

            cur = 1ll * sz(left) * median - left_sum + right_sum - 1ll * sz(right) * median;
            minimize(extra, cur + (i > 0 ? pref[i - 1] : 0));
        }

        answer += extra;
    }

    cout << answer << '\n';
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    #define filename "task"
    if(fopen(filename".inp", "r")){
        freopen(filename".inp", "r", stdin);
        freopen(filename".out", "w", stdout);
    }

    int T = 1; //cin >> T;
    while(T--) testcase();

    return 0;
}

Compilation message

bridge.cpp: In function 'bool minimize(T&, const T&)':
bridge.cpp:15:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   15 |     if(a > b) return a = b, true; return false;
      |     ^~
bridge.cpp:15:35: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   15 |     if(a > b) return a = b, true; return false;
      |                                   ^~~~~~
bridge.cpp: In function 'bool maximize(T&, const T&)':
bridge.cpp:20:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   20 |     if(a < b) return a = b, true; return false;
      |     ^~
bridge.cpp:20:35: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   20 |     if(a < b) return a = b, true; return false;
      |                                   ^~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  140 |         freopen(filename".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:141:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  141 |         freopen(filename".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 592 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 592 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 592 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 468 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 592 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -