POJ 3320 Jessica's Reading Problem (尺取法,时间复杂度O(n logn))
题目:

解法:定义左索引和右索引
1.先让右索引往右移,直到得到所有知识点为止;
2.然后让左索引向右移,直到刚刚能够得到所有知识点;
3.用右索引减去左索引更新答案,因为这是满足要求的子串。
4.不断重复1,2,3。直到搜索到最后,不论怎样都获得不了所有的知识点时跳出。
代码:
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <map>
#include <set>
#include <queue>
using namespace std;
typedef long long ll;
#define INF 2147483647 int n;
int a[]; map <int,int> m; int main(){
int n;
cin >> n;
set <int> q;
for(int i = ;i < n; i++){
scanf("%d",&a[i]);
q.insert(a[i]);
// cout << a[i] << endl;
}
int num = q.size();
// cout << num << endl;
int res = INF; int sum = ;
int s = ;int t = ;
while(true){
while(t < n && sum != num){
if(m[a[t]] == )
sum++;
m[a[t]]++;
t++;
}
while(m[a[s]] > ){
m[a[s]]--;
s++;
if(m[a[s-]] == ){
sum--;
break;
}
}
if(sum < num-) break;
// cout << s << " " << t << endl;
res = min(res,t-s+);
} cout << res << endl;
return ;
}
POJ 3320 Jessica's Reading Problem (尺取法,时间复杂度O(n logn))的更多相关文章
- 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 PJroblem 尺取法 -map和set的使用
jessica's Reading PJroblem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9134 Accep ...
- 尺取法 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
Jessica's Reading Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6001 Accept ...
- 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 ...
- poj3061 Subsequence&&poj3320 Jessica's Reading Problem(尺取法)
这两道题都是用的尺取法.尺取法是<挑战程序设计竞赛>里讲的一种常用技巧. 就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针 ...
随机推荐
- Lucene倒排索引结构及关系
- POJ 2524 Ubiquitous Religions 【并查集】
解题思路:输入总人数 n,和m组数据:即和杭电畅通工程相类似,对这m组数据做合并操作后,求最后一共有多少块区域. #include<stdio.h> int pre[50001]; int ...
- C++ should define all local variable outside the loop?
see the following two examples, the conclusion is that we should define variable in the loop if it c ...
- 修改properties文件后系统运行异常
今天修改了项目的properties配置文件以后,运行会报异常,即使将内容改回,异常仍然存在.中间还会出现项目报错等问题,现将解决方法整理出来. 1.修改properties的打开方式,将打开方式从p ...
- dp入门—数塔
在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少? 已经告诉你了,这是个DP的题 ...
- [USACO 2009 Feb Gold] Fair Shuttle (贪心+优先队列)
题目大意:有N个站点的轻轨站,有一个容量为C的列车起点在1号站点,终点在N号站点,有K组牛群,每组数量为Mi(1≤Mi≤N),行程起点和终点分别为Si和Ei(1≤Si<Ei≤N).计算最多有多少 ...
- Shell(二)运算符
基本运算符 Shell 和其他编程语言一样,支持多种运算符,包括: 算数运算符 关系运算符 布尔运算符 字符串运算符 文件测试运算符 原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 ...
- C/C++中的段错误(Segmentation fault)[转]
Segment fault 之所以能够流行于世,是与Glibc库中基本所有的函数都默认型参指针为非空有着密切关系的. 来自:http://oss.lzu.edu.cn/blog/article.php ...
- vue如何每次打开子组件弹窗都进行初始化
:visible.sync 与 v-if同时使用即可.
- poj 3420 Quad Tiling (状压dp+多米诺骨牌问题+矩阵快速幂)
还有这种操作?????? 直接用pre到now转移的方式构造一个矩阵就好了. 二进制长度为m,就构造一个长度为1 << m的矩阵 最后输出ans[(1 << m) - 1][( ...