제출 #67795

#제출 시각아이디문제언어결과실행 시간메모리
67795Just_Solve_The_Problem전선 연결 (IOI17_wiring)C++11
100 / 100
89 ms75352 KiB
#include <bits/stdc++.h>
#include "wiring.h"
//#include "grader.cpp"

#define pii pair < int, int >
#define fr first
#define sc second
#define ll long long
#define pb push_back
#define sz(s) (int)s.size()

using namespace std;

const int N = (int)2e5 + 7;
const ll linf = (ll)1e18 + 7;

pii a[N];
int n, m;
ll dp[N];
int last[N];
ll pref[N];

ll min_total_length(vector<int> R, vector<int> B) {
  vector < ll > r, b;
  n = sz(R);
  m = sz(B);
  for (int i = 0; i < n; i++) r.pb(R[i]);
  for (int i = 0; i < m; i++) b.pb(B[i]);
  int i, j;
  i = j = 0;
  while (i < n && j < m) {
    if (r[i] < b[j]) {
      a[i + j + 1] = {r[i], 1};
      i++;
    } else {
      a[i + j + 1] = {b[j], 0};
      j++;
    }
  }
  while (i < n) a[i + j + 1] = {r[i], 1}, i++;
  while (j < m) a[i + j + 1] = {b[j], 0}, j++;
  for (int i = 1; i <= n + m; i++)
    dp[i] = linf;
  memset(last, -1, sizeof last);
  int balance = 1e5;
  last[balance] = 0;
  j = 0;
  int q = 0;
  ll lastred, lastblue;
  lastred = lastblue = -linf;
  b.pb(linf);
  r.pb(linf);
  for (int i = 1; i <= n + m; i++) {
    ll x = a[i].fr;
    ll k = a[i].sc;
    balance += (k + k - 1);
    pref[i] = pref[i - 1] + ((k == 1) ? -x : x);
    if (k == 1) {
      while (j < m && b[j] < x) j++;
      dp[i] = dp[i - 1] + min(b[j] - x, x - lastblue);
    } else {
      while (q < n && r[q] < x) q++;
      dp[i] = dp[i - 1] + min(r[q] - x, x - lastred);
    }
    if (last[balance] != -1) {
      dp[i] = min(dp[i], dp[last[balance]] + abs(pref[i] - pref[last[balance]]));
    }
    if (k == 1) lastred = x;
    else lastblue = x;
    last[balance] = i;
  }
  return dp[n + m];
}
#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...