제출 #1329197

#제출 시각아이디문제언어결과실행 시간메모리
1329197DanielPr8전선 연결 (IOI17_wiring)C++20
0 / 100
0 ms344 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvl = vector<vll>;
using pll = pair<ll,ll>;
using vpl = vector<pll>;
using vvp = vector<vpl>;
#define f first
#define s second
#define pb push_back
#define all(v) v.begin(),v.end()
vpl ord;ll n;
ll ran(ll l, ll r, ll m){
    ll ans=0;
    if(r-m>m-l){
        for(ll i = m; i < r;++i){
            ans += abs(ord[i].f-ord[min(m-1,i-m+l)].f);
        }
    }
    else{
        for(ll i = l; i < m;++i){
            ans += abs(ord[i].f-ord[max(m,i-l+m)].f);
        }
    }
    return ans;
}

long long min_total_length(std::vector<int> r, std::vector<int> b) {
    for(ll i: r)ord.pb({i,1});
    for(ll i: b)ord.pb({i,0});
    sort(all(ord));
    n = ord.size();
    vll dp(n+1, 1e17);
    ll las=0, bf=0;
    dp[0]=0;
    for(ll i = 1; i < n; ++i){
        if(ord[i].s!=ord[i-1].s){bf=las;las=i;}
        if(las==bf+1){
            dp[i+1]=min(dp[i+1], min(dp[bf],dp[bf+1])+ran(bf,i+1,las));
        }
        else{
            for(ll j = bf; j < las; ++j){
                dp[i+1] = min(dp[i+1], dp[j]+ran(j,i+1, las));
            }
        }
    }
    return dp[n];
}


// int main() {
// 	int n, m;
//     assert(2 == scanf("%d %d", &n, &m));

// 	vector<int> r(n), b(m);
// 	for(int i = 0; i < n; i++)
// 		assert(1 == scanf("%d", &r[i]));
// 	for(int i = 0; i < m; i++)
// 		assert(1 == scanf("%d", &b[i]));

// 	long long res = min_total_length(r, b);
// 	printf("%lld\n", res);

// 	return 0;
// }

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...