제출 #1135996

#제출 시각아이디문제언어결과실행 시간메모리
1135996nightfal전선 연결 (IOI17_wiring)C++20
55 / 100
20 ms4924 KiB
// #include "wiring.h" #include <cassert> #include <cstdio> #include <vector> #include <climits> #include <stdlib.h> #include <algorithm> using namespace std; bool isSubtask1(const vector<int>& r, const vector<int>& b) {return r.size()<=200 && b.size()<=200;} bool isSubtask2(const vector<int>& r, const vector<int>& b) {return r[r.size()-1]<b[0];} bool isSubtask3(const vector<int>& r, const vector<int>& b) { int n=r.size(), m=b.size(); vector<int> a(n+m); for(int i=0, j=0; i<n || j<m;) i==n? a[i+j++]=1: j==m? a[j+i++]=0: r[i]<b[j]? a[j+i++]=0:a[i+j++] = 1; int cnt = 1; for(int k=1; k<n+m; k++) { cnt = a[k]==a[k-1]? cnt+1:0; if (cnt > 6) return false; } return true; } bool isSubtask4(const vector<int>& r, const vector<int>& b) { return min(r[0],b[0])==1 && max(r[r.size()-1],b[b.size()-1])==r.size()+b.size(); } long long subtask1(const vector<int>&, const vector<int>&); long long subtask2(const vector<int>&, const vector<int>&); pair<int,int> initSubtask2(const vector<int>&, const vector<int>&, vector<long long>&); long long subtask3(const vector<int>&, const vector<int>&); long long subtask4(const vector<int>&, const vector<int>&); long long fulltask(vector<int>, vector<int>); long long min_total_length(vector<int>, vector<int> ); long long min_total_length(vector<int> r, vector<int> b) { // return fulltask(r,b); if (isSubtask4(r,b)) return subtask4(r,b); else if (isSubtask3(r,b)) return subtask3(r,b); else if (isSubtask2(r,b)) return subtask2(r,b); else if (isSubtask1(r,b)) return subtask1(r,b); return 0; } long long fulltask(vector<int> r, vector<int> b) { int n = r.size(), m = b.size(); vector<pair<int,int>> a(n+m); // vector<int> val(n+m), color(n+m); for(int i=0, j=0; i<n || j<m;) // ++ is not permitted in make_pair i==n? a[i+j++]=make_pair(j,1): j==m? a[j+i++]=make_pair(i,0): r[i]<b[j]? a[j+i++]=make_pair(i,0): a[i+j++]=make_pair(j,1); // if (i==n || j<m && r[i]>b[j]) {color[i+j]=1; val[i+j]=b[j++]; } // else {val[i+j]=r[i++];} vector<long long> dp(n+m,LLONG_MAX); long long sumL=0, cntL=0, sumR=0, cntR=0, gap=0; int prev=-1, curr=0, optIdx=0; for(int i=1; i<n+m; i++) { vector<int> *left, *right; if (a[i].second != a[i-1].second) { prev=curr; curr=i; sumR=cntR=0; sumL=0; optIdx=curr-1; if (a[prev].second==0) {left=&r; right=&b;} else {left=&b; right=&r;} gap = (*right)[a[curr].first]-(*left)[a[curr-1].first]; } if (prev==-1) continue; sumR += (*right)[a[i].first]-(*right)[a[curr].first]; cntR++; for(int j=optIdx; j>=prev; j--) { cntL=curr-j; sumL += (*left)[a[curr-1].first]-(*left)[a[j].first]; if(prev==0 && j!=0) continue; long long temp = sumL + sumR + max(cntL,cntR)*gap; if (prev!=0) temp += min(dp[j-1],dp[j]); if (dp[i]>temp) {dp[i]=temp; optIdx=j;} else break; } for(int k=max(optIdx-1,prev); k<=optIdx; k++) sumL -= (*left)[a[curr-1].first]-(*left)[a[k].first]; } return dp[n+m-1]; } long long subtask4(const vector<int>& r, const vector<int>& b) { int n = r.size(), m = b.size(); vector<int> color(n+m); for(int i=0, j=0; i<n || j<m;) i==n? color[i+j++]=1: j==m? color[j+i++]=0: r[i]<b[j]? color[j+i++]=0: color[i+j++]=1; long long dp1=LLONG_MAX, dp2=LLONG_MAX; long long cntL=0, cntR=1; int startL=-1, startR=0; for(int i=1; i<n+m; i++) { if (color[i]!=color[i-1]) {startL=startR; startR=i; cntL=cntR, cntR=0;} cntR++; if(i<n+m-1 && color[i]==color[i+1]) continue; long long cntL1 = cntL/2, cntL2 = (cntL+1)/2; long long cntR1 = cntR/2, cntR2 = (cntR+1)/2; long long sumL = cntL*(cntL-1)/2, sumR = cntR*(cntR-1)/2; long long sumL1 = cntL1*(cntL1-1)/2, sumL2 = cntL2*(cntL2-1)/2; long long sumR1 = cntR1*(cntR1-1)/2, sumR2 = cntR2*(cntR2-1)/2; if(i==n+m-1) { if(startL==0) // can be omitted because it is the same as subtask2 return sumL + max(cntL,cntR) + sumR; else return min(dp1 + sumL2 + max(cntL2,cntR), dp2 + sumL1 + max(cntL1,cntR)) + sumR; } else if (color[i]!= color[i+1]) { if (startL==0) { dp1 = sumL + max(cntL,cntR1) + sumR1; dp2 = sumL + max(cntL,cntR2) + sumR2; } else { long long new_dp1 = min(dp1 + sumL2 + max(cntL2,cntR1), dp2 + sumL1 + max(cntL1,cntR1)) + sumR1; long long new_dp2 = min(dp1 + sumL2 + max(cntL2,cntR2), dp2 + sumL1 + max(cntL1,cntR2)) + sumR2; dp1 = new_dp1; dp2 = new_dp2; } } } } long long subtask3(const vector<int>& r, const vector<int>& b) { int n = r.size(), m = b.size(); vector<int> val(n+m), color(n+m); for(int i=0, j=0; i<n || j<m;) if (j==m || i<n && r[i]<b[j]) val[j+i++]=r[i]; else {color[i+j]=1; val[i+j++]=b[j];} vector<long long> dp(n+m,LLONG_MAX); int endL=0, endR=n+m-1; tie(endL,endR) = initSubtask2(val,color,dp); long long sumL=0, cntL=0, sumR=0, cntR=0, gap=0; int startL=0, startR=endL+1; for(int i=endR+1; i<n+m; i++) { if(color[i]!=color[i-1]) { startL=startR; startR=i; sumR=cntR=0; gap=val[startR]-val[startR-1]; } sumR += val[i]-val[startR]; cntR++; for(int j=startR-1; j>=startL; j--) { sumL += val[startR-1]-val[j]; cntL++; long long temp = sumL + sumR + max(cntL,cntR)*gap; temp += min(dp[j-1],dp[j]); dp[i] = min(dp[i], temp); } sumL=cntL=0; } return dp[n+m-1]; } pair<int,int> initSubtask2(const vector<int>& val, const vector<int>& color, vector<long long>& dp) { long long sum1=0, sum2=0; int n = val.size(); int endL=n-1, endR=n-1; for(int i=1; i<n; i++) if (color[i] != color[i-1]) {endL = i-1; break;} for(int i=endL+2; i<n; i++) if (color[i] != color[i-1]) {endR = i-1; break;} for(int i=0; i<endL; i++) sum1 += val[endL] - val[i]; for(int i=endL+1; i<=endR; i++) { sum2 += val[i] - val[endL+1]; dp[i] = sum1+sum2+max(endL+1,i-endL)*(val[endL+1]-val[endL]); } return {endL,endR}; } long long subtask2(const vector<int>& r, const vector<int>& b) { long long sum1=0, sum2=0, n=r.size(), m=b.size(); for(int i=0; i<n; i++) sum1 += r[n-1] - r[i]; for(int i=0; i<m; i++) sum2 += b[i] - b[0]; return sum1+sum2+max(n,m)*(b[0]-r[n-1]); } long long subtask1(const vector<int>& r, const vector<int>& b) { int n = r.size(), m = b.size(); long long dp[n][m]; dp[0][0] = abs(r[0]-b[0]); for(int i=1; i<n; i++) dp[i][0] = dp[i-1][0] + abs(r[i]-b[0]); for(int j=1; j<m; j++) dp[0][j] = dp[0][j-1] + abs(r[0]-b[j]); for(int i=1; i<n; i++) for(int j=1; j<m; j++) dp[i][j] = min(min(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1]) + abs(r[i]-b[j]); return dp[n-1][m-1]; }

컴파일 시 표준 에러 (stderr) 메시지

wiring.cpp: In function 'long long int subtask4(const std::vector<int>&, const std::vector<int>&)':
wiring.cpp:135:1: warning: control reaches end of non-void function [-Wreturn-type]
  135 | }
      | ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...