이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "wiring.h"
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define X first
#define Y second
#define pb push_back
void minn(int &a,int b){
if(b<a) a=b;
}
void maxx(int &a,int b){
if(b<a) a=b;
}
using ll =long long;
const int MAX=2e5+2,inf=2e9+4;
const ll oo=1e16;
using ll =long long;
int dp[2][MAX];
int F[MAX],D[MAX];
#undef int
long long min_total_length(std::vector<int> r, std::vector<int> b)
#define int long long
{
int n=r.size(),m=b.size();
r.pb(inf);b.pb(inf);
bool tt=(r[0]>b[0]);
int ans=0;
int u=0,v=0;
int cn=0;
memset(F,0x3f,sizeof F);
memset(dp,0x3f,sizeof dp);
dp[!tt][0]=0;
while(u!=n || v!=m){
if(tt){
int st=v;
while(b[v]<r[u]) v++;
int pre;
if(u==0) pre=-inf;
else pre=r[u-1];
int res=0,pl=b[st]-pre;
F[cn+1]=oo;
for(int i=cn;i>=0;i--)
F[i]=min(F[i+1]+pl,dp[!tt][i]);
D[0]=dp[!tt][0];
for(int i=1;i<=cn;i++)
D[i]=min(D[i-1],dp[!tt][i]);
for(int i=st;i<=v;i++){
dp[tt][v-i]=res+min(F[i-st],D[min(i-st,cn)]);
res+=b[i]-pre;
}
res=0;
for(int i=v-1;i>=st;i--){
res+=b[v-1]-b[i];
dp[tt][v-i]+=res;
}
for(int i=v-st;i<=cn;i++) F[i]=D[i]=oo;
cn=v-st;
}
else{
int st=u;
while(r[u]<b[v]) u++;
int pre;
if(v==0) pre=-inf;
else pre=b[v-1];
int res=0,pl=r[st]-pre;
F[cn+1]=oo;
for(int i=cn;i>=0;i--)
F[i]=min(F[i+1]+pl,dp[!tt][i]);
D[0]=dp[!tt][0];
for(int i=1;i<=cn;i++)
D[i]=min(D[i-1],dp[!tt][i]);
for(int i=st;i<=u;i++){
dp[tt][u-i]=res+min(F[i-st],D[min(i-st,cn)]);
res+=r[i]-pre;
}
res=0;
for(int i=u-1;i>=st;i--){
res+=r[u-1]-r[i];
dp[tt][u-i]+=res;
}
for(int i=u-st;i<=cn;i++) F[i]=D[i]=oo;
cn=u-st;
}
//cout<<"ST "<<tt<<' '<<cn<<' '<<dp[tt][cn]<<'\n';
//for(int i=0;i<=cn;i++) cout<<dp[tt][i]<<' ';cout<<'\n';
tt^=1;
}
ans=dp[!tt][0];
return ans;
}
# | 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... |