読者です 読者をやめる 読者になる 読者になる

Codeforces Round #365 Div2 C: Chris and Road

競技プログラミング Codeforces

問題

Problem - C - Codeforces

xy 平面上で (0, 0) にいて、(0, w) に行きたい。自分は y 軸方向に最大 u の速さで動くことができる。N この頂点を持つ凸多角形が x 軸方向に -v の速さで動く。多角形の最初の座標が与えられるので、多角形にぶつからずに(辺上はセーフ) (0, w) に到着するまでの最小の時間を求めよ。

解法

次のような場合分けを考える。

  1. 多角形がy軸上を通過し始める前に多角形の前を横切るパターン。
  2. 多角形がy軸上を通過し終わった後にゴールに向かうパターン。

1. の時は、「多角形の各点が y 軸上に来た時に、自分は既にその y 座標よりも上にいるかどうか」を調べれば分かる。

2. の時は、多角形の下半分の辺にぶつからないように適度に待ちながら行けばOK

コード

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.NoSuchElementException;

/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            pass System Test!
*/

public class C {
  private static class Task {
    double w, v, u;
    double[] x, y;
    double waitAndDash(int head, int tail) {
      int N = x.length;
      int pos = head;
      double t = 0;
      double ny = 0;
      while (true) {
        if (x[pos] <= 0) {
          pos = (pos + 1) % N;
          continue;
        }
        double t1 = x[pos] / v;
        ny = Math.min((t1 - t) * u + ny, y[pos]);
        t = t1;
        if (pos == tail) break;
        pos = (pos + 1) % N;
      }
      return t + (w - ny) / u;
    }

    void solve(FastScanner in, PrintWriter out) throws Exception {
      int N = in.nextInt();
      w = in.nextInt();
      v = in.nextInt();
      u = in.nextInt();
      x = new double[N];
      y = new double[N];
      for (int i = 0; i < N; i++) {
        x[i] = in.nextInt();
        y[i] = in.nextInt();
      }

      int head = 0, tail = 0;
      for (int i = 0; i < N; i++) {
        if (x[head] > x[i]) head = i;
        if (x[head] == x[i] && y[head] > y[i]) head = i;

        if (x[tail] < x[i]) tail = i;
        if (x[tail] == x[i] && y[tail] > y[i]) tail = i;
      }

      if (x[tail] <= 0) {
        out.println(w / u);
        return;
      }
      if (x[head] <= 0 && x[tail] >= 0) {
        out.println(waitAndDash(head, tail));
        return;
      }

      for (int i = 0; i < N; i++) {
        if (x[i] <= 0) throw new Exception();
        double t = x[i] / v;
        if (y[i] > t * u) {
          out.println(waitAndDash(head, tail));
          return;
        }
      }
      out.println(w / u);

    }
  }

  /**
   * ここから下はテンプレートです。
   */
  public static void main(String[] args) throws Exception {
    OutputStream outputStream = System.out;
    FastScanner in = new FastScanner();
    PrintWriter out = new PrintWriter(outputStream);
    Task solver = new Task();
    solver.solve(in, out);
    out.close();
  }
  private static class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int bufferLength = 0;

    private boolean hasNextByte() {
      if (ptr < bufferLength) {
        return true;
      } else {
        ptr = 0;
        try {
          bufferLength = in.read(buffer);
        } catch (IOException e) {
          e.printStackTrace();
        }
        if (bufferLength <= 0) {
          return false;
        }
      }
      return true;
    }

    private int readByte() {
      if (hasNextByte()) return buffer[ptr++];
      else return -1;
    }

    private static boolean isPrintableChar(int c) {
      return 33 <= c && c <= 126;
    }

    private void skipUnprintable() {
      while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
    }

    boolean hasNext() {
      skipUnprintable();
      return hasNextByte();
    }

    public String next() {
      if (!hasNext()) throw new NoSuchElementException();
      StringBuilder sb = new StringBuilder();
      int b = readByte();
      while (isPrintableChar(b)) {
        sb.appendCodePoint(b);
        b = readByte();
      }
      return sb.toString();
    }

    long nextLong() {
      if (!hasNext()) throw new NoSuchElementException();
      long n = 0;
      boolean minus = false;
      int b = readByte();
      if (b == '-') {
        minus = true;
        b = readByte();
      }
      if (b < '0' || '9' < b) {
        throw new NumberFormatException();
      }
      while (true) {
        if ('0' <= b && b <= '9') {
          n *= 10;
          n += b - '0';
        } else if (b == -1 || !isPrintableChar(b)) {
          return minus ? -n : n;
        } else {
          throw new NumberFormatException();
        }
        b = readByte();
      }
    }

    double nextDouble() {
      return Double.parseDouble(next());
    }

    double[] nextDoubleArray(int n) {
      double[] array = new double[n];
      for (int i = 0; i < n; i++) {
        array[i] = nextDouble();
      }
      return array;
    }

    double[][] nextDoubleMap(int n, int m) {
      double[][] map = new double[n][];
      for (int i = 0; i < n; i++) {
        map[i] = nextDoubleArray(m);
      }
      return map;
    }

    public int nextInt() {
      return (int) nextLong();
    }

    public int[] nextIntArray(int n) {
      int[] array = new int[n];
      for (int i = 0; i < n; i++) array[i] = nextInt();
      return array;
    }

    public long[] nextLongArray(int n) {
      long[] array = new long[n];
      for (int i = 0; i < n; i++) array[i] = nextLong();
      return array;
    }

    public String[] nextStringArray(int n) {
      String[] array = new String[n];
      for (int i = 0; i < n; i++) array[i] = next();
      return array;
    }

    public char[][] nextCharMap(int n) {
      char[][] array = new char[n][];
      for (int i = 0; i < n; i++) array[i] = next().toCharArray();
      return array;
    }
  }
}