Submission #700860

# Submission time Handle Problem Language Result Execution time Memory
700860 2023-02-19T09:28:42 Z Abrar_Al_Samit Palembang Bridges (APIO15_bridge) C++17
78 / 100
280 ms 13552 KB
#include<bits/stdc++.h>
using namespace std;

#define pii pair<int,int>

const long long INF = 1e18;
void PlayGround() {
  int k, n;
  cin>>k>>n;

  long long ans = 0;
  vector<pair<int,int>>bag;
  for(int i=0; i<n; ++i) {
    char p, q;
    int s, t;
    cin>>p>>s>>q>>t;
    if(s>t) swap(s, t);
    if(p==q) {
      ans += t - s;
    } else {
      bag.emplace_back(s, t);
    }
  }  

  int m = bag.size();
  ans += m;

  if(m==0) {
    cout<<ans<<'\n';
    return;
  }

  sort(bag.begin(), bag.end(), [&] (pii x, pii y) {
    return x.first+x.second<y.first+y.second;
  });

  long long pre[m];

  long long S1 = 0, S2 = 0;
  multiset<int>m1, m2;
  for(int i=0; i<m; ++i) {
    m1.insert(bag[i].first), m2.insert(bag[i].second);
    S1 += bag[i].first;
    S2 += bag[i].second;

    while(*prev(m1.end())>*m2.begin()) {
      S1 -= *prev(m1.end());
      S1 += *m2.begin();
      S2 -= *m2.begin();
      S2 += *prev(m1.end());

      m2.insert(*prev(m1.end()));
      m1.erase(prev(m1.end()));

      m1.insert(*m2.begin());
      m2.erase(m2.begin());
    }

    int med = *m2.begin();

    pre[i] = med * (i+1) - S1 + S2 - med * (i+1);
  }

  long long best = INF;
  S1 = S2 = 0;
  m1.clear(), m2.clear();
  for(int i=m-1; i>=0; --i) {
    m1.insert(bag[i].first), m2.insert(bag[i].second);
    S1 += bag[i].first;
    S2 += bag[i].second;

    while(*prev(m1.end())>*m2.begin()) {
      S1 -= *prev(m1.end());
      S1 += *m2.begin();
      S2 -= *m2.begin();
      S2 += *prev(m1.end());

      m2.insert(*prev(m1.end()));
      m1.erase(prev(m1.end()));

      m1.insert(*m2.begin());
      m2.erase(m2.begin());
    }

    long long med = *m2.begin();

    long long cur = med * (m-i) - S1 + S2 - med * (m-i);
    if(i) cur += pre[i-1];
    best = min(best, cur);
  }

  ans += best;
  cout<<ans<<'\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  PlayGround();
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Incorrect 1 ms 340 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 236 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Incorrect 1 ms 340 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 328 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 1 ms 316 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 324 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 324 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 320 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 1 ms 316 KB Output is correct
9 Correct 0 ms 320 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 388 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 2 ms 352 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 110 ms 11976 KB Output is correct
26 Correct 183 ms 12220 KB Output is correct
27 Correct 209 ms 12976 KB Output is correct
28 Correct 235 ms 13492 KB Output is correct
29 Correct 280 ms 13508 KB Output is correct
30 Correct 85 ms 7276 KB Output is correct
31 Correct 109 ms 12884 KB Output is correct
32 Correct 170 ms 13540 KB Output is correct
33 Correct 101 ms 13224 KB Output is correct
34 Correct 171 ms 13552 KB Output is correct
35 Correct 146 ms 13060 KB Output is correct
36 Correct 178 ms 13244 KB Output is correct
37 Correct 106 ms 12048 KB Output is correct