codeforces 224B(思维+双指针)

时间:2022-07-28
本文章向大家介绍codeforces 224B(思维+双指针),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

题意描述

给定n个数字,求一段区间l,r,要求区间内有k个不同的数

思路

由于区间内有k个不同的数,所以r-l+1至少为k。所以可以找到第一个r,然后再从r开始向左找l,找到的区间l和r即为答案

AC代码

#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#define x first
#define y second
#define PB push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define rep(x,l,u) for(ll x=l;x<u;x++)
#define rrep(x,l,u) for(ll x=l;x>=u;x--)
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long ll;
const int N=1e5+10;
const int M=1e6+10;
const int INF=0x3f3f3f3f;
int a[N];
bool st[N];
void solve(){
    int n,k;cin>>n>>k;
    rep(i,1,n+1) cin>>a[i];
    int cnt=0,fg=0,r=0,l=0;
    rep(i,1,n+1){
        if(st[a[i]]) continue;
        st[a[i]]=1;
        cnt++;
        if(cnt==k){
            r=i;
            fg=1;
            break;
        }
    }
    mst(st,false);
    cnt=0;
    rrep(i,r,1){
        if(st[a[i]]) continue;
        st[a[i]]=1;
        cnt++;
        if(cnt==k){
            l=i;
            break;
        }
    }
    if(fg) cout<<l<<' '<<r<<endl;
    else cout<<-1<<' '<<-1<<endl;
}
int main(){
    //IOS;
    solve();
    return 0;
}