Submission #95256

#TimeUsernameProblemLanguageResultExecution timeMemory
95256andy627Wiring (IOI17_wiring)C++17
100 / 100
59 ms15736 KiB
#include "wiring.h" #include <bits/stdc++.h> #define N 200005 #define x first #define y second using namespace std; typedef long long ll; typedef pair<ll,int> P; int n,m,k; P a[N]; ll dp[N],mp[N],l[N],r[N],d[N],s[N],plc=1e5,rp,bp; bool vis[N]; ll min_total_length(vector<int> R,vector<int> B) { n=R.size(),m=B.size(),k=n+m; for(int i=1;i<=n;i++) a[i]=P(R[i-1],1); for(int i=1;i<=m;i++) a[i+n]=P(B[i-1],0); sort(a+1,a+1+k); rp=bp=-1; for(int i=1;i<=k;i++){ if(a[i].y){ rp=a[i].x; l[i]=bp; } else{ bp=a[i].x; l[i]=rp; } } rp=bp=-1; for(int i=k;i>=1;i--){ if(a[i].y){ rp=a[i].x; r[i]=bp; } else{ bp=a[i].x; r[i]=rp; } } vis[plc]=true; for(int i=1;i<=k;i++){ if(l[i]==-1) d[i]=r[i]-a[i].x; if(r[i]==-1) d[i]=a[i].x-l[i]; if(l[i]!=-1&&r[i]!=-1) d[i]=min(r[i]-a[i].x,a[i].x-l[i]); } for(int i=1;i<=k;i++){ dp[i]=dp[i-1]+d[i]; if(a[i].y){ plc++; s[i]=s[i-1]+a[i].x; } else{ plc--; s[i]=s[i-1]-a[i].x; } if(vis[plc]) dp[i]=min(dp[i],dp[mp[plc]]+abs(s[i]-s[mp[plc]])); vis[plc]=true; mp[plc]=i; } return dp[k]; }
#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...