QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#690849#2016. 贪吃蛇hos_lyric100 ✓199ms17736kbC++144.5kb2024-10-31 05:46:222024-10-31 05:46:22

Judging History

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

  • [2024-10-31 05:46:22]
  • 评测
  • 测评结果:100
  • 用时:199ms
  • 内存:17736kb
  • [2024-10-31 05:46:22]
  • 提交

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")



/*
  pair (value, index)
  (a, u) - (b, v) := (a-b, u)
  
  p[0] < p[1] < ... < p[n-1]
  
  Lemma. n >= 2 && p[1] < p[n-1] - p[0] ==> p[n-1] is safe to eat p[0]
    (proof) n = 2 ==> OK
            p[n-2] < p[n-1] - p[0] ==> OK
            p[n-2] does not eat p[1] ==> OK
            p[n-2] eats p[1]:
              p[n-2] - p[1] < p[n-1] - p[0]
              (p[n-1] - p[0]) is eaten in final result ==> (p[n-2] - p[1]) is eaten in final result
              ==> p[n-2] should not have eaten p[1]
  
  in simulation
  round k (0 <= k <= N-2): (a[k], u[k]) eats (b[k], v[k])
    (N-k) snakes ~~> (N-k-1) snakes
  take 0 <= K < L s.t.
    - 0 <= k < K ==> ((a[k], u[k]) - (b[k], v[k])) becomes not min
    - K <= k < L ==> ((a[k], u[k]) - (b[k], v[k])) becomes min (==> u[k] = v[k+1])
    -      k = L ==> ((a[k], u[k]) - (b[k], v[k])) becomes not min
  
  K exists (K <= N-2)
  v[0], ..., v[K-1]: eaten in final result
  
  if no such L exists, L := N-2
  
  - round L: want to eats
  - round L-1: do not wan to eat
  - round L-2: wan to eat
  - ...
  
  - K == L (mod 2): v[K] is eaten, stop at round K+1
  - K != L (mod 2): stop at round K
  
  round [0, K)
    max decreases
    min increases
    0 <= k < K ==> (a[k], u[k]) - (b[k], v[k]) <= (a[k+1], u[k+1]) - (b[k+1] - v[k+1])
*/

using P = pair<int, int>;

int N;
vector<int> A;

int solve() {
  int K = -1, L = -1;
  deque<P> que[3];
  que[0].resize(N);
  for (int i = 0; i < N; ++i) que[0][i] = make_pair(A[i], i);
  for (int k = 0; ; ++k) {
    assert((int)que[0].size() + (int)que[1].size() >= 2);
    const int hMx = (que[0].size() && (!que[1].size() || que[0].back() > que[1].back())) ? 0 : 1;
    const P mx = que[hMx].back();
    que[hMx].pop_back();
    const int hMn = (que[0].size() && (!que[1].size() || que[0].front() < que[1].front())) ? 0 : 1;
    const P mn = que[hMn].front();
    que[hMn].pop_front();
    const P p(mx.first - mn.first, mx.second);
    assert(!que[1].size() || p < que[1].front());
    if (!que[0].size() || p < que[0].front()) {
      K = k;
      que[hMn].push_front(mn);
      que[hMx].push_back(mx);
      break;
    } else {
      que[1].push_front(p);
    }
  }
  que[2].resize((int)que[0].size() + (int)que[1].size());
  merge(que[0].begin(), que[0].end(), que[1].begin(), que[1].end(), que[2].begin());
  for (int k = K; ; ++k) {
    assert((int)que[2].size() >= 2);
    if ((int)que[2].size() == 2) {
      L = k;
      break;
    }
    const P mx = que[2].back();
    que[2].pop_back();
    const P mn = que[2].front();
    que[2].pop_front();
    const P p(mx.first - mn.first, mx.second);
    assert(que[2].size());
    if (p < que[2].front()) {
      que[2].push_front(p);
    } else {
      L = k;
      break;
    }
  }
  return N - (((L - K) & 1) ? K : (K + 1));
}

