답안 #989607

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
989607 2024-05-28T12:00:02 Z aykhn Palembang Bridges (APIO15_bridge) C++17
100 / 100
76 ms 8820 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#define int long long
#define inf 0x3F3F3F3F3F3F3F3F
 
signed main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int k, n, S = 0;
  cin >> k >> n;
  vector<array<int, 2>> v;
  for (int i = 0; i < n; i++)
  {
    char a, b;
    int l, r;
    cin >> a >> l >> b >> r;
    if (l > r) swap(l, r);
    if (a == b) S += r - l;
    else v.push_back({l, r});
  }
  if (v.empty())
  {
    cout << S << '\n';
    return 0;
  }
  n = v.size();
  sort(v.begin(), v.end(), [&](const array<int, 2> &a, const array<int, 2> &b)
  {
    return a[0] + a[1] < b[0] + b[1];
  });
  vector<int> bs(n, 0), bp(n, 0);
  priority_queue<int> L;
  priority_queue<int, vector<int>, greater<int>> R;
  int sL = 0, sR = 0;
  for (int i = 0; i < n; i++)
  {
    L.push(v[i][0]), L.push(v[i][1]);
    sL += v[i][0] + v[i][1];
    while (!L.empty() && !R.empty() && L.top() > R.top()) R.push(L.top()), sR += L.top(), sL -= L.top(), L.pop();
    while (L.size() > R.size()) R.push(L.top()), sR += L.top(), sL -= L.top(), L.pop();
    while (L.size() < R.size()) L.push(R.top()), sL += R.top(), sR -= R.top(), R.pop();
    bp[i] = sR - sL;
  }
  while (!L.empty()) L.pop();
  while (!R.empty()) R.pop();
  sL = sR = 0;
  for (int i = n - 1; i >= 0; i--)
  {
    L.push(v[i][0]), L.push(v[i][1]);
    sL += v[i][0] + v[i][1];
    while (!L.empty() && !R.empty() && L.top() > R.top()) R.push(L.top()), sR += L.top(), sL -= L.top(), L.pop();
    while (L.size() > R.size()) R.push(L.top()), sR += L.top(), sL -= L.top(), L.pop();
    while (L.size() < R.size()) L.push(R.top()), sL += R.top(), sR -= R.top(), R.pop();
    bs[i] = sR - sL;
  }
  if (k == 1 || v.size() == 1) cout << bp[n - 1] + S + n << '\n';
  else
  {
    int res = bp[n - 1];
    for (int i = 0; i + 1 < n; i++) res = min(res, bp[i] + bs[i + 1]);
    cout << res + S + n << '\n';
  }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 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 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 1 ms 468 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 456 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 1 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 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 27 ms 7196 KB Output is correct
13 Correct 72 ms 8652 KB Output is correct
14 Correct 61 ms 7080 KB Output is correct
15 Correct 39 ms 4816 KB Output is correct
16 Correct 28 ms 8148 KB Output is correct
17 Correct 58 ms 8640 KB Output is correct
18 Correct 47 ms 8240 KB Output is correct
19 Correct 76 ms 8820 KB Output is correct
20 Correct 41 ms 8136 KB Output is correct
21 Correct 60 ms 8396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 456 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 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 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 456 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 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 348 KB Output is correct
5 Correct 0 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 0 ms 452 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 468 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 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 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 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 344 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 352 KB Output is correct
12 Correct 0 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 344 KB Output is correct
16 Correct 0 ms 352 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 352 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 352 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 27 ms 7276 KB Output is correct
26 Correct 57 ms 7368 KB Output is correct
27 Correct 73 ms 8392 KB Output is correct
28 Correct 69 ms 8652 KB Output is correct
29 Correct 70 ms 8616 KB Output is correct
30 Correct 35 ms 4312 KB Output is correct
31 Correct 29 ms 7868 KB Output is correct
32 Correct 57 ms 8640 KB Output is correct
33 Correct 45 ms 8480 KB Output is correct
34 Correct 62 ms 8644 KB Output is correct
35 Correct 39 ms 8196 KB Output is correct
36 Correct 60 ms 8392 KB Output is correct
37 Correct 22 ms 7112 KB Output is correct