Codeforces Round #364 (Div. 2) C. They Are Everywhere __ two pointers or binary search

来源:互联网 发布:网络售药 京东 编辑:程序博客网 时间:2024/06/07 03:39

C. They Are Everywhere
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Sergei B., the young coach of Pokemons, has found the big house which consists of n flats ordered in a row from left to right. It is possible to enter each flat from the street. It is possible to go out from each flat. Also, each flat is connected with the flat to the left and the flat to the right. Flat number 1 is only connected with the flat number 2 and the flat number n is only connected with the flat number n - 1.

There is exactly one Pokemon of some type in each of these flats. Sergei B. asked residents of the house to let him enter their flats in order to catch Pokemons. After consulting the residents of the house decided to let Sergei B. enter one flat from the street, visit several flats and then go out from some flat. But they won't let him visit the same flat more than once.

Sergei B. was very pleased, and now he wants to visit as few flats as possible in order to collect Pokemons of all types that appear in this house. Your task is to help him and determine this minimum number of flats he has to visit.

Input

The first line contains the integer n (1 ≤ n ≤ 100 000) — the number of flats in the house.

The second line contains the row s with the length n, it consists of uppercase and lowercase letters of English alphabet, the i-th letter equals the type of Pokemon, which is in the flat number i.

Output

Print the minimum number of flats which Sergei B. should visit in order to catch Pokemons of all types which there are in the house.

Examples
input
3AaA
output
2
input
7bcAAcbc
output
3
input
6aaBCCe
output
5
Note

In the first test Sergei B. can begin, for example, from the flat number 1 and end in the flat number 2.

In the second test Sergei B. can begin, for example, from the flat number 4 and end in the flat number 6.

In the third test Sergei B. must begin from the flat number 2 and end in the flat number 6.



Source

Codeforces Round #364 (Div. 2)


My Solution

求包含所有种类的元素的最小区间长度


two pointers or binary search

1、binary search

int l = 0, r = n;

while(l + 1 < r){

x = (l + r) >> 1;

if(check(x)) r = x;         //O(n)的检查 sz = x 时是否满足条件, 如果满足则 r = x, 然后继续检查x更小的情况, 最后 ans == x;

else l = x;

}

复杂度 O(nlogn)


2、two pointers

用 sz 维护区间内的种类数 //如果每次用 .size()好像是 O(n) 的, 这样用个 sz 就变成 O(1)了

用m[flats[j]]、m[flats[i]] 表示 区间 [i, j) 内 各个元素的个数,

当 j 向右移一位 且原来的m[flats[j]] == 0 时 sz++, m[flats[j]]++;

当 i 向右移一位 且原来的m[flats[i]] == 1 时 sz--, m[flats[j]]--;

当sz == kinds 时, 维护答案 ans = min(ans, j - i);

复杂度 O(n)

Source code for two pointers

#include <iostream>#include <cstdio>#include <map>using namespace std;typedef long long LL;const int maxn = 1e5 + 8;char flats[maxn];map<char, int> m;int main(){    #ifdef LOCAL    freopen("a.txt", "r", stdin);    //freopen("b.txt", "w", stdout);    int T = 3;    while(T--){    #endif // LOCAL    int n, kinds, ans = maxn, sz;    scanf("%d", &n);    scanf("%s", flats);    for(int i = 0; i < n; i++)        m[flats[i]]++;    kinds = m.size();    m.clear();sz = 1;    int i = 0, j = 1;    m[flats[0]]++;    while(true){        if(i == j) break;        //if(j == n && sz != kinds) break;        while(sz != kinds){            if(j == n) break;            if(m[flats[j]] == 0) sz++;            m[flats[j]]++;            j++;        }        if(sz == kinds) ans = min(ans, j - i);        if(m[flats[i]] == 1) sz--;        m[flats[i]]--;        i++;        //cout<<i<<endl;    }    printf("%d", ans);    #ifdef LOCAL    printf("\n");    m.clear();    }    #endif // LOCAL    return 0;}

  Thank you!

                                                                                                                                               ------from ProLights 

0 0
原创粉丝点击