제출 #408806

#제출 시각아이디문제언어결과실행 시간메모리
408806Blistering_BarnaclesPalembang Bridges (APIO15_bridge)C++11
100 / 100
100 ms20596 KiB
//apig's property //Happiness can be found, even in the darkest of times, if one only remembers to turn on the light //El Pueblo Unido Jamas Sera Vencido //The saddest thing about betrayal? is that it never comes from your enemies //Do or do not... there is no try //Billions of bilious blue blistering barnacles in a thundering typhoon! #include<bits/stdc++.h> #define fast ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) #define F first #define S second #define pb push_back #define vll vector< ll > #define vi vector< int > #define pll pair< ll , ll > #define pi pair< int , int > #define all(s) s.begin() , s.end() #define sz(s) s.size() #define str string #define md ((s + e) / 2) #define mid ((l + r) / 2) #define msdp(dp) memset(dp , -1 , sizeof dp) #define mscl(dp) memset(dp , 0 , sizeof dp) #define C continue //#define R return #define B break #define lx node * 2 #define rx node * 2 + 1 #define br(o) o ; break #define co(o) o ; continue using namespace std; typedef long long ll; ll q, dp[105][100005], a[555555] , b[555555], k, l, m, n, o, p; map < ll , ll > mp; vll adj[555555]; const ll mod = 1e9+7; str s; //2 8 12 18 pll op[555555] ; ll pre[555555] , suf[555555] , Lsum , Rsum ; priority_queue < ll > L , R ; ll cmp(const pll &a , const pll &b){ return a.F + a.S < b.S + b.F ; } void solve(){ ll ans = 0 ; cin >> k >> n ; for(ll i = 1 ; i <= n; i++){ char o1 , o2 ; cin >> o1 >> o >> o2 >> p ; if(p < o)swap(o , p) ; if(o1 == o2)ans += p - o ; else op[++m] = {o , p} ; } if(!m)return void(cout << ans << endl) ; sort(op + 1 , op + m + 1 , cmp) ; for(ll i = 1 ; i <= m ; i++){ ll o1 = op[i].F , o2 = op[i].S ; //cout << op[i].F << " " << op[i].S << endl ; if(L.empty()){ L.push(o1) , Lsum += o1; R.push(-o2) , Rsum += o2; } else { ll mx = L.top(); ll mn = -R.top(); if(o2 <= mx){ L.pop(), Lsum -= mx; L.push(o1) , Lsum += o1; L.push(o2) , Lsum += o2; R.push(-mx) , Rsum += mx; } else if(o1 >= mn){ R.pop(), Rsum -= mn; R.push(-o1), Rsum += o1; R.push(-o2), Rsum += o2; L.push(mn), Lsum += mn; } else{ L.push(o1), Lsum += o1; R.push(-o2), Rsum += o2; } } pre[i] = Rsum - Lsum ; } while(sz(L))L.pop() ; while(sz(R))R.pop() ; Lsum = Rsum = 0 ; for(ll i = m ; i >= 1 ; i--){ ll o1 = op[i].F , o2 = op[i].S ; //cout << op[i].F << " " << op[i].S << endl ; if(L.empty()){ L.push(o1) , Lsum += o1; R.push(-o2) , Rsum += o2; } else { ll mx = L.top(); ll mn = -R.top(); if(o2 <= mx){ L.pop(), Lsum -= mx; L.push(o1) , Lsum += o1; L.push(o2) , Lsum += o2; R.push(-mx) , Rsum += mx; } else if(o1 >= mn){ R.pop(), Rsum -= mn; R.push(-o1), Rsum += o1; R.push(-o2), Rsum += o2; L.push(mn), Lsum += mn; } else{ L.push(o1), Lsum += o1; R.push(-o2), Rsum += o2; } } suf[i] = Rsum - Lsum ; //cout << suf[i] << " " ; } ll ans2 = pre[m] ; //cout << ans2 << endl ; if(k - 1){ for(ll i = 1 ; i <= m ; i++){ ans2 = min(ans2 , pre[i] + suf[i + 1]) ; } } cout << m + ans + ans2 << endl ; } /* 2 5 B 0 A 4 B 1 B 3 A 5 B 7 B 2 A 6 B 1 A 7 */ int main(){ fast ; //cin >> q; q = 1; while(q--){ solve(); } }
#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...