제출 #886004

#제출 시각아이디문제언어결과실행 시간메모리
886004abysmalPalembang Bridges (APIO15_bridge)C++14
0 / 100
1 ms348 KiB
#include<iostream> #include<stdio.h> #include<stdint.h> #include<iomanip> #include<algorithm> #include<utility> #include<vector> #include<stack> #include<queue> #include<set> #include<map> #include<deque> #include<math.h> #include<assert.h> #include<string.h> #include<chrono> #include<bitset> using namespace std; using namespace std::chrono; const double epsi = (double) 1e-6; const int64_t INF = (int64_t) 1e18 + 100; const int64_t mINF = (int64_t) 1e9 + 5; const int64_t MOD = 1e9 + 7; const int64_t MOD2 = 998244353; const int nbit = 24; const int ndig = 10; const int nchar = 26; const int p1 = 31; const int p2 = 53; const int D = 4; int dr[D] = {-1, 0, 1, 0}; int dc[D] = {0, 1, 0, -1}; const int Dk = 8; int drk[Dk] = {2, 2, -2, -2, 1, 1, -1, -1}; int dck[Dk] = {1, -1, 1, -1, 2, -2, 2, -2}; bool mod_cmp(pair<int, int>& t1, pair<int, int>& t2) { return t1.first + t1.second < t2.first + t2.second; } struct Solution { int k; int n; int64_t suml; int64_t sumr; priority_queue<int> left; priority_queue<int> right; vector<pair<int, int> > v; Solution() {} void solve() { cin >> k >> n; int64_t sum = 0; for(int i = 0; i < n; i++) { char a; int x; char b; int y; cin >> a >> x >> b >> y; if(a == b) sum += Abs(x - y); else v.emplace_back(x, y); } suml = 0; sumr = 0; sort(v.begin(), v.end(), mod_cmp); n = (int) v.size(); sum += n; vector<int64_t> prefix(n, 0); for(int i = 0; i < n; i++) { iinsert(v[i].first); iinsert(v[i].second); prefix[i] = sumr - suml; } if(k == 1) cout << prefix[n - 1] + sum << "\n"; else { while(!left.empty()) left.pop(); while(!right.empty()) right.pop(); suml = 0; sumr = 0; int64_t ans = INF; for(int i = n - 1; i >= 0; i--) { iinsert(v[i].first); iinsert(v[i].second); int p = 0; if(i != 0) p = prefix[i - 1]; ans = min(ans, sumr - suml + p); } cout << ans + sum << "\n"; } } void iinsert(int val) { int median = mINF; if((int) left.size() != 0) median = left.top(); if(val <= median) { left.push(val); suml += val; } else { right.push(-val); sumr += val; } if((int) right.size() + 1 < (int) left.size()) { int nxt = left.top(); left.pop(); right.push(-nxt); suml -= nxt; sumr += nxt; } else if((int) left.size() < (int) right.size()) { int nxt = -right.top(); right.pop(); left.push(nxt); sumr -= nxt; suml += nxt; } } int modadd(int t1, int t2) { int res = t1 + t2; if(res >= MOD) res -= MOD; return res; } int modsub(int t1, int t2) { int res = t1 - t2; if(res < 0) res += MOD; return res; } int modmul(int t1, int t2) { int64_t res = 1LL * t1 * t2; return res % MOD; } int64_t Abs(int64_t t1) { if(t1 < 0) return -t1; return t1; } int64_t MASK(int i) { return (1LL << i); } bool BIT(int64_t t1, int j) { return (t1 & MASK(j)); } }; void __setup() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); } int main() { __setup(); // auto start = high_resolution_clock::now(); int t = 1; // cin >> t; for(int i = 1; i <= t; i++) { Solution().solve(); } // auto stop = high_resolution_clock::now(); // auto duration = duration_cast<microseconds>(stop - start); // cerr << duration.count() << "\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...