답안 #316596

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
316596 2020-10-26T19:19:16 Z tushar_2658 Palembang Bridges (APIO15_bridge) C++14
100 / 100
323 ms 24808 KB
#include "bits/stdc++.h"
using namespace std;

const int maxn = 200005;
using ll = long long;

struct DS {
  multiset<int> P, Q;
  ll sumP, sumQ;
  DS(){
    sumP = sumQ = 0;
  }
  void add(int x){
    if(P.empty()){
      P.insert(x);
      sumP += x;
    }else if(*P.rbegin() < x){
      Q.insert(x);
      sumQ += x;
    }else {
      P.insert(x);
      sumP += x;
    }
    if(P.size() < Q.size()){
      sumP += *Q.begin();
      sumQ -= *Q.begin();
      P.insert(*Q.begin());
      Q.erase(Q.begin());
    }
    if(P.size() > Q.size() + 1){
      sumP -= *P.rbegin();
      sumQ += *P.rbegin();
      Q.insert(*P.rbegin());
      P.erase(P.find(*P.rbegin()));
    }
  }
  ll get(){
    ll mid = *P.rbegin();
    ll ans = sumQ - sumP;
    ans -= mid * Q.size();
    ans += mid * P.size();
    return ans;
  }
};

int main(int argc, char const *argv[])
{
  ios::sync_with_stdio(false);
  cin.tie(0);

  int k, n;
  cin >> k >> n;
  vector<pair<ll, ll>> v;
  ll ans = 0;
  for(int i = 0; i < n; ++i){
    char c, c1;
    ll x, y;
    cin >> c >> x >> c1 >> y;
    if(c == c1){
      ans += llabs(x - y);
    }else {
      ++ans;
      if(x > y)swap(x, y);
      v.push_back({x, y});
    }
  }
  if(v.empty()){
    cout << ans << endl;
    return 0;
  }
  n = v.size();
  sort(v.begin(), v.end(), [&](pair<ll, ll> x, pair<ll, ll> y){
    return x.first + x.second < y.first + y.second;
  });
  DS S, T; 
  vector<ll> pref(n + 1), suff(n + 1);
  for(int i = 0; i < n; ++i){
    S.add(v[i].first);
    S.add(v[i].second);
    pref[i] = S.get();
  }
  for(int i = n - 1; i >= 0; --i){
    T.add(v[i].first);
    T.add(v[i].second);
    suff[i] = T.get();
  }
  ll res = 1e18;
  for(int i = 0; i < n; ++i){
    res = min(res, pref[i] + suff[i + 1]);
  }
  if(k == 1){
    printf("%lld\n", pref[n - 1] + ans);
  }else {
    printf("%lld\n", ans + res);
  }

  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 154 ms 22264 KB Output is correct
13 Correct 323 ms 24552 KB Output is correct
14 Correct 252 ms 21224 KB Output is correct
15 Correct 133 ms 14576 KB Output is correct
16 Correct 116 ms 23912 KB Output is correct
17 Correct 159 ms 24552 KB Output is correct
18 Correct 108 ms 24168 KB Output is correct
19 Correct 160 ms 24552 KB Output is correct
20 Correct 162 ms 24168 KB Output is correct
21 Correct 165 ms 24316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 512 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
15 Correct 2 ms 512 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 1 ms 512 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 2 ms 512 KB Output is correct
21 Correct 1 ms 640 KB Output is correct
22 Correct 2 ms 640 KB Output is correct
23 Correct 2 ms 512 KB Output is correct
24 Correct 2 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 512 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
15 Correct 2 ms 512 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 512 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 2 ms 640 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 2 ms 512 KB Output is correct
23 Correct 2 ms 512 KB Output is correct
24 Correct 2 ms 512 KB Output is correct
25 Correct 154 ms 23016 KB Output is correct
26 Correct 216 ms 23168 KB Output is correct
27 Correct 288 ms 24040 KB Output is correct
28 Correct 294 ms 24680 KB Output is correct
29 Correct 312 ms 24808 KB Output is correct
30 Correct 125 ms 13212 KB Output is correct
31 Correct 118 ms 23912 KB Output is correct
32 Correct 163 ms 24552 KB Output is correct
33 Correct 110 ms 24168 KB Output is correct
34 Correct 164 ms 24684 KB Output is correct
35 Correct 165 ms 24192 KB Output is correct
36 Correct 164 ms 24376 KB Output is correct
37 Correct 160 ms 23016 KB Output is correct