이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "wiring.h"
//#include "grader.cpp"
#include <bits/stdc++.h>
#define mk make_pair
#define sc second
#define fr first
#define pb push_back
using namespace std;
const int N = (1e6 + 5);
const long long inf = (1e18 + 7);
const long long M = (1e10 + 7);
long long dp[N];
long long lastr,lastb;
long long cn[N];
vector <pair<long long,long long> > vec;
deque <int> red,blue;
long long min_total_length(vector<int> r, vector<int> b) {
int n = r.size();
int m = b.size();
vec.pb(mk(-M,-1));
for (int i = 0; i < r.size(); i ++)
vec.pb(mk(r[i],0));
for (int i = 0; i < b.size(); i ++)
vec.pb(mk(b[i],1));
sort (vec.begin(),vec.end());
for (int i = 1; i <= n + m; i ++) dp[i] = inf;
dp[0] = 0;
lastr = lastb = -1;
for (int i = 1; i <= n + m; i ++) {
long long val = vec[i].fr,tp = vec[i].sc;
long long sum = 0;
if (tp == 0) {
if (lastr == i - 1) {
if (cn[i - 1] >= 1) dp[i] = min(dp[i],dp[i - 1] + abs(val - vec[i - 1].fr)),cn[i] = max(0ll,cn[i - 1] - 1);
else {
if (lastb != -1) dp[i] = min(dp[i],dp[i - 1] + abs(val - vec[lastb].fr));
}
}
else {
for (int j = i - 1; j >= 1; j --) {
if (vec[j].sc == 0) break;
sum += vec[j].fr;
if (dp[i] >= dp[j - 1] + abs(val * (i - j) - sum))
dp[i] = dp[j - 1] + abs(val * (i - j) - sum),cn[i] ++;
}
cn[i] --;
}
lastr = i;
}
else {
if (lastb == i - 1) {
if (cn[i - 1] >= 1) dp[i] = min(dp[i],dp[i - 1] + abs(val - vec[i - 1].fr)),cn[i] = max(0ll,cn[i - 1] - 1);
else {
if (lastr != -1) dp[i] = min(dp[i],dp[i - 1] + abs(val - vec[lastr].fr));
}
}
else {
for (int j = i - 1; j >= 1; j --) {
if (vec[j].sc == 1) break;
sum += vec[j].fr;
if (dp[i] >= dp[j - 1] + abs(val * (i - j) - sum))
dp[i] = dp[j - 1] + abs(val * (i - j) - sum),cn[i] ++;
}
cn[i] --;
}
lastb = i;
}
}
return dp[n + m];
}
컴파일 시 표준 에러 (stderr) 메시지
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:29:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < r.size(); i ++)
~~^~~~~~~~~~
wiring.cpp:32:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < b.size(); 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... |