이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "wiring.h"
#include <vector>
using namespace std;
using llong = long long;
const int maxN = 200000;
llong Solve1(vector<int> r, vector<int> b)
{
if (r.back() > b[0]) return -1;
llong ans = 0;
for (int i: r)
ans += b[0] - i;
for (int i: b)
ans += i - r.back();
ans -= (llong)min(r.size(), b.size()) * llong(b[0] - r.back());
return ans;
}
llong Solve2(vector<int> r, vector<int> b)
{
int n = r.size() + b.size();
vector<char> a(n);
for (int i: r)
if (1 <= i || i <= n) a[i - 1] = 'x';
else return -1;
for (int i: b)
if (1 <= i || i <= n) a[i - 1] = 'o';
else return -1;
vector<int> lone(n);
for (int i = 0; i < n; ++i)
{
int j = i;
while (j + 1 < n && a[j + 1] == a[i]) ++j;
for (int k = i; k <= j; ++k)
lone[k] = min(i ? k - (i - 1) : maxN, j + 1 < n ? j + 1 - k : maxN);
i = j;
}
vector<int> mirror(n, -1);
for (int i = 1; i < n; ++i)
if (a[i] != a[i - 1])
for (int r = i, l = i - 1; 0 <= l && r < n && a[l] == a[i - 1] && a[r] == a[i]; --l, ++r)
mirror[r] = l;
vector<llong> dp(n);
dp[0] = lone[0];
for (int i = 1; i < n; ++i)
{
dp[i] = dp[i - 1] + lone[i];
if (mirror[i] == -1) continue;
llong curdp = 0;
curdp += dp[mirror[i] - 1];
int x = (i - mirror[i] + 1) / 2;
curdp += (x * (x + 1LL)) / 2LL;
curdp += (x * (x - 1LL)) / 2LL;
dp[i] = min(dp[i], curdp);
}
return dp[n - 1];
}
llong min_total_length(vector<int> r, vector<int> b)
{
llong res = Solve1(r, b);
if (res != -1) return res;
res = Solve2(r, b);
if (res != -1) return res;
return -1;
}
# | 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... |