답안 #922870

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
922870 2024-02-06T08:32:38 Z vjudge1 Palembang Bridges (APIO15_bridge) C++17
0 / 100
2 ms 6708 KB
#include<bits/stdc++.h>
#define X first
#define Y second
#define all(x) begin(x), end(x)
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define FORD(i, b, a) for(int i = (b); i >= (a); i--)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define mxx max_element
#define mnn min_element
#define SQR(x) (1LL * (x) * (x))
#define MASK(i) (1LL << (i))
#define Point Vector
#define left Left
#define right Right
#define div Div

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair<db, db> pdb;
typedef pair<ld, ld> pld;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
typedef pair<ll, int> pli;
typedef pair<ll, pii> plii;

template<class A, class B>
    bool maximize(A& x, B y) {
        if (x < y) return x = y, true; else return false;
    }
template<class A, class B>
    bool minimize(A& x, B y) {
        if (x > y) return x = y, true; else return false;
    }
/* END OF TEMPLATE */

const int N = 3e5 + 7;

int n, k;

vector<pii> seg;

ll res = 0;

namespace sub1 {
    ll pre[N], suf[N];
    int pre_cnt[N], suf_cnt[N];
    vector<int> E;
    int idx(int x) {
        return lower_bound(all(E), x) - E.begin() + 1;
    }
    void buildE() {
        set<int> s;
        for (auto x : seg) {
            s.insert(x.X);
            s.insert(x.Y);
        }
        E = vector<int>(s.begin(), s.end());
    }
    void Process() {
        buildE();
        int m = (int)E.size();
        ll ANS = 9e18;
        pre[0] = suf[n + 1] = 0;
        pre_cnt[0] = suf_cnt[n + 1] = 0;
        for (auto x : seg) {
            pre[idx(x.Y)]+=x.Y;
            pre_cnt[idx(x.Y)]++;
            suf[idx(x.X)]+=x.X;
            suf_cnt[idx(x.X)]++;
        }
        FOR(i, 1, m) {
            pre[i]+=pre[i - 1];
            pre_cnt[i]+=pre_cnt[i - 1];
        }
        FORD(i, m, 1) {
            suf[i]+=suf[i + 1];
            suf_cnt[i]+=suf_cnt[i + 1];
        }
        FOR(i, 1, m) {
            int x = E[i - 1];
            ll ans = 2LL * (pre_cnt[i - 1] * x - pre[i - 1]) + pre_cnt[i - 1];
            ans+=2LL * (suf[i + 1] - suf_cnt[i + 1] * x) + suf_cnt[i + 1];
            minimize(ANS, res + ans + (int)seg.size() - pre_cnt[i - 1] - suf_cnt[i + 1]);
        }
        cout<<ANS;
    }
}


int main() {
//    freopen("BRIDGE.inp", "r", stdin);
//    freopen("BRIDGE.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin>>k>>n;
    FOR(i, 1, n) {
        char P, Q;
        int A, B;
        cin>>P>>A>>Q>>B;
        res+=abs(A - B);
        if (P != Q) seg.push_back({min(A, B), max(A, B)});
    }
    if (seg.empty()) return cout<<res, 0;
    if (k == 1) {
        sub1::Process();
        return 0;
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 6488 KB Output is correct
4 Correct 2 ms 6708 KB Output is correct
5 Incorrect 2 ms 6492 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Incorrect 2 ms 6492 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -