poj 3320 Jessica's Reading Problem
来源:互联网 发布:淘宝联盟怎么拿返利 编辑:程序博客网 时间:2024/06/11 02:13
这道题的题意是求能够把全部知识点都覆盖的最小页数,就比如:
1 2 2 2 1
只需要读前两页就能读完所有知识点,因此答案即为2.
那么对于这道题的做法来说,需要选择hash表跟尺取法。
我们以以下数列为例:
12 13 15 17 13 12 15 12 17 15
每一次先寻找到一个能够覆盖所有知识点的区间[s,t](t<=书页总数目),然后把开头s去掉,如果此时开头s这个知识点的出现次数为0的话,就要把区间内知识点的总数-1.
接着在寻找一个能够覆盖所有知识点的区间[s+1,t'](t'<=书页总数目),然后把开头s+1去掉,如果此时开头s+1这个知识点的出现次数也为0的话,就把区间内知识点的总数-1。以此不断的推进t跟s,每一次新加页出现某知识点的话,就要把该知识点出现的次数加1.
于是,对于每一次寻找的数列为:
12 13 15 17
13 15 17 13 12
15 17 13 12
17 13 12 15
13 12 15 12 17
12 15 12 17 15
另外,输入输出记得用cin,cout会超时。
#include <cstdio>#include <map>#include <set>int a[1000004];using namespace std;int main(){int n;while (~scanf("%d",&n)){set<int> t_set;map<int, int> t_map;for (int i = 0; i < n; i++){scanf("%d",&a[i]);t_set.insert(a[i]);}int cnt = t_set.size();int left = 0;int right = 0;int num = 0;int res = n;while (true){while (right < n&&num < cnt){if (!t_map[a[right]]){num++;}t_map[a[right]]++;right++;}if (num < cnt)break;res = res < right - left ? res : right - left;t_map[a[left]]--;if (!t_map[a[left]])num--;left++;}printf("%d\n",res);}return 0;}
0 0
- 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
- poj 3320 Jessica's Reading Problem
- Jessica's Reading Problem POJ 3320
- 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
- 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
- poj 3320 Jessica's Reading Problem
- 扫码登录流程
- leetcode---Length of Last Word---字符串
- block问题
- Fater-RCNN-Caffe Configuration
- 单例--使用枚举类型实现
- poj 3320 Jessica's Reading Problem
- 用angularjs做全选和反选遇到的问题
- c++ java相互调用
- 拓扑排序题集
- RN(react native)入坑指南-01,Hello RN,Windows下的环境搭建
- VmProtect v2.12.3 安装注册
- 【VLC核心一】播放流程梳理->live555收流+ffmpeg:AVCodec解码
- C++第三次实验——项目1-静态成员应用
- Sublime3 Terminal UBuntu install