UVA - 11572 Unique Snowflakes 滑动扫描

时间:2024-04-26 08:27:47

题目:点击打开题目链接

思路:从左往右扫描,定义扫描左端点L,右端点R,保证每次往几何中添加的都是符合要求的连续的数列中的元素,L和R从0扫到n,复杂度为O(n),使用set维护子数列,set查找删除都是O(logn),所以复杂度为O(nlogn),应特别注意set内并不是原子数列顺序,若要删除子数列起始元素,不能使用begin迭代器

AC代码:

 #include <iostream>
#include <set>
#include <algorithm>
#include <cstdio> using namespace std; const int maxn = + ;
int num[maxn]; int main()
{
//freopen("1.txt", "r", stdin);
int T; cin >> T;
while(T--) {
int n; cin >> n;
for(int i = ; i < n; i++)
cin >> num[i]; set<int> temp;
int L = , R = , ans = ; while(R < n) {
while(R < n && temp.find(num[R]) == temp.end())
temp.insert(num[R++]);
ans = max(ans, (int)temp.size());
temp.erase(num[L++]);
}
cout << ans << endl;
}
return ;
}