Блог пользователя Xbalanque

Автор Xbalanque, история, 4 часа назад, По-английски

2008H - Sakurako's Test

Editorial says Let's fix one x and try to solve this task for it.

okay makes our time complexity q*(something), moving on...

How to find number of i that ai mod x ≤ m for some m

We can use binary search of some sort, making our complexity

q*log(x)*checkerForM, how in the world is this complexity not TLE

Can anyone explain it to me.

  • Проголосовать: нравится
  • -16
  • Проголосовать: не нравится

»
4 часа назад, # |
  Проголосовать: нравится +7 Проголосовать: не нравится

read the complete tutorial.

»
4 часа назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

So, the idea is to precompute answer for all x. For each x, we found answer in $$$O(\frac{n}{x}\cdot log(n))$$$ and in sum it will be $$$O(n\cdot log^2(x))$$$. So, we can answer querry in constant time.

  • »
    »
    118 минут назад, # ^ |
    Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится
    #include<bits/stdc++.h>
    using namespace std;
     
     
    bool check(vector<int>&v, int x, int m){
        int n = v.size(); int ct = 0;
        for(int k=0;k<=n/x;k++){
            ct += upper_bound(v.begin(),v.end(),k*x+m) - upper_bound(v.begin(),v.end(),k*x-1);
        }
        return ct>=(n+2)/2;
    }
     
    int main() {
        ios::sync_with_stdio(0); cin.tie(0);
        int t; cin>>t;
        while(t--){
            int n,q; cin>>n>>q;
            vector<int>v(n);
            for(int i=0;i<n;i++) cin>>v[i];
            sort(v.begin(),v.end());
            vector<int>dp(n+1);
            for(int i=1;i<=n;i++){
                int l = 0, r = i-1;
                while(l<=r){
                    int m = l + (r-l)/2;
                    if(check(v,i,m)){
                        dp[i] = m; r=m-1;
                    }
                    else l=m+1;
                }
            }
            while(q--){
                int x; cin>>x;
                cout<<dp[x]<<" ";
            }
            cout<<'\n';
        }
    }
    

    Why does this simple code implementing your approach giving TLE

    btw Pa_sha orz

    • »
      »
      »
      74 минуты назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится
      instead of upper bound
      use prefix count
      • »
        »
        »
        »
        73 минуты назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        why does it matter, logx or logx*logx should be almost equal, why TLE in my case ?