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. Julia - 匿名函数

    Julia 中的函数可以被匿名构造,成为匿名函数,匿名函数是没有函数名的函数 julia> x -> x + 1 #3 (generic function with 1 method) 这 ...

  2. python开发_function annotations

    在看python的API的时候,发现了一个有趣的东东,即:python的方法(函数)注解(Function Annotation) 原文: 4.7.7. Function Annotations Fu ...

  3. DataMatrix二维条码源码分析检测识别图像位置

    发布时间:2014-10-31 DataMatrix的代码结构和QR码基本相同: 其中Detector的功能还是从原始图像中找出符号码的部分,并且进行透视转换纠正扭曲. 其解码流程与QR码差不多,关键 ...

  4. Unsupported compiler 'com.apple.compilers.llvmgcc42' selected for architecture 'armv7' Xcode5

    刚刚将Xcode更新到Xcode5,一运行报如下错误: Unsupported compiler 'com.apple.compilers.llvmgcc42' selected for archit ...

  5. C#关于using用法的总结

    1 作为指令,引入命名空间 using 命名空间的名字,这样可以直接使用命名空间中的类型,而不必指定类型的详细命名空间. 2 作为指令,定义别名 using 别名=详细命名空间信息的具体的类型. 3 ...

  6. 索引(index)

    #创建索引 create index index_name_pass on student(name,pass); create index index_name_id on student(name ...

  7. python gun readline

    https://github.com/ludwigschwardt/python-gnureadline

  8. boxing & unboxing

    [boxing & unboxing] Boxing is the process of converting a value type to the type object or to an ...

  9. zookeeper 面试题2 比较乱

    Zookeeper是什么框架分布式的.开源的分布式应用程序协调服务,原本是Hadoop.HBase的一个重要组件.它为分布式应用提供一致性服务的软件,包括:配置维护.域名服务.分布式同步.组服务等.应 ...

  10. Redis清理

    Redis登录: Redis目录: redis-cli.exe -h 127.0.0.1 -p 6739 login:  auth “password”      flushall