이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define MAXN 200007
using namespace std;
const long long inf=1e17;
int n,m,last[2],start,cntt;
pair<long long,int> p[MAXN];
long long dp[MAXN],curr,len;
int pref[MAXN][2];
long long sum[MAXN][2],prefmin;
int pr[MAXN][2],nxt[MAXN][2];
multiset<long long> s;
long long cnt(int l,int r,int id){
return pref[r][id]-pref[l-1][id];
}
long long getsum(int l,int r,int id){
return sum[r][id]-sum[l-1][id];
}
long long mins(){
return *s.begin();
}
void precalc(){
for(int i=1;i<=n+m;i++){
if(p[i].second==0){
pref[i][0]++; sum[i][0]+=p[i].first;
}else{
pref[i][1]++; sum[i][1]+=p[i].first;
}
pref[i][0]+=pref[i-1][0];
pref[i][1]+=pref[i-1][1];
sum[i][0]+=sum[i-1][0];
sum[i][1]+=sum[i-1][1];
}
last[0]=last[1]=0;
for(int i=1;i<=n+m;i++){
pr[i][0]=last[0]; pr[i][1]=last[1];
if(p[i].second==0)last[0]=i;
else last[1]=i;
}
last[0]=last[1]=n+m+1;
for(int i=n+m;i>=1;i--){
nxt[i][0]=last[0]; nxt[i][1]=last[1];
if(p[i].second==0)last[0]=i;
else last[1]=i;
}
}
long long min_total_length(vector<int> r,vector<int> b){
double st=clock();
n=int(r.size()); m=int(b.size());
for(int i=0;i<r.size();i++){
p[i+1]={r[i],0};
}
for(int i=0;i<b.size();i++){
p[r.size()+i+1]={b[i],1};
}
sort(p+1,p+n+m+1);
precalc();
for(int i=1;i<=n+m;i++){
if(p[i].second==0)start=pr[i][1];
else start=pr[i][0];
len=i-start;
if(len==1){
prefmin=inf; s.clear();
for(int f=start;f>=1 and p[f].second==p[start].second;f--){
curr=start-f+1;
s.insert( min(dp[f-1],dp[f])-getsum(f,start,p[start].second)+curr*p[start+1].first );
}
}
dp[i]=inf;
prefmin-=p[start].first;
if(len<=start and nxt[start-len+1][p[i].second]==start+1 and p[start-len+1].second==p[start].second){
prefmin=min(prefmin,dp[start-len]-getsum(start-len+1,start,p[start].second));
prefmin=min(prefmin,dp[start-len+1]-getsum(start-len+1,start,p[start].second));
s.erase( s.find( min(dp[start-len+1],dp[start-len])-getsum(start-len+1,start,p[start].second)+len*p[start+1].first ) );
}
dp[i]=min(dp[i], getsum(start+1,i,p[i].second) + prefmin);
if(!s.empty())dp[i]=min(dp[i], getsum(start+1,i,p[i].second) + mins() - len*p[start+1].first);
}
return dp[n+m];
}
/*
int main(){
cout<<min_total_length({1, 2, 3, 7}, {0, 4, 5, 9, 10})<<"\n";
}
*/
컴파일 시 표준 에러 (stderr) 메시지
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:64:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for(int i=0;i<r.size();i++){
| ~^~~~~~~~~
wiring.cpp:67:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(int i=0;i<b.size();i++){
| ~^~~~~~~~~
wiring.cpp:61:12: warning: unused variable 'st' [-Wunused-variable]
61 | double st=clock();
| ^~
# | 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... |