uva 11572 unique snowflakes——yhx
Emily the entrepreneur has a cool business idea: packaging and selling snow
akes. She has devised a
machine that captures snow
akes as they fall, and serializes them into a stream of snow
akes that
ow,
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
snow
ake in a package must be different from the others. Unfortunately, this is easier said than done,
because in reality, many of the snow
akes
owing through the machine are identical. Emily would like
to know the size of the largest possible package of unique snow
akes that can be created. The machine
can start lling the package at any time, but once it starts, all snow
akes
owing 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 snow
akes have
owed out of the machine.
Input
The rst 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 snow
akes processed by the machine.
The following n lines each contain an integer (in the range 0 to 109, inclusive) uniquely identifying a
snow
ake. Two snow
akes are identied by the same integer if and only if they are identical.
The input will contain no more than one million total snow
akes.
Output
For each test case output a line containing single integer, the maximum number of unique snow
akes
that can be in a package.
整体思路是用滑动窗口,每次对一段区间[i,j]操作后,左指针+1,再把右指针尽量后移,得到尽量长的区间。关键是如何判断右指针什么时候该停止,即判断一个区间是否可行。
#include<cstdio>
#include<map>
using namespace std;
int a[],last[];
map<int,int> cur;
int main()
{
int i,j,k,n,p,q,x,y,z,t,ans;
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
cur.clear();
for (i=;i<=n;i++)
scanf("%d",&a[i]);
for (i=;i<=n;i++)
{
if (cur.count(a[i]))
last[i]=cur[a[i]];
else
last[i]=-;
cur[a[i]]=i;
}
ans=;
for (i=,j=;j<=n;i++)
{
while (j<=n&&i>last[j]) j++;
if (j-i>ans) ans=j-i;
}
printf("%d\n",ans);
}
}
以上为做法一。用nlogn时间求出上一个与之相同的元素的坐标last[i],然后右指针右移直到它的last超过左指针。
求last的过程中,用map存储元素上一次出现的位置,边扫描边更新。
#include<cstdio>
#include<set>
using namespace std;
int a[];
int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
int i,j,k,m,n,p,q,x,y,z,ans,T;
scanf("%d",&T);
while (T--)
{
set<int> s;
scanf("%d",&n);
for (i=;i<=n;i++)
scanf("%d",&a[i]);
ans=-;
i=j=;
while (j<=n)
{
while (j<=n&&!s.count(a[j])) s.insert(a[j++]);
ans=max(ans,j-i);
s.erase(a[i++]);
}
printf("%d\n",ans);
}
}
以上为做法二。用set存储一个元素当前是否存在。左指针左移时删除元素,右指针右移时加入元素。
uva 11572 unique snowflakes——yhx的更多相关文章
- (白书训练计划)UVa 11572 Unique Snowflakes(窗体滑动法)
题目地址:UVa 11572 这样的方法曾经接触过,定义两个指针,不断从左向右滑动,推断指针内的是否符合要求. 这个题为了能高速推断是否有这个数,能够用STL中的set. 代码例如以下: #inclu ...
- UVa 11572 Unique snowflakes【滑动窗口】
题意:给出 n个数,找到尽量长的一个序列,使得该序列中没有重复的元素 看的紫书,滑动窗口来做的 当右端碰到有相同的数的时候,左端向前滑动一个数 模拟一个样例好理解些 #include<iostr ...
- UVA - 11572 Unique Snowflakes
/* STLsort离散化==T 手工sort离散化==T map在线==T map离线处理c==A 240ms */ #include<cstdio> #include<map&g ...
- uva 11572 - Unique Snowflakes(和书略有不同)
本书是关于使用刘汝佳set, 通过收集找到.count()和删除.erase().这种方法比我好.用较短的时间. 我想map这个任务可以完成.但是,这是不容易删除,必须先找到find()标.然后删除索 ...
- UVA 11572 Unique snowflakes (滑窗)
用set,保存当前区间出现过的数字,如果下一个数字没有出现过,加入,否则删掉左端点,直到没有重复为止 #include<bits/stdc++.h> using namespace std ...
- UVA - 11572 Unique Snowflakes 滑动扫描
题目:点击打开题目链接 思路:从左往右扫描,定义扫描左端点L,右端点R,保证每次往几何中添加的都是符合要求的连续的数列中的元素,L和R从0扫到n,复杂度为O(n),使用set维护子数列,set查找删除 ...
- UVA - 11572 Unique Snowflakes(唯一的雪花)(滑动窗口)
题意:输入一个长度为n(n <= 10^6)的序列A,找到一个尽量长的连续子序列AL~AR,使得该序列中没有相同的元素. 分析: 法一:从r=0开始不断增加r,当a[r+1]在子序列a[l~r] ...
- 11572 - Unique Snowflakes(贪心,两指针滑动保存子段最大长度)
Emily the entrepreneur has a cool business idea: packaging and selling snowflakes. She has devised a ...
- UVA 11527 Unique Snowflakes
用STL做会很方便 SET: /*by SilverN*/ #include<iostream> #include<algorithm> #include<cstring ...
随机推荐
- 页面UI注意事项,你在乎吗?
早上打开微信,看到一篇文章,下面就和大家分享一下,该文章属于前端文章系列,希望做后台开发系统的程序员也可以学习一下,只会写代码把功能实现是第一,接下来也要把界面做做好. 现在的界面风格对于手机而言,一 ...
- javascript类的理解和使用
距离上次写博客已经过去好几个月了,现在手里的项目正好都结束了,闲暇之后开始理一下开发中一些问题,这次说一下javascript当中的类,可能很多人对于写惯了前台页面效果的coder来说,对于javas ...
- C# 发邮件
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- 状态压缩DP---Hie with the Pie
http: //acm.hust.edu.cn/vjudge/contest/view.action?cid=110044#problem/B Description The Pizazz Pizze ...
- springMVC图片文件上传功能的实现
在工程依赖库下添加文件上传jar包 commons-fileupload-1.2.2.jar commons-io-2.4.jar 2.jsp页面设置form表单属性enctype 在表单中上传图片时 ...
- 【Effective Java】3、避免创建不必要的对象
创建对象的时候,有些变量可以一直保持的时候,可以不必要每次实例化对象的时候都把这些变量初始化一遍,可以使用静态和静态块的方式把这些变量的数据固定下来 package cn.xf.cp.ch02.ite ...
- ahjesus C# 4.0 Parallel 并行运算
Parallel.For - for 循环的并行运算 Parallel.ForEach - foreach 循环的并行运算 Parallel.Invoke - 并行调用多个任务 Task - 任务,基 ...
- JS创建对象、继承原型、ES6中class继承
面向对象编程:java中对象的两个基本概念:1.类:类是对象的模板,比如说Leader 这个是泛称领导,并不特指谁.2:实例:实例是根据类创建的对象,根据类Leader可以创建出很多实例:liyi,y ...
- ad组策略和sharepoint office打开文档关系
组策略管理器 组策略继承 新建组策略 更新组策略 服务器端 1.cmd命令:gpupdate /force 2.更新ad站点与服务,针对多台ad 客户端 1.cmd命令:gpupdate /force ...
- 用Path来绘制一些图形
Path是android中用来封装几何学路径的一个类,因为Path在图形绘制上占的比重还是相当大的.你可以用它来绘制各种样式的几何图形,做图表什么的都可以. 一.画线段 1.1 lineT(float ...