题意:给出 n个数,找到尽量长的一个序列,使得该序列中没有重复的元素

看的紫书,滑动窗口来做的

当右端碰到有相同的数的时候,左端向前滑动一个数

模拟一个样例好理解些

 #include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
#define mod=1e9+7;
using namespace std; typedef long long LL;
const int maxn=+;
int a[maxn]; int main(){
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=;i<n;i++) scanf("%d",&a[i]); set<int> s;
int l=,r=,ans=;
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 ;
}

UVa 11572 Unique snowflakes【滑动窗口】的更多相关文章

  1. UVA - 11572 Unique Snowflakes 滑动扫描

    题目:点击打开题目链接 思路:从左往右扫描,定义扫描左端点L,右端点R,保证每次往几何中添加的都是符合要求的连续的数列中的元素,L和R从0扫到n,复杂度为O(n),使用set维护子数列,set查找删除 ...

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

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

  3. uva 11572 unique snowflakes——yhx

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

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

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

  5. UVA - 11572 Unique Snowflakes

    /* STLsort离散化==T 手工sort离散化==T map在线==T map离线处理c==A 240ms */ #include<cstdio> #include<map&g ...

  6. uva 11572 - Unique Snowflakes(和书略有不同)

    本书是关于使用刘汝佳set, 通过收集找到.count()和删除.erase().这种方法比我好.用较短的时间. 我想map这个任务可以完成.但是,这是不容易删除,必须先找到find()标.然后删除索 ...

  7. UVA 11572 Unique snowflakes (滑窗)

    用set,保存当前区间出现过的数字,如果下一个数字没有出现过,加入,否则删掉左端点,直到没有重复为止 #include<bits/stdc++.h> using namespace std ...

  8. Unique Snowflakes(窗口滑动)

    题目: Emily the entrepreneur has a cool business idea: packaging and selling snowflakes. She has devis ...

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

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

随机推荐

  1. 一个SQL Server 2008 R2 死锁的问题解决

    问题场景:在客户那碰到一个操作卡死的现象 问题解决: 1.如何挂钩是死锁问题:通过代码跟踪,发现是指执行一个SQL语句超时,因此猜想可能是表锁住了 2.如果确认是思索问题:通过SQL发现死锁,以下是相 ...

  2. C# textbox 滚动条 随文本输入 滚动

    tb_Log.SelectionStart = tb_Log.Text.Length;//设置光标位置 tb_Log.ScrollToCaret();//随文本输入 滚动

  3. setblendstate & setdepthstencilstate

    http://msdn.microsoft.com/en-us/library/windows/desktop/ff476462(v=vs.85).aspx blendstate blendfacto ...

  4. Ignore files which are already versioned

    If you accidentally added some files which should have been ignored, how do you get them out of vers ...

  5. POJ3267The Cow Lexicon

    http://poj.org/problem?id=3267 题意 : 给你一个message,是给定字符串,然后再给你字典,让你将message与字典中的单词进行匹配,输出要删掉多少字母. 思路 : ...

  6. chmod命令详细用法

    指令名称 : chmod 使用权限 : 所有使用者 使用方式 : chmod [-cfvR] [--help] [--version] mode file... 说明 : Linux/Unix 的档案 ...

  7. 数据抓取的艺术(一):Selenium+Phantomjs数据抓取环境配置

     数据抓取的艺术(一):Selenium+Phantomjs数据抓取环境配置 2013-05-15 15:08:14 分类: Python/Ruby     数据抓取是一门艺术,和其他软件不同,世界上 ...

  8. [Mongo] error inserting documents: BSONObj size is invalid (mongoimport mongorestore 数据备份恢复)

    解决办法如下, ./mongoimport -port 6066 -d xxx -c xxx --batchSize=10 /root/mong_data/test/xxx 原因转自 http://b ...

  9. struts2中利用POI导出Excel文档并下载

    1.项目组负责人让我实现这个接口,因为以前做过类似的,中间并没有遇到什么太困难的事情.其他不说,先上代码: package com.tydic.eshop.action.feedback; impor ...

  10. struct dev_t

    device number(dev_t) linux driver 2009-08-21 10:08:03 阅读26 评论0 字号:大中小 dev_t description:     the dev ...