Description

给出一段长度为\(n\)的整数序列,求包含所有出现过的数的连续区间的最小长度。

Input

第一行给出序列长度\(n\),第二行给出\(n\)个整数,表示这个序列。\(n \leqslant 10^6\)。

Output

输出一个整数,表示满足条件的区间的最小长度。

Sample Input

5
1 8 8 8 1

Sample Output

2

Solution

求连续区间,尺取问题。限制条件是下限,区间要包含每一种数。依次推进左端点,对于每个左端点,右端点向右直到找到第一个满足条件的位置,更新答案。如果右端点直到最右端仍然找不到合法区间,就跳出。

Code

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int N = 1e6 + 10;int a[N];
map<int, int> mp;int main()
{int n;scanf("%d", &n);mp.clear();for (int i = 1; i <= n; i++) scanf("%d", a + i), mp[a[i]]++;int sum = mp.size();mp.clear();int r = 1, cnt = 0, ans = INF;for (int l = 1; l <= n; l++){while (r <= n && cnt < sum){mp[a[r]]++;if (mp[a[r]] == 1) cnt++;r++;}if (cnt < sum) break;ans = min(ans, r - l);mp[a[l]]--;if (mp[a[l]] == 0) cnt--;}printf("%d\n", ans);return 0;
}

Link

http://poj.org/problem?id=3320

转载于:https://www.cnblogs.com/dadamrx/p/7352591.html

POJ 3320 Jessica's Reading Problem (尺取)相关推荐

  1. 尺取法 POJ 3320 Jessica's Reading Problem

    题目传送门 1 /* 2 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 3 */ 4 #include <cstdio> 5 #include &l ...

  2. 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 ...

  3. POJ3320 Jessica's Reading Problem 尺取法

    Jessica's Reading Problem. Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22716 Accepted ...

  4. Jessica‘s Reading Problem(尺取法)

    Jessica's Reading Problem (尺取法) Jessica's a very lovely girl wooed by lots of boys. Recently she has ...

  5. 【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 i ...

  6. poj3320Jessica's Reading Problem—尺取法(java)

    题目链接 大意:给序列数字,找出最小子序列,包含所有的元素类型.例如 5 1 8 8 8 1 输出2,因为1 8就包含了所有元素 思路:尺取法 这个和裸的尺取优点不同的是,他需要一个map来维护判断而 ...

  7. Jessica's Reading Problem

    第一部分:题目 Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. ...

  8. POJ 3320 尺取法,Hash,map标记

    1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识 ...

  9. Bound Found POJ - 2566 (尺取+前缀和)

    题意 就是给一串序列 在给一个t 求一段区间的加和绝对值与t最小的值与区间左右端点是多少 思路 这道题一开始不知道如何去做 用尺取法求区间但是因为区间中存在负数没有单调性  没有特殊的特征 若对区间求 ...

最新文章

  1. 三大软件公司争霸赛区块链
  2. Response.Redirect:无法在发送 HTTP 标头之后进行重定向
  3. UA MATH567 高维统计专题2 Low-rank矩阵及其估计3 Rank RIP
  4. android sdk 更新用的HOSTS
  5. RabbitMQ之惰性队列(Lazy Queue)
  6. LiteOS基于Sensorhub的超声波模组移植
  7. VMware虚拟机下实现Linux与window文件夹共享
  8. 怎么制作铁闸门_咖啡师养成记 | 教你做一杯合格的拿铁咖啡
  9. 朱兰的质量管理论(转载)
  10. 软件工程基础知识--软件项目管理
  11. python3用turtle模块画一棵随机樱花树
  12. Mac上通过docker配置PHP开发环境
  13. ubuntu下载BT种子安装qBittorrent
  14. OSG智能指针---Referenced类
  15. 工作中要怎样与同事相处呢?
  16. PHP知识一:系统知识总结
  17. 电影版本名词解析 (CAM,TS,TC,DVDSCR,DVDRIP,HR-HDTV)
  18. 合肥一中2021高考成绩查询,2021年合肥重点高中名单及排名,合肥高中高考成绩排名榜...
  19. 都是博士生的西湖大学第三期新招195位学生
  20. 代数方程与差分方程模型(二):原子弹爆炸的能量估计

热门文章

  1. python简历数据提取_如何使用pyPDF2从PDF格式的多页简历中提取文本数据?
  2. 11.23关于微信JSAPI缺少参数的问题解决
  3. ios_UITextField右侧小圆叉
  4. MAC EI Capitan上更新系统自带SVN版本号(关闭SIP方能sudo rm)
  5. SpringMVC中的文件上传
  6. 1091. Acute Stroke (30)
  7. sizeof运算求结构体大小
  8. unity3d UGUI Scroll View的使用
  9. 如何Docker化任意一个应用
  10. 改变textFiled中placeholder的字体颜色的方法以及不想光标在textView的最左边设置方法...