이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include "wiring.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%I64d", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%I64d\n", (n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const ll INF = 1e18 + 7;
const int MAXN = 4e5 + 5;
const double eps = 1e-9;
int n, m;
ll dp[2005][2005];
ll dp2[MAXN], sA[MAXN], sB[MAXN];
pii a[MAXN];
int vis[MAXN], tmp[MAXN];
long long min_total_length(vi r, vi b) {
n = r.size(), m = b.size();
if (n <= 2000 && m <= 2000) {
for (int i = 1; i <= 2005; i++)
dp[0][i] = INF, dp[i][0] = INF;
dp[0][0] = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
dp[i][j] = min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + abs(r[i - 1] - b[j - 1]);
return dp[n][m];
} else if (r.back() < b.front()) {
ll ret = 0;
for (int i = 0; i < n; i++)
ret += r[n - 1] - r[i];
for (int j = 0; j < m; j++)
ret += b[j] - b[0];
ret -= max(n, m) * ((ll) r.back() - (ll) b.front());
return ret;
}
for (int i = 0; i < n; i++)
a[i+1] = {r[i], -1}, vis[i+1] = -1, tmp[i+1] = -1;
for (int j = 0; j < m; j++)
a[j+1 + n] = {b[j], 1}, vis[j+1 + n] = -1, tmp[j+1 + n] = -1;
for (int i = n + m + 1; i <= 2 * (n + m); i++)
tmp[i] = vis[i] = -1;
sort(a + 1, a + 1 + n + m);
int cur = n + m;
vis[n + m] = 0;
for (int i = 1; i <= n + m; i++) {
sA[i] = sA[i - 1], sB[i] = sB[i - 1];
if (a[i].se == -1)
sA[i] += a[i].fi, cur++;
else
sB[i] += a[i].fi, cur--;
if (vis[cur] >= 0)
tmp[i] = vis[cur];
vis[cur] = i;
}
for (int i = 1; i <= n + m; i++) {
int ret = 2e9;
if (a[i].se == -1) {
auto it = lower_bound(b.begin(), b.end(), a[i].fi);
if (it != b.end())
ret = min(ret, *it - a[i].fi);
if (it != b.begin())
ret = min(ret, a[i].fi - *(--it));
} else {
auto it = lower_bound(r.begin(), r.end(), a[i].fi);
if (it != r.end())
ret = min(ret, *it - a[i].fi);
if (it != r.begin())
ret = min(ret, a[i].fi - *(--it));
}
dp2[i] = dp2[i - 1] + ret;
if (tmp[i] != -1)
dp2[i] = min(dp2[i], dp2[tmp[i]] + abs(sA[i] - sA[tmp[i]] - sB[i] + sB[tmp[i]]));
}
return dp2[n + m];
}
컴파일 시 표준 에러 (stderr) 메시지
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:41:22: warning: iteration 2004 invokes undefined behavior [-Waggressive-loop-optimizations]
dp[0][i] = INF, dp[i][0] = INF;
~~~~~~~~~^~~~~
wiring.cpp:40:27: note: within this loop
for (int i = 1; i <= 2005; i++)
~~^~~~~~~
# | 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... |