답안 #241436

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
241436 2020-06-24T07:45:25 Z Nightlight Palembang Bridges (APIO15_bridge) C++14
100 / 100
125 ms 6124 KB
#include <bits/stdc++.h>
#define pii pair<int, int>
using namespace std;

int K, N;
pii seg[100005];int p;
long long dpL[100005];//kalau prefix jadi 1 bagian
long long dpR[100005];//kalau suffix jadi 1 bagian
priority_queue<int, vector<int>, greater<int>> mn;long long sum_mn;
priority_queue<int, vector<int>> mx;long long sum_mx;
long long tot;
long long ans;

bool comp(pii A, pii B) {
  return A.first + A.second < B.first + B.second;
}

void clean() {
  while(!mn.empty()) {
//    cout << mn.top() << "\n";
    mn.pop();
  }
  while(!mx.empty()) {
//    cout << mx.top() << "\n";
    mx.pop();
  }
  sum_mn = 0;
  sum_mx = 0;
  tot = 0;
}

long long balance() {
  while(mn.size() > tot / 2) {
    int now = mn.top();
    mn.pop();
    sum_mn -= now;
    mx.emplace(now);
    sum_mx += now;
  }
  while(mx.size() > tot / 2) {
    int now = mx.top();
    mx.pop();
    sum_mx -= now;
    mn.emplace(now);
    sum_mn += now;
  }
  while(mn.top() < mx.top()) {
    int sm = mn.top(), bg = mx.top();
    mn.pop(), mx.pop();
    mn.emplace(bg);
    mx.emplace(sm);
    sum_mn += bg - sm;
    sum_mx += sm - bg;
  }
//  cout << sum_mn << " " << sum_mx << "\n";
  return sum_mn - sum_mx;
}

long long adds(int idx) {
  mn.emplace(seg[idx].first);
  mn.emplace(seg[idx].second);
  sum_mn += seg[idx].first + seg[idx].second;
  tot += 2;
  return balance();
}

void solve() {
  for(int i = 1; i <= p; i++) {
    dpL[i] = adds(i);
  }
  if(K == 1) {
    printf("%lld\n", dpL[p] + ans + tot / 2);
    return;
  }
  clean();
  long long res = dpL[p];
  for(int i = p; i > 0; i--) {
    dpR[i] = adds(i);
    res = min(res, dpL[i - 1] + dpR[i]);
  }
  printf("%lld\n", res + ans + tot / 2);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin >> K >> N;
  int S, E;char a, b;
  for(int i = 1; i <= N; i++) {
    cin >> a >> S >> b >> E;
    if(a == b) {
      ans += abs(S - E);
    }else {
      seg[++p] = make_pair(S, E);
    }
  }
  sort(seg + 1, seg + p + 1, comp);
//  for(int i = 1; i <= p; i++) cout << seg[i].first << " " << seg[i].second << " ";
  cout << "\n";
  solve();
  cin >> N;
}

Compilation message

bridge.cpp: In function 'long long int balance()':
bridge.cpp:33:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while(mn.size() > tot / 2) {
         ~~~~~~~~~~^~~~~~~~~
bridge.cpp:40:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while(mx.size() > tot / 2) {
         ~~~~~~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 6 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 6 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 6 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 38 ms 3820 KB Output is correct
13 Correct 71 ms 5356 KB Output is correct
14 Correct 53 ms 3948 KB Output is correct
15 Correct 44 ms 3376 KB Output is correct
16 Correct 47 ms 4588 KB Output is correct
17 Correct 61 ms 5228 KB Output is correct
18 Correct 55 ms 4844 KB Output is correct
19 Correct 64 ms 5228 KB Output is correct
20 Correct 50 ms 4844 KB Output is correct
21 Correct 63 ms 4972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 7 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 512 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 7 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 6 ms 384 KB Output is correct
20 Correct 7 ms 384 KB Output is correct
21 Correct 6 ms 384 KB Output is correct
22 Correct 6 ms 384 KB Output is correct
23 Correct 6 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 6 ms 384 KB Output is correct
13 Correct 6 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 436 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 6 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 6 ms 384 KB Output is correct
20 Correct 6 ms 384 KB Output is correct
21 Correct 6 ms 384 KB Output is correct
22 Correct 6 ms 384 KB Output is correct
23 Correct 6 ms 384 KB Output is correct
24 Correct 6 ms 384 KB Output is correct
25 Correct 54 ms 4460 KB Output is correct
26 Correct 94 ms 4716 KB Output is correct
27 Correct 122 ms 5480 KB Output is correct
28 Correct 119 ms 5996 KB Output is correct
29 Correct 125 ms 5992 KB Output is correct
30 Correct 65 ms 3444 KB Output is correct
31 Correct 67 ms 5356 KB Output is correct
32 Correct 114 ms 5972 KB Output is correct
33 Correct 84 ms 5612 KB Output is correct
34 Correct 120 ms 6124 KB Output is correct
35 Correct 76 ms 5608 KB Output is correct
36 Correct 110 ms 5736 KB Output is correct
37 Correct 44 ms 4460 KB Output is correct