답안 #317903

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
317903 2020-10-30T18:23:22 Z soroush Palembang Bridges (APIO15_bridge) C++14
100 / 100
149 ms 10412 KB
/*
#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,fma,tune=native")
//*/
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll  ,ll > pii;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll maxn  = 1e5 + 100;
const ll mod =1e9+7;
const ld PI = acos((ld)-1);

#define pb push_back
#define endl '\n'
#define dokme(x) cout << x , exit(0)
#define migmig ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ms(x , y) memset(x , y , sizeof x)
ll pw(ll a, ll b, ll md = mod){ll res = 1;while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);}

int n , k;
bool H[maxn] , W[maxn];
ll h[maxn] , w[maxn];

vector < ll > vec;

struct sub1{
    ll solve(ll x){
        ll ans = 0;
        for(int i = 0 ; i < n ; i ++){
            if(H[i] == W[i])ans += abs(h[i] - w[i]);
            else
                ans += abs(h[i] - x) + abs(w[i] - x) + 1;
        }
        return(ans);
    }
    void sub(){
        ll ans = 1e18;
        for(auto x : vec)
            ans = min(ans , solve(x));
        dokme(ans);
    }
}s1;

struct sub3{
    ll solve(ll x , ll y){
        ll ans = 0;
        for(int i = 0 ; i < n ; i ++){
            if(H[i] == W[i])ans += abs(h[i] - w[i]);
            else
                ans += min(abs(h[i] - x) + abs(w[i] - x)  ,abs(h[i] - y) + abs(w[i] - y))  + 1;
        }
        return(ans);
    }
    void sub(){
        ll ans = 1e18;
        for(auto x : vec)
            for(auto y : vec)
                ans = min(ans , solve(x , y));
        dokme(ans);
    }
}s3;

bool cmp(pii a , pii b){
	return(a.first + a.second < b.first + b.second);
}	

struct sub2{
    void sub(){
        ll ans = 0;
        vec.clear();
        for(int i = 0 ; i < n ; i ++)
            if(H[i] == W[i])
                ans += abs(w[i] - h[i]);
            else
                ans ++,
                vec.pb(h[i]) , vec.pb(w[i]);
        ll mn = 5e18;
        sort(vec.begin() , vec.end());
        int N = vec.size();
        vector < ll > l(N , 0) , r(N , 0);
        for(int i = 1 ; i < N ; i ++){
            l[i] = (vec[i] - vec[i - 1])*ll(i) + l[i - 1]; 
        }
        for(int i = N - 2 , j = 1 ; i >= 0 ; i -- , j ++ ){
            r[i] = (vec[i + 1] - vec[i]) * ll(j) + r[i + 1];
        }
        for(int i = 0 ; i < N ; i ++)
            mn = min(mn , l[i] + r[i]);
        dokme(ans + mn);
    }
    ll subm(){
        ll ans = 0;
        vec.clear();
        for(int i = 0 ; i < n ; i ++)
            if(H[i] == W[i])
                ans += abs(w[i] - h[i]);
            else
                ans ++,
                vec.pb(h[i]) , vec.pb(w[i]);
        ll mn = 5e18;
        sort(vec.begin() , vec.end());
        int N = vec.size();
        vector < ll > l(N , 0) , r(N , 0);
        for(int i = 1 ; i < N ; i ++){
            l[i] = (vec[i] - vec[i - 1])*ll(i) + l[i - 1]; 
        }
        for(int i = N - 2 , j = 1 ; i >= 0 ; i -- , j ++ ){
            r[i] = (vec[i + 1] - vec[i]) * ll(j) + r[i + 1];
        }
        for(int i = 0 ; i < N ; i ++)
            mn = min(mn , l[i] + r[i]);
        return(ans + mn);
    }
}s2;

vector < pii > v;
ll l[maxn] , r[maxn];

ll mnsum = 0 , mxsum = 0;

priority_queue < ll > mx;
priority_queue < ll ,vector < ll > , greater < ll >  > mn;

void add(pii x){
	if(mx.size() == 0){
		mx.push(x.first);
		mn.push(x.second);
		mxsum += x.first;
		mnsum+=x.second;
	}
	else{
		ll MX = mx.top();
		ll MN = mn.top();
		auto [f , s] = x;
		if(f<=MX and s<=MX){
			mx.pop();
			mx.push(f);
			mx.push(s);
			mn.push(MX);
			mnsum += MX;
			mxsum+= f+s - MX;
		}
		else if(f >= MN and s >= MN){mn.pop();
			mn.push(f);
			mn.push(s);
			
			mx.push(MN);
			mxsum += MN;
			mnsum+= f+s - MN;
		}
		else{
			mx.push(x.first);
		mn.push(x.second);
		mxsum += x.first;
		mnsum+=x.second;
		}
	}
}

