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++)以后,尾指针 ...
随机推荐
- Maven 学习笔记(一)
定义 Maven 是基于项目对象模型(POM)的软件项目管理工具,它采用纯 java 编写,用于管理项目的构建,最早在 Jakata Turbine 项目中开始被使用.它包含了一个项目对象模型(Pro ...
- nginx令牌限制并发
http{ limit_req_zone $binary_remote_addr zone=req_one:10m rate=100r/s; server { listen 8080; server_ ...
- 使用http-server开启一个本地服务器
前言 在写前端页面中,经常会在浏览器运行HTML页面,从本地文件夹中直接打开的一般都是file协议,当代码中存在http或https的链接时,HTML页面就无法正常打开,为了解决这种情况,需要在在本地 ...
- Java NIO(五)套接字通道
Socket通道 Socket通道和文件通道有着不一样的特征: Socket通道类可以运行于非阻塞模式,并且是可选的.这两个特征可以激活大程序(如网络服务和中间件组件)巨大的可伸缩性和灵活性,因此再也 ...
- Unity 退出游戏 方法
Application.Quit(); 嗯,没错,这篇就这么短.
- ntp.log日志梳理
[日志]offset 正负 机器A上执行: remote refid st t when poll reach delay offset jitter ======================== ...
- 字典树Trie Tree
又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是:利用字符串的公共前缀 ...
- java中的string trim具体有什么用处。。。
去掉字符串首尾空格 防止不必要的空格导致错误public class test{ public static void main(String[] args) { String str = " ...
- event 下鼠标坐标的获取
event.clientX.event.clientY 鼠标相对于浏览器窗口可视区域的X,Y坐标(窗口坐标),可视区域不包括工具栏和滚动条.IE事件和标准事件都定义了这2个属性 event.pageX ...
- "啃下"插入排序
插入排序法基本原理 插入排序法较冒泡排序法和选择排序法更贴近生活,应该来说理解起来更快.如果你现在能够得到一副麻将,请把里面的“一万”到“六万”拿出来,打乱顺序,再重新排好,就像打麻将开始那样.是否需 ...