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

나호균 / 10월 1주차 / 목 #263

Merged
merged 1 commit into from
Oct 11, 2023
Merged

Conversation

nahokyun
Copy link
Contributor

@nahokyun nahokyun commented Oct 5, 2023


🎈boj 2660 - 회장뽑기


🗨 해결방법 : bfs


📝메모 :


✔코드 :

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

public class Main {

	private static Node[] relation;
	private static int n;

	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		n = Integer.parseInt(br.readLine());
		relation = new Node[n + 1];

		for (int i = 1; i <= n; i++) {
			relation[i] = new Node(null, i);
		}

		while (true) {
			StringTokenizer st = new StringTokenizer(br.readLine());
			int first = Integer.parseInt(st.nextToken());
			int second = Integer.parseInt(st.nextToken());

			if (first == -1 && second == -1) {
				break;
			}

			relation[first].next = new Node(relation[first].next, second);
			relation[second].next = new Node(relation[second].next, first);
		}
		// 입력 종료

		int[] score = new int[n + 1];
		for (int i = 1; i <= n; i++) {
			score[i] = bfs(i);
		}

		ArrayList<Integer> al = new ArrayList<>();
		int min = n+2;
		for (int i = 1; i <= n; i++) {
			if (score[i] < min) {
				al.clear();
				al.add(i);
				min = score[i];
			} else if (score[i] == min) {
				al.add(i);
			}
		}
		
		Collections.sort(al);
		StringBuilder sb = new StringBuilder();
		sb.append(min-1).append(" ").append(al.size()).append("\n");
		for (int i : al) {
			sb.append(i).append(" ");
		}

		System.out.println(sb.toString());
	}

	private static int bfs(int i) {
		ArrayDeque<Integer> q = new ArrayDeque<>();
		q.add(i);
		boolean[] visited = new boolean[n + 1];
		visited[i] = true;
		int depth = 0;
		while (!q.isEmpty()) {
			ArrayDeque<Integer> tmp = new ArrayDeque<>();
			
			while (!q.isEmpty()) {
				int cur = q.poll();

				for (Node next = relation[cur].next; next != null; next = next.next) {
					if (!visited[next.num]) {
						tmp.add(next.num);
						visited[next.num] = true;
					}
				}

			}
			depth++;
			q = tmp;
		}

		return depth;
	}

	static class Node {
		Node next;
		int num;

		public Node(Node next, int num) {
			super();
			this.next = next;
			this.num = num;
		}
	}
}


🎈boj 15817 - 배수 공사


🗨 해결방법 : 배낭


📝메모 :


✔코드 :

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 x = Integer.parseInt(st.nextToken());

		Pipe[] pipes = new Pipe[n+1];
		int[][] dp = new int[n+1][x + 1];

		for (int i = 1; i <= n; i++) {
			st = new StringTokenizer(br.readLine());
			int l = Integer.parseInt(st.nextToken());
			int c = Integer.parseInt(st.nextToken());

			pipes[i] = new Pipe(l, c);

		}
		// 입력 종료
		

		dp[0][0]=1;
		for (int i = 1; i <= n; i++) {
			int curLength=pipes[i].length;
			for (int j = 0; j <= x; j++) {
				if(j==0) {
					dp[i][j]=1;
					continue;
				}
				if(j<curLength) {
					dp[i][j]=dp[i-1][j];
					continue;
				}
				for (int count = 0; count <= pipes[i].quantity; count++) {
					if (j >= count * curLength) {
						dp[i][j]+=dp[i-1][j-curLength*count];
					}
				}
			}
		}
		
		
		
		System.out.println(dp[n][x]);
	}

	static class Pipe {
		int length;
		int quantity;

		public Pipe(int length, int quantity) {
			super();
			this.length = length;
			this.quantity = quantity;
		}

	}

}


🎈boj 18427 - 함께 블록 쌓기


🗨 해결방법 : 배낭


📝메모 :


✔코드 :

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());
		int h = Integer.parseInt(st.nextToken());

		int[][] dp = new int[n + 1][h + 1];
		ArrayList<Integer>[] al=new ArrayList[n+1];
		

		for (int i = 1; i <= n; i++) {
			al[i]=new ArrayList<>();
			st=new StringTokenizer(br.readLine());
			while(st.hasMoreTokens()) {
				al[i].add(Integer.parseInt(st.nextToken()));
			}
			
			
			al[i].add(0);
		}
		// 입력 종료
		dp[0][0] = 1;

		for (int i = 1; i <= n; i++) {
			dp[i][0]=1;
			for (int k = 1; k <= h; k++) {
				int tmp = 0;
				for (int j = 0; j < al[i].size(); j++) {
					int cur = al[i].get(j);
					
					if(cur<=k){
						tmp+=dp[i-1][k-cur]%10007;
					}

				}
				dp[i][k] = tmp%10007;
			}
		}

		System.out.println(dp[n][h]);

	}

}


