This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
//Dijkstra->set
//set.find_by_order(x) x-position value
//set.order_of_key(x) number of strictly less elements don't need *set.??
#define N 100005
#define wr cout << "Continue debugging\n";
#define all(x) (x).begin(), (x).end()
#define ll long long int
#define pii pair <int, int>
#define pb push_back
#define ff first
#define ss second
const ll inf = 1e18;
map <int,int> mp;
ll min_total_length(vector<int> r, vector<int> b){
sort(all(r)); sort(all(b));
if (r.back() <= b[0]){
int pos = 0;
ll ans = 0;
if ((int)r.size() > (int)b.size()){
pos = (int)r.size()-(int)b.size();
for (int i = 0; i < pos; i++) ans += b[0]-r[i];
}
for (int i = 0; i < (int)b.size(); i++){
ans += b[i]-r[min(pos, (int)r.size()-1)];
pos++;
}
return ans;
}
if ((int)r.size() <= 200 and (int)b.size() <= 200){
vector<vector<ll>> dp(205, vector<ll>(205, inf));
ll sum = 0;
for (int i = 0; i < (int)b.size(); i++){
sum += abs(r[0]-b[i]);
dp[0][i] = sum;
}
sum = 0;
for (int i = 0; i < (int)r.size(); i++){
sum += abs(r[i]-b[0]);
dp[i][0] = sum;
}
for (int i = 1; i < (int)r.size(); i++){
for (int j = 1; j < (int)b.size(); j++){
ll sum = 0;
for (int k = j; k >= 1; k--){
sum += abs(r[i]-b[k]);
dp[i][j] = min(dp[i][j], dp[i-1][k-1]+sum);
}
sum = 0;
for (int k = i; k >= 1; k--){
sum += abs(r[k]-b[j]);
dp[i][j] = min(dp[i][j], dp[k-1][j-1]+sum);
}
}
}
return dp[(int)r.size()-1][(int)b.size()-1];
}
ll ans = 0;
int n = r.size(), m = b.size();
vector<int> vis(n+m+1, 0), cur(n+m+1, 0);
for (auto i : r) cur[i] = 1;
for (auto i : b) cur[i] = 2;
n += m;
for (int i = 1; i <= n; i++){
if (vis[i]) continue;
int k = 3-cur[i], prev = i, tr = 0;
vis[i] = 1;
for (int j = i+1; j <= n; j++){
if (cur[j] == k and !vis[j]){
ans += j-prev;
prev = j;
k = 3-cur[j];
vis[j] = tr = 1;
}
}
if (!tr){
int mn = 1e9+7;
for (int j = i-1; j >= 1; j++){
if (cur[j] != cur[i]){mn = min(mn, i-j);break;}
}
for (int j = i+1; j <= n; j++){
if (cur[j] != cur[i]){mn = min(mn, j-i);break;}
}
ans += mn;
}
}
return ans;
}
// int main ()
// {
// ios::sync_with_stdio(false);
// cin.tie(0);
// int n, m;
// cin >> n >> m;
// vector<int> r(n), b(m);
// for (int i = 0; i < n; i++) cin >> r[i];
// for (int i = 0; i < m; i++) cin >> b[i];
// cout << min_total_length(r, b);
// }
# | 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... |