홈 AtCoder Typical 90 018 — Statue of Chokudai
글
취소

AtCoder Typical 90 018 — Statue of Chokudai

문제 링크

r = L / 2라고 하겠습니다. 경과 시간 E에서 관람차의 회전각은 주기 T를 이용해 theta = 2π * (E mod T) / T 라디안으로 나타낼 수 있습니다. E가 T보다 커도 E mod T를 사용하면 각도를 한 바퀴 범위 안으로 되돌릴 수 있습니다.

관람차의 최하점 높이를 0으로 두면, 회전하는 수직면에서 탑승자의 좌표는 y = -r sin(theta), z = r(1 - cos(theta))입니다. 관측자는 (X, Y, 0)에 있으므로 탑승자까지의 수평 거리는 sqrt(X² + (Y - y)²)입니다. 올려다보는 각도는 atan2(z, horizontal distance)로 구하고, 라디안에서 도 단위로 변환하면 답이 됩니다. 탑승자가 최하점에 있는 경우에도 atan2를 사용하므로 별도 처리가 필요하지 않습니다.

각 질의마다 상수 번의 산술 연산과 삼각 함수 계산을 하므로 시간 복잡도는 O(Q)입니다. 출력 버퍼에 O(Q) 공간을 사용합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.io.*;

public class Main {
  static class FastScanner {
    private final InputStream input = System.in;
    private final byte[] buffer = new byte[1 << 16];
    private int length;
    private int pointer;

    private int read() throws IOException {
      if (pointer == length) {
        length = input.read(buffer);
        pointer = 0;
        if (length == -1) return -1;
      }
      return buffer[pointer++];
    }

    long nextLong() throws IOException {
      int c;
      do {
        c = read();
      } while (c <= ' ' && c != -1);
      int sign = 1;
      if (c == '-') {
        sign = -1;
        c = read();
      }
      long value = 0;
      while (c > ' ') {
        value = value * 10 + c - '0';
        c = read();
      }
      return sign * value;
    }
  }

  public static void main(String[] args) throws IOException {
    FastScanner input = new FastScanner();
    long period = input.nextLong();
    double radius = input.nextLong() / 2.0;
    double observerX = input.nextLong();
    double observerY = input.nextLong();
    int q = (int) input.nextLong();
    StringBuilder output = new StringBuilder();

    for (int i = 0; i < q; i++) {
      long elapsed = input.nextLong();
      double theta = 2.0 * Math.PI * (elapsed % period) / period;
      double passengerY = -radius * Math.sin(theta);
      double passengerZ = radius * (1.0 - Math.cos(theta));
      double horizontal = Math.hypot(observerX, observerY - passengerY);
      double angle = Math.toDegrees(Math.atan2(passengerZ, horizontal));
      output.append(angle).append('\n');
    }

    System.out.print(output);
  }
}
이 글은 저자가 CC BY 4.0 라이선스로 배포합니다.