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. Django学习---Web框架及基础知识

    Django学习---Web框架 web框架的本质 我们在学socket,我们创建一个socketserver,然后运行起来,有一个client客户端要连接socket服务端,连接上之后,如果两边都没 ...

  2. vs2012应用程序的打包和图标设置

    最近用VS2010+QT做了一个小软件,为了把它打包发布,查了很多资料,现在总结下,便于以后查看. 本方法不限于VS2010,也不限于QT,只要你运行你的代码得到exe之后,都可以参照本方法进行. 参 ...

  3. C投票系统

  4. Java的native关键字以及JNI

    http://blog.csdn.net/yangjiali014/article/details/1633017 这篇博文相当清楚了 包括native关键字的介绍,JNI的书写步骤,以及JNI的实现 ...

  5. 第二章ARP——地址解析协议

    本章我们要讨论的问题是只对 T C P / I P协议簇有意义的I P地址.数据链路如以太网或令牌环网都有自己的寻址机制(常常为 48 bit地址),这是使用数据链路的任何网络层都必须遵从的.一个网络 ...

  6. WampServer之php、mysql环境安装

    WampServer之php.mysql环境安装 WampServer介绍: WampServer是一款由法国人开发的Apache Web服务器.PHP解释器以及MySQL数据库的整合软件包.免去了开 ...

  7. linux下使用adb查看android手机的logcat

    root@ubuntu:/home/song# adb logcat -s VLC

  8. 利用同步辅助类CountDownLatch计算多线程的运行时间

    一.CountDownLatch jdk提供的一个同步辅助类,在完成一组在在其他线程中执行的操作前,允许一个或者多个其他的线程等待,通过调用 await() 方法阻塞,直到由于 countDown() ...

  9. innobackupex 备份数据搭建 MySQL Slave

    简介: 数据量比较大时,使用 innobackupex 备份数据新增 MySQL Slave 节点. 安装 innobackupex 工具,我这里写过一次:http://www.cnblogs.com ...

  10. (转)libvirt API的基本概念

    本文摘自:http://blog.sina.com.cn/s/blog_da4487c40102v31i.html libvirt对象 libvirt的对象向外展现了虚拟化环境的所有资源.libvir ...