이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define gc getchar()
#define pc(x) putchar(x)
template<typename T> void scan(T &x){x = 0;bool _=0;T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=char(n%10+48);n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
#ifdef LOCAL
template<typename T>
void pr(T a){std::cerr<<a<<std::endl;}
template<typename T,typename... Args>
void pr(T a, Args... args) {std::cerr<<a<<' ',pr(args...);}
#else
template<typename... Args>
void pr(Args... args){}
#endif
using namespace std;
using ll = long long;
const int MM = 2e5+5;
const ll inf = 1e12;
#define f first
#define s second
int n, last[MM];
pair<ll, ll> a[MM];
ll dpl[MM], dpr[MM], psa[MM];
//dpl = this goes to left side, dpr = go to right side
//update when groups change
long long min_total_length(std::vector<int> R, std::vector<int> B){
n = 0;
for(int i: R)
a[++n] = {i, 0};
for(int i: B)
a[++n] = {i, 1};
sort(a+1, a+1+n);
for(int i = 1,m; i <= n; i++){
dpl[i] = i > 1 ? inf : 0;
psa[i] = psa[i-1]+a[i].f;
auto [x, t] = a[i];
if(t == a[i-1].s){
m = last[i] = last[i-1];
dpl[i] = min(dpl[i], dpl[i-1] + x-a[m].f); //extend
//try to 1:1 with last group
int cnt = i-m;
int l = m-cnt;
if(l >= 0 and last[m] <= l){
//(l, m] is same
dpl[i] = min(dpl[i], dpl[l] + (psa[i]-psa[m])-(psa[m]-psa[l]));
// pr("g", i, l, cnt, i, ".", m, ",", dpl[l], (psa[i]-psa[m]), -(psa[m]-psa[l]));
}
}
else{
m = last[i] = i-1;
dpl[i] = min(dpl[i], dpl[i-1] + x-a[i-1].f); //extend
dpl[i] = min(dpl[i], dpl[i-2] + x-a[i-1].f);
//calc dpr for previous group
}
// pr(i, a[i].f, a[i].s, dpl[i]);
}
return dpl[n];
}
#ifdef LOCAL
int main(){
print(min_total_length({1, 2, 3, 7}, {0, 4, 5, 9, 10}));
}
#endif
# | 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... |