-
Notifications
You must be signed in to change notification settings - Fork 4
[7주차] 이지영 #91
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
[7주차] 이지영 #91
Changes from all commits
2b313b7
c996681
34dde58
014ab17
c3e2b31
6231cbd
9a52748
ea1b258
980a13d
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 day1025; | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class JY_1535 { | ||
|
||
static int MAX_HP = 100; | ||
|
||
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[] h = new int[N+1]; | ||
st = new StringTokenizer(br.readLine()); | ||
for(int i=1; i<N+1; i++) { | ||
h[i] = Integer.parseInt(st.nextToken()); | ||
} | ||
int[] v = new int[N+1]; | ||
st = new StringTokenizer(br.readLine()); | ||
for(int i=1; i<N+1; i++) { | ||
v[i] = Integer.parseInt(st.nextToken()); | ||
} | ||
|
||
int[] dp = new int[MAX_HP+1]; | ||
for(int i=1; i<N+1; i++) { | ||
for(int j=MAX_HP; j>h[i]; j--) { | ||
dp[j] = Math.max(dp[j], dp[j-h[i]]+v[i]); | ||
} | ||
} | ||
|
||
// System.out.println(Arrays.toString(dp)); | ||
|
||
// 최대 기쁨 찾기 | ||
int maxHappy = Integer.MIN_VALUE; | ||
for(int j=1; j<101; j++) { | ||
maxHappy = Math.max(maxHappy, dp[j]); | ||
} | ||
|
||
System.out.println(maxHappy); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package day1025; | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class JY_2037 { | ||
|
||
public static void main(String[] args) throws IOException{ | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
|
||
int D = Integer.parseInt(st.nextToken()); | ||
int P = Integer.parseInt(st.nextToken()); | ||
|
||
long[] L = new long[P+1]; | ||
long[] C = new long[P+1]; | ||
long[] dp = new long[D+1]; | ||
for(int i=1; i<P+1; i++) { | ||
st = new StringTokenizer(br.readLine()); | ||
L[i] = Long.parseLong(st.nextToken()); | ||
C[i] = Long.parseLong(st.nextToken()); | ||
|
||
} | ||
|
||
// 최솟값을 찾기 위해 | ||
// j==L[i] 즉, i번쨰 하나만 선택했을 떄는 C[i]로 초기화하기 위해 MAX값으로 초기화 | ||
dp[0] = Integer.MAX_VALUE; | ||
|
||
for(int i=1; i<P+1; i++) { | ||
for(int j=D; j>=L[i]; j--) { | ||
dp[j] = Math.max(dp[j], Math.min(dp[(int)(j-L[i])], C[i])); | ||
} | ||
} | ||
// System.out.println(Arrays.toString(dp)); | ||
|
||
System.out.println(dp[D]); | ||
|
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package day1023; | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class JY_21939 { | ||
|
||
static int MAXNUM = 100001; | ||
// 문제 번호에 해당하는 난이도 저장 배열 | ||
static int[] srr; | ||
|
||
// 가장 어려운 문제 출력 우선순위 큐 | ||
static PriorityQueue<int[]> hpq = new PriorityQueue<>(new Comparator<int[]>() { | ||
@Override | ||
public int compare(int[] o1, int[] o2) { | ||
if(o1[0]==o2[0]) return o2[1] - o1[1]; | ||
return o2[0]-o1[0]; | ||
} | ||
}); | ||
// 가장 쉬운 문제 출력 우선순위 큐 | ||
static PriorityQueue<int[]> epq = new PriorityQueue<>(new Comparator<int[]>() { | ||
@Override | ||
public int compare(int[] o1, int[] o2) { | ||
if(o1[0]==o2[0]) return o1[1] - o2[1]; | ||
return o1[0] - o2[0]; | ||
} | ||
}); | ||
|
||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
|
||
srr = new int[MAXNUM]; | ||
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. 오호 번호값에 따른 난이도를 배열로 관리하면 빠르고 깔끔하군요!!! |
||
int N = Integer.parseInt(st.nextToken()); | ||
for(int i=0; i<N; i++) { | ||
st = new StringTokenizer(br.readLine()); | ||
int num = Integer.parseInt(st.nextToken()); | ||
int level = Integer.parseInt(st.nextToken()); | ||
int[] exam = {level, num}; | ||
hpq.add(exam); | ||
epq.add(exam); | ||
srr[num] = level; | ||
} | ||
|
||
int M = Integer.parseInt(br.readLine()); | ||
for(int i=0; i<M; i++) { | ||
st = new StringTokenizer(br.readLine()); | ||
String type = st.nextToken(); | ||
|
||
if(type.equals("add")) { | ||
int num = Integer.parseInt(st.nextToken()); | ||
int level = Integer.parseInt(st.nextToken()); | ||
srr[num] = level; | ||
int[] exam = {level, num}; | ||
hpq.add(exam); | ||
epq.add(exam); | ||
} | ||
|
||
else if(type.equals("recommend")) { | ||
int x = Integer.parseInt(st.nextToken()); | ||
|
||
if(x == 1) { | ||
// 해당 문제번호의 난이도와 우선순위큐에 있는 난이도가 일치하는 문제찾기 | ||
while(!hpq.isEmpty() && srr[hpq.peek()[1]] != hpq.peek()[0]) { | ||
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. 동일한 번호처리는, 난이도 배열값이랑 같은걸 찾으면 되군요! 저랑 우선순위큐 2개 이용했다는 개념은 동일한데 배열을 통해서 더 깔끔하게 푸신것 같아요!!!👍 깔끔하게 생각하기 배워갑니다😎 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. 우선순위 큐는 수빈님 코드가 더 깔끔한것 같습니다! 합쳐봐요~ ᄏᄏ 😄 |
||
// sloved된 문제라면 pass | ||
hpq.poll(); | ||
} | ||
System.out.println(hpq.peek()[1]); | ||
} else { | ||
while(!epq.isEmpty() && srr[epq.peek()[1]] != epq.peek()[0]) { | ||
// sloved된 문제라면 pass | ||
epq.poll(); | ||
} | ||
System.out.println(epq.peek()[1]); | ||
} | ||
|
||
} else { | ||
int num = Integer.parseInt(st.nextToken()); | ||
srr[num] = -1; // 난이도 -1로 solved 처리 | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package day1022; | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class JY_5021 { | ||
|
||
static int N, M; | ||
static String king; | ||
static Map<String, List<String>> fMap; | ||
static Map<String, Integer> indegree; | ||
static Map<String, Double> score; | ||
static List<String> cList; | ||
static List<String> ans; | ||
|
||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
|
||
N = Integer.parseInt(st.nextToken()); | ||
M = Integer.parseInt(st.nextToken()); | ||
|
||
|
||
fMap = new HashMap<>(); | ||
indegree = new HashMap<>(); | ||
st = new StringTokenizer(br.readLine()); | ||
king = st.nextToken(); | ||
fMap.put(king, new ArrayList<>()); | ||
indegree.put(king, 0); | ||
|
||
for(int i=0; i<N; i++) { | ||
st = new StringTokenizer(br.readLine()); | ||
String child = st.nextToken(); | ||
String p1 = st.nextToken(); | ||
String p2 = st.nextToken(); | ||
|
||
if(fMap.get(p1) == null) { | ||
fMap.put(p1, new ArrayList<>()); | ||
} | ||
if(fMap.get(p2) == null) { | ||
fMap.put(p2, new ArrayList<>()); | ||
} | ||
if(fMap.get(child) == null) { | ||
fMap.put(child, new ArrayList<>()); | ||
} | ||
// 자식 추가 | ||
fMap.get(p1).add(child); | ||
fMap.get(p2).add(child); | ||
|
||
// 진입차수 설정 | ||
indegree.put(p1, indegree.getOrDefault(p1, 0)); | ||
indegree.put(p2, indegree.getOrDefault(p2, 0)); | ||
indegree.put(child, indegree.getOrDefault(child, 0)+2); | ||
|
||
} | ||
|
||
cList = new ArrayList<>(); | ||
for(int i=0; i<M; i++) { | ||
st = new StringTokenizer(br.readLine()); | ||
cList.add(st.nextToken()); | ||
} | ||
|
||
// System.out.println(fMap); | ||
// System.out.println(indegree); | ||
// System.out.println(cList); | ||
|
||
ans = new ArrayList<>(); | ||
score = new HashMap<>(); | ||
topologySort(); | ||
|
||
// System.out.println(ans); | ||
// System.out.println(score); | ||
|
||
// 후계자 찾기 | ||
String candidate = ""; | ||
double s = 0.0; | ||
for(int i=0; i<ans.size(); i++) { | ||
String k = ans.get(i); | ||
for(String c: cList) { | ||
if(c.equals(k) && s < score.get(k)) { | ||
candidate = c; | ||
s = score.get(k); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
System.out.println(candidate); | ||
|
||
} | ||
public static void topologySort() { | ||
Queue<String> q = new LinkedList<>(); | ||
|
||
// 진입차수가 0인 노드 삽입 | ||
q.add(king); | ||
for(String key: indegree.keySet()) { | ||
if(key.equals(king)) { | ||
score.put(key, 1.0); | ||
continue; | ||
} | ||
if(indegree.get(key) == 0) { | ||
q.add(key); | ||
score.put(key, 0.5); | ||
} | ||
} | ||
|
||
while(!q.isEmpty()) { | ||
String now = q.poll(); | ||
ans.add(now); | ||
for(String c: fMap.get(now)) { | ||
indegree.put(c, indegree.get(c)-1); | ||
score.put(c, score.getOrDefault(c, 0.0)+ score.get(now)*0.5); | ||
if(indegree.get(c) == 0) { | ||
q.add(c); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
} |