Submission #443683

#TimeUsernameProblemLanguageResultExecution timeMemory
443683stat0r1cPalembang Bridges (APIO15_bridge)C++11
100 / 100
82 ms6252 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;
using ll = long long;
using ull = unsigned ll;
template<typename T> using vt = vector<T>;
using vi = vt<int>;
using vvi = vt<vi>;
using ii = pair<int, int>;
using vii = vt<ii>;
/* template<typename T>
using indexed_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; */
/* struct chash {
    static ull hash_f(ull x) {x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31);}
    size_t operator()(ull x) const {static const ull RANDOM = chrono::steady_clock::now().time_since_epoch().count();return hash_f(x + RANDOM);}
};
struct iihash {
    template <class T1, class T2>
    size_t operator () (const pair<T1,T2> &p) const {auto h1 = hash<T1>{}(p.first);auto h2 = hash<T2>{}(p.second);return h1 ^ h2;}
};
template<class T1, class T2, class T3> using ii_umap = gp_hash_table<pair<T1,T2>,T3,iihash>;
template<class T1, class T2> using f_umap = gp_hash_table<T1,T2,chash>;
template<class T1, class T2> using o_umap = gp_hash_table<T1,T2>; */
#define sqr(x) (x)*(x)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define debug(x) cerr << #x << " -> " << x << '\n'
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define eb emplace_back
const int inf = 1e9 + 7;
const ll infll = 1e18 + 10;
template <typename T> inline T fgcd(T a, T b) {while(b) swap(b, a %= b); return a;}
ll fpow(ll a, ll b) {ll o = 1; for(;b;b >>= 1) {if(b & 1) o = o * a;a = a * a;} return o;}
ll fpow(ll a, ll b, ll m) {ll o = 1; a %= m; for(;b;b >>= 1) {if(b & 1) o = o * a % m;a = a * a % m;} return o;}
template<class T> void uniq(vt<T> &a) {sort(all(a));a.resize(unique(all(a)) - a.begin());}
void setIO(string name = "", string inp = "inp", string out = "out") {
    cin.tie(0)->sync_with_stdio(0);
    if(sz(name)) {
        if(sz(inp)) freopen((name+"."+inp).c_str(), "r", stdin);
        if(sz(out)) freopen((name+"."+out).c_str(), "w", stdout);
    }
}
ll sul,sur,n,k,l,r,ans;
vt<pair<ll, ll>> a;
priority_queue<ll> pql,ltemp;
priority_queue<ll, vt<ll>, greater<ll>> pqr,rtemp;
vt<ll> b;
char x,y;
void clear() {
    pql = ltemp;
    pqr = rtemp;
    sul = sur = 0;
}
void add(ll u) {
    if(pql.empty() || u <= pql.top()) {
        sul += u;
        pql.push(u);
    }
    else {
        pqr.push(u);
        sur += u;
    }
    while(sz(pql) != sz(pqr) && sz(pql) - sz(pqr) != 1) {
        if(sz(pql) > sz(pqr)) {
            sul -= pql.top();
            sur += pql.top();
            pqr.push(pql.top());
            pql.pop();
        }
        else {
            sul += pqr.top();
            sur -= pqr.top();
            pql.push(pqr.top());
            pqr.pop();
        }
    }
}
ll get() {
    ll e = pql.top();
    return e * sz(pql) - sul + sur - e * sz(pqr);
}
int main() {
    setIO();
    cin >> k >> n;
    for(int i = 0; i < n; i++) {
        cin >> x >> l >> y >> r;
        if(x == y) ans += abs(r - l);
        else {
            a.eb(l,r);
            if(k == 1) {
                b.pb(l);
                b.pb(r);
            }
        }
    }
    if(k == 1) {
        if(!b.empty()) {
            sort(all(b));
            l = b[(sz(b) - 1) / 2];
            for(auto i : a) {
                ans += abs(i.F - l) + abs(i.S - l) + 1;
            }
        }
        cout << ans;
    }
    else {
        if(a.empty()) {
            cout << ans;
            return 0;
        }
        sort(all(a), [&](const ii &p, const ii &q) {return p.F + p.S < q.F + q.S;});
        b.resize(sz(a));
        for(int i = 0; i < sz(a); i++) {
            add(a[i].F);
            add(a[i].S);
            b[i] = get();
        }
        clear();
        for(int i = sz(a) - 1; i; i--) {
            add(a[i].F);
            add(a[i].S);
            b[i - 1] += get();
        }
        cout << ans + *min_element(all(b)) + sz(a);
    }
    return 0;
}

Compilation message (stderr)

bridge.cpp: In function 'void setIO(std::string, std::string, std::string)':
bridge.cpp:45:28: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   45 |         if(sz(inp)) freopen((name+"."+inp).c_str(), "r", stdin);
      |                     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:46:28: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   46 |         if(sz(out)) freopen((name+"."+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...