이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// In the name of the God
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define pii pair <int, int>
#define smin(x, y) (x) = min((x), (y))
#define smax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(), (x).end()
using namespace std;
const int inf = 1e9+7;
const int mod = 998244353;
const int maxn = 1e5+5;
int k, n, sd = 0, su = 0, dp[maxn];
vector <pair <int, pii> > vec;
multiset <int> ali, stu;
void reset() {
if (stu.size() > ali.size()) {
int x = *stu.begin();
stu.erase(stu.begin());
su -= x;
ali.insert(x);
sd += x;
}
if (ali.size()-1 > stu.size()) {
int x = *ali.rbegin();
ali.erase(ali.find(x));
sd -= x;
stu.insert(x);
su += x;
}
}
void add(int x) {
int m = ali.size() ? *ali.rbegin() : inf;
if (x <= m) {
ali.insert(x); sd += x;
}
else {
stu.insert(x); su += x;
}
reset();
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> k >> n;
int res = 0;
for (int i = 0; i < n; i++) {
char p, q; int s, t; cin >> p >> s >> q >> t;
if (p == q) res += abs(s-t);
else {
res++;
vec.pb(mp(s+t, mp(s, t)));
}
}
n = vec.size();
sort(all(vec));
for (int i = 0; i < n; i++) {
int s = vec[i].S.F, t = vec[i].S.S;
add(s), add(t);
dp[i] = su-sd;
}
int ans = dp[n-1];
if (k == 2) {
sd = su = 0;
ali.clear(), stu.clear();
for (int i = n-1; i >= 1; i--) {
int s = vec[i].S.F, t = vec[i].S.S;
add(s), add(t);
smin(ans, su-sd+dp[i-1]);
}
}
cout << ans+res << '\n';
return 0;
}
/*
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
# | 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... |