Skip to content

Commit 02bb5e7

Browse files
committed
2021.03.19
1 parent 7b98e7e commit 02bb5e7

File tree

10 files changed

+281
-25
lines changed

10 files changed

+281
-25
lines changed

python/gold2/1655.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
import sys
2-
import bisect
2+
import heapq
33
input = sys.stdin.readline
4+
output = sys.stdout.write
45

56
N = int(input())
6-
arr = []
7+
left,right = [],[]
8+
9+
ans = []
710
for i in range(N):
811
n = int(input())
9-
idx = bisect.insort(arr,n)
10-
print(arr[int(i/2)])
12+
if len(left)==len(right):
13+
heapq.heappush(left,-n)
14+
else:
15+
heapq.heappush(right,n)
16+
17+
if right and -left[0]>right[0]:
18+
l_v = -heapq.heappop(left)
19+
r_v = heapq.heappop(right)
20+
heapq.heappush(left,-r_v)
21+
heapq.heappush(right,l_v)
22+
ans.append(-left[0])
23+
24+
output("%s"%'\n'.join(map(str,ans)))

python/gold2/7453.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
11
import sys
2-
import bisect
32
input = sys.stdin.readline
43

54
N = int(input())
6-
a,b,c,d = [0 for _ in range(N)],[0 for _ in range(N)],[0 for _ in range(N)],[0 for _ in range(N)]
5+
a,b,c,d = [0]*N,[0]*N,[0]*N,[0]*N
76
for i in range(N):
87
a[i],b[i],c[i],d[i] = map(int, input().split())
9-
result = 0
8+
109
ab = []
11-
cd = []
12-
for i in a:
13-
for j in b:
14-
ab.append(i+j)
15-
for i in c:
16-
for j in d:
17-
cd.append(i+j)
18-
ab.sort()
19-
cd.sort()
20-
p = ab[0]
21-
r = N*N
22-
for ab_ in ab:
23-
l,r = bisect.bisect_left(cd,-ab_), bisect.bisect_right(cd,-ab_)
24-
result+=r-l
25-
print(result)
10+
cd = {}
11+
for i in range(N):
12+
for j in range(N):
13+
ab.append(a[i]+b[j])
14+
_cd = c[i]+d[i]
15+
cd[_cd] = cd.get(_cd,0)+1
16+
ans = 0
17+
for num in ab:
18+
ans+=cd.get(-num,0)
19+
print(ans)

python/gold3/11054.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
N = int(input())
5+
arr = list(map(int,input().split(' ')))
6+
7+
def getDownArrToRight(idx):
8+
p = [arr[idx]]
9+
for num in arr[idx+1:]:
10+
if num < p[-1]:
11+
p.append(num)
12+
else:
13+
l = len(p)-1
14+
while l>-1 and num>=p[l]:
15+
l-=1
16+
p[l+1]=num
17+
return len(p)
18+
19+
def getDownArrToLeft(idx):
20+
p = [arr[idx]]
21+
for num in reversed(arr[:idx]):
22+
if num < p[-1]:
23+
p.append(num)
24+
else:
25+
l = len(p)-1
26+
while l>-1 and num>=p[l]:
27+
l-=1
28+
p[l+1]=num
29+
return len(p)
30+
def sol(num): return getDownArrToLeft(num)+getDownArrToRight(num)-1
31+
ans = 0
32+
for i in range(N):
33+
if sol(i)>ans:
34+
ans=sol(i)
35+
print(ans)

python/gold3/11066.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
T = int(input())
5+
6+
7+
def sol(st,ed):
8+
if dp_arr[st][ed]!=-1:return dp_arr[st][ed]
9+
if st==ed:
10+
return arr[st]
11+
ret = 1e9
12+
sum = preSum[ed+1]-preSum[st]
13+
for i in range(st,ed):
14+
ret = min(sol(st,i)+sol(i+1,ed)+sum,ret)
15+
dp_arr[st][ed] = ret
16+
return ret
17+
18+
19+
while T:
20+
T-=1
21+
K = int(input())
22+
arr = list(map(int,input().split()))
23+
preSum = [0]*(K+1)
24+
for i in range(1,K+1):
25+
preSum[i] = preSum[i-1]+arr[i-1]
26+
dp_arr = [[-1]*501 for _ in range(501)]
27+
ans = 1e9
28+
for i in range(K):
29+
ans = min(sol(0,i)+sol(i+1,K-1),ans)
30+
print(ans)

python/gold3/14890.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
N, L = map(int,input().split(' '))
5+
6+
def checkPossible(line):
7+
st = line[0]
8+
i = 1
9+
flag = 1
10+
while i<N:
11+
num = line[i]
12+
i+=1
13+
if num==st:
14+
flag+=1
15+
continue
16+
if abs(num-st)>1: return False
17+
if num>st:
18+
if flag<L: return False
19+
flag = 1
20+
st = num
21+
continue
22+
temp = L-1
23+
#L-1개가 같은 높이여야 한다
24+
while temp:
25+
if i>=N: return False
26+
if line[i]!=num: return False
27+
temp-=1
28+
i+=1
29+
flag = 0
30+
st = num
31+
return True
32+
33+
34+
35+
arr = [list(map(int,input().split(' '))) for i in range(N)]
36+
ans = 0
37+
for i in range(N):
38+
ans+=checkPossible(arr[i])
39+
for i in range(N):
40+
ans+=checkPossible([arr[j][i] for j in range(N)])
41+
print(ans)

