이 제출은 이전 버전의 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, bool> a[MM];
ll dp[MM], psa[MM];
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);
a[0] = {-inf, 0};
for(int i = 1,m; i <= n; i++){
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];
else
m = last[i] = i-1;
//init to extend val
dp[i] = dp[i-1] + x-a[m].f;
//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
dp[i] = min(dp[i], dp[l] + (psa[i]-psa[m])-(psa[m]-psa[l]));
// pr("g", i, l, cnt, i, ".", m, ",", dp[l], (psa[i]-psa[m]), -(psa[m]-psa[l]));
}
if(m == i-1){
//move some over
ll sum = 0;
int l = last[m];
// pr("sw", i, m, l);
for(int j = m; j > l; j--){
sum += x-a[j].f;// - (a[j].f-a[l].f);
// pr("add", x-a[j].f, -(a[j].f-a[l].f), ",", x, a[j].f, a[l].f);
// pr("cur", j, "=", dp[j-1], sum);
dp[i] = min(dp[i], dp[j-1]+sum);
}
}
// pr(i, a[i].f, a[i].s, dp[i]);
}
return dp[n];
}
#ifdef LOCAL
int main(){
string sss;
cin>>sss;
int n, m;
cin>>n>>m;
vector<int> r(n), b(m);
for(int &i: r)
cin>>i;
for(int &i: b)
cin>>i;
cout<<min_total_length(r, b);
}
// 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... |