UVA 11572 Unique snowflakes (滑窗)

觉得是数据水了的题,看到一些直接用数组保存一个值最后一次出现的位置,说好的0到1e9的数据范围呢?。。

用set,保存当前区间出现过的数字,下一个数字,没有出现过,加入,直到出现重复,然后删掉左端点,继续。。。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+2;
int A[maxn];

int main()
{
    int T; scanf("%d",&T);
    while(T--){
        int n; scanf("%d",&n);
        for(int i = 0; i < n; i++) scanf("%d",A+i);
        set<int> s;
        int L = 0, R = 0, ans = 0;
        while(R < n){
            while(R < n  && !s.count(A[R])) s.insert(A[R++]);
            ans = max(ans,R-L);
            s.erase(A[L++]);
        }
        printf("%d\n",ans);
    }
    return 0;
}

数组版,最后到终点的时候也要算一次,所以令A[n] = A[n-1]。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+2;
int A[maxn];
int pos[maxn];

int main()
{
    int T; scanf("%d",&T);
    while(T--){
        int n; scanf("%d",&n);
        for(int i = 0; i < n; i++) scanf("%d",A+i);
        memset(pos,-1,sizeof(pos));
        int L = 0, ans = 0;
        A[n] = A[n-1];
        for(int i = 0; i <= n; i++){
            if(pos[A[i]] >= L){
                ans = max(ans,i-L);
                L = pos[A[i]]+1;
            }
            pos[A[i]] = i;
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-04 00:55:45

UVA 11572 Unique snowflakes (滑窗)的相关文章

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

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

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

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

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

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

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,

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 算法分析

难度:β- 用时:0 min 题目:?? 代码:?? ?? 这是一道连续区间水题. 就是判重而已. 价值在于它的 map 做法. 用数组 prev 表示前面同值的位置,这样就不用 set. 为什么要用 map?因为是值对位,值可以很大,数组存不下. 在构造 prev 时,要用 map. 下面转紫书代码. 1 for (int i = 0; i < n; i++) { 2 cin >> A[i]; 3 if (!cur.count(A[i])) last[i] = -1; 4 else l

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 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(&