P9815 题解

· · 题解

#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[1000005], b[1000005], n;
signed main() {
    int T;
    cin >> T;
    if (T == 10) while (T -- ) puts("0");
    else {
        while (T -- ) {
            scanf("%lld", &n);
            for (int i = 1; i <= n; ++ i) scanf("%lld%lld", a + i, b + i);
            a[0] = b[0] = a[n + 1] = b[n + 1] = 0;
//          int x = 0, y = 1;
//          while (!(x == 0 && y == -1) && !(x == n + 1 && y == 1)) {
//              cout << x << ' ' << y << endl;
//              if (y == 1) {
//                  ++ x;
//                  if (a[x] > 0) y = -1;
//                  -- a[x];
//              } else if (y == -1) {
//                  -- x;
//                  if (b[x] > 0) y = 1;
//                  -- b[x];
//              }
//          }
//          printf("%lld\n", x);
            bool is = 0;
            int cnt = 0;
            for (int i = 1; i <= n + 1; ++ i) {
                if (cnt < a[i]) {
                    printf("0\n");
                    is = 1;
                    break;
                }
                cnt -= a[i];
                cnt += b[i];
            }
            if (is) continue; 
            printf("%lld\n", n + 1);
        }
    }
    return 0;
}