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 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 snowflake in a package must be different from the others. Unfortunately, this is easier said than done, because in reality, many of the snowflakes flowing through the machine are identical.

Emily would like to know the size of the largest possible package of unique snowflakes that can be created. The machine can start filling the package at any time,

but once it starts, all snowflakes flowing 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 snowflakes have flowed out of the machine.

Input

The first 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 snowflakes processed by the machine.

The following n lines each contain an integer (in the range 0 to 109 , inclusive) uniquely identifying a snowflake.

Two snowflakes are identified by the same integer if and only if they are identical.

The input will contain no more than one million total snowflakes.

Output

For each test case output a line containing single integer, the maximum number of unique snowflakes that can be in a package.

Sample Input

1

5

1

2

3

2

1

Sample Output

3

题意:

给你一串数字,要你求最长的没有重复数字的子序列长度;

分析:

求没有重复数字的子序列,用set会更加方便;

窗口滑动:

left  左

right 右

left和right 初始化为1;(我的输入从1—n)

在没有找到相同元素以前,右窗口一直向右移动,碰到相同元素后,左窗口开始向右移动,在移动的同时记录下最大值

如下图(我滴天!画的好吃藕)

AC代码:

#include<iostream>
#include<cstring>
#include<set>
#include<cstdio>
using namespace std;
const int N=1000005;
int a[N];
int maxi (int a,int b)
{
    return a>b?a:b;
}
int main()
{
    int t,n;
    set<int>s;
    cin>>t;
    while (t--)
    {
        s.clear();
        cin>>n;
        for (int i=1;i<=n;i++)
            cin>>a[i];
            int left=1,right=1,num=0;
            while (right<=n)
            {
                  while (right<=n&&!s.count(a[right]))
                     s.insert(a[right++]);
                num=maxi(num,right-left);
                s.erase(a[left++]);
            }
          cout << num << endl;
    }
    return 0;
}
时间: 2024-10-25 00:37:48

Unique Snowflakes(窗口滑动)的相关文章

sed修炼系列(三):sed高级应用之实现窗口滑动技术

html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,header,hgroup,main,nav,section,summary { display: block } audio,canvas,progress,video { display: inline-block; vertical-align: baseline } audio:not([co

HDU 5056 Boring count(窗口滑动法)

题目地址:HDU 5056 我晕啊..当时绝壁脑残了...当时想到的方法是完全正确的..但是在脑算第二个样例的时候,居然一直把前三个abc当成了9种...于是后面的三个abc每个都要递增2,而不是我想的方法中的递增3...于是一直没写代码... 言归正传.. 这题的思路就是窗口滑动,两个指针,让指针内的始终保持每个字母的数量少于k个.然后递推过去.然后因为每次右指针往右移动一个的时候,就相当于右指针当前指的数可以与左边的每一个之间都形成一个子序列.所以要增加当前窗口的数量. 代码如下: #inc

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

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

TCP的窗口滑动机制

TCP的滑动窗口主要有两个作用,一是提供TCP的可靠性,二是提供TCP的流控特性.同时滑动窗口机制还体现了TCP面向字节流的设计思路. 可靠:对发送的数据进行确认 流控制:窗口大小随链路变化. 一.tcp窗口机制 tcp中窗口大小是指tcp协议一次传输多少个数据.因为TCP是一个面向连接的可靠的传输协议,既然是可靠的就需要对传输的数据进行确认.TCP的窗口机制有两种,一种是固定窗口大小,另一种是滑动窗口.数据在传输时,TCP会对所有数据进行编号,发送方在发送过程中始终保持着一个窗口,只有落在发送

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

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 <ct

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

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