Home Article Practice 尺取法,错题

尺取法,错题

2022-05-16 20:19  views:488  source:Coat    

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <cstring>
#include <set>
#include <vector>
#include<queue>
#include <math.h>
#include<map>
#include<deque>
using namespace std;
int s[1000100];
int h[1000100];
int main()
{
int P;
scanf("%d", &P);
int num = 0;
map<int, int>awa;
for (int i = 0; i < P; i++)
{
scanf("%d", &h[i]);
if (!awa[h[i]])
{
num++;
awa[h[i]]=num;
}
}
int l = 0, r = 0,t=1;
int mn = 1000100;
s[awa[h[0]]]++;
while (l <= r && r < P)
{
if (t == num)
{
mn = min(mn, r-l+1);
s[awa[h[l]]]--;
if (s[awa[h[l]]] == 0)
t--;
l++;
}
else
{
r++;
s[awa[h[r]]]++;
if (s[awa[h[r]]] == 1)
t++;
}
}
cout << mn;
}



Disclaimer: The above articles are added by users themselves and are only for typing and communication purposes. They do not represent the views of this website, and this website does not assume any legal responsibility. This statement is hereby made! If there is any infringement of your rights, please contact us promptly to delete it.

字符:    改为:
去打字就可以设置个性皮肤啦!(O ^ ~ ^ O)