Submission #701898

# Submission time Handle Problem Language Result Execution time Memory
701898 2023-02-22T09:58:51 Z dattranxxx Palembang Bridges (APIO15_bridge) C++11
100 / 100
61 ms 5656 KB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;

const int N = 1e5 + 5;

int n, k;

#define fi first
#define se second

priority_queue<int> L, R;
ll f[N], cur = 0, sumL = 0, sumR = 0;

void insert(int x) {
  if (L.empty() || x <= L.top()) {
    L.emplace(+x);
    sumL += x;
  } else {
    R.emplace(-x);
    sumR += x;
  }
  
  if (L.size() > R.size() + 1) {
    int x = L.top();
    
    R.emplace(-x);
    sumR += x;
    
    L.pop();
    sumL -= x;
  }
  
  if (L.size() < R.size()) {
    int x = -R.top();
    
    L.emplace(x);
    sumL += x;
    
    R.pop();
    sumR -= x;
  }
  
  // Ltop*Lsize - sum_l + sum_r - Ltop*Rsize
  // = L.top() * (L.size() - R.size()) + sumR - sumL
  cur = L.top() * (L.size() - R.size()) + sumR - sumL;
}


int main() {
  cin.tie(0)->sync_with_stdio(0); cout.tie(0);
  
  cin >> k >> n;
  
  char p, q;
  
  vector<pair<int, int>> vec;
  ll same = 0;
  
  for (int i = 1, s, t; i <= n; ++i) {
    cin >> p >> s >> q >> t;
    if (p == q) {
      same += abs(s - t);
    } else {
      same += 1;
      vec.emplace_back(s, t);
    }
  }
  
  sort(vec.begin(), vec.end(), [](pair<int, int>& u, pair<int, int>& v) {
    return u.fi + u.se < v.fi + v.se;
  });
  
  for (int i = 0; i < vec.size(); ++i) {
    insert(vec[i].fi);
    insert(vec[i].se);
    f[i] = cur;
  }
  
  if (k == 1) {
    cout << cur + same;
    return 0;
  }
  
  ll res = cur;
  
  L = R = priority_queue<int>();
  cur = sumR = sumL = 0;
  
  for (int i = (int)vec.size() - 1; i >= 1; --i) {
    insert(vec[i].fi);
    insert(vec[i].se);
    res = min(res, f[i - 1] + cur);
  }
  
  cout << res + same;
  
	return 0;
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:74:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |   for (int i = 0; i < vec.size(); ++i) {
      |                   ~~^~~~~~~~~~~~
# 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 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 22 ms 3412 KB Output is correct
13 Correct 45 ms 3216 KB Output is correct
14 Correct 36 ms 3096 KB Output is correct
15 Correct 26 ms 2196 KB Output is correct
16 Correct 29 ms 3272 KB Output is correct
17 Correct 35 ms 3108 KB Output is correct
18 Correct 35 ms 3144 KB Output is correct
19 Correct 45 ms 3036 KB Output is correct
20 Correct 29 ms 3484 KB Output is correct
21 Correct 38 ms 3048 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 0 ms 328 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 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 1 ms 212 KB Output is correct
12 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 332 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 328 KB Output is correct
11 Correct 1 ms 328 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 356 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 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 336 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 344 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 28 ms 4324 KB Output is correct
26 Correct 46 ms 4284 KB Output is correct
27 Correct 57 ms 4996 KB Output is correct
28 Correct 60 ms 5656 KB Output is correct
29 Correct 61 ms 5520 KB Output is correct
30 Correct 33 ms 3052 KB Output is correct
31 Correct 35 ms 5160 KB Output is correct
32 Correct 45 ms 5580 KB Output is correct
33 Correct 47 ms 5412 KB Output is correct
34 Correct 51 ms 5572 KB Output is correct
35 Correct 35 ms 5424 KB Output is correct
36 Correct 49 ms 5344 KB Output is correct
37 Correct 25 ms 4296 KB Output is correct