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. js 时间戳转时间格式

    $("#showbidMessage").append(<span>' + ChangeDateFormat(rows[i].createTime) + '</s ...

  2. C#发布和调试WebService

    一.编写并发布WebService服务 1.新建空web应用程序

  3. LeetCode算法题-N-ary Tree Level Order Traversal(Java实现)

    这是悦乐书的第225次更新,第238篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第92题(顺位题号是429).给定n-ary树,返回其节点值的级别顺序遍历.(即,从左到 ...

  4. 2018-10-18读文献总结之DCB码分多址、零基线、信号产生

    ---恢复内容开始--- 今天心血来潮,想开始把自己读文献的过程和每篇文献的收获总结一下,不知道CSDN怎么回事,一直登陆不进去,搞得我注册了一个博客园的账户,博客园新注册的还需要认证,但是很快,所以 ...

  5. 《Java大学教程》—第7章 类的实现

    统一建模语言(UML)用方框代表类.方框被分成3部分,第一部分是类名,第二部分是类的属性,第三部分是类的方法.类的属性(属性名称:属性类型):类的方法(方法名称(参数类型):返回值类型).静态的类的属 ...

  6. Django-rest-framework 接口实现 分页:(Pagination) 解析器(Parser) 渲染器(renderer)

    分页:(Pagination) rest_framework 中已经定义好了 3 种 分页模式 from rest_framework.pagination import PageNumberPagi ...

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

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

  8. 给定一个正整数n,返回从1到n构成的所有的BST

    public class C3 { public static void main(String[] args) { ArrayList<TreeNode> res = generateT ...

  9. linux 压缩文件或文件夹

    zip demo.mp4.zip demo.mp4 zip -r videos.zip videos # 压缩文件夹需要加-r

  10. [Python] Python 100例

    题目1:有四个数字:1.2.3.4,能组成多少个互不相同且无重复数字的三位数?各是多少? 程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. #程序源 ...