๊ธฐ๋ก๋ฐฉ

BOJ_17093 : Total Circle ๋ณธ๋ฌธ

CodingTest/Java

BOJ_17093 : Total Circle

Soom_1n 2022. 10. 19. 16:12

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

17093๋ฒˆ: Total Circle

์ขŒํ‘œํ‰๋ฉด์ƒ์˜ ์ ์˜ ๋ฐฐ์—ด P = P1, P2, โ‹ฏ, PN์™€ Q = Q1, Q2, โ‹ฏ, QM์ด ์žˆ๋‹ค. Q ๋ฐฐ์—ด ์ƒ์˜ ํ•œ ์ ์„ ์ค‘์‹ฌ์œผ๋กœ, P ๋ฐฐ์—ด ์ƒ์˜ ๋ชจ๋“  ์ ์„ ํฌํ•จํ•˜๋Š” ์ตœ์†Œ ๋„“์ด์˜ ์›์˜ ๋ฐ˜์ง€๋ฆ„ ์ค‘ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•˜์‹œ์˜ค.

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ํ•œ ์ ์„ ์ค‘์ ์œผ๋กœ ์—ฌ๋Ÿฌ ์ขŒํ‘œ๋ฅผ ์ง€๋‚˜๋Š” ์›์€ ํƒ€์›๋ชจ์–‘๊นŒ์ง€ ์ƒ๊ฐํ•˜๋ฉด ๋ฌด์ˆ˜ํžˆ ๋งŽ๋‹ค.
    • ๋ฌธ์ œ ์กฐ๊ฑด์—์„œ ์ตœ์†Œํฌ๊ธฐ์˜ ์›์˜ ์ตœ๋Œ€ ๋ฐ˜์ง€๋ฆ„์ด๋ผ ํ–ˆ์œผ๋ฏ€๋กœ, ์ค‘์ ๊ณผ ๊ฐ€์žฅ ๋จผ ์ขŒํ‘œ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋‹ค.
  • ๋ชจ๋“  ์ค‘์ ์—์„œ ๊ตฌํ•œ ๋ฐ˜์ง€๋ฆ„์˜ ์ตœ๋Œ€๊ฐ’๋“ค ์ค‘์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.
  • ์ตœ๋Œ€๊ฐ’ ์ฐพ๊ธฐ๊ฐ€ 2๋ฒˆ์žˆ๋Š” ์…ˆ์ด๊ณ , for๋ฌธ์ด ์ค‘์ฒฉ๋˜์–ด ํ•œ ๋ฒˆ์”ฉ ๋น„๊ตํ•˜๋Š” ํ˜•ํƒœ์ด๋‹ค.
    • n, m์˜ ์ตœ๋Œ€๊ฐ’์€ 1000์ด๋ฏ€๋กœ O(n^2)์—์„œ ์ตœ๋Œ€ ๊ณ„์‚ฐ๋Ÿ‰์€ 1,000,000 (๋ฐฑ๋งŒ)์œผ๋กœ 1์ดˆ ๋ฏธ๋งŒ์ด๋‹ค.
      ๋”ฐ๋ผ์„œ ์ œํ•œ์‹œ๊ฐ„ 1์ดˆ๋ฅผ ๋„˜์ง€ ์•Š๋Š”๋‹ค.
  • x, y์˜ ํฌ๊ธฐ๊ฐ€ 10^7 == 10,000,000(์ฒœ๋งŒ) ๊นŒ์ง€ ์ด๊ณ , ๋ฐ˜์ง€๋ฆ„ ๊ณ„์‚ฐ์—์„œ ์ œ๊ณฑํ•˜๊ฒŒ ๋œ๋‹ค.
    • ์ œ๊ณฑํ•˜๋ฉด 100,000,000,000,000(๋ฐฑ๊ฒฝ) ์ด๋ฏ€๋กœ int๋ฅผ ์ดˆ๊ณผํ•œ๋‹ค...long์„ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());
        ArrayList<Pair> nl = new ArrayList<>();
        ArrayList<Pair> ml = new ArrayList<>();

        for (int i = 0; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            nl.add(new Pair(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())));
        }
        for (int i = 0; i < m; i++) {
            st = new StringTokenizer(br.readLine());
            ml.add(new Pair(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())));
        }

        long answer = 0;
        for (int i = 0; i < m; i++){
            long rr = 0;
            for (int j = 0; j < n; j++){
                long temp = (long)(Math.pow(nl.get(j).x - ml.get(i).x, 2) + Math.pow(nl.get(j).y - ml.get(i).y, 2));
                if (temp > rr) {
                    rr = temp;
                }
            }
            if (rr > answer){
                answer = rr;
            }
        }
        System.out.println(answer);
    }
}

