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

题意:求没有重复数字的最长区间.

思路:由于数组中元素的范围太大,上限为1e9,但是最多只有1e5个数,所以我们需要离散化一下,也可以不用离散化,直接用map进行标记。

然后快乐的尺取法。

我们维护一个区间,[ l , r ] ,使这个区间里的元素总是没有重复的,

当R向右移动后,如果有重复的就把L一直向右推,直到区间里没有重复元素,(因为[L,R] 不符合条件,那么[L,R+1]一定也不符合,所以要改变的是L,)

每一次符合条件后就立即停止L的右推,更新答案,然后移动R,

总体时间复杂度O(n*long n+ n)

细节见我的AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int t;
int n;
int a[maxn];
int b[maxn];
// map<int,int> bj;
int bj[maxn];
int main()
{
gg(t);
while(t--)
{
gg(n);
repd(i,,n)
{
gg(a[i]);
b[i]=a[i];
}
sort(b+,b++n);
int cnt=unique(b+,b++n)-b-;
repd(i,,n)
{
a[i]=lower_bound(b+,b++cnt,a[i])-b;
}
// repd(i,1,n)
// {
// db(a[i]); int ans=;
int l=;
int r=;
MS0(bj);
bj[a[]]++;bj[a[]]++;
while(r<=n)
{
while(bj[a[r]]>)
bj[a[l++]]--;
ans=max(ans,r>n?r-l:r-l+);
bj[a[++r]]++;
}
printf("%d\n",ans );
}
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}

Unique Snowflakes UVA - 11572 (离散化+尺取法)的更多相关文章

  1. 51Nod 1686 第K大区间(离散化+尺取法)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1686 题意: 思路: 第K大值,所以可以考虑二分法,然后用尺取法去扫描, ...

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

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

  3. 【例题 8-7 UVA - 11572】Unique Snowflakes

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 类似尺取法. 用set判断这段区间有没有重复的数字. 有的话,就把头节点的那个数字删掉,直到没有为止. [代码] /* 1.Shou ...

  4. nyoj133_子序列_离散化_尺取法

    子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 给定一个序列,请你求出该序列的一个连续的子序列,使原串中出现的所有元素皆在该子序列中出现过至少1次. 如2 8 ...

  5. UVA 11990 `Dynamic'' Inversion CDQ分治, 归并排序, 树状数组, 尺取法, 三偏序统计 难度: 2

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  6. 离散化+线段树/二分查找/尺取法 HDOJ 4325 Flowers

    题目传送门 题意:给出一些花开花落的时间,问某个时间花开的有几朵 分析:这题有好几种做法,正解应该是离散化坐标后用线段树成端更新和单点询问.还有排序后二分查找询问点之前总花开数和总花凋谢数,作差是当前 ...

  7. 子序列 NYOJ (尺取法+队列+hash) (尺取法+离散化)

    子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 给定一个序列,请你求出该序列的一个连续的子序列,使原串中出现的所有元素皆在该子序列中出现过至少1次. 如2 8 ...

  8. 洛谷P1712 [NOI2016]区间 尺取法+线段树+离散化

    洛谷P1712 [NOI2016]区间 noi2016第一题(大概是签到题吧,可我还是不会) 链接在这里 题面可以看链接: 先看题意 这么大的l,r,先来个离散化 很容易,我们可以想到一个结论 假设一 ...

  9. NOI2016区间bzoj4653(线段树,尺取法,区间离散化)

    题目描述 在数轴上有 \(N\) 个闭区间 \([l_1,r_1],[l_2,r_2],...,[l_n,r_n]\) .现在要从中选出 \(M\) 个区间,使得这 \(M\) 个区间共同包含至少一个 ...

随机推荐

  1. forfiles删除过期文件robocopy

    forfiles /p "F:\SDSC16B" /s /m *.bak /d -20 /c "cmd /c del @FILE" /p:指定目录 /s:递归搜 ...

  2. Linux下进程的创建过程分析(_do_fork do_fork详解)--Linux进程的管理与调度(八)

    Unix标准的复制进程的系统调用时fork(即分叉),但是Linux,BSD等操作系统并不止实现这一个,确切的说linux实现了三个,fork,vfork,clone(确切说vfork创造出来的是轻量 ...

  3. JavaScript -- 时光流逝(五):js中的 Date 对象的方法

    JavaScript -- 知识点回顾篇(五):js中的 Date 对象的方法 Date 对象: 用于处理日期和时间. 1. Date对象的方法 <script type="text/ ...

  4. February 26th, 2018 Week 9th Monday

    A good beginning is half done. 良好的开端是成功的一半. We can't finish anything if we don't start, sometimes ge ...

  5. #012python实验课

    通过三到四周的学习Python选修课程已经学到了网络爬虫这一环节. 基础语法混乱 这是,在进行周四实验课程的时候,一直遇到的一个问题.写着写着,就往C语言的语法方向跑了,可以说之前我仅仅是对,pyth ...

  6. #006 C语言大作业学生管理系统第三天

    还差最后两部分 读取文件 恢复删除的学生信息 先学会处理文件的 知识点,再继续跟着视频做这个作业. 应该明天周六能把视频里手把手教的学生管理系统敲完 第二周尽量自己能完成C语言课本最后面那道学生管理系 ...

  7. aliyun mysql

    https://segmentfault.com/q/1010000009603559?sort=created

  8. linux学习笔记整理(八)

    第九章 文件的归档和压缩本节所讲内容:9.1 tar命令进行文件的归档和压缩9.2 zip管理压缩文件9.3 了解gzip-bzip2- xz管理压缩文件-file-sort查看文件 9.1 tar命 ...

  9. 【转】Android多进程总结一:生成多进程(android:process属性)

    前言 正常情况下,一个apk启动后只会运行在一个进程中,其进程名为apk的包名,所有的组件都会在这个进程中运行,以下为DDMS的进程截屏: com.biyou.multiprocess为进程名,也是a ...

  10. 【转】svn冲突问题详解 SVN版本冲突解决详解

    (摘自西西软件园,原文链接http://www.cr173.com/html/46224_1.html) 解决版本冲突的命令.在冲突解决之后,需要使用svnresolved来告诉subversion冲 ...