AtCoder AISing Programming Contest 2019 Task D. Nearest Card Game

Posted patt

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AtCoder AISing Programming Contest 2019 Task D. Nearest Card Game相关的知识,希望对你有一定的参考价值。

题目分析在代码注释里。

int main() {
#if defined LOCAL && !defined DUIPAI
  ifstream in("main.in");
  cin.rdbuf(in.rdbuf());
//  ofstream out("main.out");
//  cout.rdbuf(out.rdbuf());
#endif
  int n, q;
  scan(n, q);
  vi a(n);
  scan(a);
  vl sum(n);
  sum[0] = a[0];
  rng (i, 1, n) {
    sum[i] = a[i] + sum[i - 1];
  }
  vl parity_sum(n);
  parity_sum[0] = a[0];
  parity_sum[1] = a[1];
  rng (i, 2, n) {
    parity_sum[i] = a[i] + parity_sum[i - 2];
  }

  rep (q) {
    int x;
    scan(x);
    // 二分查找Aoki拿走的最大的数。
    // 设Aoki拿走的最大的数是a[i]。
    // 拿了a[i]之后,Aoki手里共有 i + 1 - LB(a, x - abs(x - a[i])) 个数。
    // 而此时Takahashi手里至多有n - i 个数,因此i必须满足i + 1 - LB(a, x - abs(x - a[i])) <= n - i
    int l = 0, r = n - 1;
    while (l + 1 < r) {
      bool flag = false;
      int mid = get_mid(l, r);
      int at_most = n - 1 - mid;
      auto t = LB(a, x - abs(x - a[mid]));
      int taken = mid - t + 1;
      if (taken <= at_most) {
        l = mid;
      } else {
        r = mid;
      }
    }
    ll ans = sum[n - 1] - sum[l];
    int num = n - 1 - l;
    if (n - 2 * num - 1 >= 0) {
      ans += parity_sum[n - 2 * num - 1];
    }
    println(ans);
  }
#if defined LOCAL && !defined DUIPAI
  cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.
";
#endif
  return 0;
}

以上是关于AtCoder AISing Programming Contest 2019 Task D. Nearest Card Game的主要内容,如果未能解决你的问题,请参考以下文章

AISing Programming Contest 2021(AtCoder Beginner Contest 202)ABCD

AISing Programming Contest 2021(AtCoder Beginner Contest 202)ABCD

AISing Programming Contest 2021(AtCoder Beginner Contest 202) E

AISing Programming Contest 2021(AtCoder Beginner Contest 202)(补题)

AISing Programming Contest 2021(AtCoder Beginner Contest 202)D - aab aba baa

Programmin in Lua的翻译