class Pair {
    int x,y;
    Pair(int x, int y){
        this.x = x;
        this.y = y;
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ์ขŒํ‘œ ๊ณ„์‚ฐ์„ ํ•ด์•ผํ•˜๋ฏ€๋กœ, Pair๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ x, y๋ฅผ ํ•จ๊ป˜ ์ €์žฅํ–ˆ๋‹ค.
    • ArrayLIst<> ์˜ ์ธ์ž๋กœ ๋„ฃ์œผ๋ฉฐ, ์‚ฌ์šฉํ•  ์ž๋ฃŒํ˜•์„ Pair๋กœ ์ง€์ •ํ•œ๋‹ค.
  • ์ค‘์ ๊ณผ ์ขŒํ‘œ๋“ค์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.
    • [๋ฐ˜์ง€๋ฆ„^2 == (x-x`)^2 + (y-y`)^2 ] ๊ณต์‹์„ ์‚ฌ์šฉํ•œ๋‹ค. (์ œ๊ณฑ ์‚ฌ์šฉ -> longํ˜• ํ•„์š”)
    • ๋‚˜์˜จ ๋ฐ˜์ง€๋ฆ„์˜ ์ œ๊ณฑ๊ฐ’์ค‘ ์ตœ๋Œ€๊ฐ’์„ rr์— ์ €์žฅํ•œ๋‹ค.
  • ๋‚˜์˜ค๋Š” rr์ค‘ ์ตœ๋Œ€๊ฐ’์„ answer์— ์ €์žฅํ•˜๊ณ  ์ตœ์ข… ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ๊ทธ๋ ค์ง€๋Š” ์›์ด ์ •ํ™•ํ•œ ์›์ธ ์ค„ ์•Œ๊ณ  ์ž˜๋ชป ์ ‘๊ทผํ•ด์„œ ๋งŽ์ด ํ‹€๋ ธ์—ˆ๋‹ค.
  • ํƒ€์›ํ˜•์œผ๋กœ ๊ทธ๋ ค์ง„๋‹ค๊ณ  ์ƒ๊ฐ์ด ๊ฐ€๋‹ˆ, ์™œ ์ตœ์†Œ๋„“์ด์˜ ์ตœ๋Œ€ ๋ฐ˜์ง€๋ฆ„์ธ์ง€ ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค.
  • ํ’€์ด๊ฐ€ ์ ์€ ๋ฌธ์ œ์—ฌ์„œ ๋” ๋‚ด ์˜ค๋ฅ˜๋ฅผ ์•Œ์•„์น˜๋ฆฌ๊ธฐ ์–ด๋ ค์› ์ง€๋งŒ, ํ’€๊ณ ๋‚˜๋‹ˆ ๊ฐ„๋‹จํ–ˆ๊ณ  ๋ธŒ๋ก ์ฆˆ 1๋ฌธ์ œ์˜€์ง€๋งŒ ๋งŽ์€ ๊ณต๋ถ€๊ฐ€ ๋œ ๊ฒƒ ๊ฐ™๋‹ค.

728x90