Codeforces Hello 2024 1919C - Grouping Increases Solution


 

Problem Link : https://codeforces.com/contest/1919/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 ; cin >> n ;
  11. int a[n+1] ;
  12. for ( int i = 0 ; i < n ; i++){
  13. cin >> a[i] ;
  14. }
  15. int last_S = n + 5 ;
  16. int last_T = n + 5 ;
  17. int ans = 0 ;
  18. for ( int i = 0 ; i < n ; i++){
  19. if ( last_S > last_T) {
  20. swap(last_S,last_T) ;
  21. }
  22. if (a[i] <= last_S){
  23. last_S = a[i] ;
  24. }
  25. else if (a[i] <= last_T){
  26. last_T = a[i] ;
  27. }
  28. else {
  29. ans++ ;
  30. last_S = a[i] ;
  31. }
  32. }
  33. cout << ans <<'\n';
  34. }
  35. return 0 ;
  36. }

No comments

Most View Post

Recent post

Codeforces Round 925 (Div. 3) 1931D. Divisible Pairs Solution

    Problem Link  :   https://codeforces.com/contest/1931/problem/D S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. ...

Powered by Blogger.