Advertisement
Guest User

Untitled

a guest
Jun 27th, 2013
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class YaF {
  5.  
  6.     final static String file = "taxi";
  7.  
  8.     public static void solve(Input in, PrintWriter out) throws IOException {
  9.         long len = in.nextLong();
  10.         long d = in.nextLong();
  11.         int n = in.nextInt();
  12.         long[] xs = new long[n];
  13.         for (int i = 0; i < n; ++i) {
  14.             xs[i] = in.nextLong();
  15.         }
  16.         Arrays.sort(xs);
  17.         int reserved = 0;
  18.         while (reserved < n && xs[reserved] < len - d) {
  19.             ++reserved;
  20.         }
  21.         if (reserved == n) {
  22.             out.println(0);
  23.             return;
  24.         }
  25.         if (xs[n - 1] >= d + len) {
  26.             out.println(1);
  27.             return;
  28.         }
  29.         long x = 0;
  30.         int ans = 0;
  31.         for (int i = n - 1; i >= 0; --i) {
  32.             if (i == reserved) {
  33.                 continue;
  34.             }
  35.             if (xs[i] < d - x) {
  36.                 out.println(0);
  37.                 return;
  38.             }
  39.             ans++;
  40.             x += xs[i] - (d - x);
  41.             if (x >= len) {
  42.                 out.println(ans);
  43.                 return;
  44.             }
  45.             if (x + xs[reserved] - (d - x) >= len) {
  46.                 out.println(ans + 1);
  47.                 return;
  48.             }
  49.         }
  50.         out.println(0);
  51.     }
  52.  
  53.     public static void main(String[] args) throws IOException {
  54.         PrintWriter out = new PrintWriter(file + ".out");
  55.         solve(new Input(new BufferedReader(new FileReader(file + ".in"))), out);
  56.         out.close();
  57.     }
  58.  
  59.     static class Input {
  60.         BufferedReader in;
  61.         StringTokenizer st;
  62.  
  63.         public Input(BufferedReader in) {
  64.             this.in = in;
  65.             eat("");
  66.         }
  67.  
  68.         public Input(String s) {
  69.             this.in = new BufferedReader(new StringReader(s));
  70.         }
  71.  
  72.         public void eat(String str) {
  73.             st = new StringTokenizer(str);
  74.         }
  75.  
  76.         public String next() throws IOException {
  77.             while (!st.hasMoreTokens()) {
  78.                 String line = in.readLine();
  79.                 if (line == null) {
  80.                     return null;
  81.                 }
  82.                 eat(line);
  83.             }
  84.             return st.nextToken();
  85.         }
  86.  
  87.         public int nextInt() throws IOException {
  88.             return Integer.parseInt(next());
  89.         }
  90.  
  91.         public long nextLong() throws IOException {
  92.             return Long.parseLong(next());
  93.         }
  94.  
  95.         public double nextDouble() throws IOException {
  96.             return Double.parseDouble(next());
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement