QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#736524#5420. InscryptionYoruKiriWA 101ms3640kbC++202.9kb2024-11-12 11:37:382024-11-12 11:37:39

Judging History

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

  • [2024-11-12 11:37:39]
  • 评测
  • 测评结果:WA
  • 用时:101ms
  • 内存:3640kb
  • [2024-11-12 11:37:38]
  • 提交

answer

/*--------------------------
YoruKiri         2024.11.12
https://qoj.ac/problem/5420
--------------------------*/
#include <bits/stdc++.h>
#define rep(i, j, k) for(int i=(j);i<=(k);++i)
#define per(i, j, k) for(int i=(j);i>=(k);--i)
#define mem(a, b) memset(a,b,sizeof(a))
#define pii pair<int,int>
#define sz(x) (int)(x).size()
#define lson (p<<1)
#define rson (p<<1|1)
#define int long long
#define pb push_back
#define eb emplace_back
#define enter putchar('\n')
#define space putchar(' ')
#define yes "YES"
#define no "NO"
const int mod=998244353, N=1e5+5, INF=0x3f3f3f3f3f3f3f3f;
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') f = c == '-' ? -1 : f, c = getchar();
  while (c >= '0' && c <= '9') x = (x<<3)+(x<<1)+(c^48), c = getchar();
  return x*f;
}
inline void write(int x) {
  if (x < 0) x = -x, putchar('-');
  if (x > 9) write(x/10);
  putchar('0' + x%10);
}

struct choice {
  int p, q;
  bool operator > (const choice& other) const {
    return p * other.q > q * other.p;
  }
  bool operator < (const choice& other) const {
    return p * other.q < q * other.p;
  }
  friend choice operator + (const choice& is, const choice& other) {
    return {is.p + other.p, is.q + other.q};
  }
};
const choice op1 = {1, 1}, op2 = {0, -1};

int gcd(int a, int b) {
  if(b)
    while((a %= b)&&(b %= a));
  return a + b;
}

void solve() {
  int n = read();
  vector<int> a(n + 1);
  int sum = 0, cnt = 0, ans = 0;
  rep(i, 1, n) a[i] = read();
  vector<vector<choice>> dp(n+1, vector<choice>(2, choice{0, 0}));
  dp[0][0] = dp[0][1] = op1;
  rep(i, 1, n) {
    if (a[i] == 0) {
      dp[i][0] = max(dp[i-1][0] + op1, dp[i-1][1] + op1);
      if (dp[i-1][0].q > 1 && dp[i-1][1].q > 1)
        dp[i][1] = max(dp[i-1][0] + op2, dp[i-1][1] + op2);
      else if (dp[i-1][0].q > 1)
        dp[i][1] = dp[i][0] + op2;
      else if (dp[i-1][1].q > 1)
        dp[i][1] = dp[i-1][1] + op2;
      else {
        dp[i][1] = dp[i][0];
      }
    } else if (a[i] == 1)
      dp[i][0] = dp[i][1] = max(dp[i-1][0] + op1, dp[i-1][1] + op1);
    else if (a[i] == -1) {
      if (dp[i-1][0].q > 1 && dp[i-1][1].q > 1)
        dp[i][1] = max(dp[i-1][0] + op2, dp[i-1][1] + op2);
      else if (dp[i-1][0].q > 1)
        dp[i][1] = dp[i-1][0] + op2;
      else if (dp[i-1][1].q > 1)
        dp[i][1] = dp[i-1][1] + op2;
      else {
        write(-1), enter;
        return;
      }
    }
    // printf("dp[%lld][0] = %lld/%lld dp[%lld][1] = %lld/%lld\n", i, dp[i][0].p, dp[i][0].q, i, dp[i][1].p, dp[i][1].q);
  }
  if(dp[n][0] > dp[n][1])
    write(dp[n][0].p/gcd(dp[n][0].p, dp[n][0].q)), space, write(dp[n][0].q/gcd(dp[n][0].p, dp[n][0].q)), enter;
  else
    write(dp[n][1].p/gcd(dp[n][1].p, dp[n][1].q)), space, write(dp[n][1].q/gcd(dp[n][1].p, dp[n][1].q)), enter;
}

signed main() {
  int t = 1;
  t = read();
  while(t --) {
    solve();
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

6
7
1 1 1 -1 1 1 -1
4
1 0 -1 0
4
0 -1 -1 0
1
0
2
0 0
1
-1

output:

3 2
3 1
-1
1 1
2 1
-1

result:

ok 6 lines

Test #2:

score: 0
Accepted
time: 101ms
memory: 3612kb

input:

1000000
1
1
1
-1
1
1
1
1
1
1
1
1
1
-1
1
-1
1
0
1
0
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1
-1
3 1
-1
1 1
-1
2 1
1 1
-1
1 1
-1
1 1
3 2
-1
-1
-1
-1
3 2
5 1
1 1
-1
3 1
-1
-1
1 1
-1
6 1
3 2
-1
3 2
4 3
2 1
-1
5 3
3 1
6 1
-1
2 1
5 4
-1
1 1
-1
3 1
-1
-1
5 3
1 1
2 1
5 2
-1
3 1
4 3
...

result:

wrong answer 7th lines differ - expected: '3 1', found: '-1'