백준 11116번 교통량 Java

: ) YOUNG·2024년 10월 3일
1

알고리즘

목록 보기
403/417
post-thumbnail

백준 11116번 교통량 Java

https://www.acmicpc.net/problem/11116

문제



생각하기


  • 완전탐색 문제이다.

  • 좋은 문제인 것 같음



동작



결과


코드


배열


import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {

    // input
    private static BufferedReader br;

    // variables
    private static int M;
    private static int[] left = new int[201];
    private static int[] right = new int[201];
    private static final int TIME = 1000;

    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int tc = Integer.parseInt(br.readLine());
        while (tc-- > 0) {
            input();

            bw.write(solve());
        }

        bw.close();
    } // End of main()

    private static String solve() {
        StringBuilder sb = new StringBuilder();

        int ans = 0;
        for (int i = 0; i < M; i++) {
            int temp = left[i];

            int time = Arrays.binarySearch(right, 0, M, temp + TIME);
            if (time >= 0) {
                ans++;
            }
        }

        sb.append(ans / 2).append('\n');
        return sb.toString();
    } // End of solve()

    private static void input() throws IOException {
        M = Integer.parseInt(br.readLine());

        StringTokenizer st = new StringTokenizer(br.readLine());
        for (int i = 0; i < M; i++) {
            left[i] = Integer.parseInt(st.nextToken());
        }

        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < M; i++) {
            right[i] = Integer.parseInt(st.nextToken());
        }
    } // End of input()
} // End of Main class


HashSet

import java.io.*;
import java.util.HashSet;
import java.util.StringTokenizer;

public class Main {

    // input
    private static BufferedReader br;

    // variables
    private static int M;
    private static final int TIME = 1000;
    private static HashSet<Integer> leftSet;
    private static HashSet<Integer> rightSet;

    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int tc = Integer.parseInt(br.readLine());
        while (tc-- > 0) {
            input();

            bw.write(solve());
        }

        bw.close();
    } // End of main()

    private static String solve() {
        StringBuilder sb = new StringBuilder();

        int ans = 0;
        for (int left : leftSet) {

            if (rightSet.contains(left + TIME)) {
                ans++;
            }
        }

        sb.append(ans / 2).append('\n');
        return sb.toString();
    } // End of solve()

    private static void input() throws IOException {
        M = Integer.parseInt(br.readLine());
        leftSet = new HashSet<>();
        rightSet = new HashSet<>();

        StringTokenizer st = new StringTokenizer(br.readLine());
        for (int i = 0; i < M; i++) {
            leftSet.add(Integer.parseInt(st.nextToken()));
        }

        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < M; i++) {
            rightSet.add(Integer.parseInt(st.nextToken()));
        }
    } // End of input()
} // End of Main class

0개의 댓글