제출 #914645

#제출 시각아이디문제언어결과실행 시간메모리
914645tosivanmak전선 연결 (IOI17_wiring)C++17
0 / 100
279 ms70424 KiB
#include<bits/stdc++.h> using namespace std; #define ll long long struct node{ ll val; bool operator<(const node& n)const{ return val>n.val; } }; long long min_total_length(vector<int> r, vector<int> b) { r.insert(r.begin(), -1); b.insert(b.begin(),-1); vector<ll>adj[r.size()+5]; ll n=r.size()-1,m=b.size()-1; set<ll>str,stb; set<node>sttr,sttb; map<ll,ll>mp; for(int i=1;i<=n;i++){ str.insert(r[i]); sttr.insert({r[i]}); mp[r[i]]=i; } for(int i=1;i<=m;i++){ stb.insert(b[i]); sttb.insert({b[i]}); mp[b[i]]=i; } str.insert(1000000000000000000); sttr.insert({-1000000000000000000}); stb.insert(1000000000000000000); sttb.insert({-1000000000000000000}); for(int i=1;i<=n;i++){ ll lol=*stb.lower_bound(r[i]); node lol2=*sttb.lower_bound({r[i]}); ll lol3=lol2.val; if(abs(lol-r[i])>abs(lol3-r[i])){ adj[i].push_back(mp[lol3]); } else if(abs(lol-r[i])<abs(lol3-r[i])){ adj[i].push_back(mp[lol]); } else{ adj[i].push_back(mp[lol]); adj[i].push_back(mp[lol3]); } } for(int i=1;i<=m;i++){ ll lol=*str.lower_bound(b[i]); node lol2=*sttr.lower_bound({b[i]}); ll lol3=lol2.val; if(abs(lol-b[i])>abs(lol3-b[i])){ adj[mp[lol3]].push_back(i); } else if(abs(lol-b[i])<abs(lol3-b[i])){ adj[mp[lol]].push_back(i); } else{ adj[mp[lol]].push_back(i); adj[mp[lol3]].push_back(i); } } for(int i=1;i<=n;i++){ sort(adj[i].begin(),adj[i].end()); } map<ll,ll>dp[n+5]; for(int i=1;i<=n;i++){ for(auto& u: adj[i]){ dp[i][u]=1000000000000000000; dp[i-1][u]=1000000000000000000; dp[i][u-1]=1000000000000000000; dp[i-1][u-1]=1000000000000000000; } } dp[0][0]=0; for(int i=1;i<=n;i++){ for(auto& u: adj[i]){ // cout<<u<<" "; if(i==1&&u==1){ dp[i][u]=abs(r[1]-b[1]); } else{ dp[i][u]=min({dp[i-1][u],dp[i][u-1],dp[i-1][u-1]})+abs(r[i]-b[u]); } } // cout<<'\n'; } return dp[n][m]; // return 1; } #ifdef LOCAL int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n,m; cin>>n>>m; vector<int>v,v2; for(int i=1;i<=n;i++){ ll x; cin>>x; v.push_back(x); } for(int i=1;i<=m;i++){ ll x; cin>>x; v2.push_back(x); } cout<<min_total_length(v,v2)<<'\n'; } #endif
#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...