【Codeforces 701C】They Are Everywhere

题目传送门:http://codeforces.com/contest/701/problem/C

第一眼反应:这™不是O(nlogn)的滑动窗口吗?
然后看题解确定对错,发现是O(n)(╯‵□′)╯︵┻━┻
ps:网上管这个叫“尺取法”?这不是滑动窗口吗?

#include<iostream>
#include<cstdio>
#include<set>
using namespace std;

const int MAXN = 100000+9;
const int N = 60;
const int INF = 1000000;

int n,cnt[MAXN],arr[MAXN],tot,vout=INF;
bool tag[N];

inline int read(){
	char c=getchar(); 
	while ((c < 'a' || c>'z') && (c < 'A' || c > 'Z')) c=getchar();
	if (c <= 'z' && c >= 'a') return c-'a'+1;
	else return c-'A'+27;
}

int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++) {
		arr[i] = read();
		if (!tag[arr[i]]) tag[arr[i]] = 1, tot++;
	}
	for (int i=1,bak=1,w=0;i<=n;i++) {
		if (!cnt[arr[i]]++) w++;
		while (cnt[arr[bak]] > 1) cnt[arr[bak++]]--;
		if (w == tot) vout = min(vout, i-bak+1);
	}
	printf("%d\n",vout);
	return 0;
}

2 thoughts to “【Codeforces 701C】They Are Everywhere”

  1. I conceive this internet site holds some very great information for everyone. “I have learned to use the word ‘impossible’ with the greatest caution.” by Wernher von Braun.

  2. It¦s in point of fact a nice and useful piece of info. I¦m satisfied that you simply shared this useful information with us. Please stay us informed like this. Thanks for sharing.

Leave a Reply

Your email address will not be published. Required fields are marked *