Codeforces Round #364 (Div. 2) C. They Are Everywhere __ two pointers or binary search
来源:互联网 发布:网络售药 京东 编辑:程序博客网 时间:2024/06/07 03:39
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.
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.
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.
3AaA
2
7bcAAcbc
3
6aaBCCe
5
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
- Codeforces Round #364 (Div. 2) C. They Are Everywhere __ two pointers or binary search
- Codeforces Round #364 div.2 C. They Are Everywhere 【尺追法】
- Codeforces Round #364 (Div. 2) C. They Are Everywhere(二分)
- Codeforces Round #364 (Div. 2)C. They Are Everywhere
- Codeforces Round #364 (Div. 2) C. They Are Everywhere
- codeforces 701C. They Are Everywhere two pointers
- Codeforces Round #364 (Div. 2) C. They Are Everywhere (尺取法)
- Codeforces Round #364 (Div. 2) C They Are Everywhere(滑窗)
- Codeforces Round #364 (Div. 2) C. They Are Everywhere (窗口滑动)
- Codeforces Round #364 (Div. 2) C. They Are Everywhere(stl+乱搞)
- Codeforces Round #364 (Div. 2) C. They Are Everywhere (尺取法)
- Codeforces Round #364 (Div. 2) C. They Are Everywhere (尺取法)
- Codeforces #364(Div.2)C.They Are Everywhere【思维】
- div.2/C. They Are Everywhere<two pointer>
- CF #364 (Div. 2)(C. They Are Everywhere 尺取法)
- 【Codeforces Round 364 (Div 2)C】【前缀和 双指针】They Are Everywhere 含有所有字符的最短区间长度
- Codeforces Round #364 C. They Are Everywhere分析及题解 尺取法
- CodeForces 701C They Are Everywhere
- Linux - C进程间通信(预习内容八)
- oracle_(3)工具配置
- 微信开发简易教程
- Mac OS下的Mysql的基本操作
- 旅行中的观光游乐(第一节 到达观光场所)
- Codeforces Round #364 (Div. 2) C. They Are Everywhere __ two pointers or binary search
- java编程思想读书笔记 第十章内部类(中)
- javaIO之对象流
- 使用命令行展开功能来完成练习
- JAVA语言遗漏
- A*算法 学习记录
- 【软工】初识软工-《软件工程导论》
- 我的博文之路由此开启
- |洛谷|分治|P1010 幂次方