Submission #962848

# Submission time Handle Problem Language Result Execution time Memory
962848 2024-04-14T08:59:16 Z zNatsumi Palembang Bridges (APIO15_bridge) C++14
100 / 100
90 ms 8064 KB
#include <bits/stdc++.h>
#define int long long

using namespace std;
const int oo = 1e14, N = 1e5 + 5;
int k, n, res, tst;
int lsum, rsum, pre[N];
vector<pair<int, int>> v;
priority_queue<int> pql;
priority_queue<int, vector<int>, greater<>> pqr;

bool comp(pair<int, int> x, pair<int, int> y){
  return x.first + x.second < y.first + y.second;
}

void add(int x){
  int mid = (pql.size() ? pql.top() : oo);
  if(x <= mid){
    pql.push(x);
    lsum += x;
  }else{
    pqr.push(x);
    rsum += x;
  }

  if(pqr.size() + 2 <= pql.size()){
    int t = pql.top(); pql.pop();
    pqr.push(t);
    lsum -= t;
    rsum += t;
  }else if(pql.size() < pqr.size()){
    int t = pqr.top(); pqr.pop();
    pql.push(t);
    lsum += t;
    rsum -= t;
  }
}

int32_t main()
{
  cin.tie(0)->sync_with_stdio(0); cout.tie(0);
  if(fopen("BRIDGES.inp", "r")){
    freopen("BRIDGES.inp", "r", stdin);
    freopen("BRIDGES.out", "w", stdout);
  }
  cin >> k >> n;
  v.push_back({0, 0});
  for(int i = 1; i <= n; i++){
    char p, q;
    int s, t;
    cin >> p >> s >> q >> t;
    if(p == q) tst += abs(s - t);
    else v.push_back({s, t});
  }
  sort(v.begin(), v.end(), comp);
  n = v.size()-1;
  tst += n;
  for(int i = 1; i <= n; i++){
    add(v[i].first);
    add(v[i].second);
    pre[i] = rsum - lsum;
  }
  res = pre[n];
  if(k == 2){
    while(pql.size()) pql.pop();
    while(pqr.size()) pqr.pop();
    lsum = rsum = 0;
    for(int i = n; i >= 1; i--){
      add(v[i].first);
      add(v[i].second);
      res = min(res, rsum - lsum + pre[i-1]);
    }
  }
  cout << tst + res;

  return 0;
}

Compilation message

bridge.cpp: In function 'int32_t main()':
bridge.cpp:43:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   43 |     freopen("BRIDGES.inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:44:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   44 |     freopen("BRIDGES.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 516 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 476 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 500 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 648 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 22 ms 6348 KB Output is correct
13 Correct 45 ms 7624 KB Output is correct
14 Correct 34 ms 6844 KB Output is correct
15 Correct 30 ms 5024 KB Output is correct
16 Correct 33 ms 7124 KB Output is correct
17 Correct 34 ms 7840 KB Output is correct
18 Correct 33 ms 7904 KB Output is correct
19 Correct 44 ms 7980 KB Output is correct
20 Correct 29 ms 7372 KB Output is correct
21 Correct 38 ms 7624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 468 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 472 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 516 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 428 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 520 KB Output is correct
20 Correct 1 ms 484 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 520 KB Output is correct
24 Correct 1 ms 476 KB Output is correct
25 Correct 30 ms 6356 KB Output is correct
26 Correct 55 ms 7108 KB Output is correct
27 Correct 73 ms 7656 KB Output is correct
28 Correct 90 ms 8064 KB Output is correct
29 Correct 73 ms 7596 KB Output is correct
30 Correct 39 ms 4048 KB Output is correct
31 Correct 35 ms 6456 KB Output is correct
32 Correct 63 ms 7656 KB Output is correct
33 Correct 64 ms 6932 KB Output is correct
34 Correct 60 ms 7368 KB Output is correct
35 Correct 39 ms 7116 KB Output is correct
36 Correct 67 ms 7560 KB Output is correct
37 Correct 26 ms 6352 KB Output is correct