1514D - Cut and Stick - CodeForces Solution


binary search data structures greedy implementation sortings *2000

Please click on ads to support us..

C++ Code:

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

#define ll long long
#define pb push_back
#define ppb pop_back
#define int long long
#define itn long long
#define endl "\n"
#define fi first
#define se second
#define forf(i, a, b) for (int i = (a); i < (b); i++)
#define forr(i, a, b) for (int i = (a); i >=(b); i -= 1)
#define prdouble(x) cout<< fixed << setprecision(10)<< x;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pair<int,int>> vpii;
typedef pair<ll, ll> pll;
typedef unordered_map<int,int> umap;
#define yeah cout<<"YES"<<endl;
#define nope cout<<"NO"<<endl;
#define all(v) v.begin(),v.end()
#define print(vec, l, r)                                                       \
    for (int i = l; i < r; i++)                                               \
        cout << vec[i] << " ";                                                 \
    cout << endl;
#define debug(v) cout<<v<<endl;

const int N=1e5+5;
const ll mod = 1e9 + 7;
const ll inf = 1e17;

long long ceil(int n,int i)
{
    return (n+i-1)/i;
}

long long power(long long a,long long b,long long m=mod){
    long long p=1;
    a%=m;
    while(b){
        if(b&1) p=(p*1LL*a)%m;
        a=(a*1LL*a)%m;
        b>>=1;
    }
    p%=m;
    return p;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());


void solve()
{
    int n,q;
    cin>>n>>q;
    vector<int> v(n,0);
    vvi freq(n+1);
    for(int i=0;i<n;i++)
    {
        cin>>v[i];
        freq[v[i]].pb(i);
    }
    while(q--)
    {
        int l,r;
        cin>>l>>r;
        l--,r--;
        int ans=1;
        forf(i,0,50)
        {
            int k=rng();
            k=k%(r-l+1);
            k+=l;
            int count=upper_bound(all(freq[v[k]]),r)-freq[v[k]].begin();
            count-=lower_bound(all(freq[v[k]]),l)-freq[v[k]].begin();
            ans=max(ans,2*count-(r-l+1));
        }
        debug(ans);
    }

    
    return;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t=1;
    // cin>>t;
    while(t--)
    {
        solve();
    }
} 


Comments

Submit
0 Comments
More Questions

214A - System of Equations
287A - IQ Test
1108A - Two distinct points
1064A - Make a triangle
1245C - Constanze's Machine
1005A - Tanya and Stairways
1663F - In Every Generation
1108B - Divisors of Two Integers
1175A - From Hero to Zero
1141A - Game 23
1401B - Ternary Sequence
598A - Tricky Sum
519A - A and B and Chess
725B - Food on the Plane
154B - Colliders
127B - Canvas Frames
107B - Basketball Team
245A - System Administrator
698A - Vacations
1216B - Shooting
368B - Sereja and Suffixes
1665C - Tree Infection
1665D - GCD Guess
29A - Spit Problem
1097B - Petr and a Combination Lock
92A - Chips
1665B - Array Cloning Technique
1665A - GCD vs LCM
118D - Caesar's Legions
1598A - Computer Game