poj 3320 jessica's Reading PJroblem 尺取法 -map和set的使用
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9134 | Accepted: 2951 |
Description
Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent little time on it. If she wants to pass it, she has to master all ideas included in a very thick text book. The author of that text book, like other authors, is extremely fussy about the ideas, thus some ideas are covered more than once. Jessica think if she managed to read each idea at least once, she can pass the exam. She decides to read only one contiguous part of the book which contains all ideas covered by the entire book. And of course, the sub-book should be as thin as possible.
A very hard-working boy had manually indexed for her each page of Jessica's text-book with what idea each page is about and thus made a big progress for his courtship. Here you come in to save your skin: given the index, help Jessica decide which contiguous part she should read. For convenience, each idea has been coded with an ID, which is a non-negative integer.
Input
The first line of input is an integer P (1 ≤ P ≤ 1000000), which is the number of pages of Jessica's text-book. The second line contains P non-negative integers describing what idea each page is about. The first integer is what the first page is about, the second integer is what the second page is about, and so on. You may assume all integers that appear can fit well in the signed 32-bit integer type.
Output
Output one line: the number of pages of the shortest contiguous part of the book which contains all ideals covered in the book.
Sample Input
5
1 8 8 8 1
Sample Output
2
Source
int num[1000100],a[1000100];
int main()
{
int n;
while(~scanf("%d",&n))
{
set<int > w;
memset(num,0,sizeof(num));
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
w.insert(a[i]);
}
int cnt=w.size();
int p=1,q=1,t=1,res=n+1;num[a[p]]++;//第一次re的代码,因为a[i]是整形范围的
//所以num就会爆内存,故只能换map了
下面是AC代码
#include<cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include<map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
int a[1000100];
int main()
{
int n;
while(~scanf("%d",&n))
{
set<int> w;map<int,int> num;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
w.insert(a[i]);//set相当于集合
}
int cnt=w.size();
int p=1,q=1,t=1,res=n+1;num[a[p]]++;
for(;;)
{
while(t<cnt&&q<=n){
q++;
if(!num[a[q]])
t++;
num[a[q]]++;
}
if(t<cnt)
break;
if(res>q-p+1) res=q-p+1;
num[a[p]]--;
if(!num[a[p]]) t--;
p++;
}
printf("%d\n",res);
}
return 0;
}
poj 3320 jessica's Reading PJroblem 尺取法 -map和set的使用的更多相关文章
- POJ 3320 Jessica's Reading Problem 尺取法/map
Jessica's Reading Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7467 Accept ...
- 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 ...
- 尺取法 POJ 3320 Jessica's Reading Problem
题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...
- POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13955 Accepted: 5896 Desc ...
- POJ 3320 Jessica‘s Reading Problem(哈希、尺取法)
http://poj.org/problem?id=3320 题意:给出一串数字,要求包含所有数字的最短长度. 思路: 哈希一直不是很会用,这道题也是参考了别人的代码,想了很久. #include&l ...
- POJ 3320 Jessica's Reading Problem (尺取法)
Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is co ...
- 题解报告: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 ...
- POJ 3320 Jessica's Reading Problem
Jessica's Reading Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6001 Accept ...
- poj3061 Subsequence&&poj3320 Jessica's Reading Problem(尺取法)
这两道题都是用的尺取法.尺取法是<挑战程序设计竞赛>里讲的一种常用技巧. 就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针 ...
随机推荐
- Java代码执行顺序及多态体现
/** * Description: * 基类的引用变量可以只想基类的实例对象也可指向其子类的事来对象 * 接口的引用变量也可以指向实现类的实例对象 * 程序调用的方法在运行期才动态绑定 * 绑定指将 ...
- Django中ajax发送post请求,报403错误CSRF验证失败解决办法
今天学习Django框架,用ajax向后台发送post请求,直接报了403错误,说CSRF验证失败:先前用模板的话都是在里面加一个 {% csrf_token %} 就直接搞定了CSRF的问题了:很显 ...
- Django项目运行端口被占用
error:以一种访问权限不允许的方式做了一个访问套接字的尝试,是8000端口被其他程序占用了,杀掉占用的程序就可以 (1)查找哪个进程占用了8000端口 `E:\sign_system\guest& ...
- O015、OpenStack 架构
参考https://www.cnblogs.com/CloudMan6/p/5340622.html 终于正式进入OpenStack 部分了. 今天开始正式学习OpenStack,OpenSt ...
- 重学JavaScript之匿名函数
1. 什么是匿名函数? 匿名函数就是没有名字的函数,有时候也称为< 拉姆达函数>.匿名函数是一种强大的令人难以置信的工具.如下: function a(a1, a2, a3) { // 函 ...
- C语言typedef详解
原文链接 C语言允许用户使用 typedef 关键字来定义自己习惯的数据类型名称,来替代系统默认的基本类型名称.数组类型名称.指针类型名称与用户自定义的结构型名称.共用型名称.枚举型名称等.一旦用户在 ...
- 批处理 使用默认浏览器 打开html文件
@echo offfor /f "tokens=3,4" %%a in ('"reg query HKEY_CLASSES_ROOT\http\shell\open\co ...
- pathlib:处理文件路径的瑞士军刀
pathlib是python3.4中引入的模块,专门用来处理路径的.个人觉得这是一个非常非常强大的模块,可以说是处理路径的一把瑞士军刀,下面我们就来看看它的功能. 在pathlib中有一个Path这个 ...
- iQOO5G手机卡槽公布
华为.中兴.一加.OPPO.vivo各自的5G手机相继拿到了3C强制认证,小米的也在路上,5G手机真的越来越近了. 今天,vivo iQOO还公布了其5G手机所用的卡槽样式,可以看到采用了双卡设计,同 ...
- jquery前端问题随记
1.图片名称不变,但要求刷新,web页面不重新加载渲染,在url地址后面加t=时间戳 js脚本 img.src=url+"?t="+Math.random() 如果是jsp页面,要 ...