Submission #1364328

#TimeUsernameProblemLanguageResultExecution timeMemory
1364328AvianshSprinklers (CEOI24_sprinklers)C++20
3 / 100
2094 ms2748 KiB
#include <bits/stdc++.h>

using namespace std;

#define int long long

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n,m;
    cin >> n >> m;
    int s[n];
    for(int i = 0;i<n;i++){
        cin >> s[i];
    }
    int f[m];
    for(int i = 0;i<m;i++){
        cin >> f[i];
    }
    sort(f,f+m);
    sort(s,s+n);
    char ans[n];
    auto check = [&] (int k){
        fill(ans,ans+n,'L');
        int cnt[m];
        fill(cnt,cnt+m,0);
        for(int i = 0;i<n;i++){
            int lo = s[i]-k;
            int ind = lower_bound(f,f+m,lo)-f;
            while(ind<m&&f[ind]<=s[i]){
                cnt[ind]++;
                ind++;
            }
        }
        //assuming left shift done
        for(int i = m-1;i>=0;i--){
            if(cnt[i]){
                //already done
                continue;
            }
            //not done take closest redundant to the left and do.
            for(int j = n-1;j>=0;j--){
                if(s[j]>f[i])
                    continue;
                if(f[i]-s[j]>k){
                    //too far to make a difference
                    continue;
                }
                //this is closest to the left now so see if it is redundant
                int lo = s[j]-k;
                int ind = lower_bound(f,f+m,lo)-f;
                bool r=1;
                while(ind<m&&f[ind]<s[j]){
                    assert(cnt[ind]);
                    if(cnt[ind]==1){
                        r=0;
                    }
                    ind++;
                }
                if(r){
                    //redundant so can flip this
                    ans[j]='R';
                    int lo = s[j]-k;
                    int ind = lower_bound(f,f+m,lo)-f;
                    while(ind<m&&f[ind]<=s[j]){
                        cnt[ind]--;
                        ind++;
                    }
                    lo=s[j];
                    ind=lower_bound(f,f+m,lo)-f;
                    while(ind<m&&f[ind]<=s[j]+k){
                        cnt[ind]++;
                        ind++;
                    }
                    assert(cnt[i]);
                    break;
                }
            }
            if(cnt[i]){
                //did it good so continue
            }
            else{
                //impossible
                return 0;
            }
        }
        return 1;
    };
    int lo = 0;
    int hi = 2e9;
    while(lo<hi){
        int mid = (lo+hi)/2;
        if(check(mid)){
            hi=mid;
        }
        else{
            lo=mid+1;
        }
    }
    if(lo==2e9){
        cout << -1;
        return 0;
    }
    check(lo);
    cout << lo << "\n";
    for(char c:ans){
        cout << c;
    }
    return 0;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...