Submission #111185

#TimeUsernameProblemLanguageResultExecution timeMemory
111185oToToTPalembang Bridges (APIO15_bridge)C++14
100 / 100
100 ms2808 KiB
#include <cstdio>
#include <algorithm>
#include <functional>

using namespace std;

const greater<int> gr;

struct Median {
  int sz;
  int mn[100007];
  int mx[100007];
  void clear() { sz = 0; }
  long long insert(int a, int b, long long d) { // a <= b
    if (!sz) return sz++, d + (*mx = b) - (*mn = a);
    if (b < *mn) {
      d += *mn * 2 - a - b;
      mx[sz] = *mn;
      pop_heap(mn, mn + sz);
      mn[sz - 1] = a; mn[sz] = b;
      push_heap(mn, mn + sz);
    } else if (a > *mx) {
      d += a + b - *mx * 2;
      mn[sz] = *mx;
      pop_heap(mx, mx + sz, gr);
      mx[sz - 1] = a; mx[sz] = b;
      push_heap(mx, mx + sz, gr);
    } else {
      d += b - a;
      mn[sz] = a; mx[sz] = b;
    }
    push_heap(mn, mn + ++sz);
    push_heap(mx, mx + sz, gr);
    return d;
  }
} med;

struct Point {
  int a, b;
  bool operator<(const Point& pt) const { return a + b < pt.a + pt.b; }
} pt[100003];

int g[200003];
long long ansl[100003];

int main() {
  int K, N;
  scanf("%d%d", &K, &N);
  char ca[3], cb[3];
  if (K == 1) {
    int a, b, tN = 0;
    long long ans = 0;
    for (int i = 0; i < N; i++) {
      scanf("%s%d%s%d", ca, &a, cb, &b);
      if (*ca == *cb) ans += abs(b - a);
      else g[tN++] = a, g[tN++] = b, ans++;
    }
    nth_element(g, g + (tN / 2), g + tN);
    for (int i = 0; i < tN / 2; i++) ans += g[tN / 2] - g[i];
    for (int i = tN / 2; i < tN; i++) ans += g[i] - g[tN / 2];
    printf("%lld", ans);
    return 0;
  }

  int tN = 0;
  long long pre = 0;
  for (int i = 0; i < N; i++) {
    auto& z = pt[tN];
    scanf("%s%d%s%d", ca, &z.a, cb, &z.b);
    if (z.a > z.b) swap(z.a, z.b);
    if (*ca == *cb) pre += z.b - z.a;
    else pre++, tN++;
  }

  if (tN) {
    sort(pt, pt + tN);
    med.clear();
    for (int i = 0; i < tN; i++) ansl[i + 1] = med.insert(pt[i].a, pt[i].b, ansl[i]);

    long long ans = ansl[tN], now = 0;
    med.clear();
    for (int i = tN - 1; i >= 0; i--) {
      now = med.insert(pt[i].a, pt[i].b, now);
      if (now + ansl[i] < ans) ans = now + ansl[i];
    }
    printf("%lld", ans + pre);
  } else printf("%lld", pre);
}

Compilation message (stderr)

bridge.cpp: In function 'int main()':
bridge.cpp:48:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &K, &N);
   ~~~~~^~~~~~~~~~~~~~~~
bridge.cpp:54:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       scanf("%s%d%s%d", ca, &a, cb, &b);
       ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%s%d%s%d", ca, &z.a, cb, &z.b);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...