Unique Snowflakes

 1 #include<set>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 using namespace std;
 7
 8 const int maxn=1000005;
 9
10 int T,n;
11 int a[maxn];
12
13 int main()
14 {   cin>>T;
15     while(T--){
16         cin>>n;
17         for(int i=0;i<n;i++) scanf("%d",&a[i]);
18
19         set<int> q;
20         int l=0,r=0,ans=0;
21         while(r<n){
22             while(r<n&&!q.count(a[r])) q.insert(a[r++]);          // count()返回某个值元素的个数 !
23             ans=max(ans,r-l);
24             q.erase(a[l++]);                                      // erase()删除集合中的元素 !
25         }
26         cout<<ans<<endl;
27     }
28     return 0;
29 }
时间: 2024-08-30 10:36:08

Unique Snowflakes的相关文章

Unique Snowflakes(窗口滑动)

题目: Emily the entrepreneur has a cool business idea: packaging and selling snowflakes. She has devised a machine that captures snowflakes as they fall, and serializes them into a stream of snowflakes that flow, one by one, into a package. Once the pa

uva 11572 - Unique Snowflakes(与书上方法略有不同)

刘汝佳书上用的是set, 通过集合来查找.count()和删除.erase().这个方法比我的要好,用时更短. 我觉得map也能完成这个任务,但是其删除并不方便,需要先查找find()下标,然后删除此下标对应的元素 但是map有map的用法,下面的方法就是比较容易实现的一种方法. 我本想着这个一边读完就计算出了ans,应该更快一点的,但是事实上还不如先读再用set处理来得快. #include<cstdio> #include<iostream> #include<map&g

uva 11572 unique snowflakes——yhx

Emily the entrepreneur has a cool business idea: packaging and selling snowakes. She has devised amachine that captures snowakes as they fall, and serializes them into a stream of snowakes that ow,one by one, into a package. Once the package is full,

11572 - Unique Snowflakes(贪心,两指针滑动保存子段最大长度)

Emily the entrepreneur has a cool business idea: packaging and selling snow?akes. She has devised a machine that captures snow?akes as they fall, and serializes them into a stream of snow?akes that ?ow, one by one, into a package. Once the package is

UVA 11572 Unique Snowflakes

题意: 给n个数, n<=100W,求一个连续子序列,这个子序列中没有重复的数,问这个子序列最长是多少? 分析: 直接暴力破解就行,看当前数字的下一次出现在什么地方 代码: #include<cstdio>#include<cstring>#include<map>using namespace std;int num[1000010];int pos[1000010];int main(){ int t; scanf("%d",&t)

UVa 11572 (滑动窗口) Unique Snowflakes

滑动窗口挺有意思的,如果符合条件右端点一直向前走,不符合的话,左端点向前走. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 set<int> Set; 5 6 const int maxn = 1000000 + 10; 7 int a[maxn]; 8 9 int Scan() { //输入外挂 10 int res = 0; 11 char ch; 12 while((ch = getchar()) >= '0

UVA 11527 Unique Snowflakes

用STL做会很方便 SET: 1 /*by SilverN*/ 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #include<set> 8 using namespace std; 9 const int mxn=1000020; 10 int a[mxn]; 11 int n

11572 - Unique Snowflakes

紫书上将这道题的方法成为"滑动窗口" ,它还应该有另一个名字叫--取尺法, 用两个首尾"指针"通过不断更新它们来逐步得到最优解,适合于解决连续序列的问题. #include<bits/stdc++.h> using namespace std; const int maxn = 1000000+5; int T,n,A[maxn]; int main(){ scanf("%d",&T); while(T--){ scanf(&

UVa11572 Unique Snowflakes (滑动窗口)

链接:http://vjudge.net/problem/UVA-11572 分析:维护一个set即可. 1 #include <cstdio> 2 #include <algorithm> 3 #include <set> 4 using namespace std; 5 6 const int maxn = 1000000 + 5; 7 8 int n, a[maxn]; 9 10 int main() { 11 int T; 12 scanf("%d&q

(白书训练计划)UVa 11572 Unique Snowflakes(窗体滑动法)

题目地址:UVa 11572 这样的方法曾经接触过,定义两个指针,不断从左向右滑动,推断指针内的是否符合要求. 这个题为了能高速推断是否有这个数,能够用STL中的set. 代码例如以下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <