From 8066484f2049dc0616e9e1f99ee13d79c3da35d3 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Tue, 7 Feb 2023 10:11:23 +0800 Subject: [PATCH] G - Inscryption https://codeforces.com/gym/104128/submission/192548681 --- Gym/104128/G/G.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Gym/104128/G/G.cpp diff --git a/Gym/104128/G/G.cpp b/Gym/104128/G/G.cpp new file mode 100644 index 00000000..ca7dfb22 --- /dev/null +++ b/Gym/104128/G/G.cpp @@ -0,0 +1,60 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + + cin >> t; + + while (t--) { + int n, cnt = 0, cnt_del = 0; + bool flag = false; + + cin >> n; + + for (int i = 1, x; i <= n; i++) { + cin >> x; + + if (x == 1) { + cnt++; + } else if (x == -1) { + cnt--; + + if (cnt < 0) { + if (cnt_del) { + cnt_del--; + cnt += 2; + } else { + flag = true; + } + } + } else { // x == 0 + if (cnt) { + cnt--; + cnt_del++; + } else { + cnt++; + } + } + } + + if (flag) { + cout << -1 << endl; + } else { + int p = cnt + (n - cnt) / 2 + 1, + q = cnt + 1, + g = std::__gcd(p, q); + + cout << p / g << ' ' << q / g << endl; + } + } + + return 0; +}