Codeforces Round 865 (Div. 2) 1816C. Ian and Array Sorting Solution


 

Problem Link : https://codeforces.com/contest/1816/problem/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. vector<long long> a(n);
  13. for (int i = 0; i < n; i++) {
  14. cin >> a[i];
  15. }
  16. for (int i = 1; i < n - 1; i++) {
  17. if (a[i] < a[i - 1]) {
  18. a[i + 1] +=(a[i - 1] - a[i]);
  19. a[i] = a[i - 1];
  20. }
  21. }
  22. for (int i = n - 2; i > 0; i--) {
  23. if (a[i] > a[i + 1]) {
  24. a[i - 1] -= (a[i] - a[i + 1]);
  25. a[i] = a[i + 1];
  26. }
  27. }
  28. if (is_sorted(a.begin(), a.end()))
  29. cout << "YES" << endl;
  30. else
  31. cout << "NO" << endl;
  32. }
  33. }

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 ...

Powered by Blogger.