# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
966804 | Amr | Wiring (IOI17_wiring) | C++17 | 1090 ms | 9648 KiB |
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 "wiring.h"
#include<bits/stdc++.h>
using namespace std;
#define sz size()
typedef long long ll;
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
const int N = 2e5+2;
ll a[N];
ll dp[N];
long long min_total_length(std::vector<int> r, std::vector<int> b) {
ll n = r.sz, m = b.sz;
vector<pair<ll,ll> > v;
v.push_back({-1,-1});
for(int i = 0; i < r.sz; i++) v.push_back({r[i],0});
for(int i = 0; i < b.sz; i++) v.push_back({b[i],1});
sort(all(v));
ll start = 1,prevstart=1;
for(int i = 1; i <= n+m; i++)
{
if(i==n+m||(v[i].S!=v[i+1].S))
{
ll en = i;
if(start==1)
{
for(int j = 1; j <= en; j++) dp[j] = -1;
prevstart = start; start = i+1; continue;
}
ll allme = 0;
for(int j = start; j <= en; j++)
{
dp[j] = 1e18;
allme+=v[j].F;
ll allprev = 0;
for(ll k = start-1; k >= prevstart; k--)
{
allprev+=v[k].F;
ll kk = k-1;
ll mm = 0;
if(j-start+1>start-k) mm = -((j-start+1)-(start-k))*v[start-1].F;
else mm = (start-k-(j-start+1))*v[start].F;
if(dp[kk]!=-1){
dp[j] = min(dp[j],dp[kk]+allme-allprev+mm);
//if(j==6&&k==3) cout << allme << " " << allprev << " " << dp[kk] << endl;
}
if(k==prevstart)
{
if(dp[k]!=-1)
dp[j]=min(dp[j],dp[k]+allme-allprev+mm);
}
}
}
prevstart = start,start=i+1;
}
}
// b r b rrrr
return dp[n+m];
}
Compilation message (stderr)
# | 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... |