Unique Snowflakes UVA - 11572 (离散化+尺取法)
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 (离散化+尺取法)的更多相关文章
- 51Nod 1686 第K大区间(离散化+尺取法)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1686 题意: 思路: 第K大值,所以可以考虑二分法,然后用尺取法去扫描, ...
- (白书训练计划)UVa 11572 Unique Snowflakes(窗体滑动法)
题目地址:UVa 11572 这样的方法曾经接触过,定义两个指针,不断从左向右滑动,推断指针内的是否符合要求. 这个题为了能高速推断是否有这个数,能够用STL中的set. 代码例如以下: #inclu ...
- 【例题 8-7 UVA - 11572】Unique Snowflakes
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 类似尺取法. 用set判断这段区间有没有重复的数字. 有的话,就把头节点的那个数字删掉,直到没有为止. [代码] /* 1.Shou ...
- nyoj133_子序列_离散化_尺取法
子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 给定一个序列,请你求出该序列的一个连续的子序列,使原串中出现的所有元素皆在该子序列中出现过至少1次. 如2 8 ...
- UVA 11990 `Dynamic'' Inversion CDQ分治, 归并排序, 树状数组, 尺取法, 三偏序统计 难度: 2
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- 离散化+线段树/二分查找/尺取法 HDOJ 4325 Flowers
题目传送门 题意:给出一些花开花落的时间,问某个时间花开的有几朵 分析:这题有好几种做法,正解应该是离散化坐标后用线段树成端更新和单点询问.还有排序后二分查找询问点之前总花开数和总花凋谢数,作差是当前 ...
- 子序列 NYOJ (尺取法+队列+hash) (尺取法+离散化)
子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 给定一个序列,请你求出该序列的一个连续的子序列,使原串中出现的所有元素皆在该子序列中出现过至少1次. 如2 8 ...
- 洛谷P1712 [NOI2016]区间 尺取法+线段树+离散化
洛谷P1712 [NOI2016]区间 noi2016第一题(大概是签到题吧,可我还是不会) 链接在这里 题面可以看链接: 先看题意 这么大的l,r,先来个离散化 很容易,我们可以想到一个结论 假设一 ...
- NOI2016区间bzoj4653(线段树,尺取法,区间离散化)
题目描述 在数轴上有 \(N\) 个闭区间 \([l_1,r_1],[l_2,r_2],...,[l_n,r_n]\) .现在要从中选出 \(M\) 个区间,使得这 \(M\) 个区间共同包含至少一个 ...
随机推荐
- firefox浏览器 插件--【维基百科+谷歌翻译】高级应用之 带图翻译
[维基词典+谷歌翻译]插件地址: https://addons.mozilla.org/zh-CN/firefox/addon/google-dictionary-and-google-t/?src= ...
- js 编辑数组
删除数组第一个元素使用var length = arr.shift(); 删除arr的第一个元素后, 返回值是删除后的数组长度 删除数组最后一个元素使用var length = a ...
- 【Teradata】gtwglobal查看
使用root登录TD数据库节点 cnsterm 6 start gtwglobal cnsterm 3 //上一个命令的window编号 h //帮助 QUIT //退出
- rem实现自适应
总结一下,rem实现自适应:用rem代替px,配合媒体查询设置font-size:n%. 首先,px是死的.若一律用px,那就会大的显得小,小的屏幕显得大.其次,rem是活的,通过设置字体大小可以引起 ...
- Java(使用JNA)调用DLL库与C#调用DLL库的对比
前言:在项目中经常使用DLL库对硬件进行操作,在发卡过程中使用频率尤为多,今天就Java与C#中调用DLL库的使用区别做一个介绍,本文着重具体的代码编写,具体过程看以下代码. 前提条件: 笔者已经封装 ...
- Djangoorm的多表建立与queryset对象的合并
使用这个方法的前提是:我们用Django的orm和REST来实现的 数据库提前建好了,而且表中字段相同,但是表名是不一样.需要动态在model中建表.我们使用Book_来模拟实现的 from djan ...
- UVA1442-Cav(扫描法)
Problem UVA1442-Cav Accept: 185 Submit: 679Time Limit: 3000 mSec Problem Description Input The inpu ...
- mysql-备份数据库脚本
备份数据库脚本,包括单库,全库备份脚本 #!/bin/bashc_user=rootc_password=12345678c_date=`date +"%Y%m%d"`c_dir= ...
- Spring Kafka整合Spring Boot创建生产者客户端案例
每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code 创建一个kafka-producer-master的maven工程.整个项目结构如下: ...
- Linux:Day6(下) vim编辑器
vim编辑器 简介: vi:Visual Interface,文本编辑器 文本:ASCII,Unicode 文本编辑种类: 行编辑器:sed 全屏编辑器:nano,vi VIM - Vi IMprov ...