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 full, it is closed and shipped to be sold. The marketing motto for the company is “bags of uniqueness.” To live up to the motto, every snow?ake in a package must be di?erent from the others. Unfortunately, this is easier said than done, because in reality, many of the snow?akes ?owing through the machine are identical. Emily would like to know the size of the largest possible package of unique snow?akes that can be created. The machine can start ?lling the package at any time, but once it starts, all snow?akes ?owing from the machine must go into the package until the package is completed and sealed. The package can be completed and sealed before all of the snow?akes have ?owed out of the machine.
Input
The ?rst line of input contains one integer specifying the number of test cases to follow. Each test case begins with a line containing an integer n, the number of snow?akes processed by the machine. The following n lines each contain an integer (in the range 0 to 109, inclusive) uniquely identifying a snow?ake. Two snow?akes are identi?ed by the same integer if and only if they are identical. The input will contain no more than one million total snow?akes.
Output
For each test case output a line containing single integer, the maximum number of unique snow?akes that can be in a package.
Sample Input
1 5 1 2 3 2 1
Sample Output
3

分析:

从一个有n个数的序列中,找到一个最大的子段(连续的),使得这个最大子段中没有重复元素,输出最大子段的长度。

注意:序列是不连续的,子段必须是连续的

做法:

对于该类段查找问题可以采用经典的滑动窗口方法,即维护一个窗口,窗口的左右边界用两个变量L,R代表,先增加R直到出现重复数字,再增加L,再增加R,直到R达到n

贴张图形象得一批~

set好用!

code:

#include<stdio.h>
#include <iostream>
#include <math.h>
#include <queue>
#include<set>
using namespace std;
#define max_v 1000005
int a[max_v];
int main()
{
    set<int> s;
    int t,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(int i=0;i<n;i++)
            cin>>a[i];
        s.clear();
        int left=0,right=0,ans=0;
        while(right<n)
        {
            while(right<n&&!s.count(a[right]))
            {
                s.insert(a[right++]);
            }
            ans=max(ans,right-left);
            s.erase(a[left++]);
        }
        cout<<ans<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/yinbiao/p/9397004.html

时间: 2024-10-12 08:07:22

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

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

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 (滑窗)

觉得是数据水了的题,看到一些直接用数组保存一个值最后一次出现的位置,说好的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--){

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

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