-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
437 additions
and
115 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
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
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,20 @@ | ||
n = int(input()) | ||
nums = list(map(int, input().split())) | ||
|
||
# dp ν μ΄λΈ μ΄κΈ°ν | ||
dp = [[0] * 21 for _ in range(n)] # 0 μ΄μ 20 μ΄νμΈ μ«μμ΄λ―λ‘ ν¬κΈ°κ° 21μΈ λ°°μ΄ | ||
|
||
# 첫 λ²μ§Έ μ«μλ₯Ό dp ν μ΄λΈμ μ΄κΈ°κ°μΌλ‘ μ€μ | ||
dp[0][nums[0]] = 1 | ||
|
||
# dp ν μ΄λΈ μ±μ°κΈ° | ||
for i in range(1, n - 1): | ||
for j in range(21): | ||
if dp[i - 1][j]: | ||
if j + nums[i] <= 20: # λνκΈ° | ||
dp[i][j + nums[i]] += dp[i - 1][j] | ||
if j - nums[i] >= 0: # λΉΌκΈ° | ||
dp[i][j - nums[i]] += dp[i - 1][j] | ||
|
||
# λ§μ§λ§ μ«μλ₯Ό λͺ©ν κ°μΌλ‘ λ§λ€ μ μλ κ²½μ°μ μ μΆλ ₯ | ||
print(dp[n-2][nums[-1]]) |
Binary file not shown.
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,113 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#define MAX_STACK_SIZE 100 | ||
#define MAX_TEXT_SIZE 100 // μ€μ νκΈ° μμμ μ΅λ κΈΈμ΄ | ||
|
||
typedef char element; | ||
|
||
typedef struct { | ||
element data[MAX_STACK_SIZE]; | ||
int top; | ||
} StackType; | ||
|
||
// μ€ν μ΄κΈ°ν ν¨μ | ||
void init_stack(StackType *s) { | ||
s->top = -1; | ||
} | ||
|
||
// 곡백 μν κ²μΆ ν¨μ | ||
int is_empty(StackType *s) { | ||
return (s->top == -1); | ||
} | ||
|
||
// ν¬ν μν κ²μΆ ν¨μ | ||
int is_full(StackType *s) { | ||
return (s->top == (MAX_STACK_SIZE - 1)); | ||
} | ||
|
||
// μ½μ ν¨μ | ||
void push(StackType *s, element item) { | ||
if (is_full(s)) { | ||
fprintf(stderr, "μ€ν ν¬ν μλ¬\n"); | ||
return; | ||
} else { | ||
s->data[++(s->top)] = item; | ||
} | ||
} | ||
|
||
// μμ ν¨μ | ||
element pop(StackType *s) { | ||
if (is_empty(s)) { | ||
fprintf(stderr, "μ€ν 곡백 μλ¬\n"); | ||
exit(1); | ||
} else { | ||
return s->data[(s->top)--]; | ||
} | ||
} | ||
|
||
// μ€νμ topμ μλ μμλ₯Ό λ°ννλ ν¨μ (μμ νμ§ μμ) | ||
element peek(StackType *s) { | ||
if (is_empty(s)) { | ||
fprintf(stderr, "μ€ν 곡백 μλ¬\n"); | ||
exit(1); | ||
} else { | ||
return s->data[s->top]; | ||
} | ||
} | ||
|
||
// μ°μ°μμ μ°μ μμλ₯Ό λ°ν | ||
int prec(char op) { | ||
switch (op) { | ||
case '(': case ')': return 0; | ||
case '+': case '-': return 1; | ||
case '*': case '/': return 2; | ||
} | ||
return -1; | ||
} | ||
|
||
// μ€μ νκΈ° μμ -> νμ νκΈ° μμ | ||
void infix_to_postfix(char exp[]) { | ||
int i = 0; | ||
char ch, top_op; | ||
int len = strlen(exp); | ||
StackType s; | ||
|
||
init_stack(&s); // μ€ν μ΄κΈ°ν | ||
for (i = 0; i < len; i++) { | ||
ch = exp[i]; | ||
switch (ch) { | ||
case '+': case '-': case '*': case '/': // μ°μ°μ | ||
// μ€νμ μλ μ°μ°μμ μ°μ μμκ° λ ν¬κ±°λ κ°μΌλ©΄ μΆλ ₯ | ||
while (!is_empty(&s) && (prec(ch) <= prec(peek(&s)))) | ||
printf("%c", pop(&s)); | ||
push(&s, ch); | ||
break; | ||
case '(':// μΌμͺ½ κ΄νΈ | ||
push(&s, ch); | ||
break; | ||
case ')':// μ€λ₯Έμͺ½ κ΄νΈ | ||
top_op = pop(&s); | ||
// μΌμͺ½ κ΄νΈλ₯Ό λ§λ λκΉμ§ μΆλ ₯ | ||
while (top_op != '(') { | ||
printf("%c", top_op); | ||
top_op = pop(&s); | ||
} | ||
break; | ||
default: // νΌμ°μ°μ | ||
printf("%c", ch); | ||
break; | ||
} | ||
} | ||
while (!is_empty(&s)) // μ€νμ μ μ₯λ μ°μ°μλ€ μΆλ ₯ | ||
printf("%c", pop(&s)); | ||
} | ||
|
||
int main(void) { | ||
char s[MAX_TEXT_SIZE]; | ||
scanf("%s", s); | ||
infix_to_postfix(s); | ||
printf("\n"); | ||
return 0; | ||
} |
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,65 @@ | ||
MAX_STACK_SIZE = 100 | ||
MAX_EXPR_SIZE = 100 | ||
|
||
class Stack: | ||
def __init__(self): | ||
self.items = [] | ||
|
||
def is_empty(self): | ||
return not self.items | ||
|
||
def push(self, item): | ||
if len(self.items) >= MAX_STACK_SIZE: | ||
raise OverflowError("μ€ν ν¬ν μλ¬: μ΅λ ν¬κΈ° μ΄κ³Ό") | ||
self.items.append(item) | ||
|
||
def pop(self): | ||
if self.is_empty(): | ||
raise IndexError("μ€ν 곡백 μλ¬") | ||
return self.items.pop() | ||
|
||
def peek(self): | ||
if self.is_empty(): | ||
raise IndexError("μ€ν 곡백 μλ¬") | ||
return self.items[-1] | ||
|
||
def prec(op): | ||
if op in {'+', '-'}: | ||
return 1 | ||
elif op in {'*', '/'}: | ||
return 2 | ||
else: | ||
return 0 | ||
|
||
def infix_to_postfix(exp): | ||
if len(exp) > MAX_EXPR_SIZE: | ||
raise ValueError("μμ κΈΈμ΄ μ΄κ³Ό μλ¬: μ΅λ κΈΈμ΄ μ΄κ³Ό") | ||
|
||
s = Stack() | ||
postfix = [] | ||
for ch in exp: | ||
if ch.isalnum(): # νΌμ°μ°μ (μ«μ, λ¬Έμ λ±) | ||
postfix.append(ch) | ||
elif ch == '(': | ||
s.push(ch) | ||
elif ch == ')': | ||
while not s.is_empty() and s.peek() != '(': | ||
postfix.append(s.pop()) | ||
s.pop() # '(' λ²λ¦Ό | ||
else: # μ°μ°μ | ||
while not s.is_empty() and prec(ch) <= prec(s.peek()): | ||
postfix.append(s.pop()) | ||
s.push(ch) | ||
|
||
while not s.is_empty(): # μ€νμ λ¨μ μλ λͺ¨λ μ°μ°μλ₯Ό μΆλ ₯ | ||
postfix.append(s.pop()) | ||
|
||
return ''.join(postfix) | ||
|
||
def main(): | ||
expr = input() | ||
postfix_expr = infix_to_postfix(expr) | ||
print(postfix_expr) | ||
|
||
if __name__ == "__main__": | ||
main() |
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,25 @@ | ||
import heapq | ||
import sys | ||
|
||
n = int(sys.stdin.readline()) | ||
|
||
heap = [] # λͺ¨λ κ°μμ μμ μκ°κ³Ό μ’ λ£ μκ°μ μ μ₯ν μ΅μ ν | ||
q = [] # νμ¬ μ¬μ© μ€μΈ κ°μμ€μ μ’ λ£ μκ°μ μ μ₯ν μ΅μ ν | ||
|
||
# μ£Όμ΄μ§ κ°μ μλ§νΌ λ°λ³΅νλ©΄μ κ°μ μ 보λ₯Ό μ λ ₯λ°μ μ΅μ νμ μ μ₯ | ||
for _ in range(n): | ||
num, start, end = map(int, sys.stdin.readline().split()) | ||
heapq.heappush(heap, [start, end, num]) | ||
|
||
# 첫 λ²μ§Έ κ°μλ₯Ό μ΅μ νμμ κΊΌλ΄μ ν΄λΉ κ°μμ μ’ λ£ μκ°μ λ€λ₯Έ νμ μ μ₯ | ||
start, end, num = heapq.heappop(heap) | ||
heapq.heappush(q, end) | ||
|
||
while heap: | ||
start, end, num = heapq.heappop(heap) | ||
# κ°μ₯ 빨리 λλλ κ°μμ€μ μ’ λ£ μκ°μ΄ νμ¬ κ°μμ μμ μκ°λ³΄λ€ μκ±°λ κ°μΌλ©΄ μ’ λ£ μκ°μ μ΅μ νμμ μ κ±° | ||
if q[0] <= start: | ||
heapq.heappop(q) | ||
heapq.heappush(q, end) # νμ¬ κ°μμ μ’ λ£ μκ°μ μ΅μ νμ μΆκ° | ||
|
||
print(len(q)) # μ΅μ ν qμ ν¬κΈ°κ° νμν κ°μμ€μ μ |
Binary file not shown.
Oops, something went wrong.