1851A - Escalator Conversations - CodeForces Solution


brute force constructive algorithms implementation math

Please click on ads to support us..

Python Code:


t = int(input())
for j in range(t):
    S = 0
    n, m, k, H = map(int, input().split())
    h = list(map(int, input().split()))
    for i in range(n):
        R = abs(H - h[i])
        if ((R % k) == 0) and (0 < (R // k) < m):
            S += 1
    print(S)





C++ Code:

#include <bits/stdc++.h>
#include <string>
typedef long long ll;
using namespace std;

    int parent[200005];
    int Size[200005];
    void make(int v){
        parent[v]=v;
        Size[v]=1;
    }
    int find(int v){
        if(v==parent[v])return v;
        return parent[v]=find(parent[v]);
    }
    void Union(int a,int b){
        a=find(a);
        b=find(b);
        if(a!=b){
            if(Size[a]<Size[b])swap(a,b);
            parent[b]=a;
            Size[a]+=Size[b];
        }
    }
    /*
int mod=1e9+7;
//int mx=31623;
bool isPrime[31623];
vector<int>prime;
void sieve(){
    for (int i = 0; i < 31623; i++) {
        //* code 
        isPrime[i]=true;
    }
    isPrime[0]=false;
    isPrime[1]=false;
    for (int i = 2; i*i < 31623; i++) {
        code
        if(isPrime[i]){
            for(int j=i*i;j<31623;j+=i){
                isPrime[j]=false;
            }
        }
    }
    for(int i=2;i<31623;i++){
        if(isPrime[i]){
            prime.push_back(i);
        }
    }
}
vector<int> parent;
int find(int x){
    if(parent[x]<0) return x;
    return parent[x]=find(parent[x]);
}
 
bool uni(int u,int v){
    u=find(u);
    v=find(v);
    if(u==v) return true;
    if(parent[u]>parent[v]){
        swap(u,v);
    }
    parent[u]+=parent[v];
    parent[v]=u;
    return false;
}
//int cnst=200000;


bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
  //suppose n=7 that is prime and its pair are (1,7)
  //so if a no. is prime then it can be check by numbers smaller than square root
  // of the n
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0)
            return false;
    }
    return true;
}
int nCr(int n, int r) {
    // If r is greater than n, return 0
    if (r > n) return 0;
    // If r is 0 or equal to n, return 1
    if (r == 0 || n == r) return 1;
    // Initialize the logarithmic sum to 0
    double res = 0;
    // Calculate the logarithmic sum of the numerator and denominator using loop
    for (int i = 0; i < r; i++) {
        // Add the logarithm of (n-i) and subtract the logarithm of (i+1)
        res += log(n-i) - log(i+1);
    }
    // Convert logarithmic sum back to a normal number
    return (int)round(exp(res));
}*/
//sparse table
/*
const int MAX_N = 100'005;
const int LOG = 17;
int a[MAX_N];
int m[MAX_N][LOG]; // m[i][j] is minimum among a[i..i+2^j-1]
int bin_log[MAX_N];

int query(int L, int R) { // O(1)
	int length = R - L + 1;
	int k = bin_log[length];
	return min(m[L][k], m[R-(1<<k)+1][k]);
}


to write in main

int n;
	cin >> n;
	bin_log[1] = 0;
	for(int i = 2; i <= n; i++) {
		bin_log[i] = bin_log[i/2]+1;
	}
	for(int i = 0; i < n; i++) {
		cin >> a[i];
		m[i][0] = a[i];
	}
	// 2) preprocessing, O(N*log(N))
	for(int k = 1; k < LOG; k++) {
		for(int i = 0; i + (1 << k) - 1 < n; i++) {
			m[i][k] = min(m[i][k-1], m[i+(1<<(k-1))][k-1]);
		}
	}
	// 3) answer queries
	int q;
	cin >> q;
	for(int i = 0; i < q; i++) {
		int L, R;
		cin >> L >> R;
		cout << query(L, R) << "\n";
	}
*/
/*
struct node{
   node* child[2];
   int value;
   node(){
       child[0]=NULL;
       child[1]=NULL;
       value=0;
   }
};
class trie{
   public:
   node* root;
   trie(){
       root= new node;
   }
   void insert(int num){
       node* temp=root;
       for(int i=31;i>=0;i--){
           bool bit=num&(1<<i);
           if(temp->child[bit]==NULL){
               temp->child[bit]=new node;
           }
           temp=temp->child[bit];
       }
       temp->value=num;
   }
   int query(int num){
       node* temp=root;
       for(int i=31;i>=0;i--){
           bool bit=num&(1<<i);
           if(temp->child[1-bit]!=NULL){
               temp=temp->child[1-bit];
           }else if(temp->child[bit]!=NULL){
               temp=temp->child[bit];
           }
       }
       return (temp->value)^num;
   }
};*/
// int helper(int n,int k,int l,int sl){
//     if(k==0 && n>=0)return 1;
//     if(n<0)return 1;
//     int ans=0;
//     for(int i=0;i<=n;i++){
//         ans+=helper(n-i,k--,)
//     }
// }
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
  //suppose n=7 that is prime and its pair are (1,7)
  //so if a no. is prime then it can be check by numbers smaller than square root
  // of the n
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0)
            return false;
    }
    return true;
}

int main()
{   
    ios::sync_with_stdio(0);
    cin.tie(0);
	int t;
    cin>>t;
    // t=1;
    while(t--){
        int n,m,k,h;
        cin>>n>>m>>k>>h;
        int a[n];
        int ans=0;
        for(int i=0;i<n;i++)cin>>a[i];
        for(int i=0;i<n;i++){
            int t=abs(a[i]-h);
            if(t%k!=0 || t/k==0)continue;
            if(t/k<m)ans++;
        }
        cout<<ans<<endl;
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets