Codeforces Hello 2024 1919C - Grouping Increases Solution
Problem Link : https://codeforces.com/contest/1919/problem/C
Solution in C++:
- /// Author : AH_Tonmoy
- #include <bits/stdc++.h>
- using namespace std;
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int t;
- cin >> t;
- while (t--) {
- int n ; cin >> n ;
- int a[n+1] ;
- for ( int i = 0 ; i < n ; i++){
- cin >> a[i] ;
- }
- int last_S = n + 5 ;
- int last_T = n + 5 ;
- int ans = 0 ;
- for ( int i = 0 ; i < n ; i++){
- if ( last_S > last_T) {
- swap(last_S,last_T) ;
- }
- if (a[i] <= last_S){
- last_S = a[i] ;
- }
- else if (a[i] <= last_T){
- last_T = a[i] ;
- }
- else {
- ans++ ;
- last_S = a[i] ;
- }
- }
- cout << ans <<'\n';
- }
- return 0 ;
- }
No comments