제출 #764925

#제출 시각아이디문제언어결과실행 시간메모리
764925marvinthang전선 연결 (IOI17_wiring)C++17
100 / 100
36 ms7100 KiB
/*************************************
*    author: marvinthang             *
*    created: 24.06.2023 11:57:30    *
*************************************/

#include "wiring.h"
#include <bits/stdc++.h>

using namespace std;

#define                  fi  first
#define                  se  second
#define                left  ___left
#define               right  ___right
#define                TIME  (1.0 * clock() / CLOCKS_PER_SEC)
#define             MASK(i)  (1LL << (i))
#define           BIT(x, i)  ((x) >> (i) & 1)
#define  __builtin_popcount  __builtin_popcountll
#define              ALL(v)  (v).begin(), (v).end()
#define           REP(i, n)  for (int i = 0, _n = (n); i < _n; ++i)
#define          REPD(i, n)  for (int i = (n); i--; )
#define        FOR(i, a, b)  for (int i = (a), _b = (b); i < _b; ++i) 
#define       FORD(i, b, a)  for (int i = (b), _a = (a); --i >= _a; ) 
#define       FORE(i, a, b)  for (int i = (a), _b = (b); i <= _b; ++i) 
#define      FORDE(i, b, a)  for (int i = (b), _a = (a); i >= _a; --i) 
#define        scan_op(...)  istream & operator >> (istream &in, __VA_ARGS__ &u)
#define       print_op(...)  ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
    #include "debug.h"
#else
    #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
    #define DB(...) 23
    #define db(...) 23
    #define debug(...) 23
#endif

template <class U, class V> scan_op(pair <U, V>)  { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>)  { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>)  { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")";  else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
template <class A, class B> bool minimize(A &a, B b)  { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b)  { if (a < b) { a = b; return true; } return false; }

// end of template

const int INF = 1e9 + 7;

long long min_total_length(vector <int> r, vector <int> b) {
	vector <pair <int, int>> p;
	p.emplace_back(-INF, 0);
	for (int x: r) p.emplace_back(x, 0);
	for (int x: b) p.emplace_back(x, 1);
	int n = (int) p.size() - 1;
	sort(ALL(p));
	p[0].se = p[1].se ^ 1;
	vector <long long> dp(n + 1, 1e18);
	dp[0] = 0;
	for (int cur = 1, nxt = 1, prv = 0; cur <= n; ) {
		while (nxt <= n && p[nxt].se == p[cur].se) ++nxt;
		FOR(i, prv, cur) minimize(dp[i], dp[i - 1] + p[cur].fi - p[i].fi);
		long long s = 0;
		for (int i = cur, j = cur - 1; i < nxt && j >= prv; ++i, --j) {
			s += p[i].fi - p[j].fi;
			minimize(dp[i], (j ? dp[j - 1] : 0) + s);
		}
		FOR(i, cur, nxt) minimize(dp[i], dp[i - 1] + p[i].fi - p[cur - 1].fi);
		prv = cur;
		cur = nxt;
	}
	return dp[n];
}

#ifdef LOCAL
#include "wiring.h"

#include <cassert>
#include <cstdio>

using namespace std;

int main() {
	file("wiring");
	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;
}
#endif
#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...