博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
uva 11572 unique snowflakes——yhx
阅读量:4572 次
发布时间:2019-06-08

本文共 3187 字,大约阅读时间需要 10 分钟。

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,再把右指针尽量后移,得到尽量长的区间。关键是如何判断右指针什么时候该停止,即判断一个区间是否可行。

1 #include
2 #include
3 using namespace std; 4 int a[1000010],last[1000010]; 5 map
cur; 6 int main() 7 { 8 int i,j,k,n,p,q,x,y,z,t,ans; 9 scanf("%d",&t);10 while (t--)11 {12 scanf("%d",&n);13 cur.clear();14 for (i=1;i<=n;i++)15 scanf("%d",&a[i]);16 for (i=1;i<=n;i++)17 {18 if (cur.count(a[i]))19 last[i]=cur[a[i]];20 else21 last[i]=-1;22 cur[a[i]]=i;23 }24 ans=0;25 for (i=1,j=1;j<=n;i++)26 {27 while (j<=n&&i>last[j]) j++;28 if (j-i>ans) ans=j-i;29 }30 printf("%d\n",ans);31 }32 }

以上为做法一。用nlogn时间求出上一个与之相同的元素的坐标last[i],然后右指针右移直到它的last超过左指针。

求last的过程中,用map存储元素上一次出现的位置,边扫描边更新。

1 #include
2 #include
3 using namespace std; 4 int a[1000100]; 5 int max(int a,int b) 6 { 7 return a>b?a:b; 8 } 9 int main()10 {11 int i,j,k,m,n,p,q,x,y,z,ans,T;12 scanf("%d",&T);13 while (T--)14 {15 set
s;16 scanf("%d",&n);17 for (i=1;i<=n;i++)18 scanf("%d",&a[i]);19 ans=-1;20 i=j=1;21 while (j<=n)22 {23 while (j<=n&&!s.count(a[j])) s.insert(a[j++]);24 ans=max(ans,j-i);25 s.erase(a[i++]);26 }27 printf("%d\n",ans);28 }29 }

以上为做法二。用set存储一个元素当前是否存在。左指针左移时删除元素,右指针右移时加入元素。

转载于:https://www.cnblogs.com/AwesomeOrion/p/5414272.html

你可能感兴趣的文章
NOIP2009 潜伏者
查看>>
本地预览的vue项目,在githubpage静态展示
查看>>
SC命令---安装、开启、配置、关闭 cmd命令行和bat批处理操作windows服务
查看>>
Register Form Code
查看>>
iphone 如何清空UIWebView的缓存
查看>>
Java——变量
查看>>
定时关闭AWS上的EC2机器实例
查看>>
grep、awk、sed命令详解1
查看>>
Jenkins邮件配置
查看>>
MYSQL数据库的设计与调优
查看>>
在Apache下开启SSI配置
查看>>
多线程上下文切换
查看>>
基于django后端的html、js简单实现含中文csv文件下载
查看>>
MySQL的InnoDB的幻读问题
查看>>
【转】 HTML解析:基于XPath的C#类库HtmlAgiliytyPack
查看>>
传递引用
查看>>
POJ 1611.The Suspects
查看>>
新的环境 新的生活 新的开始
查看>>
给有C或C++基础的Python入门 :Python Crash Course 1 - 3
查看>>
mysql的查询、子查询及连接查询
查看>>