P1638 逛画展-(二分+尺取+单调队列)

总结

这个题我的第一想法是尺取,看了一下别人的写法,很多不是尺取就是二分,写单调队列的还没看到,我也不知道怎么写,就很难受,本来写这个题的目的就是为了熟练单调队列。

解析

尺取:考虑左边界是否可以右移动
二分:就是logn考虑滑动窗口是否满足条件。[1,n]满足单调性
单调队列:我也不知道

题目链接

/*
                ____________    ______________       __
               / _________  /\ /_____   _____/\     / /\
              / /\       / /  \\    /  /\    \ \   / /  \
             / /  \_____/ /   / \__/  /  \____\/  / /   /
            / /   /    / /   /    /  /   /       / /   /
           / /   /    / /   /    /  /   /       / /   /
          / /   /    / /   /    /  /   /       / /   /
         / /___/____/ /   /    /  /   /       / /___/________
        /____________/   /    /__/   /       /______________/\
        \            \  /     \  \  /        \              \ \
         \____________\/       \__\/          \______________\/
           ___       ___               ___    __________
          /  /\     /  /\             /  /\  /_______  /\
         /  /__\___/  /  \           /  /  \ \      /  /  \
        /____    ____/   /          /  /   /  \____/  /   /
        \   /   /\   \  /          /  /   /       /  /   /
         \_/   /  \___\/ ___      /  /   /       /  /   /
          /   /   /     /  /\    /  /   /       /  /   /
         /   /   /     /  /__\__/  /   /       /  /___/____
        /___/   /     /___________/   /       /___________/\
        \   \  /      \           \  /        \           \ \
         \___\/        \___________\/          \___________\/

          A CODE OF CBOY

*/
#include<bits/stdc++.h>
//typedef long long ll;
//#define ull       unsigned long long
//#define int       long long
#define F           first
#define S           second
#define endl        "\n"//<<flush
#define eps         1e-6
#define lowbit(x)   (x&(-x))
#define PI          acos(-1.0)
#define inf         0x3f3f3f3f
#define MAXN        0x7fffffff
#define INF         0x3f3f3f3f3f3f3f3f
#define pa          pair<int,int>
#define ferma(a,b)  pow(a,b-2)
#define pb          push_back
#define all(x)      x.begin(),x.end()
#define memset(a,b) memset(a,b,sizeof(a));
#define IOS         ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
void file()
{
#ifdef ONLINE_JUDGE
#else
    freopen("cin.txt","r",stdin);
    //  freopen("cout.txt","w",stdout);
#endif
}
const int N=1e6+5;
int a[N],vis[N];
signed main()
{
    IOS;
    //file();
    int n,m;
    cin>>n>>m;
    int ans=inf,x,y;
    deque<int>de;
    set<int>se;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        se.insert(a[i]);
    }
    int len=se.size();
    int cnt=0;
    for(int i=1;i<=n;i++)
    {
        de.push_back(i);
        vis[a[i]]++;
        if(vis[a[i]]==1)
            cnt++;
        while(vis[a[de.front()]]>=2)vis[ a[de.front()] ]--,de.pop_front();
        if(de.back()-de.front()+1<ans&&cnt==len)
            ans=de.back()-de.front()+1,x=de.front(),y=de.back();
    }
    cout<<x<<" "<<y<<endl;
    return 0;
}

发布了149 篇原创文章 · 获赞 5 · 访问量 6901

猜你喜欢

转载自blog.csdn.net/weixin_44224825/article/details/104557559
今日推荐