-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from INIT-Algorithm/joonhee
#10 : Week2_준희이티
- Loading branch information
Showing
3 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#2023-04-15-Week2-과제 | ||
#11279_최대 힙 | ||
|
||
''' | ||
배열에 자연수 x를 넣는다. | ||
배열에서 가장 큰 값을 출력하고, 그 값을 배열에서 제거한다. | ||
프로그램은 처음에 비어있는 배열에서 시작하게 된다. | ||
첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. | ||
다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 | ||
주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, | ||
x가 0이라면 배열에서 가장 큰 값을 출력하고 | ||
그 값을 배열에서 제거하는 경우이다. | ||
입력되는 자연수는 231보다 작다. | ||
입력에서 0이 주어진 회수만큼 답을 출력한다. | ||
만약 배열이 비어 있는 경우인데 | ||
가장 큰 값을 출력하라고 한 경우에는 0을 출력하면 된다. | ||
''' | ||
|
||
|
||
''' | ||
import heapq | ||
heap = [] | ||
num = int(input()) | ||
for i in range(num) : | ||
a = int(input()) | ||
if a==0: | ||
if heap : | ||
print(heapq.heappop(heap)[1]) | ||
else : | ||
print(0) | ||
elif a>0 : | ||
heapq.heappush(heap, (-a,a)) | ||
''' | ||
|
||
#채점 결과 시간 초과 | ||
#input() -> sys.stdin.readline() | ||
|
||
import heapq | ||
import sys | ||
heap = [] | ||
num = int(sys.stdin.readline()) | ||
|
||
for i in range(num) : | ||
a = int(sys.stdin.readline()) | ||
if a==0: #a가 0이라면 | ||
if heap : | ||
print(heapq.heappop(heap)[1]) # 배열에서 가장 큰 값을 출력하고 그 값을 배열에서 제거하는 경우이다. | ||
else : # | ||
print(0) | ||
elif a>0 : #a가 자연수라면 | ||
heapq.heappush(heap, (-a,a)) #a라는 값을 넣는 연산 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#2023-04-27-Week3 | ||
#11729_하노이 탑.py | ||
|
||
|
||
''' | ||
하노이탑 원판 1개인 경우 -> top(1, 1, 2, 3) | ||
이동 과정 | ||
1. 1번 1->3 | ||
하노이탑 원판 2개인 경우 -> top(2, 1, 2, 3) | ||
이동 과정 | ||
1. 1번 1->2 | ||
2. 2번 1->3 | ||
3. 1번 2->3 | ||
하노이탑 원판 3개인 경우 -> top(3, 1, 2, 3) | ||
이동 과정 | ||
1. 1번 1->3 | ||
2. 2번 1->2 | ||
3. 1번 3->2 | ||
4. 3번 1->3 | ||
5. 1번 2->1 | ||
6. 2번 2->3 | ||
7. 1번 1->3 | ||
''' | ||
|
||
n = int(input()) | ||
|
||
def cnt(n) : | ||
if n==1 : | ||
k = 2 | ||
elif n<=0 : | ||
k = 1 | ||
else : | ||
k = 2*cnt(n-1) | ||
return k | ||
|
||
def top(n, a, b, c) : | ||
if n==1 : | ||
print(a,c) | ||
else : | ||
#else 코드는 스스로 작성하지 못함 | ||
top(n-1,a,c,b) | ||
print(a,c) | ||
top(n-1,b,a,c) | ||
|
||
''' | ||
함수 쓰지 않고 바로 계산해도 됨 | ||
sum = 2**n-1 | ||
print(sum) | ||
''' | ||
print(cnt(n)) | ||
top(n, 1, 2, 3) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#2023-04-15-Week2-과제 | ||
#1966_프린터 큐 | ||
|
||
''' | ||
여러분도 알다시피 여러분의 프린터 기기는 여러분이 인쇄하고자 하는 문서를 | ||
인쇄 명령을 받은 ‘순서대로’, 즉 먼저 요청된 것을 먼저 인쇄한다. | ||
여러 개의 문서가 쌓인다면 Queue 자료구조에 쌓여서 | ||
FIFO - First In First Out - 에 따라 인쇄가 되게 된다. | ||
하지만 상근이는 새로운 프린터기 내부 소프트웨어를 개발하였는데, | ||
이 프린터기는 다음과 같은 조건에 따라 인쇄를 하게 된다. | ||
현재 Queue의 가장 앞에 있는 문서의 ‘중요도’를 확인한다. | ||
나머지 문서들 중 현재 문서보다 중요도가 높은 문서가 하나라도 있다면, | ||
이 문서를 인쇄하지 않고 Queue의 가장 뒤에 재배치 한다. | ||
그렇지 않다면 바로 인쇄를 한다. | ||
예를 들어 Queue에 4개의 문서(A B C D)가 있고, 중요도가 2 1 4 3 라면 C를 인쇄하고, | ||
다음으로 D를 인쇄하고 A, B를 인쇄하게 된다. | ||
여러분이 할 일은, 현재 Queue에 있는 문서의 수와 중요도가 주어졌을 때, | ||
어떤 한 문서가 몇 번째로 인쇄되는지 알아내는 것이다. | ||
예를 들어 위의 예에서 C문서는 1번째로, A문서는 3번째로 인쇄되게 된다. | ||
입력 | ||
첫 줄에 테스트케이스의 수가 주어진다. 각 테스트케이스는 두 줄로 이루어져 있다. | ||
테스트케이스의 첫 번째 줄에는 문서의 개수 N(1 ≤ N ≤ 100)과, | ||
몇 번째로 인쇄되었는지 궁금한 문서가 현재 Queue에서 | ||
몇 번째에 놓여 있는지를 나타내는 정수 M(0 ≤ M < N)이 주어진다. | ||
이때 맨 왼쪽은 0번째라고 하자. 두 번째 줄에는 N개 문서의 중요도가 차례대로 주어진다. | ||
중요도는 1 이상 9 이하의 정수이고, 중요도가 같은 문서가 여러 개 있을 수도 있다. | ||
''' | ||
|
||
#큐 = FIFO | ||
|
||
t = int(input()) | ||
|
||
for i in range(t): | ||
|
||
n, m = map(int, input().split()) | ||
queue = list(map(int, input().split())) | ||
cnt = 0 | ||
|
||
while m != -1: | ||
if queue[0] == max(queue): | ||
del queue[0] | ||
m -= 1 | ||
cnt += 1 | ||
else: | ||
queue.append(queue[0]) | ||
del queue[0] | ||
|
||
if m==0 : | ||
m = len(queue) -1 | ||
else : | ||
m -= 1 | ||
|
||
#print(cnt) -> 오답 원인 : print문 위치 | ||
print(cnt) |