이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 {
int 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]);
return init + ans;
}
}
namespace sub5 {
void balance(multiset<int>&f, multiset<int>&se, int&fSum, int&seSum) {
if (f.size() < se.size()) f.insert(*se.begin()), fSum += *se.begin(), seSum -= *se.begin(), se.erase(se.find(*se.begin()));
else if (f.size() > se.size() + 1) 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 1e9;
int median = *f.rbegin(), fSz = f.size(), seSz = se.size();
return median*fSz - fSum + seSum - seSz*median;
}
void add(multiset<int>&f, multiset<int>&se, int&fSum, int&seSum, int v) {
if (v <= *f.rbegin() || f.empty()) f.insert(v), fSum += v;
else se.insert(v), seSum += v;
balance(f, se, fSum, seSum);
}
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;
balance(f, se, fSum, seSum);
}
int 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++) {
add(thi, four, thiSum, fourSum, s[arr[i]]);
add(thi, four, thiSum, fourSum, t[arr[i]]);
}
int ans = compute(thi, four, thiSum, fourSum);
for (int i = 0; i < arr.size(); i++) {
remove(thi, four, thiSum, fourSum, s[arr[i]]);
remove(thi, four, thiSum, fourSum, t[arr[i]]);
add(f, se, fSum, seSum, s[arr[i]]);
add(f, se, fSum, seSum, t[arr[i]]);
ans = min(ans, compute(f, se, fSum, seSum) + compute(thi, four, thiSum, fourSum));
}
return init + ans;
}
}
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];
int ans = sub2::main(n, k, s, t, p, q);
if (k == 2) ans = min(ans, sub5::main(n, k, s, t, p, q));
cout << ans << '\n';
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
bridge.cpp: In function 'long long int sub5::main(long long int, long long int, long long int*, long long int*, char*, char*)':
bridge.cpp:62: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]
62 | for (int i = 0; i < arr.size(); i++) {
| ~~^~~~~~~~~~~~
bridge.cpp:67: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]
67 | for (int i = 0; i < arr.size(); i++) {
| ~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |