Все задачи на 5.10.17 13:00
This commit is contained in:
BIN
uts/uts_2k17_march_py/DER/A
Executable file
BIN
uts/uts_2k17_march_py/DER/A
Executable file
Binary file not shown.
75
uts/uts_2k17_march_py/DER/A.cpp
Normal file
75
uts/uts_2k17_march_py/DER/A.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
#define MAXN 100007
|
||||
|
||||
int a[MAXN], tree[4 * MAXN], upd[4 * MAXN], n;
|
||||
|
||||
void build_tree(int v, int tl, int tr)
|
||||
{
|
||||
if(tl == tr) tree[v] = a[tl];
|
||||
else
|
||||
{
|
||||
int tm = (tl + tr) >> 1;
|
||||
build_tree(v + v, tl, tm);
|
||||
build_tree(v + v + 1, tm + 1, tr);
|
||||
tree[v] = tree[v + v] + tree[v + v + 1];
|
||||
}
|
||||
}
|
||||
|
||||
int query_tree(int v, int tl, int tr, int l, int r)
|
||||
{
|
||||
if(l > r) return 0;
|
||||
if(upd[v] != -1) return upd[v] * (r - l + 1);
|
||||
if(l == tl && r == tr) return tree[v];
|
||||
int tm = (tl + tr) >> 1;
|
||||
return query_tree(v + v, tl, tm, l, min(r, tm)) + query_tree(v + v + 1, tm + 1, tr, max(l, tm + 1), r);
|
||||
}
|
||||
|
||||
void update_tree(int v, int tl, int tr, int l, int r, int color)
|
||||
{
|
||||
if(l > r) return;
|
||||
if(l == tl && tr == r)
|
||||
{
|
||||
upd[v] = color;
|
||||
tree[v] = color * (r - l + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
upd[v] = -1;
|
||||
int tm = (tl + tr) >> 1;
|
||||
update_tree(v + v, tl, tm, l, min(r, tm), color);
|
||||
update_tree(v + v + 1, tm + 1, tr, max(l, tm + 1), r, color);
|
||||
tree[v] = tree[v + v] + tree[v + v + 1];
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ifstream cin("sum.in");
|
||||
ofstream cout("sum.out");
|
||||
int k;
|
||||
cin >> n >> k;
|
||||
for(int i = 0; i < 4 * MAXN; i++) upd[i] = -1; //Изначально все отрезки(вершины) не обновлены
|
||||
build_tree(1, 0, n-1);
|
||||
int q, x, l, r;
|
||||
char type;
|
||||
scanf("%d", &q);
|
||||
for(int i = 0; i < k; i++)
|
||||
{
|
||||
cin >> type;
|
||||
cin >> l;
|
||||
cin >> r;
|
||||
if(type == 'Q')
|
||||
{
|
||||
printf("%d\n", query_tree(1, 0, n - 1, l, r));
|
||||
}
|
||||
else if(type == 'A')
|
||||
{
|
||||
update_tree(1, 0, n - 1, l, l, r);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
42
uts/uts_2k17_march_py/DER/A.py
Normal file
42
uts/uts_2k17_march_py/DER/A.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import sys
|
||||
problem_name = str('sum')
|
||||
sys.stdin = open(problem_name + ".in", "r")
|
||||
#sys.stdout = open(problem_name+".out", "w")
|
||||
|
||||
def deld(a):
|
||||
left = len(a) // 2
|
||||
right = len(a) // 2 + len(a) % 2
|
||||
print(0, ' ', left, ' ', right, ' ', left+right)
|
||||
b = [a[i] for i in range(left)]
|
||||
c = [a[i] for i in range(left, left+right)]
|
||||
return b, c
|
||||
|
||||
def build(a, fab):
|
||||
if len(a) <= 1:
|
||||
return
|
||||
left, right = deld(a)
|
||||
fab.append([0])
|
||||
fab.append([0])
|
||||
build(left, fab[0])
|
||||
build(right, fab[1])
|
||||
|
||||
def build_linkers(a, fab):
|
||||
|
||||
|
||||
n, k = list(map(int, input().split()))
|
||||
mas = [[0] for i in range(n)]
|
||||
fab = []
|
||||
build(mas, fab)
|
||||
query = []
|
||||
for i in range(k-1):
|
||||
s = input().split()
|
||||
s[1] = int(s[1])-1
|
||||
s[2] = int(s[2])
|
||||
if s[0] == 'A':
|
||||
mas[s[1]] = [s[2]]
|
||||
else:
|
||||
query.append(s)
|
||||
|
||||
print(query)
|
||||
print(mas)
|
||||
print(fab)
|
||||
10
uts/uts_2k17_march_py/DER/sum.in
Normal file
10
uts/uts_2k17_march_py/DER/sum.in
Normal file
@@ -0,0 +1,10 @@
|
||||
5 9
|
||||
A 2 2
|
||||
A 3 1
|
||||
A 4 2
|
||||
Q 1 1
|
||||
Q 2 2
|
||||
Q 3 3
|
||||
Q 4 4
|
||||
Q 5 5
|
||||
Q 1 5
|
||||
9
uts/uts_2k17_march_py/Dun/A.py
Normal file
9
uts/uts_2k17_march_py/Dun/A.py
Normal file
@@ -0,0 +1,9 @@
|
||||
import sys
|
||||
from math import factorial
|
||||
problem_name = str('combs')
|
||||
sys.stdin = open(problem_name + ".in", "r")
|
||||
#sys.stdout = open(problem_name+".out", "w")
|
||||
n, k = list(map(int, input().split()))
|
||||
print(int(factorial(n)/(factorial(k)*factorial(n-k))))
|
||||
f = open('safdsf')
|
||||
f.close()
|
||||
14
uts/uts_2k17_march_py/Dun/B.py
Normal file
14
uts/uts_2k17_march_py/Dun/B.py
Normal file
@@ -0,0 +1,14 @@
|
||||
import sys
|
||||
problem_name = str('slalom')
|
||||
sys.stdin = open(problem_name + ".in", "r")
|
||||
sys.stdout = open(problem_name+".out", "w")
|
||||
|
||||
n= int(input())
|
||||
a = []
|
||||
for i in range(n):
|
||||
a.append(list(map(int, input().split())))
|
||||
count=0
|
||||
for i in reversed(range(len(a)-1)):
|
||||
for j in range(len(a[i])):
|
||||
a[i][j] = a[i][j] + max(a[i+1][j], a[i+1][j+1])
|
||||
print(a[0][0])
|
||||
5
uts/uts_2k17_march_py/Dun/C.py
Normal file
5
uts/uts_2k17_march_py/Dun/C.py
Normal file
@@ -0,0 +1,5 @@
|
||||
import sys
|
||||
problem_name = str('mushroom')
|
||||
sys.stdin = open(problem_name + ".in", "r")
|
||||
sys.stdout = open(problem_name+".out", "w")
|
||||
|
||||
18
uts/uts_2k17_march_py/Dun/D.py
Normal file
18
uts/uts_2k17_march_py/Dun/D.py
Normal file
@@ -0,0 +1,18 @@
|
||||
import sys
|
||||
problem_name = str('field')
|
||||
sys.stdin = open(problem_name + ".in", "r")
|
||||
sys.stdout = open(problem_name+".out", "w")
|
||||
|
||||
n, m = map(int, input().split())
|
||||
a = []
|
||||
b = [[10**9] * n for i in range(m)]
|
||||
for i in range(m):
|
||||
a.append(list(map(int, input().split())))
|
||||
b[0][0] = 0
|
||||
for i in range(m):
|
||||
for j in range(n):
|
||||
if i - 1 >= 0:
|
||||
b[i][j] = abs(a[i][j] - a[i - 1][j]) + b[i - 1][j]
|
||||
if j - 1 >= 0:
|
||||
b[i][j] = min(b[i][j], b[i][j - 1] + abs(a[i][j] - a[i][j - 1]))
|
||||
print(b[m - 1][n - 1])
|
||||
22
uts/uts_2k17_march_py/Dun/E.py
Normal file
22
uts/uts_2k17_march_py/Dun/E.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import sys
|
||||
problem_name = str('goat')
|
||||
sys.stdin = open(problem_name + ".in", "r")
|
||||
#sys.stdout = open(problem_name+".out", "w")
|
||||
|
||||
m, n, k = map(int, input(). split())
|
||||
T = [[False]*(n+1) for i in range(m+1)]
|
||||
for i in range(k):
|
||||
n, y = map(int, input().split())
|
||||
T[n - 1][y - 1] = True
|
||||
C = [[0]*(n+1) for i in range(m+1)]
|
||||
C[1][1] = 1
|
||||
dead = 0
|
||||
for i in range(1, m+1):
|
||||
for j in range(1, n+1):
|
||||
if i == 1 and j == 1:
|
||||
comb = 1
|
||||
else:
|
||||
comb = C[i-1][j] + C[i][j-1]
|
||||
if T[i][j]:
|
||||
dead += comb
|
||||
comb = 0
|
||||
9
uts/uts_2k17_march_py/Dun/H.py
Normal file
9
uts/uts_2k17_march_py/Dun/H.py
Normal file
@@ -0,0 +1,9 @@
|
||||
import sys
|
||||
import re
|
||||
problem_name = str('patterns')
|
||||
sys.stdin = open(problem_name + ".in", "r")
|
||||
sys.stdout = open(problem_name+".out", "w")
|
||||
if re.compile(input().replace('*', '.*').replace('?', '.')).match(input()) != None:
|
||||
print('YES')
|
||||
else:
|
||||
print('NO')
|
||||
1
uts/uts_2k17_march_py/Dun/combs.in
Normal file
1
uts/uts_2k17_march_py/Dun/combs.in
Normal file
@@ -0,0 +1 @@
|
||||
3 2
|
||||
3
uts/uts_2k17_march_py/Dun/field.in
Normal file
3
uts/uts_2k17_march_py/Dun/field.in
Normal file
@@ -0,0 +1,3 @@
|
||||
4 2
|
||||
1 2 3 5
|
||||
3 8 4 7
|
||||
5
uts/uts_2k17_march_py/Dun/goat.in
Normal file
5
uts/uts_2k17_march_py/Dun/goat.in
Normal file
@@ -0,0 +1,5 @@
|
||||
2 2 1
|
||||
1 2
|
||||
2 2 2
|
||||
1 2
|
||||
2 1
|
||||
6
uts/uts_2k17_march_py/Dun/mushroom.in
Normal file
6
uts/uts_2k17_march_py/Dun/mushroom.in
Normal file
@@ -0,0 +1,6 @@
|
||||
4 4 3 2
|
||||
1 4
|
||||
2 3
|
||||
4 3
|
||||
2 2
|
||||
3 4
|
||||
2
uts/uts_2k17_march_py/Dun/patterns.in
Normal file
2
uts/uts_2k17_march_py/Dun/patterns.in
Normal file
@@ -0,0 +1,2 @@
|
||||
k?t?n
|
||||
kitten
|
||||
5
uts/uts_2k17_march_py/Dun/slalom.in
Normal file
5
uts/uts_2k17_march_py/Dun/slalom.in
Normal file
@@ -0,0 +1,5 @@
|
||||
4
|
||||
1
|
||||
4 3
|
||||
5 6 7
|
||||
8 9 0 9
|
||||
55
uts/uts_2k17_march_py/Str/A.py
Normal file
55
uts/uts_2k17_march_py/Str/A.py
Normal file
@@ -0,0 +1,55 @@
|
||||
import sys
|
||||
|
||||
ID = "strings"
|
||||
sys.stdin = open(ID + ".in", "r")
|
||||
|
||||
|
||||
# sys.stdout = open(ID + ".out", "w")
|
||||
|
||||
def KMP(s, x):
|
||||
S = x + '#' + s # Большая строка
|
||||
print('S = ' + S)
|
||||
P = [0] * len(S) # Префикс-функция
|
||||
m = len(x) # Длина подстроки
|
||||
# print(template)
|
||||
found = False
|
||||
for i in range(1, len(S)):
|
||||
# Префикс-функция для предыдущего
|
||||
# символа
|
||||
# i - индекс в строке
|
||||
# j - индекс в префиксе (подстроке)
|
||||
print()
|
||||
print('Символ: ' + S[:i] + "[" + S[i] + "]" + S[(i + 1):], end=' ')
|
||||
j = P[i - 1] # незачем смотреть всю строку, можно посмотреть со сдвига прошлой подстроки
|
||||
print('P[' + str(i - 1) + '] = ' + str(j), end=' ')
|
||||
while j > 0 and S[j] != S[i]:
|
||||
print('j: ' + str(j - 1) +
|
||||
' -> P[' + str(j - 1) + '] = '
|
||||
+ str(P[j - 1]), end=' ')
|
||||
j = P[j - 1]
|
||||
if S[j] == S[i]:
|
||||
j += 1
|
||||
print(' S[' + str(j) + '] == S[' + str(i) + '] == ' + str(S[i]) + ' ' +
|
||||
'P[' + str(i) + '] = ' + str(j), end=' ')
|
||||
P[i] = j
|
||||
# Если видим длину подстроки
|
||||
# то мы её нашли
|
||||
if j == m:
|
||||
if found:
|
||||
print(' ', end='')
|
||||
found = True
|
||||
print(i - m * 2 + 1)
|
||||
if not found:
|
||||
print('none')
|
||||
print(P)
|
||||
|
||||
|
||||
while True:
|
||||
try:
|
||||
s = input()
|
||||
t = input()
|
||||
except EOFError:
|
||||
break
|
||||
KMP(s, t)
|
||||
|
||||
sys.stdout.close()
|
||||
7
uts/uts_2k17_march_py/Str/B.py
Normal file
7
uts/uts_2k17_march_py/Str/B.py
Normal file
@@ -0,0 +1,7 @@
|
||||
import sys
|
||||
import collections
|
||||
problem_name = str('substr')
|
||||
sys.stdin = open(problem_name + ".in", "r")
|
||||
sys.stdout = open(problem_name+".out", "w")
|
||||
|
||||
print(collections.Counter(input()).most_common()[0][0])
|
||||
5
uts/uts_2k17_march_py/Str/C.py
Normal file
5
uts/uts_2k17_march_py/Str/C.py
Normal file
@@ -0,0 +1,5 @@
|
||||
import sys
|
||||
problem_name = str('double')
|
||||
sys.stdin = open(problem_name + ".in", "r")
|
||||
#sys.stdout = open(problem_name+".out", "w")
|
||||
|
||||
4
uts/uts_2k17_march_py/Str/D.py
Normal file
4
uts/uts_2k17_march_py/Str/D.py
Normal file
@@ -0,0 +1,4 @@
|
||||
import sys
|
||||
problem_name = str('genstr')
|
||||
sys.stdin = open(problem_name + ".in", "r")
|
||||
sys.stdout = open(problem_name+".out", "w")
|
||||
2
uts/uts_2k17_march_py/Str/double.in
Normal file
2
uts/uts_2k17_march_py/Str/double.in
Normal file
@@ -0,0 +1,2 @@
|
||||
aaaaaab
|
||||
5
|
||||
2
uts/uts_2k17_march_py/Str/strings.in
Normal file
2
uts/uts_2k17_march_py/Str/strings.in
Normal file
@@ -0,0 +1,2 @@
|
||||
abbabas
|
||||
aba
|
||||
1
uts/uts_2k17_march_py/Str/substr.in
Normal file
1
uts/uts_2k17_march_py/Str/substr.in
Normal file
@@ -0,0 +1 @@
|
||||
abc
|
||||
Reference in New Issue
Block a user