이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#pragma GCC optimize("03,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
// Shortcuts for common operations
#define pb push_back
#define p push
#define ppb pop_back
#define f first
#define s second
#define all(x) (x).begin(), (x).end()
#define ll long long
//#define int ll
#define endl "\n"
#define sz(x) (int)x.size()
// Type definitions for convenience
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<vi> vvi;
typedef vector<pii> vii;
// Debugging macros
#define debug(x) cerr << #x << " = " << (x) << '\n'
#define debug_vector(v) _debug_vector(#v, v)
template<typename T>
void _debug_vector(const string& name, const vector<T>& a) {
cerr << name << " = [ ";
for(const auto &x : a) cerr << x << ' ';
cerr << "]\n";
}
// I/O redirection for local testing
#define iofile(io) \
freopen((io + ".in").c_str(), "r", stdin); \
freopen((io + ".out").c_str(), "w", stdout);
// delta for floodfill
vi dx = {0, 1, 0, -1};
vi dy = {1, 0, -1, 0};
// extended deltas for floodfill
vi edx = {0, 1, 0, -1, 1, 1, -1, -1};
vi edy = {1, 0, -1, 0, 1, -1, 1, -1};
// Common outputs
void yes() { cout << "YES" << '\n'; }
void no() { cout << "NO" << '\n'; }
struct person{
ll home, office;
bool homeZone, officeZone;
person(ll _home, ll _office, char _homeZone, char _officeZone){
home = min(_home, _office);
office = max(_home, _office);
homeZone = (_homeZone - 'A');
officeZone = (_officeZone - 'A');
}
bool operator < (const person &per) const {
return ((home+office) < (per.home+per.office));
}
};
struct med{
priority_queue<ll>a;
priority_queue<ll, vll, greater<ll>>b;
ll _a = 0, _b = 0;
void insert(int num){
a.p(num);
_a += num;
if(a.size() > b.size()+1){
_a -= a.top();
_b += a.top();
b.p(a.top());
a.pop();
}
while(a.size() and b.size() and a.top() > b.top()){
_a = _a + b.top() - a.top();
_b = _b + a.top() - b.top();
auto item = a.top();
a.pop();
a.p(b.top());
b.pop();
b.p(item);
}
}
ll res(){
return (a.top() * a.size() - _a + _b - a.top() * b.size());
}
};
void fx() {
int k, n;
cin >> k >> n;
vector<person>people;
med str;
ll sameSide = 0;
for(int i = 0; i < n; i++){
ll h, o;
char hz, oz;
cin >> hz >> h >> oz >> o;
person per(h, o, hz, oz);
if(per.homeZone^per.officeZone) people.pb(per);
else sameSide += (per.office - per.home);
}
sort(all(people));
n = people.size();
vll precost(n);
for(int i = 0; i < n; i++) str.insert(people[i].home), str.insert(people[i].office), precost[i] = str.res();
while(!str.a.empty()) str.a.pop();
while(!str.b.empty()) str.b.pop();
str._a = 0; str._b = 0;
ll res = 1e15;
if(k == 1){ cout << (precost[n-1] + sameSide + n) << endl; return; }
for(int i = n-1; i >= 1; i--) str.insert(people[i].home), str.insert(people[i].office), res = min(res, precost[i-1] + str.res());
cout << (res + sameSide + n) << endl;
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
// Uncomment the following lines for file I/O
// iofile(string("hello"));
// Uncomment the following lines for multiple test cases
// int t; cin >> t; while(t--) fx();
// Single test case
fx();
return 0;
}
# | 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... |