POJ 3320 (尺取法+Hash)
题目链接: http://poj.org/problem?id=3320
题目大意:一本书有P页,每页有个知识点,知识点可以重复。问至少连续读几页,使得覆盖全部知识点。
解题思路:
知识点是有重复的,因此需要统计不重复元素个数,而且需要记录重复个数。
最好能及时O(1)反馈不重复的个数。那么毫无疑问,得使用Hash。
推荐使用map,既能Hash,也能记录对于每个key的个数。
尺取的思路:
①不停扩展R,并把扫过知识点丢到map里,直到map的size符合要求。
②更新结果。
②L++,map里的对应key(a[l++])的个数-1,相当于移出这页。
如果对应的key的个数<=0,则应该erase掉这个key,防止map::size()的误判。
#include "cstdio"
#include "map"
using namespace std;
int a[];
int main()
{
//freopen("in.txt","r",stdin);
int n,s,l=,r=,ans=0x3f3f3f3f;
scanf("%d",&n);
map<int,int> Hash,x;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
Hash[a[i]]++;
}
s=Hash.size();
while(true)
{
while(x.size()<s&&r<=n) x[a[r++]]++;
if(x.size()<s) break;
ans=min(ans,r-l);
x[a[l++]]--;
if(x[a[l-]]<=) x.erase(a[l-]);
}
printf("%d\n",ans);
}
| 13593020 | neopenx | 3320 | Accepted | 1520K | 422MS | C++ | 549B | 2014-11-03 00:30:01 |
POJ 3320 (尺取法+Hash)的更多相关文章
- POJ 3320 尺取法,Hash,map标记
1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识 ...
- POJ 3320 尺取法(基础题)
Jessica's Reading Problem Description Jessica's a very lovely girl wooed by lots of boys. Recently s ...
- poj 3320(尺取法)
传送门:Problem 3320 参考资料: [1]:挑战程序设计竞赛 题意: 一本书有 P 页,每页都有个知识点a[i],知识点可能重复,求包含所有知识点的最少的页数. 题解: 相关说明: 设以a[ ...
- A - Jessica's Reading Problem POJ - 3320 尺取
A - Jessica's Reading Problem POJ - 3320 Jessica's a very lovely girl wooed by lots of boys. Recentl ...
- poj 2100(尺取法)
Graveyard Design Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 6107 Accepted: 1444 ...
- POJ 2566 尺取法(进阶题)
Bound Found Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4297 Accepted: 1351 Spe ...
- POJ 3320 Jessica‘s Reading Problem(哈希、尺取法)
http://poj.org/problem?id=3320 题意:给出一串数字,要求包含所有数字的最短长度. 思路: 哈希一直不是很会用,这道题也是参考了别人的代码,想了很久. #include&l ...
- 尺取法 POJ 3320 Jessica's Reading Problem
题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...
- POJ 3320 Jessica's Reading Problem 尺取法
Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...
随机推荐
- android中一个解决办法
E Trace(732): error opening trace file: No such file or directory (2)android api 的版本和模拟器的版本不一致导致的
- svn不知道这样的主机
重做服务器后,计算机名称肯定是不一样的.我们之前的项目还是老计算机名字,只要更改一下计算机名字即可实现.或者更改
- ****php:require_once(dirname(__FILE__)."/./config_uc.php");
Q:麻烦清楚地讲解一下这句的意思,具体路径是怎样的,这个文解在 根目录,如果我想放在根目录下的tieba文件夹里,应该怎么修改/./ 这个是表示什么? A: require_once(dirname( ...
- Delphi中的基础数据类型
参考http://www.cnblogs.com/del/archive/2007/12/04/982167.html 在学习之初,在这么多的数据类型中,最好记住这五种标准数据类型(整型.实型.字符型 ...
- 【翻译十三】java-并发之饥饿与活锁
Starvation and Livelock Starvation and livelock are much less common a problem than deadlock, but ar ...
- 攻城狮在路上(叁)Linux(三十)--- 光盘写入工具
一.基本步骤: 1.用mkisofs命令将所需备份的数据构建成镜像文件. 2.用cdrecord命令将镜像文件刻录至光盘或者DVD中. 二.mkisofs:新建镜像文件 mkisofs [-0 镜像文 ...
- WPF中使用ReportViewer报表
本篇博客将介绍如何在WPF中使用ReportViewer控件. 1. 环境准备:下载安装最新版ReportViewer(PS:需要安装Microsoft SQL Server System CLR T ...
- apt-get常见错误——Unmet dependencies
转自:http://blog.sina.com.cn/s/blog_4980828b0100zicn.html 安装错误:“E: Unmet dependencies.”原因:非正常停止apt-get ...
- 配置ogg异构mysql-oracle 单向同步
从mysql到oracle和oracle到mysql差不多.大致步骤如下: 环境是:192.168.0.165 (Mysql ) —> 192.168.0.164 ( Oracle )想将mys ...
- 解决因为I_JOB_NEXT问题导致job执行不正常,不停生成trace文件问题
今天同事说有个项目生产环境的目录老是满.查看了一下bdump目录,发现确实是平均1分钟生成一个8M左右的trace文件.查询了一下alert日志,发现是个job的报错引起的.具体查看了一下trace文 ...