Submission #657906

# Submission time Handle Problem Language Result Execution time Memory
657906 2022-11-11T16:41:33 Z TS_2392 Palembang Bridges (APIO15_bridge) C++14
100 / 100
87 ms 5220 KB
#include <bits/stdc++.h>
#define SPEED ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define rall(x) (x).rbegin(), (x).rend()
#define all(x) (x).begin(), (x).end()
#define sqr(x) (x) * (x)

#define eb emplace_back
#define lwb lower_bound
#define upb upper_bound

#define mp make_pair
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef long double ldb;
typedef unsigned int uint;
typedef unsigned long long ull;

typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<int, int> pii;
typedef pair<ldb, ldb> pld;
typedef pair<double, double> pdd;
const int N = 1e5 + 3, oo = 1e9 + 3;
bool cmp(const pii &a, const pii &b){
    return a.fi + a.se < b.fi + b.se;
}
pii a[N];
int k, n, sz;
ll res, sumup, sumlow, cost[N];
void sol1(){
    int tmp[N << 1], median;
    for(int i = 1; i <= sz; ++i){
        tmp[i] = a[i].fi;
        tmp[i + sz] = a[i].se;
    }
    sort(tmp + 1, tmp + 1 + 2 * sz);
    median = tmp[sz];
    for(int i = 1; i <= 2 * sz; ++i){
        res += (ll)abs(tmp[i] - median);
    }
    cout << res;
}
priority_queue<int> lowq;
priority_queue<int, vector<int>, greater<int> > upq;
void balace(){
    if (upq.size() + 1 < lowq.size()) {
		int nxt = lowq.top();
		lowq.pop();
		upq.push(nxt);
		sumlow -= nxt;
		sumup += nxt;
	} else if (lowq.size() < upq.size()) {
		int nxt = upq.top();
		upq.pop();
		lowq.push(nxt);
		sumup -= nxt;
		sumlow += nxt;
	}
}
void ins(int x){
    int median = (lowq.size() ? lowq.top() : 1000000001);
    if (x <= median) {
		lowq.push(x);
		sumlow += x;
	} else {
		upq.push(x);
		sumup += x;
	}
    balace();
}
void sol2(){
    sort(a + 1, a + 1 + sz, cmp);
    sumup = sumlow = 0;
    for(int i = 1; i <= sz; ++i){
        ins(a[i].fi); ins(a[i].se);
        cost[i] = sumup - sumlow;
    }
    sumup = sumlow = 0; ll ans = cost[sz];
    while(!lowq.empty()) lowq.pop();
    while(!upq.empty()) upq.pop();
    for(int i = sz; i >= 1; --i){
        ins(a[i].fi); ins(a[i].se);
        ans = min(ans, sumup - sumlow + cost[i - 1]);
    }
    cout << res + ans;
}
int main(){
    SPEED;
    cin >> k >> n;
    for(int i = 1; i <= n; ++i){
        char z1, z2; int p1, p2;
        cin >> z1 >> p1 >> z2 >> p2;
        if(z1 == z2){
            res += (ll)abs(p1 - p2);
            continue;
        }
        ++sz; ++res;
        a[sz] = mp(p1, p2);
    }
    if(k == 1) sol1();
    else sol2();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1100 KB Output is correct
2 Correct 1 ms 1108 KB Output is correct
3 Correct 1 ms 1104 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 1 ms 1112 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
7 Correct 1 ms 1108 KB Output is correct
8 Correct 1 ms 1108 KB Output is correct
9 Correct 1 ms 1112 KB Output is correct
10 Correct 1 ms 1108 KB Output is correct
11 Correct 1 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1100 KB Output is correct
2 Correct 1 ms 1108 KB Output is correct
3 Correct 1 ms 1108 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 1 ms 1128 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
7 Correct 1 ms 1108 KB Output is correct
8 Correct 1 ms 1108 KB Output is correct
9 Correct 1 ms 1108 KB Output is correct
10 Correct 1 ms 1132 KB Output is correct
11 Correct 1 ms 1108 KB Output is correct
12 Correct 19 ms 2588 KB Output is correct
13 Correct 48 ms 4224 KB Output is correct
14 Correct 28 ms 2936 KB Output is correct
15 Correct 23 ms 2892 KB Output is correct
16 Correct 28 ms 3480 KB Output is correct
17 Correct 39 ms 4096 KB Output is correct
18 Correct 31 ms 3780 KB Output is correct
19 Correct 37 ms 4184 KB Output is correct
20 Correct 28 ms 3756 KB Output is correct
21 Correct 40 ms 3960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 320 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 328 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 268 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 328 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 1 ms 328 KB Output is correct
13 Correct 1 ms 328 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 34 ms 3616 KB Output is correct
26 Correct 60 ms 3664 KB Output is correct
27 Correct 80 ms 4608 KB Output is correct
28 Correct 87 ms 5048 KB Output is correct
29 Correct 82 ms 5204 KB Output is correct
30 Correct 43 ms 2836 KB Output is correct
31 Correct 42 ms 4444 KB Output is correct
32 Correct 61 ms 5124 KB Output is correct
33 Correct 70 ms 4648 KB Output is correct
34 Correct 68 ms 5220 KB Output is correct
35 Correct 44 ms 4708 KB Output is correct
36 Correct 67 ms 4964 KB Output is correct
37 Correct 28 ms 3648 KB Output is correct