SHARE
TWEET

helpcross

a guest Feb 15th, 2017 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.FileReader;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.io.PrintWriter;
  7. import java.util.Arrays;
  8. import java.util.Comparator;
  9. import java.util.StringTokenizer;
  10.  
  11. public class helpcross {
  12.     public static int c, n;
  13.     public static int[] chickens;
  14.     public static Cow[] cows;
  15.     public static void main(String[] args) throws IOException {
  16.         BufferedReader br = new BufferedReader(new FileReader("helpcross.in"));
  17.         PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("helpcross.out")));
  18.         StringTokenizer st = new StringTokenizer(br.readLine());
  19.         c = Integer.parseInt(st.nextToken());
  20.         n = Integer.parseInt(st.nextToken());
  21.         chickens = new int[c];
  22.         cows = new Cow[n];
  23.         for(int i = 0; i < c; i++) {
  24.             chickens[i] = Integer.parseInt(br.readLine());
  25.         }
  26.        
  27.         for(int i = 0; i < n; i++) {
  28.             st = new StringTokenizer(br.readLine());
  29.             Cow temp = new Cow(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
  30.             cows[i] = temp;
  31.         }
  32.        
  33.         Arrays.sort(chickens);
  34.         Arrays.sort(cows, new Comparator<Cow>() {
  35.             public int compare(Cow c1, Cow c2) {
  36.                 if(c1.end < c2.end)
  37.                     return -1;
  38.                 if(c2.end < c1.end)
  39.                     return 1;
  40.                 if(c1.start < c2.start)
  41.                     return -1;
  42.                 if(c2.start < c1.start)
  43.                     return 1;
  44.                 return 0;
  45.             }
  46.            
  47.         });
  48. //      System.out.println(Arrays.toString(chickens));
  49. //      System.out.println(Arrays.toString(cows));
  50.         boolean[] visited = new boolean[c];
  51.         for(Cow moo: cows) {
  52.             for(int i = 0; i < c; i++) {
  53.                 if(!visited[i] && moo.start <= chickens[i] && chickens[i] <= moo.end) {
  54.                     visited[i] = true;
  55.                     break;
  56.                 }
  57.             }
  58.         }
  59.         int ans = 0;
  60.         for(boolean b: visited) {
  61.             if(b)
  62.                 ans++;
  63.         }
  64.         out.println(ans);
  65.         out.close();
  66.     }
  67.    
  68.     static class Cow {
  69.         int start, end;
  70.         public Cow(int start, int end) {
  71.             this.start = start;
  72.             this.end = end;
  73.         }
  74.        
  75.         public String toString() {
  76.             return start + "," + end;
  77.         }
  78.     }
  79.  
  80. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top