题目:

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=;
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=;i<=n;i++)
cin>>a[i];
int left=,right=,num=;
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 ;
}

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

  1. UVa 11572 Unique snowflakes【滑动窗口】

    题意:给出 n个数,找到尽量长的一个序列,使得该序列中没有重复的元素 看的紫书,滑动窗口来做的 当右端碰到有相同的数的时候,左端向前滑动一个数 模拟一个样例好理解些 #include<iostr ...

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

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  3. Unique Snowflakes UVA - 11572 (离散化+尺取法)

    Emily the entrepreneur has a cool business idea: packaging and selling snowflakes. She has devised a ...

  4. TCP的窗口滑动机制

    TCP的滑动窗口主要有两个作用,一是提供TCP的可靠性,二是提供TCP的流控特性.同时滑动窗口机制还体现了TCP面向字节流的设计思路. 可靠:对发送的数据进行确认 流控制:窗口大小随链路变化. 一.t ...

  5. TCP的可靠性 窗口滑动 拥塞控制

    看这篇文章: http://www.cnblogs.com/woaiyy/p/3554182.html 窗口滑动,如下图: 流量控制 流量控制方面主要有两个要点需要掌握.一是TCP利用滑动窗口实现流量 ...

  6. UVa 11572 (滑动窗口) Unique Snowflakes

    滑动窗口挺有意思的,如果符合条件右端点一直向前走,不符合的话,左端点向前走. #include <bits/stdc++.h> using namespace std; set<in ...

  7. UVA - 11572 Unique Snowflakes(唯一的雪花)(滑动窗口)

    题意:输入一个长度为n(n <= 10^6)的序列A,找到一个尽量长的连续子序列AL~AR,使得该序列中没有相同的元素. 分析: 法一:从r=0开始不断增加r,当a[r+1]在子序列a[l~r] ...

  8. 【uva 11572】Unique Snowflakes(算法效率--滑动窗口,3种实现方法)

    题意:求长度为N的序列中,最长的一个无重复元素的连续子序列. 解法:[L,R]每次R++或L++延伸就可以得到答案. 实现:(1)next[],last[]--O(n): 1 #include< ...

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

    Emily the entrepreneur has a cool business idea: packaging and selling snowflakes. She has devised a ...

随机推荐

  1. 个人训练记录(UPD 9.16)

    本文章记录一些较难的题,摘自自己的blog中的其他文章.也有些单独成章有点浪费的题也写在里面了. 2019.7.15-2019.7.21 1182F(2900) 题意:求在区间 \([a,b]\) 中 ...

  2. 第一章 mysql 的架构与历史

    一.mysql 的逻辑架构 1.连接管理与安全性 2.优化与执行 二.并发控制 1.读写锁 2.锁粒度 三.事物 1.隔离级别 2.死锁 3.事物日志 四.多版本并发控制 五.Mysql 的存储引擎

  3. vi几个常用的命令

    1.同时打开多个文件:vi 1.txt 2.txt 3.txt 在多个文件中来回切换,命令行模式输入“:next"表示下一个,输入":previous"代表进入上一个,” ...

  4. easyui分页控件的应用

    1.首先应用easyui的js和css文件 <link rel="stylesheet" type="text/css" href="../.. ...

  5. SaltSack 中Job管理

    一.简介 Jid: job id的格式为%Y%m%d%H%M%S%f master在下发指令消息时,会附带上产生的jid,minion在接收到指令开始执行时,会在本地的cachedir(默认是/var ...

  6. 肯德基联手亚马逊Kindle试水咖啡主题店中店能成功吗?

    互联网上始终有一个传说:kindle与泡面是绝配.因为用kindle压着泡面,泡出来的味道格外的好.当然,这只是一个调侃.毕竟很多人购买kindle的动力是为了摆脱其他电子设备的诱惑,想去好好去读书. ...

  7. CF1137E Train Car Selection(单调栈维护凸函数)

    首先本题的关键是一次性加0操作只有第一个0是有用的.然后对于1 k操作,其实就是把之前的所有数删除.对于其他的情况,维护一次函数的和,将(i,a[i])看成平面上的一个点,用单调栈维护一下. #inc ...

  8. SVN服务器的搭建(一)

    1.基本概念 1.1.什么是版本控制 简单点来说,版本控制就是数据仓库,它可以记录你对文件的每次更改.这样,就算你在昏天黑地的改了几个月后老板说不要了,还是按照过去那样,你也不会抓狂,简单的恢复版本操 ...

  9. E - Apple Tree(树状数组+DFS序)

    There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. ...

  10. 透过F5获取服务器真实内网IP

    渗透测试过程中,经常会遇到目标服务器使用F5 LTM做负载均衡. 如果能获取到目标服务器的真实IP地址,会给后续渗透带来一定便利. 本文既是最近渗透遇到的一点点经验分享. F5修改cookie机制 F ...