Submission #972221

# Submission time Handle Problem Language Result Execution time Memory
972221 2024-04-30T08:55:50 Z Halym2007 Palembang Bridges (APIO15_bridge) C++17
100 / 100
180 ms 19912 KB
#include "bits/stdc++.h"
#define ll long long int
#define pb push_back
#define pii pair<ll,ll>
#define ff first
#define ss second
#define sz size()

const int N = 2e5 + 1;

using namespace std;

ll n, k, x[N], y[N], ans, p[N], q[N], mn = 1e15, cnt, cnt1, sum, sum1;

char s[N], d[N];

vector <pii> v;
vector <int> v1;

multiset <int> l, r;

bool cmp(pii a, pii b) {
	return a.ff + a.ss < b.ff + b.ss;
}

void med(int a){
	if(l.empty()) {
		l.insert(a);
		sum += a;
	}
	else if((int)l.sz <= (int)r.sz){
		auto t = r.begin();
		ll aa = *t;
		if (a > aa) {
			sum += aa;
			l.insert(aa);
			sum1 += a;
			sum1 -= aa;
			r.erase(t);
			r.insert(a);
		}
		else {
			l.insert(a);
			sum += a;
		}
	}
	else{
		auto t = l.end();
		t--;
		ll aa = *t;
		if (a < aa){
			sum -= (ll)aa;
			sum += a;
			sum1 += aa;
			r.insert(aa);
			l.erase(t);
			l.insert(a);
		}
		else{
			r.insert(a);
			sum1 += a;
		}
	}
}

int main() {
//	freopen ("input.txt", "r", stdin);
    ios::sync_with_stdio(false); cin.tie(nullptr);
    cin >> k >> n;
    for(int i = 1; i <= n; i++){
    	cin >> s[i] >> x[i] >> d[i] >> y[i];
    	if(s[i] != d[i]){
    		ans++;
    		v.pb({x[i],y[i]});
    	}
    	else ans += abs(x[i]-y[i]);
    }
    sort(v.begin(), v.end(), cmp);
    for(int i = 0; i < (int)v.sz; i++){
    	med(v[i].ff);
		med(v[i].ss);
		auto t = l.end();
    	t--;
    	ll med1 = *t;
    	cnt = (int)l.sz;cnt1 = (int)r.sz;
    	p[i] = (cnt * med1 - sum) + (sum1 - cnt1 * med1);
    }
    cnt = sum = cnt1 = sum1 = 0;
    l.clear();r.clear();
    for(int i = (int)v.sz-1; i >= 0; i--){
		med(v[i].ff);
		med(v[i].ss);
		auto t = l.end();
    	t--;
    	ll med1 = *t;
    	cnt = (int)l.sz;cnt1 = (int)r.sz;
    	q[i] = (cnt * med1 - sum) + (sum1 - cnt1 * med1);
    }
    for(int i = 0; i < (int)v.sz; i++){
    	mn = min(mn, p[i] + q[i+1]);
    }
    mn = min(mn,q[0]);
//    cout << sum << " " << sum1;
//    return 0;a
    if(k == 1) mn = q[0];
//    assert (q[0] == p[(int)v.sz - 1]);
    cout << mn + ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Correct 2 ms 6748 KB Output is correct
5 Correct 2 ms 6748 KB Output is correct
6 Correct 2 ms 6748 KB Output is correct
7 Correct 2 ms 6748 KB Output is correct
8 Correct 2 ms 6748 KB Output is correct
9 Correct 2 ms 6748 KB Output is correct
10 Correct 2 ms 6744 KB Output is correct
11 Correct 2 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6612 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Correct 2 ms 6748 KB Output is correct
5 Correct 3 ms 6744 KB Output is correct
6 Correct 2 ms 6616 KB Output is correct
7 Correct 2 ms 6608 KB Output is correct
8 Correct 2 ms 6672 KB Output is correct
9 Correct 2 ms 6748 KB Output is correct
10 Correct 2 ms 6748 KB Output is correct
11 Correct 2 ms 6492 KB Output is correct
12 Correct 81 ms 18440 KB Output is correct
13 Correct 156 ms 19640 KB Output is correct
14 Correct 180 ms 17612 KB Output is correct
15 Correct 77 ms 14288 KB Output is correct
16 Correct 89 ms 19144 KB Output is correct
17 Correct 95 ms 19656 KB Output is correct
18 Correct 82 ms 19444 KB Output is correct
19 Correct 103 ms 19912 KB Output is correct
20 Correct 94 ms 19344 KB Output is correct
21 Correct 99 ms 19424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 2 ms 6488 KB Output is correct
10 Correct 1 ms 6488 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6612 KB Output is correct
13 Correct 2 ms 6492 KB Output is correct
14 Correct 2 ms 6748 KB Output is correct
15 Correct 2 ms 6748 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 1 ms 6748 KB Output is correct
20 Correct 2 ms 6728 KB Output is correct
21 Correct 2 ms 6744 KB Output is correct
22 Correct 2 ms 6748 KB Output is correct
23 Correct 2 ms 6516 KB Output is correct
24 Correct 2 ms 6748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6488 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6744 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6748 KB Output is correct
14 Correct 2 ms 6496 KB Output is correct
15 Correct 2 ms 6752 KB Output is correct
16 Correct 1 ms 6496 KB Output is correct
17 Correct 1 ms 6496 KB Output is correct
18 Correct 1 ms 6496 KB Output is correct
19 Correct 2 ms 6752 KB Output is correct
20 Correct 2 ms 6752 KB Output is correct
21 Correct 2 ms 6752 KB Output is correct
22 Correct 2 ms 6496 KB Output is correct
23 Correct 2 ms 6672 KB Output is correct
24 Correct 2 ms 6744 KB Output is correct
25 Correct 84 ms 18220 KB Output is correct
26 Correct 139 ms 18376 KB Output is correct
27 Correct 149 ms 19160 KB Output is correct
28 Correct 150 ms 19864 KB Output is correct
29 Correct 151 ms 19908 KB Output is correct
30 Correct 71 ms 13384 KB Output is correct
31 Correct 87 ms 18888 KB Output is correct
32 Correct 97 ms 19492 KB Output is correct
33 Correct 80 ms 19140 KB Output is correct
34 Correct 104 ms 19300 KB Output is correct
35 Correct 91 ms 19040 KB Output is correct
36 Correct 101 ms 19408 KB Output is correct
37 Correct 78 ms 18116 KB Output is correct