ll calc(){
	return(mnsum-mxsum);
}	

int32_t main(){
    migmig;
    cin >> k >> n;
    for(int i = 0 ; i < n ; i ++){
        string s;
        cin >> s;
        H[i] = (s == "A");
        cin >> h[i];
        cin >> s;
        W[i] = (s == "A");
        cin >> w[i];
        vec.pb(h[i]);
        vec.pb(w[i]);
    }
    sort(vec.begin() , vec.end());
    vec.resize(unique(vec.begin() , vec.end()) - vec.begin());
    if(k == 1 and n <= 1000){
        s1.sub();
    }
    if(k == 2 and n <= 100){
        s3.sub();
    }
    if(k == 1){
        s2.sub();
    }
	assert(k == 2);
	assert(n <= 1e5);
	ll ans = 0;
	for(int i = 0 ; i < n ; i ++){
		if(H[i] == W[i])
			ans += abs(h[i] - w[i]);
		else
			v.pb({min(h[i] , w[i]) , max(h[i] , w[i])});
	}
	ll Ans = s2.subm();
	sort(v.begin() , v.end() , cmp);
	n = v.size();
	if(n == 0)
		dokme(ans);
	for(int i = 0 ; i < n ; i ++){
		add(v[i]);
		l[i] = calc();
	}
	reverse(v.begin() , v.end());
	mxsum = mnsum = 0;
	while(mx.size())mx.pop();
	while(mn.size())mn.pop();
	for(int i = 0 ; i < n ; i ++){
		add(v[i]);
		r[i] = calc();
		Ans = min(Ans , r[i] + l[n - i - 2] + n);
	}
	cout << ans + Ans;
    return(0);
}

Compilation message

bridge.cpp: In function 'void add(pii)':
bridge.cpp:141:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  141 |   auto [f , s] = x;
      |        ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 512 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 5 ms 512 KB Output is correct
12 Correct 49 ms 6888 KB Output is correct
13 Correct 83 ms 6888 KB Output is correct
14 Correct 62 ms 6248 KB Output is correct
15 Correct 48 ms 4332 KB Output is correct
16 Correct 55 ms 6888 KB Output is correct
17 Correct 55 ms 6892 KB Output is correct
18 Correct 71 ms 7016 KB Output is correct
19 Correct 78 ms 7016 KB Output is correct
20 Correct 54 ms 6888 KB Output is correct
21 Correct 73 ms 6888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 13 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 15 ms 384 KB Output is correct
9 Correct 15 ms 384 KB Output is correct
10 Correct 13 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 14 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 13 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 14 ms 384 KB Output is correct
9 Correct 15 ms 384 KB Output is correct
10 Correct 14 ms 512 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 15 ms 384 KB Output is correct
13 Correct 1 ms 512 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
15 Correct 2 ms 512 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 1 ms 512 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 2 ms 512 KB Output is correct
23 Correct 1 ms 448 KB Output is correct
24 Correct 2 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 13 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 15 ms 384 KB Output is correct
9 Correct 15 ms 384 KB Output is correct
10 Correct 17 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 15 ms 384 KB Output is correct
13 Correct 2 ms 512 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
15 Correct 2 ms 512 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 1 ms 512 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 2 ms 512 KB Output is correct
23 Correct 1 ms 512 KB Output is correct
24 Correct 2 ms 512 KB Output is correct
25 Correct 79 ms 9512 KB Output is correct
26 Correct 107 ms 9892 KB Output is correct
27 Correct 142 ms 10412 KB Output is correct
28 Correct 149 ms 10276 KB Output is correct
29 Correct 146 ms 10404 KB Output is correct
30 Correct 74 ms 5488 KB Output is correct
31 Correct 74 ms 9772 KB Output is correct
32 Correct 109 ms 9764 KB Output is correct
33 Correct 101 ms 9764 KB Output is correct
34 Correct 140 ms 9760 KB Output is correct
35 Correct 84 ms 9764 KB Output is correct
36 Correct 133 ms 9892 KB Output is correct
37 Correct 70 ms 9808 KB Output is correct