-
Notifications
You must be signed in to change notification settings - Fork 1
[6주차] 지상일 백준 문제 2002, 9252, 15486, 15565, 2616, 7453 풀이 #14
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
base: main
Are you sure you want to change the base?
Changes from all commits
ca961c4
f4ddf05
ccbcafb
ed1064e
f2ced37
924e4f7
4bf736f
d330685
476ded0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package week6; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
| import java.util.Arrays; | ||
| import java.util.StringTokenizer; | ||
|
|
||
| public class JUN15486_Jisangil { | ||
| public static void main(String[] args) throws IOException { | ||
| BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
| int n = Integer.parseInt(br.readLine()); | ||
| int[][] dp = new int[n + 1][2]; // 누적할 배열 사실 1차원으로 만들어도됨 | ||
| int[][] arr = new int[n + 1][2]; // 입력받은 값을 넣을 배열 | ||
| for (int i = 1; i < n + 1; i++) { | ||
| StringTokenizer st = new StringTokenizer(br.readLine()); | ||
| for (int j = 0; j < 2; j++) { | ||
|
|
||
| dp[i][j] = Integer.parseInt(st.nextToken()); | ||
| arr[i][j] = dp[i][j]; | ||
| if (arr[i][0] + i > n + 1) { // 상담할 날이 퇴사할 날을 넘어가면 | ||
| dp[i][1] = 0; // dp값은 0 | ||
|
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| int max = 0; | ||
| for (int i = 1; i < n + 1; i++) { //첫날부터 끝까지 | ||
| int tmp = dp[i][0]; // 현재 날짜 상담의 소요 날 | ||
| int tmp1 = dp[Math.min(i + tmp, n)][0]; // tmp의 소요날이 끝나는 날짜의 소요 날 n이 넘어갈경우엔 n으로 | ||
| for (int j = i + tmp; j <= i + tmp + tmp1; j++) { // 현재날의 상담이 끝나는 날부터 상담끝나는날의 소요날이 끝나는 날까지 | ||
| if (j > n) { //범위 넘어가면 break | ||
| break; | ||
| } | ||
| if (dp[j][0] + j <= n + 1) { // 그날의 상담이 범위를 넘지 않는다면 | ||
| dp[j][1] = Math.max(dp[j][1], arr[j][1] + dp[i][1]); //현재 날과 가능한 그 다음 날들의 맥스 값 | ||
| } | ||
|
|
||
| } | ||
| max = Math.max(max, dp[i][1]); | ||
| } | ||
| System.out.println(max); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package week6; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.StringTokenizer; | ||
|
|
||
| public class JUN15565_Jisangil { | ||
| 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()); | ||
|
|
||
|
|
||
| List<Integer> lion = new ArrayList<>(); | ||
| st = new StringTokenizer(br.readLine()); | ||
| for (int i = 0; i < n; i++) { | ||
| int tmp = Integer.parseInt(st.nextToken()); | ||
| if (tmp == 1){ //라이언배열의 라이언 위치만 추가 | ||
| lion.add(i); | ||
| } | ||
| } | ||
| if (k>lion.size()){ //라이언이 k개보다 적으면 | ||
| System.out.println(-1); // 리턴 | ||
| return; | ||
| } | ||
|
|
||
| int min = Integer.MAX_VALUE; | ||
| for (int i = 0; i <= lion.size()-k; i++) { // 라이언의 위치가 0, 4, 6, 9 라고 하고 k가 3이라면 046과 469만 가능하므로 | ||
| min = Math.min(min, lion.get(i + k - 1) - lion.get(i) + 1); //k의 크기만큼에서 위치 빼주기 | ||
| } | ||
|
|
||
| System.out.println(min); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package week6; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
| import java.util.*; | ||
|
|
||
| public class JUN2002_Jisangil { | ||
|
|
||
| public static void main(String[] args) throws IOException { | ||
| BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
| int n = Integer.parseInt(br.readLine()); | ||
| Map<String, Integer> map = new HashMap<>(); //차와 현재 위치를 저장할 맵 | ||
| for (int i = 1; i <= n; i++) { | ||
| map.put(br.readLine(), i); | ||
| } | ||
| int[] add = new int[n + 1]; // 만약 4번이 1번위치로 올경우 1번은 2번이여야 하므로 더할 배열 | ||
| int count = 0; | ||
| for (int i = 1; i <= n; i++) { | ||
| String tmp = br.readLine(); | ||
| int tmpValue = map.get(tmp); // 추월하기 전에 위치의 값 | ||
| if (tmpValue+add[map.get(tmp)] > i){ // 추월하기 전의 값과 몇대가 추월해서 누적된 값의 합보다 현재 위치가 작으면 | ||
| count++; // 카운트 증가 | ||
| for (int j = 1; j < tmpValue; j++) { | ||
| add[j]++; // 4번이 1번으로 올경우 1번부터 3번까지는 +1씩 누적해준다고 보면됨 | ||
| } | ||
| } | ||
| } | ||
|
|
||
| System.out.println(count); | ||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package week6; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
| import java.util.StringTokenizer; | ||
|
|
||
| public class JUN2616_Jisangil { | ||
| public static void main(String[] args) throws IOException { | ||
| BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
| int n = Integer.parseInt(br.readLine()); | ||
| int[] arr = new int[n + 1]; //입력받은 배열 | ||
| int[] dp = new int[n + 1]; // 누적할 배열 | ||
| StringTokenizer st = new StringTokenizer(br.readLine()); | ||
| for (int i = 1; i < n + 1; i++) { | ||
| arr[i] = Integer.parseInt(st.nextToken()); | ||
| dp[i] = arr[i] + dp[i - 1]; // 누적합 | ||
| } | ||
| int max = Integer.parseInt(br.readLine()); // 최대 칸수 입력 | ||
|
|
||
|
|
||
| int[][] dp2 = new int[n + 1][4]; // dp 2차원 배열 | ||
|
|
||
| int result = 0; | ||
|
|
||
| for (int i = 1; i < 4; i++) { // 몇대를 이용했을 때의 최대값 구하는 반복문 | ||
| for (int j = max; j < n + 1; j++) { // 누적되있으므로 max부터 시작 max전에는 최대칸만큼 누적이 안됬기 때문 | ||
| dp2[j][i] = Math.max(dp[j] - dp[j - max] + dp2[j - max][i - 1], dp2[j - 1][i]); | ||
| // dp[j] == 현재 칸까지 누적된 값이고, dp[j-max]는 최대 칸수외에 누적된 값을 빼준 값 | ||
| // dp2[j-max][i-1] 은 한대를 추가적으로 이용하기 전에 현재 칸 이전에 가능한 최대 값 | ||
| // dp2[j-1][i] 는 현재 칸 이전의 값 | ||
|
|
||
| result = Math.max(result, dp2[j][3]); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| // for (int i = max; i < n + 1; i++) { | ||
| // dp2[i][0] = dp[i]; | ||
| // for (int j = i - max; j >= 0; j--) { | ||
| //// dp2[i][0] = Math.max(dp2[i][0], dp2[j][0]); | ||
| // if (dp2[j][0] != 0) { | ||
| // dp2[i][1] = Math.max(dp2[i][1], dp[i] + dp2[j][0]); | ||
| // } | ||
| // if (dp2[j][1] != 0) { | ||
| // dp2[i][2] = Math.max(dp2[i][2], dp[i] + dp2[j][1]); | ||
| // } | ||
| // } | ||
| // result = Math.max(result, dp2[i][2]); | ||
| // } | ||
|
|
||
| // System.out.println(Arrays.toString(dp)); | ||
| // Arrays.stream(dp2).forEach(a -> System.out.println(Arrays.toString(a))); | ||
| System.out.println(result); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package week6; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
| import java.util.Arrays; | ||
| import java.util.StringTokenizer; | ||
|
|
||
| public class JUN7453_Jisangil { | ||
| public static void main(String[] args) throws IOException { | ||
| BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
| int n = Integer.parseInt(br.readLine()); | ||
| int[][] arr = new int[4][n]; | ||
|
|
||
| for (int i = 0; i < n; i++) { | ||
| StringTokenizer st = new StringTokenizer(br.readLine()); | ||
| for (int j = 0; j < 4; j++) { | ||
| arr[j][i] = Integer.parseInt(st.nextToken()); | ||
| } | ||
| } | ||
| int[] ab = new int[n * n]; // ab를 합한 배열 | ||
| int[] cd = new int[n * n]; // cd를 합한 배열 | ||
| int idx = 0; | ||
| for (Integer tmp : arr[0]) { | ||
| for (Integer tmp1 : arr[1]) { | ||
| ab[idx++] = tmp + tmp1; // ab가능한 값 만들기 | ||
| } | ||
| } | ||
|
|
||
| idx = 0; | ||
| for (Integer tmp : arr[2]) { | ||
| for (Integer tmp1 : arr[3]) { | ||
| cd[idx++] = tmp + tmp1; | ||
| } | ||
| } | ||
|
|
||
| Arrays.sort(ab); // 정렬 | ||
| Arrays.sort(cd); | ||
| long count = 0; | ||
| int start = 0; //ab의 시작지점 | ||
| int last = cd.length - 1; // cd의 시작지점 | ||
| while (start < n * n && last >= 0) { | ||
| int a = ab[start]; // 가장 작은 | ||
| int b = cd[last]; // 가장 큰 | ||
| if (a + b > 0) { // 합이 0보다 크면 | ||
| last--; // 큰 값 낮추기 | ||
| } else if (a + b < 0) { | ||
| start++; // 작은 값 낮추기 | ||
| } else { // 같으면 | ||
| int aCount = 0; | ||
| for (int i = start; i < ab.length; i++) { | ||
| if (a == ab[i]) { | ||
| aCount++; | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
| int bCount = 0; | ||
| for (int i = last; i >= 0; i--) { | ||
| if (b == cd[i]) { | ||
| bCount++; | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
|
Comment on lines
+37
to
+65
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아... 이래서 정렬에 투포인터.. 배워갑니다 역시 갓상일,, |
||
| count += (long) aCount * bCount; // aCount는 ab 배열에서 같은 값의 개수 bCount도 마찬가지 가능한 개수만큼 더해줌 | ||
| start += aCount; //같은거 다음으로 증가 | ||
| last -= bCount; | ||
| } | ||
| } | ||
|
|
||
| System.out.println(count); // | ||
| } | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 같은 풀이입니다 |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package week6; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
| import java.util.Arrays; | ||
|
|
||
| public class JUN9252_Jisangil { | ||
| public static void main(String[] args) throws IOException { | ||
| BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
| String[] a = br.readLine().split(""); //첫번째 스트링 | ||
| String[] b = br.readLine().split(""); // 두번째 스트링 | ||
| int[] dp; // 누적할 디피 값 | ||
| String[] sdp; //누적할 문자열 | ||
| if (a.length < b.length) { // 만약 두번째 문자열이 길면 | ||
| dp = new int[b.length]; //긴 곳으로 dp 생성 | ||
| sdp = new String[b.length]; | ||
| for (int i = 0; i < a.length; i++) { //첫번째 문자열의 첫번째 문자부터 시작 | ||
| int tmp = 0; // 시작은 0으로 초기화 | ||
| String stmp = ""; // 문자열도 공백 | ||
| for (int j = 0; j < b.length; j++) { // 두번째 문자열을 전체 돔 | ||
| if (tmp < dp[j]) { //만약 dp값이 더크면 | ||
| tmp = dp[j]; // tmp를 올려줌 | ||
| stmp = sdp[j]; // 문자열도 추가 | ||
| if (a[i].equals(b[j])){ // 만약 dp값을 설정해주었는데 같은 문자열이면 맥스값으로만 설정하고 넘어감 | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| if (a[i].equals(b[j])) { // 만약 같은 문자열이면 | ||
| dp[j] = tmp + 1; // 현재 최고 값인 tmp를 현재 문자열 위치에 tmp+1 | ||
| sdp[j] = stmp + b[j]; //문자열도 현재 문자열 추가 | ||
| } | ||
|
Comment on lines
+22
to
+33
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 갓.... 발표 요청하겠습니다.. |
||
|
|
||
| } | ||
| } | ||
| } else { // 반대의 경우 똑같은 코드 | ||
| dp = new int[a.length]; | ||
| sdp = new String[a.length]; | ||
| for (int i = 0; i < b.length; i++) { | ||
| int tmp = 0; | ||
| String stmp = ""; | ||
| for (int j = 0; j < a.length; j++) { | ||
| if (tmp < dp[j]) { | ||
| tmp = dp[j]; | ||
| stmp = sdp[j]; | ||
| if (b[i].equals(a[j])){ | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| if (b[i].equals(a[j])) { | ||
| dp[j] = tmp + 1; | ||
| sdp[j] = stmp + a[j]; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| int idx = 0; | ||
| int max = 0; | ||
| for (int i = 0; i < dp.length; i++) { | ||
| if (max < dp[i]) { | ||
| idx = i; | ||
| max = dp[i]; | ||
| } | ||
| } | ||
|
|
||
| //max의 인덱스를 찾아 | ||
| System.out.println(dp[idx]); | ||
| if (dp[idx] != 0) { | ||
| System.out.println(sdp[idx]); | ||
| } | ||
| } | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이거 설명해주실 수 있나유 |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아 해쉬맵으로 푸는 문제였어요? 짱짱bbb