🎈boj 14699 - 관악산 등산


🗨 해결방법 : DP


📝메모 :


✔코드 :

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

public class Main {

    private static Node[] nodes;
    private static int[] dp;

    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());

        st=new StringTokenizer(br.readLine());

        ArrayList<Pair> breakPoint=new ArrayList<>();
        dp = new int[n+1];

        for(int i=1;i<=n;i++){
            breakPoint.add(new Pair(Integer.parseInt(st.nextToken()),i));
        }

        nodes = new Node[n+1];
        for(int i=1;i<=n;i++){
            nodes[i]=new Node(null,i);
        }


        for(int i=0;i<m;i++){
            st=new StringTokenizer(br.readLine());

            int first=Integer.parseInt(st.nextToken());
            int second=Integer.parseInt(st.nextToken());

            nodes[first].next=new Node(nodes[first].next,second);
            nodes[second].next=new Node(nodes[second].next,first);
        }

        Collections.sort(breakPoint);

        for(Pair cur:breakPoint){
            find(cur.num);
        }

        StringBuilder sb=new StringBuilder();
        for(int i=1;i<=n;i++){
            sb.append(dp[i]).append('\n');
        }
        System.out.println(sb);

    }
    private static void find(int cur){
        int max=0;
        for(Node next=nodes[cur].next;next!=null;next=next.next){
            if(dp[next.num]>max){
                max=dp[next.num];
            }
        }

        dp[cur]=max+1;

    }

    static class Pair implements Comparable<Pair>{
        int height;
        int num;

        public Pair(int height, int num) {
            this.height = height;
            this.num = num;
        }

        @Override
        public int compareTo(Pair o) {
            return o.height-this.height;
        }
    }


    static class Node{
        Node next;
        int num;

        public Node(Node next, int num) {
            this.next = next;
            this.num = num;
        }
    }
}


🎈boj 17404 - RGB거리 2


🗨 해결방법 : 첫 출발지점을 고려한 dp


📝메모 :


✔코드 :

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 NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());

        int[][] colors = new int[n][3];
        int[][] dp = new int[n][3];
        for (int i = 0; i < n; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            for (int j = 0; j < 3; j++) {
                colors[i][j] = Integer.parseInt(st.nextToken());
            }
        }
        //입력 종료
        

        int result=10000000;
        for(int c=0;c<3;c++) {
            for(int i=0;i<3;i++){
                dp[0][i]=i==c?colors[0][i]:10000;
            }

            for (int i = 1; i < n; i++) {
                dp[i][0] = (Math.min(dp[i - 1][1], dp[i - 1][2])) + colors[i][0];
                dp[i][1] = (Math.min(dp[i - 1][0], dp[i - 1][2])) + colors[i][1];
                dp[i][2] = (Math.min(dp[i - 1][0], dp[i - 1][1])) + colors[i][2];
            }

            for(int i = 0 ; i < 3; i++)
                if(i != c)
                    result = Math.min(result, dp[n-1][i]);
        }
        System.out.println(result);

    }
}


🎈boj 1300 - K번째 수


🗨 해결방법 : 이분탐색


📝메모 : 접근할때 이분탐색을 생각하지 못했음


✔코드 :

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	private static long n;
	private static long k;

	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		
		n = Integer.parseInt(br.readLine());
		k = Integer.parseInt(br.readLine());

		long left=0;
		long right=n*n;
		long mid=0;
		
		while(left+1<right) {
			mid=(left+right)>>1;
		
			if(check(mid)) {
				left=mid;
			}else {
				right=mid;
			}
		}
		System.out.println(right);
		
	}

	private static boolean check(long mid) {
		long count=0;
		
		for(long i=1;i<=n;i++) {
			count+=mid/i>=n?n:mid/i;
			
			if(count>=k) {//수가 많으면 false 리턴 =>수를 줄여야함
				return false;
			}
		}
		
		
		return count>=k?false:true;
	}

}

@nahokyun nahokyun requested review from MadCom96 and haisley77 October 5, 2023 08:26
@nahokyun nahokyun merged commit 0071ced into SSAFY-10th-Seoul17:main Oct 11, 2023
1 check passed
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.

3 participants