-
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
1 parent
a0dbcd6
commit 5af78f5
Showing
6 changed files
with
111 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,13 @@ | ||
import sys | ||
def input(): return sys.stdin.readline().strip() | ||
|
||
string = input() | ||
stack, answer, pre = 0, 0, '_' | ||
for ch in string: | ||
if ch == '(': | ||
stack = stack + 1 | ||
else: | ||
stack = stack - 1 | ||
answer = answer + (stack if pre == '(' else 1) | ||
pre = ch | ||
print(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,13 @@ | ||
from itertools import permutations | ||
import sys | ||
def input(): return sys.stdin.readline().strip() | ||
|
||
N = int(input()) | ||
data = list(map(int, input().split())) | ||
answer = 0 | ||
for arr in permutations(data): | ||
temp = 0 | ||
for i in range(N - 1): | ||
temp = temp + abs(arr[i] - arr[i + 1]) | ||
answer = max(answer, temp) | ||
print(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,30 @@ | ||
from heapq import heappop, heappush | ||
import sys | ||
def input(): return sys.stdin.readline().strip() | ||
|
||
V, E = map(int, input().split()) | ||
|
||
edges = [[] for _ in range(V)] | ||
for _ in range(E): | ||
a, b, c = map(int, input().split()) | ||
edges[a - 1].append((b - 1, c)) | ||
edges[b - 1].append((a - 1, c)) | ||
|
||
pq = [(0, 0)] | ||
solved = [False] * (V + 1) | ||
answer = 0 | ||
for _ in range(V): | ||
while True: | ||
weight, here = heappop(pq) | ||
if not solved[here]: | ||
break | ||
|
||
solved[here] = True | ||
answer = answer + weight | ||
|
||
for there, w in edges[here]: | ||
if solved[there]: | ||
continue | ||
|
||
heappush(pq, (w, there)) | ||
print(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,8 @@ | ||
import sys | ||
def input(): return sys.stdin.readline().strip() | ||
|
||
N = int(input()) | ||
data = list(sorted([int(input()) for _ in range(N)], reverse=True)) | ||
for i in range(N): | ||
data[i] = data[i] * (i + 1) | ||
print(max(data)) |
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,15 @@ | ||
from collections import defaultdict | ||
from graphlib import TopologicalSorter | ||
import sys | ||
def input(): return sys.stdin.readline().strip() | ||
|
||
N, M = map(int, input().split()) | ||
|
||
graph = defaultdict(set) | ||
for i in range(1, N + 1): | ||
graph[i] | ||
for _ in range(M): | ||
a, b = map(int, input().split()) | ||
graph[b].add(a) | ||
|
||
print(*TopologicalSorter(graph).static_order()) |
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 @@ | ||
from collections import deque | ||
import sys | ||
def input(): return sys.stdin.readline().strip() | ||
|
||
N = int(input()) | ||
a, b = map(int, input().split()) | ||
M = int(input()) | ||
|
||
edges = [[] for _ in range(N + 1)] | ||
for _ in range(M): | ||
x, y = map(int, input().split()) | ||
edges[x].append(y) | ||
edges[y].append(x) | ||
|
||
queue = deque([(a, 0)]) | ||
visited = [False] * (N + 1) | ||
visited[a] = True | ||
while(queue): | ||
here, cost = queue.popleft() | ||
|
||
if here == b: | ||
print(cost) | ||
break | ||
|
||
for there in edges[here]: | ||
if visited[there]: | ||
continue | ||
|
||
visited[there] = True | ||
queue.append((there, cost + 1)) | ||
else: | ||
print(-1) |