POJ 3320 Jessica's Reading Problem [尺取法] 《挑战程序设计竞赛》3.2
来源:互联网 发布:90后的中年危机 知乎 编辑:程序博客网 时间:2024/06/05 19:43
题目链接: POJ 3320 Jessica’s Reading Problem
题目大意:
一个数组中有P个数字, 其中会有重元素。求这个数组的最短的可以包含数组中所有不同元素的区间的长度
样例输入
5 (元素个数P)
1 8 8 8 1
样例输出:
2 (区间[0, 1]就可以包含所有不同的元素)
题解:
尺取法的训练题, 同时复习一下set 、map的用法
所谓尺取法,就是在有个可变长的小区间在大区间上向前移动,像毛毛虫在树枝上爬行一样,头部向前伸一点,尾部再向前缩一点。就这样爬过整条树枝。
代码:
#include <iostream>#include <cstdio>#include <set>#include <map>#define MAXP 1000010using namespace std;int P, total = 0;int a[MAXP], b[MAXP];set<int> s;map<int, int> m;int solve() { int res = P; int q = 0, t = 0; for (;;) { while (t < P && m.size() < total) { m[a[t]]++; t++; } if (m.size() < total) break; res = min(res, t-q); m[a[q]]--; if (m[a[q]] == 0) m.erase(a[q]); q++; } return res;}int main() { scanf("%d", &P); for (int i = 0; i < P; i++) { scanf("%d", &a[i]); s.insert(a[i]); } total = s.size(); printf("%d\n", solve()); return 0;}
0 0
- POJ 3320 Jessica's Reading Problem [尺取法] 《挑战程序设计竞赛》3.2
- POJ 3320 Jessica's Reading Problem 尺取法
- POJ 3320 Jessica's Reading Problem (尺取法)
- poj 3320 Jessica’s Reading Problem 【尺取法】
- poj 3320 Jessica's Reading Problem 【尺取法+STLmap set】
- poj 3320 Jessica's Reading Problem(尺取法+map/hash)
- POJ 3320 Jessica's Reading Problem(尺取法)
- POJ - 3320 Jessica's Reading Problem(尺取法)
- poj 3320 Jessica's Reading(尺取法 + Hash) Problem
- POJ - 3320 Jessica's Reading Problem(尺取法)
- poj 3320 Jessica's Reading Problem 【尺取法】
- poj 3320 Jessica's Reading Problem(尺取法)
- POJ-3320Jessica's Reading Problem(尺取法)
- POJ 3320 Jessica's Reading Problem(尺取法)
- Poj 3320 Jessica's Reading Problem【动态尺取法】
- POJ 3320 Jessica's Reading Problem(尺取法)
- poj--3320 Jessica's Reading Problem(尺取法)
- POJ 3320 Jessica's Reading Problem(尺取法)
- [集合框架] List 实现
- JSON的理解
- 基于卷积神经网络(CNN)的中文垃圾邮件检测
- Linux中fork()函数详解
- C语言 gets()和scanf()函数的区别
- POJ 3320 Jessica's Reading Problem [尺取法] 《挑战程序设计竞赛》3.2
- 2017年6款免费的移动产品原型和线框图设计工具
- Angular中ngCookies模块介绍
- Android Studio:layout-sw600dp文件夹中创建activity_main.xml
- 关键路径
- 工厂方法模式
- 什么情况下使用缓存
- power designer 16.5 使用总结
- 判断一个请求是否为Ajax请求