-
Notifications
You must be signed in to change notification settings - Fork 2
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
14 changed files
with
441 additions
and
23 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
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,26 @@ | ||
from collections import deque | ||
|
||
def solution(bridge_length, weight, truck_weights): | ||
time = 0 | ||
onBridge = deque() # ํ์ฌ ๋ค๋ฆฌ ์์ ์๋ ํธ๋ญ | ||
truckNum = 0 # ํ์ฌ ๋ค๋ฆฌ ์์ ์๋ ํธ๋ญ ์ | ||
sum = 0 # ํ์ฌ ๋ค๋ฆฌ ์์ ์๋ ํธ๋ญ ๋ฌด๊ฒ์ ํฉ | ||
truck_weights = deque(truck_weights) | ||
|
||
while truck_weights or onBridge: | ||
time += 1 | ||
|
||
# ํธ๋ญ์ด ๋ค๋ฆฌ๋ฅผ ์ง๋๊ฐ๋ ๊ฒฝ์ฐ ์ฒ๋ฆฌ | ||
if onBridge and time - onBridge[0][1] >= bridge_length: | ||
sum -= onBridge.popleft()[0] | ||
truckNum -= 1 | ||
|
||
# ๋ค์ ํธ๋ญ์ด ๋ค๋ฆฌ์ ์ฌ๋ผ๊ฐ ์ ์๋ ๊ฒฝ์ฐ ์ฒ๋ฆฌ | ||
# ํธ๋ญ์ด ์๊ณ ํฉ์ด weight์ ๋์ง ์์ผ๋ฉฐ, ์๊ฐ bridge_length๋ฅผ ๋๊ธฐ์ง ์๋ ๊ฒฝ์ฐ | ||
if len(truck_weights) != 0 and sum + truck_weights[0] <= weight and truckNum + 1 <= bridge_length: | ||
truck = truck_weights.popleft() # pop | ||
onBridge.append((truck, time)) # ๋ค๋ฆฌ ์์ truck์ tuple ์ถ๊ฐ | ||
sum += truck # ๋ฌด๊ฒ ์ถ๊ฐ | ||
truckNum += 1 # ๊ฑด๋๊ณ ์๋ ํธ๋ญ ์ถ๊ฐ | ||
|
||
return time |
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,19 @@ | ||
import sys | ||
import heapq | ||
|
||
line = int(sys.stdin.readline().strip()) | ||
|
||
heap = [] | ||
heapq.heapify(heap) | ||
|
||
for i in range(line): | ||
data = list(map(int, sys.stdin.readline().strip().split())) | ||
for s in data: | ||
if len(heap) < line: | ||
heapq.heappush(heap, s) | ||
else: | ||
if s > heap[0]: | ||
heapq.heappop(heap) | ||
heapq.heappush(heap, s) | ||
|
||
print(heap[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,32 @@ | ||
import sys | ||
import heapq | ||
|
||
n, m = map(int, sys.stdin.readline().rstrip().split()) | ||
|
||
graph = [[] for _ in range(n+1)] | ||
inDegree = [0 for _ in range(n+1)] | ||
q = [] | ||
answer = [] | ||
|
||
# ์ ๋ ฅ๋ฐ์์ ๋ฃ๊ธฐ | ||
for _ in range(m): | ||
p1, p2 = map(int, sys.stdin.readline().rstrip().split()) | ||
graph[p1].append(p2) # p1์ p2์ ์ฐ๊ฒฐ๋ ๋ฌธ์ | ||
inDegree[p2] += 1 # ๊ฐ์ ์ถ๊ฐ | ||
|
||
# ์ง์ ์ฐจ์๊ฐ 0์ด๋ฉด ํ์ ๋ฃ๊ธฐ | ||
for i in range(1, n+1): | ||
if inDegree[i] == 0: | ||
heapq.heappush(q, i) | ||
|
||
# answer์ ๋ฃ๊ณ , ๊ฐ์ ์ ๊ฑฐ | ||
while q: | ||
prob = heapq.heappop(q) | ||
answer.append(prob) | ||
for i in graph[prob]: # ๊ฐ์ ์ ๊ฑฐ & ์ง์ ์ฐจ์ 0์ธ ๊ฒ๋ค ์ฒ๋ฆฌ | ||
inDegree[i] -= 1 | ||
if inDegree[i] == 0: | ||
heapq.heappush(q, i) | ||
|
||
for result in answer: | ||
print(result, end=" ") |
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,80 @@ | ||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
struct Trie { | ||
struct Node { | ||
Node *children[10]{}; | ||
bool isTerminal = false; | ||
}; | ||
|
||
Node *root = new Node; | ||
|
||
bool insert(string &s) const { | ||
auto cur = root; | ||
|
||
for (char ch: s) { | ||
int digit = ch - '0'; | ||
|
||
if (!cur->children[digit]) { | ||
cur->children[digit] = new Node(); | ||
cur = cur->children[digit]; | ||
continue; | ||
} | ||
|
||
if (cur->children[digit]->isTerminal) { | ||
return false; | ||
} | ||
|
||
cur = cur->children[digit]; | ||
} | ||
|
||
for (auto child: cur->children) { | ||
if (child) { | ||
return false; | ||
} | ||
} | ||
|
||
cur->isTerminal = true; | ||
|
||
return true; | ||
} | ||
}; | ||
|
||
int main() { | ||
ios_base::sync_with_stdio(false); | ||
cin.tie(nullptr); | ||
|
||
auto solve = []() { | ||
auto trie = new Trie; | ||
|
||
int n; | ||
cin >> n; | ||
|
||
vector<string> numbers(n); | ||
|
||
for (auto &number: numbers) { | ||
cin >> number; | ||
} | ||
|
||
for (auto number: numbers) { | ||
if (!trie->insert(number)) { | ||
cout << "NO\n"; | ||
delete trie; | ||
return; | ||
} | ||
} | ||
|
||
cout << "YES\n"; | ||
}; | ||
|
||
int t; | ||
cin >> t; | ||
|
||
while (t--) { | ||
solve(); | ||
} | ||
|
||
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
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,53 @@ | ||
def solution(coin, cards): | ||
a = set(cards[:len(cards) // 3]) | ||
b = set() | ||
t = len(cards) + 1 | ||
r = 1 | ||
|
||
for i in range(len(cards) // 3 + 1, len(cards), 2): | ||
c1, c2 = cards[i - 1], cards[i] | ||
b.add(c1) | ||
b.add(c2) | ||
|
||
removed = False | ||
# ํ์ฌ ๊ฐ์ง๊ณ ์๋ ์นด๋ ๋ชฉ๋ก ์ค n + 1์ด ๊ฐ๋ฅํ ๊ฒฝ์ฐ ํ์ธ | ||
for x in list(a): | ||
if t - x in a: | ||
a.remove(t - x) | ||
a.remove(x) | ||
removed = True | ||
break | ||
|
||
if removed: | ||
r += 1 | ||
continue | ||
|
||
# ์ฝ์ธ์ผ๋ก ๊ตํํด์ ์ป์ ์ ์๋ ์นด๋ ์ค์์ n + 1์ด ๋๋ ๊ฒฝ์ฐ๋ฅผ ์ฐพ์์ผ ํจ | ||
# ์ฝ์ธ์ด ์์ผ๋ฏ๋ก ์ข ๋ฃ | ||
if not coin: | ||
break | ||
|
||
# `ํ์ฌ ๊ฐ๊ณ ์๋ ์นด๋ + ์ป์ ์ ์๋ ์นด๋` = n + 1์ด ๋๋ ๊ฒฝ์ฐ๋ฅผ ํ์ธ | ||
for x in list(b): | ||
if t - x in a: | ||
a.remove(t - x) | ||
b.remove(x) | ||
removed = True | ||
coin -= 1 | ||
break | ||
# ๋ง์ง๋ง ๋ฐฉ๋ฒ์ผ๋ก, ์ค์ง ๊ตํ์ผ๋ก ์ป์ ์ ์๋ ์นด๋ ๋ชฉ๋ก ์ค์์ n + 1์ด ๋๋ ๊ฒฝ์ฐ๋ฅผ ํ์ธ | ||
if not removed and coin >= 2: | ||
for x in list(b): | ||
if t - x in b: | ||
b.remove(t - x) | ||
b.remove(x) | ||
removed = True | ||
coin -= 2 | ||
break | ||
|
||
# n + 1์ ์ด๋ค ๊ฒฝ์ฐ์๋ ๋ชป ๋ง๋ค๋ฉด ์ข ๋ฃ | ||
if not removed: | ||
break | ||
r += 1 | ||
|
||
return r |
Oops, something went wrong.