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

김미서 / 11월 2주차 / 월,목 #332

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

seomiii
Copy link
Contributor

@seomiii seomiii commented Nov 9, 2023


🎈boj 14226 - 이모티콘


🗨 해결방법 :


✔코드 :

import java.util.*;
import java.io.*;
public class BOJ14226_이모티콘 {
    static int S;
    static int[] dp;
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        S = Integer.parseInt(br.readLine());
        dp = new int[S*2 + 1]; // 마지막에 빼주는 경우도 생각하기 // s+2 까지만 해서 틀렸었음!!
        dp[2] = 2;

        for(int i=3; i<=dp.length-1; i++){
            dp[i] = i;
            // i를 i-1까지 나눠주면서 나누어 떨어지면 -> 나누어준 값 + 몫 으로 비교하기
            // 다 구했으면 -> 현재 값 + 1 이 이전 값보다 작으면 갱신해주기
            for (int div=2; div<i; div++){
                if (i % div == 0){ // 나누어 떨어지면
                    dp[i] = Math.min(dp[i], dp[div] + i/div );
                }
            }

            // 뒤에까지 계속 보면서 갱신해주기
            for (int j=i; j>=2; j--){
                dp[j-1] = Math.min(dp[j-1], dp[j]+1);
            }
        }
        System.out.println(dp[S]);
    }
}


🎈boj 23829 - 인물예술탐사주간


🗨 해결방법 :

  • longlong!!!! long 끼리 계산할 때는 long 처리를 합시다~~

✔코드 :

import java.io.*;
import java.util.*;

public class BOJ23829_인물예술탐사주간 {
    static int N,Q,P;
    static long result;
    static int[] trees;
    static long[] sums;
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        StringBuilder sb = new StringBuilder();
        N = Integer.parseInt(st.nextToken());
        Q = Integer.parseInt(st.nextToken());
        trees = new int[N];
        sums = new long[N];

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

        Arrays.sort(trees); // 정렬
        // 구간합 구하기
        sums[0] = trees[0];
        for (int i=1; i<N; i++){
            sums[i] = sums[i-1] + trees[i];
        }

        for (int i=0; i<Q; i++){ // 사진 수만큼
            P = Integer.parseInt(br.readLine());
            int location = find(P);
            long smallerSum = 0;
            int smallerCnt = 0;
            long biggerSum = 0;
            int bigCnt = 0;

            if (location == 0){
                biggerSum = sums[N-1];
                bigCnt = N;

            } else{
                smallerSum = sums[location-1];
                smallerCnt = location;
                biggerSum = sums[N-1] - sums[location-1];
                bigCnt = N - (location);
            }

            result = 0;
            result += ((long)P * smallerCnt) - smallerSum;
            result += biggerSum - ((long)P * bigCnt);
            sb.append(result).append("\n");
        }
        System.out.println(sb);
    }
    private static int find(int p){ // 이분 탐색으로 위치 찾기
        int start = 0;
        int end = N;

        while (start < end){
            int mid = (start + end) / 2;
            if (trees[mid] < p){ // 크면 -> end 이동
                start = mid + 1;
            } else{
                end = mid;
            }
        }
        return end;
    }
}


🎈boj 25795 - 예쁜 초콜릿과 숫자놀이


🗨 해결방법 :

  • 문자열을 모두 만들려고 하지말자!!

✔코드 :

import java.io.*;
import java.util.*;

public class BOJ25795_예쁜초콜릿과숫자놀이 {
    static int N;
    static long a,b,c;
    static long result;
    static long pretty;
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        a = Long.parseLong(st.nextToken());
        b = Long.parseLong(st.nextToken());
        c = Long.parseLong(st.nextToken());
        result = 0;
        pretty = (a+b)*c;

        if (N == 1){ // N이 1이면 -> WD밖에 안됨
            result = ((a+b) * c) % 100000;
        } else{
            solve(0,0,a);
        }
        System.out.println(result);
    }

    private static void solve(int white, int black, long r){
        if( white == N && black == N){ // 모두 다 썼으면
            result = Math.max(result, r);
            return;
        }
        if (white < N ){
            solve(white+1, black, (r+b) % 100000 );
        }
        if ( white > black && black < N) {
            solve(white, black+1, (r*c) % 100000 );
        }
    }
}

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

Successfully merging this pull request may close these issues.

2 participants