796A - Buying A House - CodeForces Solution


brute force implementation *800

Please click on ads to support us..

Python Code:

n,m,k=map(int,input().split())
r=[*map(int,input().split())]
mini=10000
for i in range(n):
 if r[i] and k>=r[i]:mini=min(mini,abs(m-i-1)*10)
print(mini)

C++ Code:

#include<bits/stdc++.h>
using namespace std;

#define int long long int

signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n,m,k;
cin>>n>>m>>k;
int a[n];
for(int i=0;i<n;i++){
    cin>>a[i];

} 
int ans=-1;
if((m-1)==0 || m==n){
    if(m-1==0){
        int pri=m;
        while(pri!=n){
            if(a[pri]!=0 && a[pri]<=k){ans=pri;
                break;
            }
            else pri++;
        }
          cout<<(abs(m-ans)+1)*10;
    return 0;
    }
    else if(m==n){
        int pri=m-2;
         while(pri>=0){
            if(a[pri]!=0 && a[pri]<=k){
                ans=pri;
                break;
            }
            else pri--;
        }
    }
    cout<<(abs(m-ans)-1)*10;
    return 0;
}

else{
int st=m-2,en=m;
   while(st>=0 || en!=n){
       if(a[st]!=0 && a[st]<=k){
            ans=st;
            break;
       }
       if(a[en]!=0 && a[en]<=k){
        ans=en;
        break;
       }
       else {
        if(st!=0)st--;
        if(en!=n)en++;
       }
   }

cout<<abs(m-ans-1)*10;
}

}


Comments

Submit
0 Comments
More Questions

1606A - AB Balance
1658C - Shinju and the Lost Permutation
1547C - Pair Programming
550A - Two Substrings
797B - Odd sum
1093A - Dice Rolling
1360B - Honest Coach
1399C - Boats Competition
1609C - Complex Market Analysis
1657E - Star MST
1143B - Nirvana
1285A - Mezo Playing Zoma
919B - Perfect Number
894A - QAQ
1551A - Polycarp and Coins
313A - Ilya and Bank Account
1469A - Regular Bracket Sequence
919C - Seat Arrangements
1634A - Reverse and Concatenate
1619C - Wrong Addition
1437A - Marketing Scheme
1473B - String LCM
1374A - Required Remainder
1265E - Beautiful Mirrors
1296A - Array with Odd Sum
1385A - Three Pairwise Maximums
911A - Nearest Minimums
102B - Sum of Digits
707A - Brain's Photos
1331B - Limericks