답안 #408806

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
408806 2021-05-19T16:36:17 Z Blistering_Barnacles Palembang Bridges (APIO15_bridge) C++11
100 / 100
100 ms 20596 KB
//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();
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 13260 KB Output is correct
2 Correct 8 ms 13272 KB Output is correct
3 Correct 8 ms 13416 KB Output is correct
4 Correct 9 ms 13388 KB Output is correct
5 Correct 9 ms 13436 KB Output is correct
6 Correct 8 ms 13388 KB Output is correct
7 Correct 9 ms 13388 KB Output is correct
8 Correct 8 ms 13388 KB Output is correct
9 Correct 8 ms 13388 KB Output is correct
10 Correct 8 ms 13388 KB Output is correct
11 Correct 9 ms 13388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 13260 KB Output is correct
2 Correct 8 ms 13336 KB Output is correct
3 Correct 8 ms 13388 KB Output is correct
4 Correct 9 ms 13388 KB Output is correct
5 Correct 9 ms 13388 KB Output is correct
6 Correct 9 ms 13428 KB Output is correct
7 Correct 8 ms 13388 KB Output is correct
8 Correct 8 ms 13388 KB Output is correct
9 Correct 9 ms 13388 KB Output is correct
10 Correct 8 ms 13388 KB Output is correct
11 Correct 9 ms 13388 KB Output is correct
12 Correct 54 ms 18276 KB Output is correct
13 Correct 98 ms 18108 KB Output is correct
14 Correct 82 ms 17816 KB Output is correct
15 Correct 60 ms 16212 KB Output is correct
16 Correct 65 ms 18300 KB Output is correct
17 Correct 86 ms 18284 KB Output is correct
18 Correct 65 ms 18412 KB Output is correct
19 Correct 89 ms 18260 KB Output is correct
20 Correct 63 ms 18284 KB Output is correct
21 Correct 89 ms 18320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 13308 KB Output is correct
2 Correct 8 ms 13260 KB Output is correct
3 Correct 9 ms 13388 KB Output is correct
4 Correct 9 ms 13376 KB Output is correct
5 Correct 9 ms 13388 KB Output is correct
6 Correct 8 ms 13388 KB Output is correct
7 Correct 8 ms 13388 KB Output is correct
8 Correct 8 ms 13380 KB Output is correct
9 Correct 8 ms 13376 KB Output is correct
10 Correct 8 ms 13388 KB Output is correct
11 Correct 9 ms 13388 KB Output is correct
12 Correct 9 ms 13336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 13260 KB Output is correct
2 Correct 8 ms 13260 KB Output is correct
3 Correct 8 ms 13388 KB Output is correct
4 Correct 8 ms 13388 KB Output is correct
5 Correct 9 ms 13388 KB Output is correct
6 Correct 8 ms 13388 KB Output is correct
7 Correct 8 ms 13376 KB Output is correct
8 Correct 9 ms 13376 KB Output is correct
9 Correct 8 ms 13388 KB Output is correct
10 Correct 8 ms 13388 KB Output is correct
11 Correct 8 ms 13388 KB Output is correct
12 Correct 8 ms 13316 KB Output is correct
13 Correct 8 ms 13360 KB Output is correct
14 Correct 9 ms 13388 KB Output is correct
15 Correct 9 ms 13372 KB Output is correct
16 Correct 9 ms 13312 KB Output is correct
17 Correct 9 ms 13388 KB Output is correct
18 Correct 9 ms 13396 KB Output is correct
19 Correct 9 ms 13388 KB Output is correct
20 Correct 9 ms 13388 KB Output is correct
21 Correct 9 ms 13392 KB Output is correct
22 Correct 9 ms 13388 KB Output is correct
23 Correct 9 ms 13388 KB Output is correct
24 Correct 9 ms 13480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 13260 KB Output is correct
2 Correct 8 ms 13260 KB Output is correct
3 Correct 8 ms 13388 KB Output is correct
4 Correct 8 ms 13376 KB Output is correct
5 Correct 9 ms 13372 KB Output is correct
6 Correct 9 ms 13340 KB Output is correct
7 Correct 8 ms 13280 KB Output is correct
8 Correct 8 ms 13388 KB Output is correct
9 Correct 9 ms 13388 KB Output is correct
10 Correct 9 ms 13324 KB Output is correct
11 Correct 9 ms 13376 KB Output is correct
12 Correct 8 ms 13388 KB Output is correct
13 Correct 8 ms 13408 KB Output is correct
14 Correct 9 ms 13380 KB Output is correct
15 Correct 9 ms 13388 KB Output is correct
16 Correct 8 ms 13388 KB Output is correct
17 Correct 9 ms 13388 KB Output is correct
18 Correct 9 ms 13388 KB Output is correct
19 Correct 9 ms 13384 KB Output is correct
20 Correct 9 ms 13388 KB Output is correct
21 Correct 9 ms 13388 KB Output is correct
22 Correct 9 ms 13468 KB Output is correct
23 Correct 9 ms 13388 KB Output is correct
24 Correct 9 ms 13392 KB Output is correct
25 Correct 54 ms 18988 KB Output is correct
26 Correct 77 ms 19128 KB Output is correct
27 Correct 95 ms 20040 KB Output is correct
28 Correct 100 ms 20528 KB Output is correct
29 Correct 99 ms 20476 KB Output is correct
30 Correct 56 ms 17228 KB Output is correct
31 Correct 52 ms 19932 KB Output is correct
32 Correct 86 ms 20596 KB Output is correct
33 Correct 62 ms 20272 KB Output is correct
34 Correct 90 ms 20512 KB Output is correct
35 Correct 62 ms 20092 KB Output is correct
36 Correct 90 ms 20424 KB Output is correct
37 Correct 48 ms 19084 KB Output is correct