From 0ca41bd4e4cfa52a441c02bf2ce8528f835435a5 Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Sat, 2 Jan 2021 21:50:58 +0800 Subject: [PATCH] =?UTF-8?q?#183.=20=E6=98=9F=E7=A9=BA=E8=AF=AD=E8=A8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://sjzezoj.com/submission/7446 --- S2OJ/183/183.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 S2OJ/183/183.cpp diff --git a/S2OJ/183/183.cpp b/S2OJ/183/183.cpp new file mode 100644 index 00000000..3fd95100 --- /dev/null +++ b/S2OJ/183/183.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +int n, a[110], i; +bool t1, t2; +long long l = 0, r = 1000000000, mid, t[110], top; + +bool check(long long x) { + top = 0; + for (i = 1; i < n; i++) { + if (a[i] == -1) { + t[++top] = x; + } + else if (a[i] > 0) { + t[++top] = a[i]; + } + else if (top > 1) { + t[top - 1] += t[top]; + top--; + } + } + return t[top] <= a[n]; +} + +int main() { + while (cin >> a[++n]) {} + n--; + t1 = check(0); + if (t[top] == a[n]) { + cout << 0 << endl; + return 0; + } + t2 = check(1000000001); + if (t1 == t2) { + cout << -1 << endl; + return 0; + } + while (l < r) { + mid = l + r + 1 >> 1; + if (check(mid)) { + l = mid; + } + else { + r = mid - 1; + } + } + cout << l << endl; + return 0; +} \ No newline at end of file