-
Notifications
You must be signed in to change notification settings - Fork 0
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
[이분 탐색] 4월 14일 #11
base: main
Are you sure you want to change the base?
The head ref may contain hidden characters: "04\uC6D4-08\uC77C---\uC774\uBD84-\uD0D0\uC0C9"
[이분 탐색] 4월 14일 #11
Conversation
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.
p1.
선택 2문제 제출 확인되셨습니다! 구현문제가 쉽지 않았을텐데 훌륭하게 해결해주셨네요..👍19637번 문제 수정하시고 저 리뷰어로 호출해주세요! 수고하셨습니다~
alp[ch - 'A'] = true; //알파벳 사용 여부 체크 | ||
} else if (wheel[arrow] != ch) { //모순 있으면 바퀴 존재x | ||
return false; | ||
} |
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.
p3.
지금 return false 부분이 중복되고 있는데요! wheel[arrow] == ch
인경우를 continue로 먼저 제외 시키고나면 바퀴를 만들지 못하는 경우의 조건을 ||로 이어서 같이 처리해 볼수도 있어요!
04월 08일 - 이분 탐색/19637.cpp
Outdated
cin >> character[i]; | ||
} | ||
|
||
sort(character.begin(), character.end()); //오름차순 정렬 |
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.
p1.
맵과 셋은 원소를 정렬된 상태로 저장하기 때문에 알고리즘 헤더 없이 자체적으로 이분 탐색 함수를 사용할 수 있어요! map에 key값으로 숫자를 넣고 value값으로 문자열을 넣으면 key값 기준으로 오름차순으로 자동 정렬이 가능하죠!
for (int j = 0; j < cnt; j++) { | ||
cout << title[i] << '\n'; | ||
} | ||
} |
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.
p1.
지금 칭호의 개수 N (1 ≤ N ≤ 105)과 칭호를 출력해야 하는 캐릭터들의 개수 M (1 ≤ M ≤ 105)이므로 m개를 다 탐색하는 O(mn)으로 푼다면 시간초과가 발생하므로 log 의 시간이 걸리는 이분 탐색을 활용해주셔야 한다는 점까지는 잘 캐치해주셨어요! 문제에 적합한 upper_bound라는 라이브러리도 같이 찾아주셨네요.
이걸 어떤식으로 활용해볼 수 있을까요?
저희가 궁금한건 여러가지 칭호들의 상한 조건이 담겨있는 컨테이너에서 입력받은 캐릭터의 전투력보다 같거나 큰 숫자가 배열 몇 번째에서 처음 등장하는지에요. 반면 upper_bound
는 찾으려는 key 값을 초과하는 숫자가 배열 몇 번째에서 처음 등장하는지를 찾아주는 함수이죠! 물론 upper_bound를 활용해서 초과하는 순서에 있는 원소를 찾고 칭호를 한단계 down 시키는 방법도 있겠지만 본 문제에서는 lower_bound를 활용하는 편이 깔끔해보여요!
캐릭터들의 전투력을 key값으로 다음과 같이 map을 만든다면
map <int,string> // 전투력, 칭호이름
[{10,very weak} ,{20,weak},{30,mid},{50,strong},{70,very strong}...]
이제 캐릭터의 전투력을 하나씩 입력받으며, 해당 값보다 같거나 큰 숫자가 몇 번째에서 처음 등장하는지 찾고, 그때의 칭호를 출력해주면 되겠죠! lower_bound는 이분탐색 O(logN) 을 활용하는 알고리즘이니 시간초과도 나지 않을거에요.
lower_bound 와 upper_bound에 대한 활용법 참고하시면 좋을 것 같아 관련 링크 첨부해두겠습니다~!
https://chanhuiseok.github.io/posts/algo-55/
//타순 결정한 상태에서 게임 진행 후 결과값과 기존 ans 중 최댓값 업데이트 | ||
ans = max(ans, playGame(n, lineup)); | ||
} while (next_permutation(lineup.begin(), lineup.end())); | ||
|
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.
저희 샘플코드는 이부분을 재귀 백트래킹으로 수행하는 풀이도 준비했으니 참고해보셔도 좋을 것 같아요! 👍
쿠폰사용하셔서 총 필수 2문제 선택 3문제 제출 확인되셨습니다! 메인에 머지하셔도 좋습니다 :) 수고하셨습니다 ~! |
내용 & 질문
<기존 제출>
<추가 제출>