QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#338679#2103. Termityhos_lyricWA 0ms4056kbC++143.3kb2024-02-26 08:13:512024-02-26 08:13:52

Judging History

你现在查看的是最新测评结果

  • [2024-02-26 08:13:52]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:4056kb
  • [2024-02-26 08:13:51]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


/*
  problem
    2-player game
    move: choose an element adjacent to 0, get score, change it to 0
*/

struct Stack {
  vector<Int> as;
  void push(Int a) {
    for (; as.size() >= 2 && as.back() >= a; ) {
      const Int a1 = as.back(); as.pop_back();
      const Int a2 = as.back(); as.pop_back();
      a = a - a1 + a2;
    }
    as.push_back(a);
  }
  void get(vector<Int> &goods, vector<Int> &bads) {
    for (; as.size(); ) {
      if (as.size() == 2 && as[0] >= as[1]) {
        bads.push_back(as[1] - as[0]);
        break;
      }
      goods.push_back(as.back());
      as.pop_back();
    }
  }
};

int N;
vector<Int> A;

int main() {
  for (; ~scanf("%d", &N); ) {
    A.resize(N);
    for (int i = 0; i < N; ++i) {
      scanf("%lld", &A[i]);
    }
    
    vector<Int> goods, bads;
    
    vector<int> cuts;
    for (int i = 0; i < N; ++i) if (!A[i]) {
      cuts.push_back(i);
    }
    const int cutsLen = cuts.size();
    assert(cutsLen >= 1);
    
    Stack f, g;
    for (int i = 0; i < cuts[0]; ++i) f.push(A[i]);
    for (int i = N; --i > cuts[cutsLen - 1]; ) g.push(A[i]);
    f.get(goods, bads);
    g.get(goods, bads);
    
    for (int j = 1; j < cutsLen; ++j) {
      vector<Int> as;
      for (int i = cuts[j - 1] + 1; i < cuts[j]; ++i) {
        Int a = A[i];
        for (; as.size() >= 2 && as[as.size() - 2] <= as.back() && as.back() >= a; ) {
          const Int a1 = as.back(); as.pop_back();
          const Int a2 = as.back(); as.pop_back();
          a = a - a1 + a2;
        }
        as.push_back(a);
      }
      goods.insert(goods.end(), as.begin(), as.end());
    }
    
// cerr<<"goods = "<<goods<<endl;
// cerr<<"bads = "<<bads<<endl;
    
    sort(goods.begin(), goods.end(), greater<Int>{});
    Int ans = 0;
    int sig = +1;
    for (const Int good : goods) {
      ans += sig * good;
      sig = -sig;
    }
    for (const Int bad : bads) {
      ans += sig * bad;
    }
    
    Int sumA = 0;
    for (int i = 0; i < N; ++i) sumA += A[i];
    printf("%lld %lld\n", (sumA + ans) / 2, (sumA - ans) / 2);
  }
  return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3780kb

input:

1
0

output:

0 0

result:

ok single line: '0 0'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3796kb

input:

2
0 17

output:

17 0

result:

ok single line: '17 0'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3832kb

input:

3
0 13 0

output:

13 0

result:

ok single line: '13 0'

Test #4:

score: 0
Accepted
time: 0ms
memory: 3812kb

input:

8
1 2 0 3 7 4 0 9

output:

17 9

result:

ok single line: '17 9'

Test #5:

score: 0
Accepted
time: 0ms
memory: 4056kb

input:

10
0 0 0 1 0 0 0 5 0 0

output:

5 1

result:

ok single line: '5 1'

Test #6:

score: 0
Accepted
time: 0ms
memory: 3788kb

input:

24
55 33 93 169 237 0 219 125 39 13 96 166 200 278 0 99 2 18 110 0 58 57 0 42

output:

1125 984

result:

ok single line: '1125 984'

Test #7:

score: -100
Wrong Answer
time: 0ms
memory: 3780kb

input:

17
805750847 0 100661314 433925858 0 84353896 0 0 998898815 548233368 610515435 585990365 374344044 760313751 477171088 356426809 945117277

output:

3924326413 3157376454

result:

wrong answer 1st lines differ - expected: '4023568786 3058134081', found: '3924326413 3157376454'