Submission #900065

#TimeUsernameProblemLanguageResultExecution timeMemory
900065nightfalWiring (IOI17_wiring)C++14
100 / 100
25 ms7852 KiB
// #include "wiring.h" #include <cassert> #include <cstdio> #include <vector> #include <climits> #include <stdlib.h> #include <algorithm> using namespace std; bool isSubtask1(std::vector<int> r, std::vector<int> b) {return r.size()<=200 && b.size()<=200;} bool isSubtask2(std::vector<int> r, std::vector<int> b) {return r[r.size()-1]<b[0];} bool isSubtask3(std::vector<int> r, std::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(std::vector<int> r, std::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(std::vector<int>, std::vector<int>); long long subtask2(std::vector<int> &, std::vector<int> &, int, int, int, int); long long subtask3(std::vector<int>, std::vector<int>); long long subtask4(std::vector<int>, std::vector<int>); long long fulltask(std::vector<int>, std::vector<int>); long long min_total_length(std::vector<int>, std::vector<int> ); long long min_total_length(std::vector<int> r, std::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,0,r.size()-1,0,b.size()-1); // else if (isSubtask1(r,b)) return subtask1(r,b); return 0; } long long fulltask(std::vector<int> r, std::vector<int> b) { int n = r.size(), m = b.size(); vector<pair<int,int>> a(n+m); for(int i=0, j=0; i<n || j<m;) 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); 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]; // gap= a[prev].second==0? b[a[curr].first]-r[a[curr-1].first]: r[a[curr].first]-b[a[curr-1].first]; } if (prev==-1) continue; sumR += a[curr].second==0? r[a[i].first]-r[a[curr].first]:b[a[i].first]-b[a[curr].first]; cntR++; for(int j=optIdx; j>=prev; j--) { cntL=curr-j; sumL += a[prev].second==0? r[a[curr-1].first]-r[a[j].first]:b[a[curr-1].first]-b[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 -= a[prev].second==0? r[a[curr-1].first]-r[a[k].first]:b[a[curr-1].first]-b[a[k].first]; } return dp[n+m-1]; } long long subtask4(std::vector<int> r, std::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; long long dp1=LLONG_MAX, dp2=LLONG_MAX; long long cntL=0, cntR=1; int prev=-1, curr=0; for(int i=1; i<n+m; i++) { if (a[i]!=a[i-1]) {prev=curr; curr=i; cntL=cntR, cntR=0;} cntR++; 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(prev==0) // subtask2와 동일하므로 생략가능 return sumL + max(cntL,cntR) + sumR; else return min(dp1 + sumL2 + max(cntL2,cntR), dp2 + sumL1 + max(cntL1,cntR)) + sumR; } else if (a[i]!= a[i+1]) { if (prev==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(std::vector<int> r, std::vector<int> b) { int n = r.size(), m = b.size(); vector<pair<int,int>> a(n+m); for(int i=0, j=0; i<n || j<m;) // 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); 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; for(int i=1; i<n+m; i++) { if (a[i].second != a[i-1].second) { prev=curr; curr=i; sumR=cntR=0; gap= a[prev].second==0? b[a[curr].first]-r[a[curr-1].first]: r[a[curr].first]-b[a[curr-1].first]; } if (prev==-1) continue; sumR += a[curr].second==0? r[a[i].first]-r[a[curr].first]:b[a[i].first]-b[a[curr].first]; cntR++; for(int j=curr-1; j>=prev; j--) { cntL++; sumL += a[prev].second==0? r[a[curr-1].first]-r[a[j].first]:b[a[curr-1].first]-b[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]); dp[i] = min(dp[i], temp); } sumL=cntL=0; } return dp[n+m-1]; } long long subtask2(std::vector<int> &arr1, std::vector<int> &arr2, int s1, int e1, int s2, int e2) { long long sum1=0, sum2=0, n = e1-s1+1, m=e2-s2+1; for(int i=s1; i<=e1; i++) sum1 += arr1[e1] - arr1[i]; for(int i=s2; i<=e2; i++) sum2 += arr2[i] - arr2[s2]; return sum1+sum2+max(n,m)*(arr2[s2]-arr1[e1]); } long long subtask1(std::vector<int> r, std::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]; }

Compilation message (stderr)

wiring.cpp: In function 'bool isSubtask4(std::vector<int>, std::vector<int>)':
wiring.cpp:28:65: warning: comparison of integer expressions of different signedness: 'const int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |     return min(r[0],b[0])==1 && max(r[r.size()-1],b[b.size()-1])==r.size()+b.size();
      |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
wiring.cpp: In function 'long long int subtask1(std::vector<int>, std::vector<int>)':
wiring.cpp:182:7: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  182 |       for(int j=1; j<m; j++)
      |       ^~~
wiring.cpp:185:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  185 |  return dp[n-1][m-1];
      |  ^~~~~~
wiring.cpp: In function 'long long int subtask4(std::vector<int>, std::vector<int>)':
wiring.cpp:93:22: warning: control reaches end of non-void function [-Wreturn-type]
   93 |     vector<int> a(n+m);
      |                      ^
#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...