Submission #1172410

#TimeUsernameProblemLanguageResultExecution timeMemory
1172410vibeduckPalembang Bridges (APIO15_bridge)C++20
22 / 100
54 ms6700 KiB
#include <bits/stdc++.h> 
using namespace std;

#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define int long long
#define all(x) (x).begin(), (x).end()

typedef long double ld;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<ll>> vvll;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef map<int, int> mii;
typedef unordered_map<int, int> umii;

const int mod = 1e9 + 7;
const int inf = INTMAX_MAX;
const bool tc = false;

bool cmp(pii &x, pii &y) {
    if (x.se != y.se) return x.se < y.se;
    return x.fi < y.fi;
}

inline void solve() {
    int k, n;
    cin >> k >> n;
    int un = 0; vector<pii> a;

    for (int i = 0; i < n; i++) {
        char p, q; int s, t;
        cin >> p >> s >> q >> t;
        if (p == q) {
            un += abs(s - t);
            continue;
        }
        if (p == 'B') swap(s, t);
        if (s > t) swap(s, t);
        a.pb({s, t});
        un++;
        //cout << s << " " << t << " is a segment\n";
    }

    //cout << '\n';

    vector<pii> sweep;
    for (int i = 0; i < a.size(); i++) {
        sweep.pb({a[i].fi, 1});
        sweep.pb({a[i].se, -1});
    }
    sort(all(sweep));

    vi d(sweep.size()); int l = 0, o = 0;
    pii best = {1e18, -1e18};
    for (int i = 0; i < sweep.size(); i++) {
        auto cur = sweep[i];
        if (cur.se == 1) o++;

        int r = a.size() - o - l;
        assert(r >= 0);
        d[i] = abs(r - l);
        //cout << cur.fi << " " << o << " " << l << " " << r << '\n';
        
        if (d[i] < best.fi) {
            best = {d[i], i};
        }
        if (d[i] == best.fi) {
            if (r > l) best = {d[i], i};
        }

        if (cur.se == -1) {
            l++;
            o--;
        }

        //cout << '\n';
    }

    int md = sweep[best.se].fi;
    int cn = 0;
    for (int i = 0; i < a.size(); i++) {
        if (a[i].fi <= md && md <= a[i].se) {
            cn += a[i].se - a[i].fi;
            continue;
        }
        if (a[i].fi > md) {
            cn += a[i].fi - md + a[i].se - md;
            continue;
        }
        if (a[i].se < md) {
            cn += md - a[i].se + md - a[i].fi;
            continue;
        }
    }

    cout << cn + un << '\n';
}

// dont forget un

void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

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

    int t = 1;
    if (tc) {
        cin >> t;
    }

    while (t--) {
        solve();
    }
}

Compilation message (stderr)

bridge.cpp: In function 'void setIO(std::string)':
bridge.cpp:115:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  115 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:116:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  116 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...