python/gold3/1937.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
input = sys.stdin.readline
3+
sys.setrecursionlimit(30000)
4+
5+
N = int(input())
6+
arr = [list(map(int,input().split())) for _ in range(N)]
7+
8+
d = [(0,1),(1,0),(-1,0),(0,-1)]
9+
10+
dp_arr = [[-1]*N for _ in range(N)]
11+
12+
def sol(y,x):
13+
if dp_arr[y][x]!=-1: return dp_arr[y][x]
14+
ret = 1
15+
for dy,dx in d:
16+
ny,nx = dy+y, dx+x
17+
if ny<0 or ny>=N or nx<0 or nx>=N:continue
18+
if arr[ny][nx]>arr[y][x]:
19+
ret = max(sol(ny,nx)+1,ret)
20+
dp_arr[y][x] = ret
21+
return ret
22+
ans = 0
23+
for i in range(N):
24+
for j in range(N):
25+
ans=max(ans,sol(i,j))
26+
print(ans)

python/gold3/2836.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
pst = arr[0][0]
1414
arr.append([1e9+1,1e9+1])
1515
for st,ed in arr:
16-
if ed<=ped:continue
17-
else:
16+
if ed<=ped:continue # 돌아가는 거리에 포함되는 사람 제외
17+
else:
1818
if st>ped:
1919
ans+=(ped-pst)*2
2020
pst = st
2121
ped = ed
22-
else:
22+
else: # 태워서 돌아오는 도착 지점에 딱 뒤로 가는 손님이 있는 경우
2323
ped = ed
2424
print(ans)

python/plat2/15480.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
N = int(input())
5+
adj = [[] for _ in range(N+1)]
6+
for _ in range(N-1):
7+
a,b = map(int,input().split())
8+
adj[a].append(b)
9+
adj[b].append(a)
10+
11+
depth = [0]*(N+1)
12+
arr = [[0]*21 for _ in range(N+1)]
13+
def dfs(idx):
14+
st = [idx]
15+
depth[idx]=1
16+
while st:
17+
now = st.pop()
18+
for nxt in adj[now]:
19+
if depth[nxt]: continue
20+
arr[nxt][0]=now
21+
depth[nxt]=depth[now]+1
22+
st.append(nxt)
23+
dfs(1)
24+
25+
for y in range(1,21):
26+
for x in range(1,N+1):
27+
arr[x][y] = arr[arr[x][y-1]][y-1]
28+
def LCA(x,y):
29+
if depth[x]!=depth[y]:
30+
if depth[x]>depth[y]:x,y=y,x
31+
for i in range(20,-1,-1):
32+
if depth[y]-depth[x]>=(1<<i):
33+
y=arr[y][i]
34+
if x==y: return x
35+
for i in range(20,-1,-1):
36+
if arr[x][i]!=arr[y][i]:
37+
x = arr[x][i]
38+
y = arr[y][i]
39+
return arr[x][0]
40+
41+
for _ in range(int(input())):
42+
r, u, v = map(int,input().split())
43+
ans = LCA(r,u)
44+
tmp = LCA(u,v)
45+
ans = ans if depth[ans]>=depth[tmp] else tmp
46+
tmp = LCA(r,v)
47+
ans = ans if depth[ans]>=depth[tmp] else tmp
48+
print(ans)
File renamed without changes.

python/plat2/3640.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
def dfs():
5+
ret = 0
6+
while True:
7+
prev=[-1]*(v+1)
8+
visited = [False]*(v+1)
9+
dist = [1e9]*(v+1)
10+
dist[st]=0
11+
q = [st]
12+
visited[st]=True
13+
while q:
14+
n = q.pop()
15+
visited[n]=False
16+
for nx in adj[n]:
17+
if cap[n][nx]>fl[n][nx] and dist[nx]>dist[n]+cdj[n][nx]:
18+
dist[nx]=dist[n]+cdj[n][nx]
19+
prev[nx]=n
20+
if not visited[nx]:
21+
q.append(nx)
22+
visited[nx]=True
23+
if prev[ed]==-1:break
24+
flow = 1e9
25+
now = ed
26+
while now!=st:
27+
flow = min(flow,cap[prev[now]][now] - fl[prev[now]][now])
28+
now = prev[now]
29+
now = ed
30+
while now!=st:
31+
fl[prev[now]][now]+=1
32+
fl[now][prev[now]]-=1
33+
ret+=cdj[prev[now]][now]*flow
34+
now = prev[now]
35+
return ret
36+
37+
while True:
38+
try:
39+
v, e = map(int,input().split())
40+
st,ed = 0, (v<<1)-1
41+
tmp = v
42+
v=v<<1|1
43+
cap = [[0]*v for _ in range(v)]
44+
fl = [[0]*v for _ in range(v)]
45+
adj = [[] for _ in range(v)]
46+
cdj = [[0]*2001 for _ in range(2001)]
47+
for i in range(1,tmp+1):
48+
_in = (i<<1)-2
49+
_out = (i<<1)-1
50+
adj[_in].append(_out)
51+
cap[_in][_out]=1
52+
cap[0][1]=2
53+
cap[ed-1][ed]=2
54+
for _ in range(e):
55+
a,b,c = map(int,input().split())
56+
in_a = (a<<1)-2
57+
out_a = (a<<1)-1
58+
in_b = (b<<1)-2
59+
out_b = (b<<1)-1
60+
cdj[out_a][in_b]=c
61+
cdj[in_b][out_a]=-c
62+
adj[out_a].append(in_b)
63+
adj[in_b].append(out_a)
64+
cap[out_a][in_b]=1
65+
print(dfs())
66+
except:
67+
break
68+

0 commit comments

Comments
 (0)