QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#736581 | #5420. Inscryption | YoruKiri | WA | 103ms | 3612kb | C++20 | 3.0kb | 2024-11-12 11:49:42 | 2024-11-12 11:49:42 |
Judging History
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-1][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);
// dp[i][0] = dp[i][1] = max(dp[i][0], dp[i][1]);
}
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: 3552kb
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: 103ms
memory: 3548kb
input:
1000000 1 1 1 -1 1 1 1 1 1 1 1 1 1 -1 1 -1 1 0 1 0 1 1 1 0 1 -1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 0 1 0 1 1 1 -1 1 1 1 1 1 -1 1 0 1 1 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