题目:

解法:定义左索引和右索引

  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))的更多相关文章

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

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

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

  3. poj 3320 jessica's Reading PJroblem 尺取法 -map和set的使用

    jessica's Reading PJroblem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9134   Accep ...

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

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

  5. POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13955   Accepted: 5896 Desc ...

  6. POJ 3320 Jessica's Reading Problem

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

  7. POJ 3320 Jessica‘s Reading Problem(哈希、尺取法)

    http://poj.org/problem?id=3320 题意:给出一串数字,要求包含所有数字的最短长度. 思路: 哈希一直不是很会用,这道题也是参考了别人的代码,想了很久. #include&l ...

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

  9. 题解报告: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 ...

  10. poj3061 Subsequence&&poj3320 Jessica's Reading Problem(尺取法)

    这两道题都是用的尺取法.尺取法是<挑战程序设计竞赛>里讲的一种常用技巧. 就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针 ...

随机推荐

  1. BZOJ 2141 分块 线段树

    思路: a[i] //By SiriusRen #include <cmath> #include <cstdio> #include <cstring> #inc ...

  2. nginx报错日志:see security.limit_extensions

    访问出现部分.js.css等部分文件被拒绝错误日志如下: 19:20:13 [error] 1181#0: *287 FastCGI sent in stderr: "Access to t ...

  3. Hua Wei 机试题目三---2014

    一.根据对应规则进行翻译输出 描述:已知有如下的对应规则: ,则输入任意个正整数,输出经过规则翻译以后对应的结果. 例如:输入:1234:输出bcde. 题目很简单,我觉得需要注意的问题就是对于大整数 ...

  4. 对ListView的Item子控件监听并跳转页面

    public class MyAdapteforOwner extends BaseAdapter{ List<OwnerDevice>datas; private Context con ...

  5. ASP.NET 页面缓存OutputCache用法实例(附ab压力测试对比图)

    本文主要介绍Web窗体页面中的使用方法,MVC中使用方法,大家自行百度. 一.简单一行指令即可实现 <%@ OutputCache VaryByParam=" %> 这样整个页面 ...

  6. 指定的WSDL可能与所选的工具包不兼容

    使用LoadRunner在webservice协议下,import service的时候,报错: can not fetch WSDL,the specified WSDL datathe speci ...

  7. Type system

    Type system[edit] Main articles: Data type, Type system, and Type safety A type system defines how a ...

  8. CF 949C Data Center Maintenance_强联通分量_思维题

    题意: 某土豪公司建立了n个数据中心,把m份资料每份在其中的两个数据中心备份. 每个数据中心在一天h个小时当中有一个小时需要维护,此时不提供资料下载服务. 现在土豪公司想要将其中若干个数据中心的维护时 ...

  9. HDU 3068 最长回文( Manacher模板题 )

    链接:传送门 思路:Manacher模板题,寻找串中的最长回文子串 /***************************************************************** ...

  10. hive的mysql作元数据的hive-site.xml配置

    <property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://s ...