제출 #739134

#제출 시각아이디문제언어결과실행 시간메모리
739134Alihan_8Palembang Bridges (APIO15_bridge)C++17
22 / 100
37 ms2656 KiB
#include <bits/stdc++.h> using namespace std; #define all(x) x.begin(), x.end() #define pb push_back #define ln '\n' #define int long long template <class _T> bool chmin(_T &x, const _T &y){ bool flag = false; if ( x > y ){ x = y; flag |= true; } return flag; } template <class _T> bool chmax(_T &x, const _T &y){ bool flag = false; if ( x < y ){ x = y; flag |= true; } return flag; } signed main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int k, n; cin >> k >> n; if ( k == 1 ){ int C = 0; vector <int> p; for ( int i = 0; i < n; i++ ){ char a, b; int x, y; cin >> a >> x >> b >> y; if ( a == b ){ C += abs(x - y); } else{ p.pb(x), p.pb(y); } } sort(all(p)); int sz = (int)p.size(), Mn = 1e18; if ( !sz ) Mn = 0; int pref = 0, suff = accumulate(all(p), 0ll); for ( int i = 0; i < sz; i++ ){ suff -= p[i]; chmin(Mn, i * p[i] - pref + suff - (sz - i - 1) * p[i]); pref += p[i]; } cout << Mn + C + sz / 2 << ln; return 0; } int C = 0; vector <pair<int,int>> p; for ( int i = 0; i < n; i++ ){ char a, b; int x, y; cin >> a >> x >> b >> y; if ( a == b ){ C += abs(x - y); } else{ if ( x > y ) swap(x, y); p.pb({x, y}); } } auto f = [&](vector <int> p){ sort(all(p)); int sz = (int)p.size(), Mn = 1e18; if ( !sz ) Mn = 0; int pref = 0, suff = accumulate(all(p), 0ll); for ( int i = 0; i < sz; i++ ){ suff -= p[i]; chmin(Mn, i * p[i] - pref + suff - (sz - i - 1) * p[i]); pref += p[i]; } return Mn + sz / 2; }; int Mn = p.empty() ? 0 : 1e18; sort(all(p)); vector <int> v, q; for ( int i = (int)p.size() - 1; i >= 0; i-- ){ q.pb(p[i].first), q.pb(p[i].second); } for ( int i = 0; i < (int)p.size(); i++ ){ v.pb(p[i].first); v.pb(p[i].second); q.pop_back(); q.pop_back(); chmin(Mn, f(q) + f(v)); } cout << Mn + C; cout << '\n'; } /* 1 5 B 0 A 4 B 1 B 3 A 5 B 7 B 2 A 6 B 1 A 7 2 5 B 0 A 4 B 1 B 3 A 5 B 7 B 2 A 6 B 1 A 7 */
#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...