-
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
16 changed files
with
496 additions
and
24 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,27 @@ | ||
import sys | ||
|
||
def input(): | ||
return sys.stdin.readline().rstrip() | ||
|
||
def solution(k): | ||
location = 0 | ||
cnt = 0 | ||
|
||
while table: | ||
if cnt == k - 1: | ||
answer.append(table.pop(location)) | ||
location -= 1 # μμ ν κ²½μ°μλ μμΉλ₯Ό μμΌλ‘ μ΄λ | ||
cnt = 0 | ||
else: | ||
cnt += 1 | ||
if len(table) != 0: | ||
location = (location + 1) % len(table) | ||
|
||
return answer | ||
|
||
n, k = map(int, input().split()) | ||
table = [i + 1 for i in range(n)] | ||
answer = [] | ||
|
||
solution(k) | ||
print("<" + ", ".join(map(str, answer)) + ">") |
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 sys | ||
from collections import * | ||
|
||
def input(): return sys.stdin.readline().strip() | ||
N = int(input()) | ||
|
||
arr = list(map(int, input().split())) | ||
arr.reverse() | ||
answer = [-1 for _ in range(N)] | ||
|
||
id = 0 | ||
|
||
stack = [] | ||
stack.append((id,arr.pop())) | ||
|
||
while arr: | ||
id += 1 | ||
arr_data = arr.pop() | ||
while stack and stack[-1][1] < arr_data: | ||
index, data = stack.pop() | ||
answer[index] = arr_data | ||
stack.append((id, arr_data)) | ||
|
||
for i in answer: | ||
print(i, 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
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
Oops, something went wrong.