1835B - Lottery - CodeForces Solution


binary search brute force greedy math sortings two pointers

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define ll long long int
#define fi first
#define se second
 
#define md1 1000000007
#define md2 998244353 //r=3  upto root of 1<<23
#define md3 1000000009
#define md4 7340033   //r=5  upto root of 1<<20
 
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
 
using namespace __gnu_pbds;
using namespace std;
 
// ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
 
ll n,m,k,a[1000006];
int main(){
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++)cin>>a[i];
    a[0]=-1;
    a[n+1]=m+1;
    sort(a+1,a+1+n);
    ll maxCnt=0,ans=0;
    a[n+1]=m+1;
    a[0]=-1;
    for(int i=1;i<=n;){
        int j=i;
        for(;j<=n && a[i]==a[j];j++);
        j--;
        ll L=0,R=0;
        if(j>k-1)L=(a[j]+a[j-(k-1)])/2+1;
        else L=0;
        if(i<n-(k-1)+1)R=(a[i]+a[i+k-1]+1)/2-1;
        else R=m;

        if(R-L+1>maxCnt){
            maxCnt=R-L+1;
            ans=a[i];
        }
        else if(R-L+1==maxCnt){
            maxCnt=R-L+1;
            ans=min(ans,a[i]);
        }

        i=j+1;
    }
    for(int i=0;i<=n+1;i++){
        ll L=0,R=0;
        if(i<n+1 && a[i]+1<a[i+1]){
            if(i>k-1)L=(a[i]+1+a[i-(k-1)])/2+1;
            else L=0;
            if(i<n-k+1)R=(a[i]+1+a[i+k]+1)/2-1;
            else R=m;
            if(R-L+1>maxCnt){
                maxCnt=R-L+1;
                ans=a[i]+1;
            }
            else if(R-L+1==maxCnt){
                maxCnt=R-L+1;
                ans=min(ans,a[i]+1);
            }
            if(a[i]+2<a[i+1]){
                if(i>k-1)L=(a[i]+2+a[i-(k-1)])/2+1;
                else L=0;
                if(i<n-k+1)R=(a[i]+2+a[i+k]+1)/2-1;
                else R=m;
                if(R-L+1>maxCnt){
                    maxCnt=R-L+1;
                    ans=a[i]+2;
                }
                else if(R-L+1==maxCnt){
                    maxCnt=R-L+1;
                    ans=min(ans,a[i]+2);
                }   
            }
        }
        if(i>0 && a[i-1]<a[i]-1){
            if(i>k)L=(a[i]-1+a[i-k])/2+1;
            else L=0;
            if(i<n-(k-1)+1)R=(a[i]-1+a[i+(k-1)]+1)/2-1;
            else R=m;
 
            // cout<<L<<" "<<R<<" "<<a[i]-1<<"\n";
            if(R-L+1>maxCnt){
                maxCnt=R-L+1;
                ans=a[i]-1;
            }
            else if(R-L+1==maxCnt){
                maxCnt=R-L+1;
                ans=min(ans,a[i]-1);
            }
            if(a[i-1]<a[i]-2){
                if(i>k)L=(a[i]-2+a[i-k])/2+1;
                else L=0;
                if(i<n-(k-1)+1)R=(a[i]-2+a[i+(k-1)]+1)/2-1;
                else R=m;
    
                // cout<<L<<" "<<R<<" "<<a[i]-1<<"\n";
                if(R-L+1>maxCnt){
                    maxCnt=R-L+1;
                    ans=a[i]-2;
                }
                else if(R-L+1==maxCnt){
                    maxCnt=R-L+1;
                    ans=min(ans,a[i]-2);
                }
            }
        }
        // cout<<L[i]<<" "<<R[i]<<"\n";
    }
    cout<<maxCnt<<" "<<ans;
    return 0;
}
//https://codeforces.com/contest/1835/problem/B


Comments

Submit
0 Comments
More Questions

963A - Alternating Sum
1191B - Tokitsukaze and Mahjong
1612G - Max Sum Array
1459B - Move and Turn
1006F - Xor-Paths
706C - Hard problem
304C - Lucky Permutation Triple
1301C - Ayoub's function
38E - Let's Go Rolling
171G - Mysterious numbers - 2
1183C - Computer Game
400C - Inna and Huge Candy Matrix
417A - Elimination
222A - Shooshuns and Sequence
1736A - Make A Equal to B
1736B - Playing with GCD
887C - Solution for Cube
1737C - Ela and Crickets
1741C - Minimize the Thickness
1741A - Compare T-Shirt Sizes
1741D - Masha and a Beautiful Tree
109B - Lucky Probability
1741B - Funny Permutation
1741E - Sending a Sequence Over the Network
344B - Simple Molecules
370A - Rook Bishop and King
546E - Soldier and Traveling
1741G - Kirill and Company
1200B - Block Adventure
1088B - Ehab and subtraction