1557B - Moamen and k-subarrays - CodeForces Solution


greedy sortings *1100

Please click on ads to support us..

Python Code:

import sys
import math
import itertools

mod = 998244353
def ri(): return int(input())
def rl(): return list(map(int, input().split()))
def rls(): return list(map(str, input().split()))
def rm(): return map(int, input().split())
def isPowerOfTwo(x): return (x and (not(x & (x - 1))))
def decimalToBinary(n): return bin(n).replace("0b", "")


def nextPowerOf2(n):
    n -= 1
    n |= n >> 1
    n |= n >> 2
    n |= n >> 4
    n |= n >> 8
    n |= n >> 16
    n += 1
    return n


def highestPowerof2(x):
    x |= x >> 1
    x |= x >> 2
    x |= x >> 4
    x |= x >> 8
    x |= x >> 16
    return x ^ (x >> 1)


for _ in range(ri()):
    n, k = rm()
    a = rl()
    c = 0
    b = sorted(a)
    d = {}
    for i in range(n):
        d[a[i]] = i
            l = []
    for i in b:
        l.append((i, d[i]))
        d.pop(i)
        c = 0
    for i in range(1, n):
        if l[i][1]-l[i-1][1] != 1:
            c += 1
    if c < k:
        print("YES")
    else:
        print("NO")

C++ Code:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vb = vector<bool>;
using vvb = vector<vb>;
using vi = vector<ll>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vc = vector<char>;
using vvc = vector<vc>;
using vs = vector<string>;
#define pb push_back
#include <bits/stdc++.h>
using namespace std;
// ll mod = 1e9 + 7 ;
// const ll maxn = 1e5+1 ;
// ll fac[maxn],invfact[maxn];
// ll power(ll x, ll y) 
// { 
//     ll res = 1; 
//     x = x % mod; 
  
//     while (y > 0) { 
//         if (y & 1) 
//             res = (res * x) % mod; 
//         y = y >> 1; 
//         x = (x * x) % mod; 
//     } 
 
//     return res; 
// } 
// ll modInverse(ll n) 
// { 
//     return power(n, mod - 2); 
// } 
// ll NCR(ll n, ll r, ll p=mod) 
// {
//     if(r < 0 || n < 0)
//         assert(false);
 
//     if( n < r ) return 0;
 
//     if (r == 0 || r == n) 
//         return 1; 
    
//     return ( fac[n] * invfact[r]  % mod) * invfact[n - r] % mod;  
// }
// void comp()
// {
//     fac[0] = 1, invfact[0] = 1; 
    
//     for (ll i = 1; i < maxn; i++){ 
//         fac[i] = (fac[i - 1] * i) % mod;
//         invfact[i] = modInverse(fac[i]);
//     } 
// }
void solve(){
    ll n , k ;cin>>n>>k ;
    vector<pair<ll,ll>> num(n);
    for (ll i = 0; i < n; i++)
    {
        ll a ;cin>>a ;
        num[i].first = a , num[i].second = i ;
    }
    sort(num.begin(),num.end()) ;
    vector<ll>after(n-1) ;ll req = 1 ;
    for (ll i = 0; i < n-1; i++)
    {
        if (num[i+1].second!=num[i].second+1)
        {
            req++ ;
        }
    }
    // cout<<req<<endl ;
    if(req>k)cout<<"No\n";
    else cout<<"Yes\n";
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // comp() ; -> to use ncr 
    ll t ;cin>>t ;
    while (t--)
    {
        solve();
    }
    
    return 0 ;
}


Comments

Submit
0 Comments
More Questions

1712A - Wonderful Permutation
1712D - Empty Graph
1712B - Woeful Permutation
1712C - Sort Zero
1028B - Unnatural Conditions
735B - Urbanization
746C - Tram
1278B - A and B
1353D - Constructing the Array
1269C - Long Beautiful Integer
1076A - Minimizing the String
913C - Party Lemonade
1313A - Fast Food Restaurant
681A - A Good Contest
1585F - Non-equal Neighbours
747A - Display Size
285A - Slightly Decreasing Permutations
515C - Drazil and Factorial
1151E - Number of Components
1151F - Sonya and Informatics
556A - Case of the Zeros and Ones
867A - Between the Offices
1569A - Balanced Substring
260A - Adding Digits
1698C - 3SUM Closure
1029B - Creating the Contest
1421A - XORwice
1029A - Many Equal Substrings
1675D - Vertical Paths
1271C - Shawarma Tent