Advertisement
Guest User

Untitled

a guest
Jun 27th, 2013
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.90 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.OutputStreamWriter;
  3. import java.util.Arrays;
  4. import java.io.BufferedWriter;
  5. import java.util.InputMismatchException;
  6. import java.io.FileOutputStream;
  7. import java.io.OutputStream;
  8. import java.io.PrintWriter;
  9. import java.io.FileInputStream;
  10. import java.io.Writer;
  11. import java.io.InputStream;
  12.  
  13. /**
  14.  * Built using CHelper plug-in
  15.  * Actual solution is at the top
  16.  * @author ilyakor
  17.  */
  18. public class Main {
  19.     public static void main(String[] args) {
  20.         InputStream inputStream;
  21.         try {
  22.             inputStream = new FileInputStream("taxi.in");
  23.         } catch (IOException e) {
  24.             throw new RuntimeException(e);
  25.         }
  26.         OutputStream outputStream;
  27.         try {
  28.             outputStream = new FileOutputStream("taxi.out");
  29.         } catch (IOException e) {
  30.             throw new RuntimeException(e);
  31.         }
  32.         InputReader in = new InputReader(inputStream);
  33.         OutputWriter out = new OutputWriter(outputStream);
  34.         taxi solver = new taxi();
  35.         solver.solve(1, in, out);
  36.         out.close();
  37.     }
  38. }
  39.  
  40. class taxi {
  41.     public void solve(int testNumber, InputReader in, OutputWriter out) {
  42.         long m = in.nextLong();
  43.         long d = in.nextLong();
  44.         int n = in.nextInt();
  45.         long[] a = new long[n];
  46.         int opti = -1;
  47.         for (int i = 0; i < n; ++i) {
  48.             a[i] = in.nextLong();
  49.             if (a[i] >= m - d) {
  50.                 if (opti == -1 || a[i] < a[opti])
  51.                     opti = i;
  52.             }
  53.         }
  54.         if (opti == -1) {
  55.             out.printLine(0);
  56.             return;
  57.         }
  58.         long L = a[opti];
  59.         a[opti] = -1;
  60.         Arrays.sort(a);
  61.         long pos = 0;
  62.         int res = 0;
  63.         for (int i = a.length - 1; i >= 0; --i) {
  64.             if (L >= Math.abs(m - pos) + Math.abs(d - pos)) {
  65.                 ++res;
  66.                 pos = m;
  67.                 break;
  68.             }
  69.             long x = a[i];
  70.             long d1 = Math.abs(d - pos);
  71.             if (d1 > x) continue;
  72.             x -= d1;
  73.             ++res;
  74.             pos += x;
  75.             if (pos >= d) break;
  76.         }
  77.         if (pos < m) {
  78.             long x = L;
  79.             long d1 = Math.abs(d - pos);
  80.             if (d1 <= x) {
  81.                 x -= d1;
  82.                 ++res;
  83.                 pos += x;
  84.             }
  85.         }
  86.         if (pos < m) res = 0;
  87.         out.printLine(res);
  88.     }
  89. }
  90.  
  91. class InputReader {
  92.     private InputStream stream;
  93.     private byte[] buffer = new byte[10000];
  94.     private int cur;
  95.     private int count;
  96.  
  97.     public InputReader(InputStream stream) {
  98.         this.stream = stream;
  99.     }
  100.  
  101.     public static boolean isSpace(int c) {
  102.         return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  103.     }
  104.  
  105.     public int read() {
  106.         if (count == -1) {
  107.             throw new InputMismatchException();
  108.         }
  109.         try {
  110.             if (cur >= count) {
  111.                 cur = 0;
  112.                 count = stream.read(buffer);
  113.                 if (count <= 0)
  114.                     return -1;
  115.             }
  116.         } catch (IOException e) {
  117.             throw new InputMismatchException();
  118.         }
  119.         return buffer[cur++];
  120.     }
  121.  
  122.     public int readSkipSpace() {
  123.         int c;
  124.         do {
  125.             c = read();
  126.         } while (isSpace(c));
  127.         return c;
  128.     }
  129.  
  130.     public int nextInt() {
  131.         int sgn = 1;
  132.         int c = readSkipSpace();
  133.         if (c == '-') {
  134.             sgn = -1;
  135.             c = read();
  136.         }
  137.         int res = 0;
  138.         do {
  139.             if (c < '0' || c > '9') {
  140.                 throw new InputMismatchException();
  141.             }
  142.             res = res * 10 + c - '0';
  143.             c = read();
  144.         } while (!isSpace(c));
  145.         res *= sgn;
  146.         return res;
  147.     }
  148.  
  149.     public long nextLong() {
  150.         long sgn = 1;
  151.         int c = readSkipSpace();
  152.         if (c == '-') {
  153.             sgn = -1;
  154.             c = read();
  155.         }
  156.         long res = 0;
  157.         do {
  158.             if (c < '0' || c > '9') {
  159.                 throw new InputMismatchException();
  160.             }
  161.             res = res * 10L + (long)(c - '0');
  162.             c = read();
  163.         } while (!isSpace(c));
  164.         res *= sgn;
  165.         return res;
  166.     }
  167.  
  168.     }
  169.  
  170. class OutputWriter {
  171.     private final PrintWriter writer;
  172.  
  173.     public OutputWriter(OutputStream outputStream) {
  174.         writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
  175.     }
  176.  
  177.     public OutputWriter(Writer writer) {
  178.         this.writer = new PrintWriter(writer);
  179.     }
  180.  
  181.     public void print(Object... objects) {
  182.         for (int i = 0; i < objects.length; i++) {
  183.             if (i != 0) {
  184.                 writer.print(' ');
  185.             }
  186.             writer.print(objects[i]);
  187.         }
  188.     }
  189.  
  190.     public void printLine(Object... objects) {
  191.         print(objects);
  192.         writer.println();
  193.     }
  194.  
  195.     public void close() {
  196.         writer.close();
  197.     }
  198.  
  199.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement