Submission #797490

#TimeUsernameProblemLanguageResultExecution timeMemory
797490acatmeowmeowPalembang Bridges (APIO15_bridge)C++11
22 / 100
33 ms4292 KiB
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 1e5; int k, n, s[N + 5], t[N + 5]; char p[N + 5], q[N + 5]; namespace sub2 { void main(int n, int k, int s[], int t[], char p[], char q[]) { vector<int> arr; int init = 0; for (int i = 1; i <= n; i++) { if (p[i] == q[i]) init += abs(s[i] - t[i]); else arr.push_back(s[i]), arr.push_back(t[i]), init++; } sort(arr.begin(), arr.end()); int ans = 0, mid = arr.size()/2; for (auto&v : arr) ans += abs(v - arr[mid]); cout << init + ans << '\n'; } } namespace sub5 { void balance(multiset<int>&f, multiset<int>&se, int&fSum, int&seSum) { while (f.size() < se.size() && se.size()) f.insert(*se.begin()), fSum += *se.begin(), seSum -= *se.begin(), se.erase(se.find(*se.begin())); while (f.size() > se.size() && f.size()) se.insert(*f.rbegin()), seSum += *f.rbegin(), fSum -= *f.rbegin(), f.erase(f.find(*f.rbegin())); } int compute(multiset<int>&f, multiset<int>&se, int&fSum, int&seSum) { if (f.empty()) return 0; int median = *f.rbegin(), fSz = f.size(), seSz = se.size(); return median*fSz - fSum + seSum - seSz*median; } void remove(multiset<int>&f, multiset<int>&se, int&fSum, int&seSum, int v) { if (f.find(v) != f.end()) f.erase(f.find(v)), fSum -= v; else se.erase(se.find(v)), seSum -= v; } void main(int n, int k, int s[], int t[], char p[], char q[]) { vector<int> arr; int init = 0; for (int i = 1; i <= n; i++){ if (p[i] == q[i]) init += abs(s[i] - t[i]); else arr.push_back(i), init++; } sort(arr.begin(), arr.end(), [&](int a, int b) { return s[a] + t[a] < s[b] + t[b]; }); multiset<int> f, se, thi, four; int fSum = 0, seSum = 0, thiSum = 0, fourSum = 0; for (int i = 0; i < arr.size(); i++) thi.insert(s[arr[i]]), thi.insert(t[arr[i]]), thiSum += s[arr[i]] + t[arr[i]]; int ans = 1e18; for (int i = 0; i < arr.size(); i++) { fSum += s[arr[i]] + t[arr[i]], f.insert(s[arr[i]]), f.insert(t[arr[i]]); remove(thi, four, thiSum, fourSum, s[arr[i]]), remove(thi, four, thiSum, fourSum, t[arr[i]]); balance(f, se, fSum, seSum); balance(thi, four, thiSum, fourSum); ans = min(ans, compute(f, se, fSum, seSum) + compute(thi, four, thiSum, fourSum)); } cout << init + ans << '\n'; } } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> k >> n; for (int i = 1; i <= n; i++) cin >> p[i] >> s[i] >> q[i] >> t[i]; if (k == 1) sub2::main(n, k, s, t, p, q); else sub5::main(n, k, s, t, p, q); return 0; }

Compilation message (stderr)

bridge.cpp: In function 'void sub5::main(long long int, long long int, long long int*, long long int*, char*, char*)':
bridge.cpp:55:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |   for (int i = 0; i < arr.size(); i++) thi.insert(s[arr[i]]), thi.insert(t[arr[i]]), thiSum += s[arr[i]] + t[arr[i]];
      |                   ~~^~~~~~~~~~~~
bridge.cpp:57:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |   for (int i = 0; i < arr.size(); i++) {
      |                   ~~^~~~~~~~~~~~
#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...