Educational Codeforces Round 148 (Rated for Div. 2) 1832C - Contrast Value Solution



 Problem Link : https://codeforces.com/problemset/problem/1832/C

Solution in C++:

  1. /// Author : AH_Tonmoy
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. int32_t main() {
  5. ios_base::sync_with_stdio(0);
  6. cin.tie(0);
  7. int t;
  8. cin >> t;
  9. while (t--) {
  10. int n;
  11. cin >> n;
  12. std::vector<int> a(n);
  13. long long sum = 0;
  14. for (int i = 0; i < n; i++) {
  15. cin >> a[i];
  16. if (i > 0) {
  17. sum += abs(a[i] - a[i - 1]);
  18. }
  19. }
  20. if (sum == 0) {
  21. cout << "1\n";
  22. } else {
  23. int ans = 2;
  24. n = unique(a.begin(), a.end()) - a.begin();
  25. for (int i = 1; i < n - 1; i++) {
  26. if ((a[i] > a[i - 1] && a[i] > a[i + 1]) ||
  27. (a[i - 1] > a[i] && a[i + 1] > a[i]))
  28. ans++;
  29. }
  30. cout << ans << "\n";
  31. }
  32. }
  33. return 0;
  34. }

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.