LA 7578. ACM World Finals 2016 L. Swap Space

Posted Planet6174

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LA 7578. ACM World Finals 2016 L. Swap Space相关的知识,希望对你有一定的参考价值。

贪心

#include <cstdio>
#include <algorithm>
using std::sort;

#define kN 1000000

struct vv {
  int a, b;
  vv(int a = 0, int b = 0): a(a), b(b) {}
}p[kN], q[kN];

bool cmp1(vv xx, vv yy) {
  if (xx.a == yy.a)
    return xx.b > yy.b;
  return xx.a < yy.a;
}
bool cmp2(vv xx, vv yy) {
  if (xx.b == yy.b)
    return xx.a < yy.a;
  return xx.b > yy.b;
}

int main() {
  int n, cnt1 = 0, cnt2 = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int s, t;
    scanf("%d%d", &s, &t);
    if (s < t)
      p[cnt1++] = vv(s, t);
    else
      q[cnt2++] = vv(s, t);
  }
  sort(p, p + cnt1, cmp1);
  sort(q, q + cnt2, cmp2);
  long long ans = 0, space = 0;
  for (int i = 0; i < cnt1; i++) {
    space -= p[i].a;
    if (space < 0) {
      ans += -space;
      space = 0; 
    }
    space += p[i].b;
  }
  for (int i = 0; i < cnt2; i++) {
    space -= q[i].a;
    if (space < 0) {
      ans += -space;
      space = 0; 
    }
    space += q[i].b;
  }
  printf("%lld\n", ans);
  return 0;
}

以上是关于LA 7578. ACM World Finals 2016 L. Swap Space的主要内容,如果未能解决你的问题,请参考以下文章

LA 7155. ACM-ICPC World Finals 2015 F. Keyboarding

LA 8043. ACM-ICPC World Finals 2017 E. Need for Speed

LA 7150. ACM-ICPC World Finals 2015 A. Amalgamated Artichokes

[UVa 213]Message Decoding,ACM/ICPC World Finals 1991 信息解码

信息解码,ACM/ICPC World Finals 1991

2018 ACM-ICPC World Finals - Beijing