제출 #1201758

#제출 시각아이디문제언어결과실행 시간메모리
1201758TimoshPalembang Bridges (APIO15_bridge)C++20
22 / 100
31 ms2496 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define int int64_t
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vi vector<int>
#define vii vector<vi>
#define ld long double
#define pii pair<int, int>
mt19937 mt(time(0));

namespace io
{
    template <typename T, typename F>
    istream &operator>>(istream &cin, pair<T, F> &pr)
    {
        cin >> pr.first >> pr.second;
        return cin;
    }
    template <typename T, typename F>
    ostream &operator<<(ostream &cout, pair<T, F> &pr)
    {
        cout << pr.first << ' ' << pr.second;
        return cout;
    }
    template <typename T>
    istream &operator>>(istream &cin, vector<T> &vec)
    {
        for (T &i : vec)
            cin >> i;
        return cin;
    }
    template <typename T>
    ostream &operator<<(ostream &cout, vector<T> vec)
    {
        for (T i : vec)
            cout << i << ' ';
        return cout;
    }
}
using namespace io;

int M = 1e9 + 7;

int pw(int x, int y)
{
    x %= M;
    int res = 1;
    while (y)
    {
        if (y & 1)
            res = res * x % M;
        x = x * x % M, y /= 2;
    }
    return res;
}

int inv(int x) { return pw(x, M - 2); }

void solve()
{
    int k, n, x, y, ans = 0;
    char A, B;
    cin >> k >> n;
    vector<pii> a;
    while (n--)
    {
        cin >> A >> x >> B >> y;
        if (x > y)
            swap(x, y);
        if (A != B)
            a.push_back({x, y}), ans++;
        else
            ans += y - x;
    }
    n = a.size();
    if (n == 0)
        return cout << ans, void();
    int mn = 1e18;
    int l = 0, r = 1e9 + 1;
    while (l <= r)
    {
        int m = (l + r) / 2;
        int A = 0, B = 0;
        for (auto &[x, y] : a)
        {
            if (x <= m && m <= y)
                A += y - x;
            else
                A += abs(y - m) + abs(x - m);
            m++;
            if (x <= m && m <= y)
                B += y - x;
            else
                B += abs(y - m) + abs(x - m);
            m--;
        }
        mn = min(mn, A);
        mn = min(mn, B);
        if (A < B)
            r = m - 1;
        else
            l = m + 1;
    }
    cout << ans + mn;
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    // cin >> t;
    while (t--)
        solve(), cout << '\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...