Submission #937841

# Submission time Handle Problem Language Result Execution time Memory
937841 2024-03-04T15:11:54 Z duckindog Palembang Bridges (APIO15_bridge) C++17
100 / 100
64 ms 6292 KB
#include<bits/stdc++.h>

using namespace std;

const int N = 100'000 + 10;
int k, n;
pair<int, int> a[N];

long long sum = 0;
priority_queue<int> lt;
priority_queue<int, vector<int>, greater<>> rt;
void rearrange() { 
  if (lt.top() <= rt.top()) return;
  int x = lt.top(), y = rt.top();
  lt.pop(), rt.pop();
  sum += 2 * x - 2 * y;
  lt.push(y); rt.push(x);
}
void clr() { 
  sum = 0;
  priority_queue<int> x;
  priority_queue<int, vector<int>, greater<>> y;
  swap(lt, x); swap(rt, y);
}
long long l[N], r[N];

int32_t main() { 
  cin.tie(0)->sync_with_stdio(0);
  
  cin >> k >> n;
  long long add = 0;
  for (int i = 1; i <= n; ++i) { 
    char A, B;
    cin >> A >> a[i].first >> B >> a[i].second;
    if (A != B) continue;
    add += abs(a[i].first - a[i].second);
    i -= 1; n -= 1;
  }
  sort(a + 1, a + n + 1, [&](const auto& a, const auto& b) { 
    return a.first + a.second < b.first + b.second;
  });
  for (int i = 1; i <= n; ++i) { 
    int x, y; tie(x, y) = a[i];
    lt.push(x); rt.push(y);
    sum += y - x;
    rearrange();
    l[i] = sum;
  }
  if (k == 1) { 
    cout << l[n] + add + n;
    return 0;
  }

  clr();
  for (int i = n; i >= 1; --i) { 
    int x, y; tie(x, y) = a[i];
    lt.push(x); rt.push(y);
    sum += y - x;
    rearrange();
    r[i] = sum;
  }
  long long answer = l[n];
  for (int i = 2; i <= n; ++i) answer = min(answer, r[i] + l[i - 1]);
  
  cout << answer + add + n << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2536 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 19 ms 4340 KB Output is correct
13 Correct 42 ms 5828 KB Output is correct
14 Correct 36 ms 4564 KB Output is correct
15 Correct 28 ms 4312 KB Output is correct
16 Correct 23 ms 5072 KB Output is correct
17 Correct 35 ms 5848 KB Output is correct
18 Correct 27 ms 5472 KB Output is correct
19 Correct 40 ms 5804 KB Output is correct
20 Correct 27 ms 5328 KB Output is correct
21 Correct 39 ms 5592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 0 ms 2516 KB Output is correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2392 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2400 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2516 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2516 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2392 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2524 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2528 KB Output is correct
24 Correct 2 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2520 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2392 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 2 ms 2396 KB Output is correct
23 Correct 1 ms 2460 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 25 ms 4668 KB Output is correct
26 Correct 48 ms 4792 KB Output is correct
27 Correct 58 ms 5560 KB Output is correct
28 Correct 61 ms 6284 KB Output is correct
29 Correct 64 ms 6292 KB Output is correct
30 Correct 31 ms 4140 KB Output is correct
31 Correct 27 ms 5288 KB Output is correct
32 Correct 52 ms 5892 KB Output is correct
33 Correct 38 ms 5560 KB Output is correct
34 Correct 57 ms 5736 KB Output is correct
35 Correct 37 ms 5260 KB Output is correct
36 Correct 55 ms 5680 KB Output is correct
37 Correct 14 ms 4596 KB Output is correct