Submission #1357745

#TimeUsernameProblemLanguageResultExecution timeMemory
1357745altern23Wiring (IOI17_wiring)C++20
0 / 100
1096 ms6676 KiB
#include "wiring.h"
#include <bits/stdc++.h>
using namespace std;

#define ll long long

long long min_total_length(vector<int> R, vector<int> B) {
	ll N = R.size(), M = B.size();
      vector <pair<ll, ll>> polls(1);
      for (int i = 0; i < N; i++) {
            polls.push_back({R[i], 0});
      }
      for (int i = 0; i < M; i++) {
            polls.push_back({B[i], 1});
      }
      sort(polls.begin(), polls.end());

      ll K = N+M;
      vector <ll> dp(K+5, 1e9);
      dp[0] = 0;

      auto Matching = [&](ll l, ll r) {
            ll ptr = 0, len = 0;
            ll ret = 0;
            for (int j = l; j <= r; j++) {
                  ret += (polls[j].second == polls[l].second ? -1 : 1)*polls[j].first;
            }
            for (int j = l; j < r; j++) {
                  len++;
                  if (polls[j].second != polls[j+1].second) {
                        ptr = j;
                        break;
                  }
            }
            ll lf = len, rg = (r-l+1)-len;
            if (lf < rg) ret -= polls[ptr].first*(rg-lf);
            else ret += polls[ptr+1].first*(lf-rg);
            return ret;
      };
      
      for (int i = 1; i <= K; i++) {
            bool z = 0;
            ll ptr = 0;
            for (int j = i-1; j >= 1; --j) {
                  if (polls[i].second == polls[j].second) {
                        if (z) break;
                  }
                  else {
                        z = 1;
                        dp[i] = min(dp[i], dp[j-1]+Matching(j, i));
                  }
            }
      }

      return dp[K];
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...