Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[백준] 2513번 통학버스 #33

Open
CMSSKKK opened this issue Oct 13, 2022 · 1 comment
Open

[백준] 2513번 통학버스 #33

CMSSKKK opened this issue Oct 13, 2022 · 1 comment
Assignees

Comments

@CMSSKKK
Copy link
Member

CMSSKKK commented Oct 13, 2022

TITLE

통학버스

LINK

  • 문제 출처 사이트 : 백준
  • Link

📷 Screenshots

댓글 양식

  • 아래 양식을 복사한 뒤 [shift]+[tab] 2회를 하고 작성하여 주세요
    ### 풀이 언어

    - python/java

    ### 코드

    ```python/java

    ```

    ### 핵심 로직 혹은 자료구조

    - 

    ### 시간 복잡도

    - O(  )

@CMSSKKK CMSSKKK self-assigned this Oct 13, 2022
@CMSSKKK
Copy link
Member Author

CMSSKKK commented Oct 13, 2022

풀이 언어

  • java

코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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 k = Integer.parseInt(st.nextToken());
        int s = Integer.parseInt(st.nextToken());
        int[] board = new int[100001];
        int min = 100000;
        int max = 0;

        for (int i = 0; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            int pos = Integer.parseInt(st.nextToken());
            min = Math.min(min, pos);
            max = Math.max(max, pos);
            int count = Integer.parseInt(st.nextToken());
            board[pos] = count;
        }
        int result = 0;
        int count = 0;
        while (min <= s) {

            if(count == 0 && board[min] != 0) {
                result += (s - min) * 2;
            }

            if(board[min] == 0) {
                min++;
                continue;
            }

            if(count + board[min] >= k) {
                board[min] -= k - count;
                count = 0;
            } else {
                count += board[min];
                board[min] = 0;
            }
        }
        count = 0;
        while (max >= s) {

            if(board[max] == 0) {
                max--;
                continue;
            }
            
            if(count == 0 && board[max] != 0) {
                result += (max - s) * 2;
            }

            if(count + board[max] >= k) {
                board[max] -= k - count;
                count = 0;
            } else {
                count += board[max];
                board[max] = 0;
            }
        }

        System.out.println(result);

    }
}

핵심 로직 혹은 자료구조

  • 더 좋은 코드가 있을 것 같은데요..

시간 복잡도

  • O( )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants