Advertisement
Guest User

Untitled

a guest
Jun 29th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.41 KB | None | 0 0
  1. import java.awt.Point;
  2. import java.io.*;
  3. import java.math.*;
  4. import java.util.*;
  5.  
  6. public class Main implements Runnable {
  7.  
  8.     int maxn = (int)1e5+200;
  9.     int mod = (int)1e9+7;
  10.     int inf = (int)1e9;
  11.     int n,m,k;
  12.     int a[] = new int[maxn];
  13.    
  14.     void solve() throws Exception {
  15.  
  16.         ArrayList<Integer> list2 = new ArrayList<>();
  17.         ArrayList<Integer> list1 = new ArrayList<>();
  18.    
  19.         list1.addAll(list2);
  20.        
  21.         int a1[] = {1,2,3,-99,0};
  22.         int a2[] = {7, 1, 5, 0, 9, -1};
  23.         int a3[] = {7, 1, 5, 0, 9, -1};
  24.         int a4[] = {7, 1, 5, 0, 9, -1};
  25.        
  26.         out.println(getDuplicates(a1, 1, 0));
  27.         out.println(getDuplicates(a2, 2, 3));
  28.         out.println(getDuplicates(a3, 2, 4));
  29.         out.println(getDuplicates(a4, 3, 1));
  30.     }
  31.    
  32.     public class Chapter {
  33.         List<Chapter> options;
  34.        
  35.         public Chapter(List<Chapter> options) {
  36.             this.options = options;
  37.         }
  38.        
  39.         public int getNumberOfStories() {
  40.             if (options==null || options.isEmpty()) return 1;
  41.             int numberOfStories = 0;
  42.             for (Chapter chapter : options) {
  43.                 numberOfStories+=chapter.getNumberOfStories();
  44.             }
  45.             return numberOfStories;
  46.         }
  47.        
  48.         public List<List<Chapter>> getAllPossibleStories() {
  49.             List<List<Chapter>> list = new ArrayList<>();
  50.             if (options==null || options.isEmpty()) {
  51.                 ArrayList<Chapter> story = new ArrayList<>();
  52.                 story.add(this);
  53.                 list.add(story);
  54.                 return list;
  55.             }
  56.            
  57.             // we can add memorization here by
  58.            
  59.             for (Chapter chapter: options) {
  60.                 List<List<Chapter>> allStories = chapter.getAllPossibleStories();
  61.                 for (List<Chapter> oneStory: allStories) { // creates all possible stories with current chapter
  62.                     List<Chapter> currentStory = new ArrayList<>();
  63.                     currentStory.add(this);
  64.                     currentStory.addAll(oneStory);
  65.                     list.add(currentStory);
  66.                 }
  67.             }
  68.             return list;
  69.         }
  70.     }
  71.    
  72.     //7, 1, 5, 0, 9, -1      2    4
  73.     public boolean getDuplicates (int a[], int k, int d) {
  74.        
  75.         TreeMap<Integer, Integer> treeMap = new TreeMap<>();
  76.         int l = 0;
  77.        
  78.         for (int r=0; r<a.length; r++) {
  79.             while (r-l>=k) {
  80.                 treeMap.put(a[l], treeMap.get(a[l])-1);
  81.                 if (treeMap.get(a[l])<=0) {
  82.                     treeMap.remove(a[l]);
  83.                 }
  84.                 l++;
  85.             }
  86.            
  87.             Integer ceiling = treeMap.ceilingKey(a[r]-d);
  88.             if (ceiling!=null && Math.abs(a[r]-ceiling)<=d) {
  89.                 return true;
  90.             }
  91.             treeMap.putIfAbsent(a[r], 0);
  92.             treeMap.put(a[r], treeMap.get(a[r])+1);
  93.         }
  94.        
  95.         return false;
  96.     }
  97.    
  98.     class Pair implements Comparable<Pair>{
  99.         int x;
  100.         int y;
  101.         public Pair (int x, int y) {
  102.             this.x = x;
  103.             this.y = y;
  104.         }
  105.  
  106.         @Override
  107.         public int compareTo(Pair p) {
  108.             if (x>p.x) return 1;
  109.             else if (x==p.x) {
  110.                 if (y>p.y) return 1;
  111.                 else if (y==p.y) return 0;
  112.                 else return -1;
  113.             } else {
  114.                 return -1;
  115.             }
  116.          
  117.         }
  118.     }
  119.      
  120.     String fileInName = "network";
  121.     boolean file = false;
  122.     boolean isAcmp = true;
  123.      
  124.     static Throwable throwable;
  125.     public static void main (String [] args) throws Throwable {
  126.         Locale.setDefault(new Locale("en", "US"));
  127.         Thread thread = new Thread(null, new Main(), "", (1 << 26));
  128.         thread.start();
  129.         thread.join();
  130.         thread.run();
  131.         if (throwable != null)
  132.             throw throwable;
  133.     }
  134.      
  135.     FastReader in;
  136.     PrintWriter out;
  137.  
  138.     public void run() {
  139.         String fileIn = "input.txt";
  140.         String fileOut = "output.txt";
  141.          
  142.         try {
  143.             if (isAcmp) {
  144.                 if (file) {
  145.                     in = new FastReader(new BufferedReader(new FileReader(fileIn)));
  146.                     out = new PrintWriter (fileOut);
  147.                 } else {
  148.                     in = new FastReader(new BufferedReader(new InputStreamReader(System.in)));
  149.                     out = new PrintWriter(System.out);
  150.                 }
  151.             } else if (file) {
  152.                 in = new FastReader(new BufferedReader(new FileReader(fileInName+".in")));
  153.                 out = new PrintWriter (fileInName + ".out");
  154.             } else {
  155.                 in = new FastReader(new BufferedReader(new InputStreamReader(System.in)));
  156.                 out = new PrintWriter(System.out);
  157.             }
  158.              
  159.             solve();
  160.         } catch(Exception e) {
  161.             throwable = e;
  162.         } finally {
  163.             out.close();
  164.         }
  165.  
  166.     }
  167. }
  168.  
  169. class FastReader {
  170.     BufferedReader bf;
  171.     StringTokenizer tk = null;
  172.  
  173.     public FastReader(BufferedReader bf) {
  174.         this.bf = bf;
  175.     }
  176.      
  177.     public String nextToken () throws Exception {
  178.         if (tk==null || !tk.hasMoreTokens()) {
  179.             tk = new StringTokenizer(bf.readLine());
  180.         }
  181.         if (!tk.hasMoreTokens()) return nextToken();
  182.         else
  183.             return tk.nextToken();
  184.     }
  185.      
  186.     public int nextInt() throws Exception {
  187.         return Integer.parseInt(nextToken());
  188.     }
  189.      
  190.     public long nextLong() throws Exception {
  191.         return Long.parseLong(nextToken());
  192.     }
  193.      
  194.     public double nextDouble() throws Exception {
  195.         return Double.parseDouble(nextToken());
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement