제출 #418703

#제출 시각아이디문제언어결과실행 시간메모리
418703SlavicG전선 연결 (IOI17_wiring)C++17
100 / 100
113 ms16436 KiB
#include "bits/stdc++.h" using namespace std; #define ll long long #define forn(i,n) for(int i=0;i<n;i++) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(),v.rend() #define pb push_back #define sz(a) (int)a.size() #define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define GR(a,n,m) vector<vector<int>> a(n, vector<int>(m, 0)); ll min_total_length(vector<int> a, vector<int> b) { vector<pair<int,int>> v; set<int> s[2]; for(auto x : a){ v.pb({x, 0}); s[0].insert(x); } for(auto x: b){ v.pb({x, 1}); s[1].insert(x); } sort(all(v)); int n = sz(v); vector<ll> dp(n + 5, 1e16); dp[0] = 0; ll k = -1, sum = 0; for(int i = 0;i < n;i++){ ll mn = 1e16; if(v[i].second == 1){ int pp = upper_bound(all(a), v[i].first) - a.begin(); if(pp < sz(a))mn = min(mn, 1LL * abs(v[i].first - a[pp])); if(pp)mn = min(mn, 1LL * abs(v[i].first - a[pp-1])); }else{ int pp = upper_bound(all(b), v[i].first) - b.begin(); if(pp < sz(b))mn = min(mn, 1LL * abs(v[i].first - b[pp])); if(pp)mn = min(mn, 1LL * abs(v[i].first - b[pp-1])); } dp[i + 1] = dp[i] + mn; if(v[i].second != v[i - 1].second){ sum = v[i].first - v[i - 1].first; k = i - 1; }else if(k > 0 && v[k].second == v[k - 1].second){ --k; sum += v[i].first - v[k].first; }else k = -1; if(k >= 0)dp[i + 1] = min(dp[i + 1], dp[k] + sum); } return dp[n]; }
#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...