Submission #1287890

#TimeUsernameProblemLanguageResultExecution timeMemory
1287890muhammad-ahmadLine Town (CCO23_day1problem3)C++20
0 / 25
1 ms572 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define endl '\n' void solve(){ int n; cin >> n; int a[n + 2] = {}; for (int i = 1; i <= n; i++) cin >> a[i]; vector<int> r(n + 1, -1); r[0] = 0; int c0 = 0, c1 = 0; for (int i = 1; i <= n; i++){ if (a[i] == -1){ if (i % 2) c1++; else c0++; if (c0 == c1){ r[i] = c0; } else r[i] = -1; } else r[i] = r[i - 1]; } vector<int> s(n + 2, -1); s[n + 1] = 0; c0 = 0, c1 = 0; for (int i = n; i >= 1; i--){ if (a[i] == 1){ if (i % 2) c1++; else c0++; if (c0 == c1){ s[i] = c0; } else s[i] = -1; } else s[i] = s[i + 1]; } // cout << s[2] << endl; int ans = 1e18; for (int i = 1; i <= n + 1; i++){ if (s[i] != -1 && r[i - 1] != -1){ ans = min(ans, s[i] + r[i - 1]); } } if (ans == 1e18) ans = -1; cout << ans << endl; } signed main(){ ios::sync_with_stdio(0); cin.tie(0); int tc = 1; // cin >> tc; while (tc--){ solve(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...