Jessica's Reading Problem——POJ3320

题目大意:

Jessica 将面临考试,她只能临时抱佛脚的在短时间内将课本内的所有知识点过一轮,课本里面的P个知识点顺序混乱,而且重复。

要求找出课本的连续页数,这些页数满足:包含全部知识点,且是包含全部知识点的页数区间里面最短的。

尺取法解题。

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner; public class Main { public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
int P=reader.nextInt();
Integer array[]=new Integer[P];
HashSet set=new HashSet();
for(int i=0;i<P;i++){
array[i]=reader.nextInt();
set.add(array[i]);
}
int n=set.size(); //知识点个数
int s=0; //开始结点
int e=0; //结束结点
int num=0; //统计目前知识点个数
int res=P;
HashMap map=new HashMap();
while(true){
while(e<P && num<n){
if(map.containsKey(array[e])==false){ //尚未包含此知识点
map.put(array[e], 1); //放入此知识点
num++;
}else{
Integer newValue=(Integer)map.get(array[e])+1; //旧知识点
map.put(array[e], newValue);
}
e++;
}
if(num<n){
break;
}
res=Math.min(res,e-s);
Integer newValue=(Integer)map.get(array[s])-1;
map.put(array[s], newValue);
if((Integer)map.get(array[s])==0){ //开始结点的知识点数<=1时,减去开始结点后知识个数会减少
map.remove(array[s]);
num--;
}
s++; //开始结点自增
}
System.out.print(res);
} }

Jessica's Reading Problem——POJ3320的更多相关文章

  1. POJ3320 Jessica's Reading Problem(尺取+map+set)

    POJ3320 Jessica's Reading Problem set用来统计所有不重复的知识点的数,map用来维护区间[s,t]上每个知识点出现的次数,此题很好的体现了map的灵活应用 #inc ...

  2. POJ3320 Jessica's Reading Problem 2017-05-25 19:55 38人阅读 评论(0) 收藏

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

  3. 【二分】Jessica's Reading Problem

    [POJ3320]Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1309 ...

  4. Greedy:Jessica's Reading Problem(POJ 3320)

    Jessica's Reading Problem 题目大意:Jessica期末考试临时抱佛脚想读一本书把知识点掌握,但是知识点很多,而且很多都是重复的,她想读最少的连续的页数把知识点全部掌握(知识点 ...

  5. POJ 3320 Jessica's Reading Problem

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

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

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

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

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

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

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

  9. POJ 3220 Jessica's Reading Problem

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

随机推荐

  1. Mysql进行复杂查询

    1.查询“生物”课程比“物理”课程成绩高的所有学生的学号: 思路: (1)获取所有选了 生物 课程的学生的成绩(学号,成绩) --临时表     (2)获取所有选了 物理 课程的学生的成绩(学号,成绩 ...

  2. TCP的交互数据流

    在TCP进行数据传输时,可以分为成块数据流和交互数据流两种,如果按字节计算,成块数据与交互数据的比例约为90%和10%,TCP需要同时处理这两类数据,且处理的算法不同. 书籍本章中以Rlogin应用为 ...

  3. RabbitMQ双活实践(转)

    有货RabbitMQ双活实践   消息服务中间件在日常工作中用途很多,如业务之间的解耦,其中 RabbitMQ 是比较容易上手且企业使用比较广泛的一种,本文主要介绍有货在使用 RabbitMQ 的一些 ...

  4. TI XDC工具入门简介

    1. XDC(Express DSP Component)是TI提供的一个命令行工具,它可以生成并使用实时软件组件包,它包括一系列工具,这些工具可以允许你将你的C语言代码组织成类似于java的包管理方 ...

  5. k8s podpreset 参数注入

    启动apiserver时,增加 参数 --runtime-config=settings.k8s.io/v1alpha1=true kind: PodPresetapiVersion: setting ...

  6. 【POJ1222】EXTENDED LIGHTS OUT

    参考博客 https://blog.csdn.net/so_so_y/article/details/76098713 题意 有一些灯泡组成了5*6的方阵.每个开关(开关会使灯泡的状态发生变化)除了控 ...

  7. shell if,case,for,while语法

    #shell if的语法 if [空格 xxx 空格] then echo xxxxx exit 1/2/3/4/.... 0表示正确. elif [空格 xxx 空格] then echo xxxx ...

  8. ios学习杂记

    commond  + alt + enter Xcode分屏.拖动xib连线

  9. 269. Alien Dictionary火星语字典(拓扑排序)

    [抄题]: There is a new alien language which uses the latin alphabet. However, the order among letters ...

  10. (转)Docker镜像中的base镜像理解

    base 镜像有两层含义: 不依赖其他镜像,从 scratch 构建. 其他镜像可以之为基础进行扩展. 所以,能称作 base 镜像的通常都是各种 Linux 发行版的 Docker 镜像,比如 Ub ...