CSES – Playlist

資料結構、滑動窗口

#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define f first
#define s second
#define fastio ios_base::sync_with_stdio(0);cin.tie(0)
using namespace std;
//declare
int n;
vector<int> v;
set<pii> st;
//
signed main(){
    fastio;
    cin>>n;
    for(int i=0;i<n;i++){
        int x; cin>>x;
        v.push_back(x);
    }
    int ans = 1;
    int L = 0, R = 1;
    st.insert({v[0],1});
    while(R <= n){
        ans = max(ans, (int)st.size());
        if(st.size() == R-L){
            pii p = {v[R],0};
            auto it = st.upper_bound(p);
            if(it == st.end() || it->f != v[R]) st.insert({v[R],1});
            else st.insert({v[R],it->s+1}), st.erase(it);
            R++;
            continue;
        }
        pii p = {v[L],0};
        auto it = st.upper_bound(p);
        if(it->s-1 == 0) st.erase(it);
        else st.insert({v[L],it->s-1}), st.erase(it);
        L++;
    }
    cout<<ans<<"\n";
    return 0;
}

相關文章

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *