Submission #943113

#TimeUsernameProblemLanguageResultExecution timeMemory
943113oblantisPalembang Bridges (APIO15_bridge)C++17
0 / 100
1 ms348 KiB
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") //#pragma GCC optimize("O3,unroll-loops") #include <bits/stdc++.h> #define all(v) v.begin(), v.end() #define pb push_back #define ss second #define ff first #define vt vector using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const ll inf = 1e18; const int mod = 1e9+7; const int maxn = 1e6 + 1; bool cmp(pll x, pll y){ return x.ff + x.ss < y.ff + y.ss; } void solve() { int k, n; cin >> k >> n; ll ans = 0, s = inf; vt<pair<ll, ll>> v; for(int i = 0; i < n; i++){ char a, b; int x, y; cin >> a >> x >> b >> y; if(x > y)swap(x, y); if(a != b){ v.pb({x, y}); ans++; } else ans += y - x; } sort(all(v), cmp); n = v.size(); ll dp[n], wt = 0; multiset<ll> m1, m2; for(int i = 0; i < n; i++){ m1.insert(v[i].ff), m2.insert(v[i].ss); wt += v[i].ss - v[i].ff; while(*m2.begin() < *m1.rbegin()){ int x = *m1.rbegin(), y = *m2.begin(); wt += (x - y) * 2; m2.erase(m2.begin()), m1.erase(--m1.end()); m2.insert(x), m1.insert(y); } dp[i] = wt; } s = dp[n - 1]; m1.clear(), m2.clear(); wt = 0; for(int i = n - 1; i >= 0; i--){ m1.insert(v[i].ff), m2.insert(v[i].ss); wt += v[i].ss - v[i].ff; while(*m2.begin() < *m1.rbegin()){ int x = *m1.rbegin(), y = *m2.begin(); wt += (x - y) * 2; m2.erase(m2.begin()), m1.erase(--m1.end()); m2.insert(x), m1.insert(y); } if(i)s = min(s, dp[i - 1] + wt); } cout << ans + s % inf << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int times = 1; //cin >> times; for(int i = 1; i <= times; i++) { solve(); } 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...