제출 #1143961

#제출 시각아이디문제언어결과실행 시간메모리
1143961alir3za_zar3Palembang Bridges (APIO15_bridge)C++20
22 / 100
56 ms2496 KiB
// Alir3za.Zar3 -> Shiraz , Iran
#include <bits/stdc++.h>
using namespace std;
#define     int     long long

const int N = 1e5+7 , Inf = 2e18;
int k,n , out=0;
vector<pair<int,int>> q;

void iN ()
{
    cin >> k >> n;
    for (int i=1; i<=n; i++)
    {
        char P,Q; int S,T;
        cin >> P >> S >> Q >> T;
        if (P == Q)
            out += abs(S - T);
        else
            q.push_back({min(S,T),max(S,T)});
    }
}

int oK (int v)
{
    int s = 0;
    for (auto [a,b] : q)
    {
        if (v > b) s += v-a + v-b + 1;
        else if (v < a) s += a-v + b-v + 1;
        else s += b-a+1;
    }
    return s;
}

void Ternary_Search ()
{
    int l=0 , r=1e9;
    while (r-l > 2)
    {
        int o = (r-l) / 3;
        int v1 = l+o , v2 = l+2*o;
        int q1 = oK(v1) , q2 = oK(v2);
        if (q1 < q2) r=v2;
        else if (q1 == q2) l=v1,r=v2;
        else l=v1;
    }
    int mn = Inf;
    for (int i=l; i<=r; i++)
        mn = min(mn , oK(i));
    out += mn;
}

void ouT ()
{
    cout << out << '\n';
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);     cout.tie(0);

    iN();
    Ternary_Search();
    ouT();
}
#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...