Submission #197847

# Submission time Handle Problem Language Result Execution time Memory
197847 2020-01-23T16:04:21 Z quocnguyen1012 Palembang Bridges (APIO15_bridge) C++14
100 / 100
419 ms 14668 KB
#include <bits/stdc++.h>

#define fi first
#define se second
#define mp make_pair
#define pb push_back

using namespace std;
typedef long long ll;

const int maxn = 1e5 + 5;

pair<int, int> a[maxn];
int n, k, N;

vector<ll> calc(void)
{
  vector<ll> ret(n + 5);
  multiset<int> l, r;
  ll suml = 0, sumr = 0;
  for (int i = 1; i <= n; ++i){
    suml += a[i].fi;
    sumr += a[i].se;
    l.insert(a[i].fi); r.insert(a[i].se);
    while (*l.rbegin() > *r.begin()){
      sumr += *l.rbegin() - *r.begin();
      suml += *r.begin() - *l.rbegin();
      l.insert(*r.begin());
      r.insert(*l.rbegin());
      l.erase(l.find(*l.rbegin()));
      r.erase(r.begin());
    }
    ret[i] = sumr - suml;
  }
  return ret;
}

signed main(void)
{
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  if (fopen("A.INP", "r")){
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  }
  cin >> k >> N;
  ll sum = 0;
  while (N--){
    int l, r;
    char t1, t2;
    cin >> t1 >> l >> t2 >> r;
    if (t1 == t2) sum += abs(l - r);
    else a[++n] = mp(l, r);
  }
  sort(a + 1, a + 1 + n, [&](pair<int, int> x, pair<int, int> y){
    return x.fi + x.se < y.fi + y.se;
  });
  vector<ll> pref = calc();
  reverse(a + 1, a + 1 + n);
  vector<ll> suff = calc();
  if (k == 1){
    cout << sum + n + pref[n];
  }
  else{
    ll res = 1e17;
    //cerr << sum << ' ' << n << '\n';
    for (int i = 0; i <= n; ++i){
      res = min(res, pref[i] + suff[n - i]);
    }
    cout << sum + n + res << '\n';
  }
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:42:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen("A.INP", "r", stdin);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
bridge.cpp:43:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen("A.OUT", "w", stdout);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 504 KB Output is correct
4 Correct 4 ms 504 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 3 ms 248 KB Output is correct
8 Correct 3 ms 376 KB Output is correct
9 Correct 3 ms 504 KB Output is correct
10 Correct 10 ms 504 KB Output is correct
11 Correct 3 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 504 KB Output is correct
4 Correct 4 ms 504 KB Output is correct
5 Correct 4 ms 376 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 0 ms 376 KB Output is correct
8 Correct 3 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 3 ms 504 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
12 Correct 195 ms 12180 KB Output is correct
13 Correct 419 ms 12144 KB Output is correct
14 Correct 373 ms 10872 KB Output is correct
15 Correct 197 ms 7288 KB Output is correct
16 Correct 183 ms 12140 KB Output is correct
17 Correct 277 ms 12160 KB Output is correct
18 Correct 148 ms 12224 KB Output is correct
19 Correct 262 ms 12152 KB Output is correct
20 Correct 236 ms 12276 KB Output is correct
21 Correct 287 ms 12280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 380 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 7 ms 504 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 0 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 248 KB Output is correct
6 Correct 2 ms 380 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 1 ms 376 KB Output is correct
13 Correct 4 ms 504 KB Output is correct
14 Correct 4 ms 504 KB Output is correct
15 Correct 4 ms 504 KB Output is correct
16 Correct 3 ms 412 KB Output is correct
17 Correct 3 ms 376 KB Output is correct
18 Correct 3 ms 376 KB Output is correct
19 Correct 3 ms 504 KB Output is correct
20 Correct 2 ms 508 KB Output is correct
21 Correct 4 ms 504 KB Output is correct
22 Correct 4 ms 632 KB Output is correct
23 Correct 4 ms 504 KB Output is correct
24 Correct 4 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 420 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 3 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 3 ms 504 KB Output is correct
14 Correct 4 ms 504 KB Output is correct
15 Correct 4 ms 504 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 3 ms 376 KB Output is correct
18 Correct 3 ms 380 KB Output is correct
19 Correct 3 ms 504 KB Output is correct
20 Correct 3 ms 504 KB Output is correct
21 Correct 4 ms 504 KB Output is correct
22 Correct 3 ms 504 KB Output is correct
23 Correct 3 ms 508 KB Output is correct
24 Correct 3 ms 504 KB Output is correct
25 Correct 194 ms 12820 KB Output is correct
26 Correct 304 ms 13132 KB Output is correct
27 Correct 396 ms 14072 KB Output is correct
28 Correct 405 ms 14500 KB Output is correct
29 Correct 386 ms 14556 KB Output is correct
30 Correct 169 ms 7928 KB Output is correct
31 Correct 190 ms 13904 KB Output is correct
32 Correct 276 ms 14668 KB Output is correct
33 Correct 151 ms 14072 KB Output is correct
34 Correct 264 ms 14584 KB Output is correct
35 Correct 226 ms 14052 KB Output is correct
36 Correct 279 ms 14328 KB Output is correct
37 Correct 149 ms 12916 KB Output is correct