Poj3320 (ruler acquisition) and poj3320

Source: Internet
Author: User

Poj3320 (ruler acquisition) and poj3320

N number. The minimum interval overwrites all the different numbers in n number.

Solution:

AC code:

Import java. util. hashMap; import java. util. hashSet; import java. util. map; import java. util. imports; import java. util. set; public class Main {/*** @ param args */static int n; static Set <Integer> set = new HashSet <Integer> (); static int a [] = new int [1000000 + 2]; static Map <Integer, Integer> map = new HashMap <Integer, Integer> (); public static void main (String [] args) {// TODO Auto-generated method stub1_scan = new dynamic (System. in); n = scan. nextInt (); for (int I = 0; I <n; I ++) {set. add (a [I] = scan. nextInt ();} int size = set. size (); // calculate the number of different knowledge points int start = 0, end = 0, sum = 0; int res = n; (;;) {while (end <n & sum <size) {Integer cnt = map. get (a [end]); if (cnt = null) {sum ++; map. put (a [end], 1);} else map. put (a [end], cnt + 1); end ++;} if (sum <size) break; res = Math. min (end-start, res); int cnt = map. get (a [start]); if (cnt = 1) {map. remove (a [start]); sum --;} else map. put (a [start], cnt-1); start ++;} System. out. println (res );}}

  

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.