forked from Nastasia8/AaDS_1_147_2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodul4.py
190 lines (160 loc) · 3.92 KB
/
modul4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#ТРЕНИРОВОЧНЫЕ ЗАДАЧИ:
#задача1
s=input()
stack=[]
isPcp=True
for i in s:
if i==")":
if not stack or stack [-1]!="(":
isPcp=False
break
stack.pop()
elif i=="]":
if not stack or stack [-1]!="[":
isPcp=False
break
stack.pop()
elif i=="}":
if not stack or stack [-1]!="{":
isPcp=False
break
stack.pop()
else:
stack.append(i)
if isPcp and not stack:
print("Yes")
else:
print("No")
#задача2
s=input().replace(" ","")
stack=[]
for i in s:
if i.isdigit():
stack.append(int(i))
else:
d2=stack.pop()
d1=stack.pop()
if i == "+":
stack.append(d1+d2)
elif i=="*":
stack.append(d1*d2)
else:
stack.append(d1-d2)
print(stack[-1])
#задача3 (сортировка фамилий)
from collections import deque
n = int(input())
q1=deque()
q2=deque()
q3=deque()
q4=deque()
for i in range(n):
num, name = map(str,input().split())
if int(num)==1:
q1.append(name)
elif int(num)==2:
q2.append(name)
elif int(num)==3:
q3.append(name)
else:
q4.append(name)
while q1:
print(1,q1.popleft())
while q2:
print(2,q2.popleft())
while q3:
print(3,q3.popleft())
while q4:
print(4,q4.popleft())
#задача4 (пьяница)
from collections import deque
first=deque(list(map(int,input().split())))
second=deque(list(map(int,input().split())))
k=0
while k<1e6 and (len(first)*len(second)):
first_card=first.popleft()
second_card=second.popleft()
if first_card == 0 and second_card == 9:
first.append(first_card)
first.append(second.card)
elif first_card == 9 and second_card == 0:
second.append(first_card)
second.append(second_card)
elif first_card>second_card:
first.append(first_card)
first.append(second_card)
else:
second.append(first_card)
second.append(second_card)
k+=1
if not first:
print("second",k)
elif not second:
print("first",k)
else:
print("botva")
#ОСНОВНЫЕ ЗАДАЧИ:
#задача1
s=input()
stack=[]
a=0
for i in s:
if i==")":
if not stack or stack [-1]!="(":
a+=1
continue
stack.pop()
else:
stack.append(i)
print(a+len(stack))
#задача2
from collections import deque
n=int(input())
a=deque(list(map(int, input().split(maxsplit=n))))
b=[]
deadlock=[]
res= list(range(1, n+1))
while a:
if not deadlock or deadlock[-1] > a[0]:
deadlock.append(a.popleft())
if a and deadlock[-1] < a[0]:
b.append(deadlock.pop())
while deadlock:
b.append(deadlock.pop())
if b == res:
print("YES")
else:
print("NO")
#задача3
n=int(input())
numbers=list(map(int,input().split(maxsplit=n)))
d={i:numbers[i] for i in range(n)}
stack = []
index = [0]*n
for i in range (n-1, -1, -1):
while stack and stack[-1][1]>=d[i]:
stack.pop()
if not stack:
index[i]= -1
else:
index[i]=stack[-1][0]
stack.append([i,d[i]])
print(*index)
#задача4 (Евгений Сергеевич разбирал на паре)
def minimum(num, win, arr):
stack = []
for i in range(win):
while stack and arr[i] <= arr[stack[-1]]:
stack.pop()
stack.append(i)
for i in range(win, num):
print(arr[stack[0]])
while stack and i - win >= stack[0]:
stack.pop(0)
while stack and arr[i] <= arr[stack[-1]]:
stack.pop()
stack.append(i)
print(arr[stack[0]])
n, k = map(int, input().split())
a = list(map(int, input().split()))
minimum(n, k, a)