aboutsummaryrefslogtreecommitdiff
path: root/contests/Round#809/A/B/main.cpp
diff options
context:
space:
mode:
authoromagdy7 <omar.professional8777@gmail.com>2022-07-18 22:11:40 +0200
committeromagdy7 <omar.professional8777@gmail.com>2022-07-18 22:11:40 +0200
commitadb97626f5553013e2758aeb136c8ffd28436627 (patch)
treefa519c03dcd6dbc61ed350351bb226a7ffd33085 /contests/Round#809/A/B/main.cpp
parent51c35987f270f7a19d83a11b2427a9ff7803e17c (diff)
downloadcompetitive-programming-adb97626f5553013e2758aeb136c8ffd28436627.tar.xz
competitive-programming-adb97626f5553013e2758aeb136c8ffd28436627.zip
Solved a couple of problems
Diffstat (limited to 'contests/Round#809/A/B/main.cpp')
-rw-r--r--contests/Round#809/A/B/main.cpp58
1 files changed, 58 insertions, 0 deletions
diff --git a/contests/Round#809/A/B/main.cpp b/contests/Round#809/A/B/main.cpp
new file mode 100644
index 0000000..90c55c2
--- /dev/null
+++ b/contests/Round#809/A/B/main.cpp
@@ -0,0 +1,58 @@
+#include <bits/stdc++.h>
+
+using namespace std;
+
+bool isCool(vector<int> &a, int i) {
+ if (i == 0) {
+ return false;
+ }
+ if (a[i - 1] < a[i] && a[i + 1] < a[i]) {
+ return true;
+ }
+ return false;
+}
+
+int main() {
+ ios_base::sync_with_stdio(false);
+ cin.tie(NULL);
+ int tt;
+ cin >> tt;
+ while (tt--) {
+ int n;
+ cin >> n;
+ vector<int> a(n);
+ for (int &x : a)
+ cin >> x;
+ long long ans = 0;
+ long long mx = 0;
+ int max_cool = (n - 1) / 2;
+ int cnt = 0;
+ if(n > 3) {
+ for (int i = 1; i < n - 1; i++) {
+ if(isCool(a,i)) {
+ cnt++;
+ }
+ if (!isCool(a, i) && i % 2 == 0) {
+ ans += abs(a[i] - max(a[i - 1], a[i + 1])) + 1;
+ } else if(!isCool(a, i) && i % 2 != 0) {
+ mx += abs(a[i] - max(a[i - 1], a[i + 1])) + 1;
+ }
+ }
+ long long mx2 = 0;
+ for (int i = 1; i < n - 1; i += 3) {
+ if (!isCool(a, i)) {
+ mx2 += abs(a[i] - max(a[i - 1], a[i + 1])) + 1;
+ }
+ }
+ cerr << mx << " " << ans << " " << mx2 << endl;
+ if(cnt < max_cool) {
+ // cout << max(mx2, max(ans, mx)) << endl;
+ } else {
+ cout << min(mx2, min(ans, mx)) << endl;
+ }
+
+ } else {
+ cout << abs(a[1] - max(a[0], a[2])) + 1 << endl;
+ }
+ }
+}