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=;
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(窗口滑动)的更多相关文章
- UVa 11572 Unique snowflakes【滑动窗口】
题意:给出 n个数,找到尽量长的一个序列,使得该序列中没有重复的元素 看的紫书,滑动窗口来做的 当右端碰到有相同的数的时候,左端向前滑动一个数 模拟一个样例好理解些 #include<iostr ...
- sed修炼系列(三):sed高级应用之实现窗口滑动技术
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...
- Unique Snowflakes UVA - 11572 (离散化+尺取法)
Emily the entrepreneur has a cool business idea: packaging and selling snowflakes. She has devised a ...
- TCP的窗口滑动机制
TCP的滑动窗口主要有两个作用,一是提供TCP的可靠性,二是提供TCP的流控特性.同时滑动窗口机制还体现了TCP面向字节流的设计思路. 可靠:对发送的数据进行确认 流控制:窗口大小随链路变化. 一.t ...
- TCP的可靠性 窗口滑动 拥塞控制
看这篇文章: http://www.cnblogs.com/woaiyy/p/3554182.html 窗口滑动,如下图: 流量控制 流量控制方面主要有两个要点需要掌握.一是TCP利用滑动窗口实现流量 ...
- UVa 11572 (滑动窗口) Unique Snowflakes
滑动窗口挺有意思的,如果符合条件右端点一直向前走,不符合的话,左端点向前走. #include <bits/stdc++.h> using namespace std; set<in ...
- UVA - 11572 Unique Snowflakes(唯一的雪花)(滑动窗口)
题意:输入一个长度为n(n <= 10^6)的序列A,找到一个尽量长的连续子序列AL~AR,使得该序列中没有相同的元素. 分析: 法一:从r=0开始不断增加r,当a[r+1]在子序列a[l~r] ...
- 【uva 11572】Unique Snowflakes(算法效率--滑动窗口,3种实现方法)
题意:求长度为N的序列中,最长的一个无重复元素的连续子序列. 解法:[L,R]每次R++或L++延伸就可以得到答案. 实现:(1)next[],last[]--O(n): 1 #include< ...
- 11572 - Unique Snowflakes(贪心,两指针滑动保存子段最大长度)
Emily the entrepreneur has a cool business idea: packaging and selling snowflakes. She has devised a ...
随机推荐
- Linux-sys文件系统
1.sys文件系统本质上和proc文件系统是一样的,都是虚拟文件系统.都在根目录下有个目录(一个是/proc目录,另一个是/sys目录),因此都不是硬盘中的文件,都是内核中的数据结构的可视化接口. 2 ...
- gcc -E xx.c
C语言代码在交给编译器之前,会先由预处理器进行一些文本替换方面的操作,例如宏展开.文件包含.删除部分代码等. 在正常的情况下,GCC 不会保留预处理阶段的输出文件,也即.i文件.然而,可以利用-E选项 ...
- Python remove()和del语句 区别和辨析 列表删除操作
del语句可以删除列表中下标处的值,表中被删除值后后面的所有值将向前移动一个下标 spam = ['A','B','C','D','E'] del spam[2] spam 打印显示:['A', 'B ...
- node 配置文件
# cat ~/.npmrc prefix=E:/Private/nodejs #registry=http://r.cnpmjs.org/ registry=http://registry.npm. ...
- 在拖放文件的同时检测shift键的状态
老板要给原来文件拖放的功能加个扩展分类,于是想在文件拖放时判断shift键的状态来区分. 一般通过keydown和keyup来判断按下与否,但这都是需要控件事件触发,而在拖放的时候是没法触发key事件 ...
- itext实现横向pdf打印内容
Document doc = new Document(PageSize.A4); 这个是用PageSize.A4设置的是纵向A4大小 进入 com.itextpdf.text.PageSize的源码 ...
- 2)#ifndef和#pragma once
#################################################################################################### ...
- 吴裕雄--天生自然python机器学习:朴素贝叶斯算法
分类器有时会产生错误结果,这时可以要求分类器给出一个最优的类别猜测结果,同 时给出这个猜测的概率估计值. 概率论是许多机器学习算法的基础 在计算 特征值取某个值的概率时涉及了一些概率知识,在那里我们先 ...
- O - Snacks(DFS序)
百度科技园内有nn个零食机,零食机之间通过n−1n−1条路相互连通.每个零食机都有一个值vv,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充,零食机的价值vv会时常发生变化.小度熊只能从编号 ...
- LeetCode No.109,110,111
No.109 SortedListToBST 有序链表转换二叉搜索树 题目 给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的 ...