이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "wiring.h"
#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> ii;
vector<int> x;
vector<int> ezreal;
vector<int> prv;
vector< vector< ll > > pref;
vector< vector< ll > > dp;
vector< vector<int> > buck;
long long min_total_length(std::vector<int> r, std::vector<int> b)
{
int ir, ib; ir = ib = 0;
while(ir< r.size() && ib< b.size())
{
if(r[ir]< b[ib])
{
x.pb(r[ir]); ezreal.pb(0);
ir++;
}
else
{
x.pb(b[ib]); ezreal.pb(1);
ib++;
}
}
while(ir< r.size()) x.pb(r[ir++]), ezreal.pb(0);
while(ib< b.size()) x.pb(b[ib++]), ezreal.pb(1);
ll last = -1e18;
int n = ezreal.size();
for(int i = 0; i< n; i++)
{
if(i && ezreal[i] != ezreal[i-1]) last = x[i-1];
prv.pb(last);
}
pref.pb(vector<ll>());
buck.pb(vector<int>());
for(int i = 0; i< n; i++)
{
if(i && ezreal[i] != ezreal[i-1])
{
pref.pb(vector<ll>());
buck.pb(vector<int>());
}
pref.back().pb(pref.back().empty()?x[i]:(pref.back().back()+x[i]));
buck.back().pb(x[i]);
}
dp.resize(pref.size());
int comps = pref.size();
for(int i = 0; i< (int) dp.size(); i++)
{
dp[i].resize(pref[i].size()+1);
}
for(int i = comps-1; i>= 0; i--)
{
if(i == comps-1) dp[i][pref[i].size()] = 0;
else dp[i][pref[i].size()] = dp[i+1][0];
//printf("%d\n", i);
for(int j = pref[i].size()-1; j>= 0; j--)
{
//printf("dp[%d][%d]\n", i, j);
int numleft = pref[i].size()-j;
//printf("numleft = %d ((%d))->(%d)\n", numleft, pref[i].size()-numleft-1, pref[i][pref[i].size()-1]);
ll sumhere = pref[i][pref[i].size()-1]-(((int)pref[i].size()-numleft-1>= 0)?pref[i][pref[i].size()-numleft-1]:0ll);
ll sumnext;
if(i+1< comps)
{
if(numleft-1< pref[i+1].size()) sumnext = pref[i+1][numleft-1];
else sumnext = pref[i+1].back() + pref[i+1][0]*(numleft-pref[i+1].size());
}
else sumnext = 1e18;
//printf("sumnext %lld sumhere %lld\n", sumnext, sumhere);
ll prev;
if(i) prev = buck[i-1].back();
else prev = -1e18;
ll nxt;
if(i+1< comps) nxt = buck[i+1][0];
else nxt = 1e18;
dp[i][j] = min(i+1< comps?dp[i+1][min(numleft, (int)pref[i+1].size())]+sumnext-sumhere:(ll)1e18, dp[i][j+1]+buck[i][j]-prev);
if(i+1< comps) dp[i][j] = min(dp[i][j], 1LL*numleft*nxt-sumhere+dp[i+1][1]);
if(i+1< comps) dp[i][j] = min(dp[i][j], dp[i][j+1]+buck[i+1][0]-buck[i][j]);
//rintf("dp[%d][%d] = %lld\n", i, j, dp[i][j]);
}
}
ll best = dp[0][0];
ll sum = pref[0].back();
int num = pref[0].size();
for(int i = 1; i<= (int) pref[1].size(); i++)
{
ll R = pref[1][i-1];
ll L = pref[0].back();
int diff = abs(i-num);
if(i> num) L += 1LL*buck[0].back()*diff;
else R += 1LL*buck[1][0]*diff;
ll here = R-L+dp[1][i];
//printf("%d: %lld %lld\n", i, L, R);
best = min(best, here);
}
return best;
}
컴파일 시 표준 에러 (stderr) 메시지
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:19:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ir< r.size() && ib< b.size())
~~^~~~~~~~~~
wiring.cpp:19:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ir< r.size() && ib< b.size())
~~^~~~~~~~~~
wiring.cpp:32:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ir< r.size()) x.pb(r[ir++]), ezreal.pb(0);
~~^~~~~~~~~~
wiring.cpp:33:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ib< b.size()) x.pb(b[ib++]), ezreal.pb(1);
~~^~~~~~~~~~
wiring.cpp:73:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(numleft-1< pref[i+1].size()) sumnext = pref[i+1][numleft-1];
~~~~~~~~~^~~~~~~~~~~~~~~~~~
wiring.cpp:91:8: warning: unused variable 'sum' [-Wunused-variable]
ll sum = pref[0].back();
^~~
# | 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... |