int main() {
  int T;
  for (; ~scanf("%d", &T); ) {
    scanf("%d", &N);
    A.resize(N);
    for (int i = 0; i < N; ++i) {
      scanf("%d", &A[i]);
    }
    for (int t = 0; t < T; ++t) {
      if (t) {
        int K;
        scanf("%d", &K);
        for (; K--; ) {
          int X, Y;
          scanf("%d%d", &X, &Y);
          --X;
          A[X] = Y;
        }
      }
      const int ans = solve();
      printf("%d\n", ans);
    }
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 5
Accepted
time: 0ms
memory: 4072kb

input:

10
3
29 46 67
3
1 15 2 34 3 114
3
1 120 2 168 3 224
3
1 65 2 132 3 293
3
1 79 2 192 3 474
3
1 24 2 64 3 562
3
1 396 2 458 3 595
3
1 44 2 317 3 592
3
1 138 2 145 3 572
3
1 602 2 726 3 831

output:

3
1
3
1
1
1
3
1
1
3

result:

ok 10 lines

Test #2:

score: 5
Accepted
time: 0ms
memory: 3856kb

input:

10
3
21 73 85
3
1 53 2 69 3 126
3
1 90 2 244 3 259
3
1 67 2 288 3 320
3
1 247 2 351 3 445
3
1 150 2 483 3 568
3
1 534 2 583 3 590
3
1 114 2 226 3 745
3
1 199 2 436 3 513
3
1 147 2 324 3 555

output:

3
1
3
3
3
3
3
1
3
1

result:

ok 10 lines

Test #3:

score: 5
Accepted
time: 0ms
memory: 4060kb

input:

10
3
31 64 74
3
1 68 2 83 3 98
3
1 1 2 81 3 102
3
1 98 2 140 3 247
3
1 20 2 76 3 276
3
1 276 2 384 3 592
3
1 25 2 275 3 361
3
1 597 2 632 3 764
3
1 72 2 151 3 455
3
1 224 2 546 3 834

output:

3
3
1
1
1
3
1
3
1
1

result:

ok 10 lines

Test #4:

score: 5
Accepted
time: 0ms
memory: 3776kb

input:

10
3
24 62 82
3
1 19 2 28 3 145
3
1 98 2 198 3 278
3
1 25 2 67 3 278
3
1 20 2 254 3 262
3
1 110 2 175 3 585
3
1 87 2 369 3 584
3
1 222 2 524 3 604
3
1 11 2 27 3 420
3
1 241 2 495 3 838

output:

3
1
3
1
3
1
1
3
1
1

result:

ok 10 lines

Test #5:

score: 5
Accepted
time: 0ms
memory: 3772kb

input:

10
10
3 9 37 42 43 55 55 67 69 95
10
1 10 2 15 3 75 4 121 5 129 6 140 7 157 8 158 9 165 10 172
10
1 2 2 34 3 44 4 48 5 129 6 166 7 169 8 170 9 253 10 295
10
1 29 2 65 3 147 4 177 5 208 6 267 7 295 8 306 9 360 10 386
10
1 64 2 131 3 133 4 151 5 186 6 245 7 259 8 324 9 347 10 422
10
1 32 2 52 3 101 4 ...

output:

7
7
5
7
7
5
7
7
5
7

result:

ok 10 lines

Test #6:

score: 5
Accepted
time: 0ms
memory: 3868kb

input:

10
10
1 11 17 21 40 51 54 75 83 91
10
1 27 2 44 3 46 4 52 5 72 6 81 7 125 8 130 9 167 10 175
10
1 14 2 56 3 66 4 68 5 95 6 133 7 164 8 269 9 271 10 274
10
1 60 2 64 3 78 4 111 5 123 6 176 7 210 8 311 9 353 10 354
10
1 25 2 89 3 149 4 172 5 281 6 283 7 338 8 437 9 463 10 470
10
1 129 2 168 3 201 4 34...

output:

5
5
5
5
7
7
5
7
7
5

result:

ok 10 lines

Test #7:

score: 5
Accepted
time: 0ms
memory: 3852kb

input:

10
10
4 13 20 58 62 67 74 77 93 93
10
1 10 2 34 3 58 4 67 5 81 6 87 7 98 8 143 9 152 10 193
10
1 35 2 80 3 100 4 106 5 171 6 173 7 184 8 240 9 278 10 297
10
1 17 2 46 3 48 4 317 5 328 6 364 7 378 8 383 9 384 10 399
10
1 22 2 34 3 58 4 96 5 99 6 100 7 156 8 190 9 414 10 443
10
1 57 2 63 3 78 4 170 5 ...

output:

7
5
7
7
3
7
7
7
7
7

result:

ok 10 lines

Test #8:

score: 5
Accepted
time: 0ms
memory: 3864kb

input:

10
10
4 18 36 41 50 58 59 78 84 92
10
1 42 2 43 3 54 4 54 5 67 6 152 7 153 8 161 9 182 10 190
10
1 9 2 49 3 58 4 64 5 82 6 185 7 258 8 272 9 277 10 286
10
1 10 2 141 3 151 4 169 5 198 6 240 7 282 8 284 9 353 10 387
10
1 3 2 73 3 158 4 196 5 245 6 265 7 311 8 324 9 378 10 448
10
1 26 2 45 3 156 4 157...

output:

7
5
5
7
7
5
5
7
3
5

result:

ok 10 lines

Test #9:

score: 5
Accepted
time: 2ms
memory: 3896kb

input:

10
2000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 8 ...

output:

1226
1222
1227
1231
1245
1189
1237
1219
1224
1255

result:

ok 10 lines

Test #10:

score: 5
Accepted
time: 2ms
memory: 3760kb

input:

10
2000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 ...

output:

1231
1239
1228
1233
1213
1233
1242
1255
1223
1261

result:

ok 10 lines

Test #11:

score: 5
Accepted
time: 2ms
memory: 3760kb

input:

10
2000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 ...

output:

1236
1236
1201
1213
1232
1235
1214
1243
1257
1229

result:

ok 10 lines

Test #12:

score: 5
Accepted
time: 44ms
memory: 4316kb

input:

10
50000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...

output:

30512
30779
30949
30796
30946
30847
30807
30937
30867
30808

result:

ok 10 lines

Test #13:

score: 5
Accepted
time: 44ms
memory: 4312kb

input:

10
50000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...

output:

30619
30660
30793
30727
30877
30771
30846
30589
30800
30975

result:

ok 10 lines

Test #14:

score: 5
Accepted
time: 44ms
memory: 4312kb

input:

10
50000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...

output:

30435
30814
30682
30922
30855
30868
30851
31023
30968
30951

result:

ok 10 lines

Test #15:

score: 5
Accepted
time: 199ms
memory: 17500kb

input:

10
1000000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...

output:

609762
609762
609762
613003
613003
613003
613003
613003
613003
609165

result:

ok 10 lines

Test #16:

score: 5
Accepted
time: 198ms
memory: 17552kb

input:

10
1000000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...

output:

609891
609892
609892
609892
609892
609892
609892
618092
618092
618092

result:

ok 10 lines

Test #17:

score: 5
Accepted
time: 188ms
memory: 17424kb

input:

10
1000000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...

output:

609983
609983
609983
609984
609984
609984
609984
609070
609070
609071

result:

ok 10 lines

Test #18:

score: 5
Accepted
time: 189ms
memory: 17736kb

input:

10
1000000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...

output:

610259
610259
610259
613586
613586
613586
613586
616676
616676
616677

result:

ok 10 lines

Test #19:

score: 5
Accepted
time: 197ms
memory: 17620kb

input:

10
1000000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...

output:

610386
610386
610386
613680
613680
613680
613680
618492
618492
618493

result:

ok 10 lines

Test #20:

score: 5
Accepted
time: 196ms
memory: 17456kb

input:

10
1000000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...

output:

610138
610138
610138
613036
613036
613036
613036
613036
613036
609198

result:

ok 10 lines

Extra Test:

score: 0
Extra Test Passed