Submission #628133

# Submission time Handle Problem Language Result Execution time Memory
628133 2022-08-13T05:55:28 Z kalash04 Palembang Bridges (APIO15_bridge) C++17
100 / 100
83 ms 6172 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define int long long int
ll MOD = 1e9 + 7;

priority_queue<int, vector<int>, greater<int>> min_heap;
priority_queue<int> max_heap;
int mx, mn;

void insert(int x) {
  int median = (max_heap.size() ? max_heap.top() : 1000000001);
  if(x <= median) {
    max_heap.push(x); 
    mx += x;
  }
  else {
    min_heap.push(x);
    mn += x;
  }
  if(min_heap.size() + 1 < max_heap.size()) {
    int change = max_heap.top();
    mx -= change;
    mn += change;
    max_heap.pop();
    min_heap.push(change);
  } else if(max_heap.size() < min_heap.size()) {
    int change = min_heap.top();
    mn -= change;
    mx += change;
    min_heap.pop();
    max_heap.push(change);
  }
}

void solve() {
  int k, n;
  cin >> k >> n;
  int dist = 0;
  vector<pair<int, int>> v = {{0, 0}};
  for(int i = 0; i < n; i++) {
    char a, b; int c, d;
    cin >> a >> c >> b >> d;
    if(a == b) dist += abs(c - d);
    else v.push_back({c, d});
  }
  sort(v.begin(), v.end(), [](auto const& a, auto const& b) {
    return a.first + a.second < b.first + b.second;
  });
  n = v.size() - 1;
  dist += n;
  mx = mn = 0;
  vector<int> prefix(n + 1);
  for(int i = 1; i <= n; i++) {
    insert(v[i].first);
    insert(v[i].second);
    prefix[i] += mn - mx;
  }
  int ans = prefix[n];
  if(k == 2) {
    while(max_heap.size()) max_heap.pop();
    while(min_heap.size()) min_heap.pop();
    mx = mn = 0;
    for(int i = n; i > 0; i--) {
      insert(v[i].first);
      insert(v[i].second);
      ans = min(ans, prefix[i - 1] + mn - mx);
    }
  }
  cout << dist + ans;
}

int32_t main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
  cout.tie(NULL);
	solve();
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 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 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 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 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 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 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 24 ms 5568 KB Output is correct
13 Correct 50 ms 5344 KB Output is correct
14 Correct 38 ms 5744 KB Output is correct
15 Correct 29 ms 3572 KB Output is correct
16 Correct 31 ms 5316 KB Output is correct
17 Correct 36 ms 5568 KB Output is correct
18 Correct 37 ms 5952 KB Output is correct
19 Correct 49 ms 5592 KB Output is correct
20 Correct 31 ms 5580 KB Output is correct
21 Correct 40 ms 5536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 292 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 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 0 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 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
# 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 0 ms 212 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 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 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 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 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 340 KB Output is correct
22 Correct 2 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 35 ms 5572 KB Output is correct
26 Correct 62 ms 6172 KB Output is correct
27 Correct 76 ms 5608 KB Output is correct
28 Correct 81 ms 5604 KB Output is correct
29 Correct 83 ms 5132 KB Output is correct
30 Correct 43 ms 2948 KB Output is correct
31 Correct 51 ms 5208 KB Output is correct
32 Correct 58 ms 5568 KB Output is correct
33 Correct 63 ms 5188 KB Output is correct
34 Correct 65 ms 5612 KB Output is correct
35 Correct 42 ms 5620 KB Output is correct
36 Correct 62 ms 5520 KB Output is correct
37 Correct 30 ms 5580 KB Output is correct