Advertisement
Guest User

Untitled

a guest
Jul 15th, 2015
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.55 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class B {
  7.    
  8.     public static void main(String[] args) {
  9.        
  10.         Scanner scanner = new Scanner(System.in);
  11.         int n = scanner.nextInt();
  12.        
  13.         int[] array = new int[n];
  14.         Map<Integer, Interval> map = new HashMap<Integer,Interval>();
  15.        
  16.         for(int i = 0; i < n; i++) {
  17.             array[i] = scanner.nextInt();
  18.             if(map.get(array[i]) == null) {
  19.                
  20.                 Interval v = new Interval();
  21.                 v.setBegin(i);
  22.                 v.occ++;
  23.                
  24.                 map.put(array[i], v);
  25.                
  26.             }
  27.             else {
  28.                 map.get(array[i]).occ++;
  29.                 map.get(array[i]).seEnd(i);
  30.                
  31.             }
  32.            
  33.         }
  34.        
  35.         int max = -1;
  36.         int key = -1;
  37.         for(Map.Entry<Integer, Interval> entry : map.entrySet()) {
  38.             if(entry.getValue().occ > max) {
  39.                 max = entry.getValue().occ;
  40.                 key = entry.getKey();
  41.                
  42.             }
  43.            
  44.         }
  45.        
  46.         int min = Integer.MAX_VALUE;
  47.         int b = 0;
  48.         int e = 0;
  49.        
  50.         for(Map.Entry<Integer, Interval> entry : map.entrySet()) {
  51.             if(entry.getValue().occ == max && entry.getValue().dis < min) {
  52.                 min = entry.getValue().dis;
  53.                 b = entry.getValue().b;
  54.                 e = entry.getValue().e;
  55.                
  56.             }
  57.            
  58.         }
  59.        
  60.         System.out.println((b + 1) + " " + (e + 1));
  61.  
  62.     }
  63.    
  64.     static class Interval {
  65.        
  66.         private int occ;
  67.         private int b;
  68.         private int e;
  69.         private int dis;
  70.        
  71.         public void setBegin(int b) {
  72.             this.b = b;
  73.             this.dis = e - b;
  74.            
  75.         }
  76.        
  77.         public void seEnd(int e) {
  78.             this.e = e;
  79.             this.dis = e - b;
  80.            
  81.         }
  82.        
  83.         public int getDis() {
  84.             return this.dis;
  85.            
  86.         }
  87.        
  88.     }
  89.    
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement