Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1) Check if there is a quadruple such that:
- (i, j, k, z): i < j < k < z and a[i] * a[k] = a[j] * a[z], https://community.topcoder.com/stat?c=problem_statement&pm=13951
- N <= 2000
- 2) Find the maximum side of a cross of 1's in a matrix, N <= 2000. Think about a harder version: https://codeforces.com/problemset/problem/677/E; Don't care about the big number part.
- 3) https://www.spoj.com/problems/ADAINDEX/, helpful drawings: https://www.webwhiteboard.com/board/6bdf3r4k
- 4) Hit https://codeforces.com/blog/entry/64250
- 5) Round 546
- """
- #####
- # 1 #
- #####
- #
- # Check if there is a quadruple such that:
- # (i, j, k, z): i < j < k < z and a[i] * a[k] = a[j] * a[z], https://community.topcoder.com/stat c=problem_statement&pm=13951 N <= 2000
- from math import gcd
- a = [int(i) for i in input().split(' ')]
- n = len(a)
- d = {}
- count = 0
- for i in range(n):
- for j in range(i + 1, n):
- g = gcd(a[i], a[j])
- a_i = a[i] // g
- a_j = a[j] // g
- d.setdefault((a_i, a_j), [])
- d[(a_i, a_j)].append(j)
- for k in range(n):
- for z in range(k + 1, n):
- g = gcd(a[k], a[z])
- a_k = a[k] // g
- a_l = a[z] // g
- division = (a_l, a_k)
- if division in d:
- for position in d[division]:
- if position < k:
- count += 1
- print(count)
- #####
- # 2 #
- #####
- #
- # Find the maximum side of a cross of 1's in a matrix, N <= 2000. Think about a harder version: https://codeforces.com/problemset/problem/677/E; Don't care about the big number part.
- n = int(input())
- a = []
- k = []
- directions = ['left', 'right', 'up', 'down']
- def process_row(i):
- j = 0
- while j < n:
- start = j
- while j < n and a[i][j] == 1:
- j += 1
- for x in range(start, j):
- value = x - start
- k[i][x]['left'] = value
- k[i][x]['right'] = j - x - 1
- j += 1
- def process_column(j):
- i = 0
- while i < n:
- start = i
- while i < n and a[i][j] == 1:
- i += 1
- for x in range(start, i):
- value = x - start
- k[x][j]['up'] = value
- k[x][j]['down'] = i - x - 1
- i += 1
- for i in range(n):
- a.append([int(i) for i in input().split(' ')])
- k.append([])
- for j in range(n):
- k[i].append({'left': 0, 'right': 0, 'up': 0, 'down': 0})
- for i in range(n):
- process_row(i)
- process_column(i)
- m = 0
- for i in range(n):
- for j in range(n):
- m = max(m, min(k[i][j].values()))
- print(m)
- #####
- # 3 #
- #####
- #
- # https://www.spoj.com/problems/ADAINDEX/, helpful drawings: https://www.webwhiteboard.com/board/6bdf3r4k
- n, q = map(int, input().split(' '))
- t = {"children": {}, "value": 0}
- for _ in range(n):
- word = input()
- t['value'] += 1
- current_node = t
- for l in word:
- current_node['children'].setdefault(l, {"children": {}, "value": 0})
- current_node['children'][l]['value'] += 1
- current_node = current_node['children'][l]
- for _ in range(q):
- word = input()
- current_node = t
- printed = False
- for l in word:
- if l not in current_node['children']:
- print(0)
- printed = True
- break
- current_node = current_node['children'][l]
- if not printed:
- print(current_node['value'])
- #####
- # 4 #
- #####
- #
- # Hit https://codeforces.com/blog/entry/64250
- #######
- # 4.1 #
- #######
- #
- # https://atcoder.jp/contests/dp/tasks/dp_a
- n = int(input())
- a = [int(i) for i in input().split(' ')]
- dp = [0] * n
- dp[0] = 0
- dp[1] = abs(a[0] - a[1])
- for i in range(2, n):
- dp[i] = min(abs(a[i - 2] - a[i]) + dp[i - 2], abs(a[i - 1] - a[i]) + dp[i - 1])
- print(dp[-1])
- #########
- # 4.2.1 #
- #########
- #
- # https://atcoder.jp/contests/dp/tasks/dp_b - python version - TLE
- n, k = map(int, input().split(' '))
- a = [int(i) for i in input().split(' ')]
- dp = [float('inf')] * n
- dp[0] = 0
- for i in range(1, n):
- for x in range(1, min(i, k) + 1):
- z = abs(a[i - x] - a[i]) + dp[i - x]
- if dp[i] > z:
- dp[i] = z
- print(dp[-1])
- #########
- # 4.2.1 #
- #########
- #
- # https://atcoder.jp/contests/dp/tasks/dp_b - got pissed, implemented it in raw C - the very same construct < 30ms o.O
- """
- #include <stdio.h>
- #include <stdlib.h>
- int min(int a, int b) {
- if (a > b) {
- return b;
- }
- return a;
- }
- int main() {
- int a[100000], dp[100000], n, k;
- const int inf = 1e9;
- scanf("%d", &n);
- scanf("%d", &k);
- for(int i = 0; i < n; i++) {
- scanf("%d", &a[i]);
- dp[i] = inf;
- }
- dp[0] = 0;
- for(int i = 1; i < n; i++){
- for(int j = 1; j < min(i, k) + 1; j++){
- dp[i] = min(dp[i], abs(a[i] - a[i - j]) + dp[i - j]);
- }
- }
- printf("%d\n", dp[n - 1]);
- return 0;
- }
- """
- #######
- # 4.3 #
- #######
- #
- # https://atcoder.jp/contests/dp/tasks/dp_c
- n = int(input())
- a = []
- b = []
- c = []
- dp = []
- for i in range(n):
- dp.append([0, 0, 0])
- for i in range(n):
- a_i, b_i, c_i = [int(i) for i in input().split(' ')]
- a.append(a_i)
- b.append(b_i)
- c.append(c_i)
- dp[0][0] = a[0]
- dp[0][1] = b[0]
- dp[0][2] = c[0]
- for i in range(1, n):
- dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + a[i]
- dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + b[i]
- dp[i][2] = max(dp[i - 1][0], dp[i - 1][1]) + c[i]
- print(max(dp[-1]))
- #####
- # 5 #
- #####
- #
- # https://codeforces.com/contest/1136
- #######
- # 5.1 #
- #######
- #
- # https://codeforces.com/contest/1136/problem/A
- def go():
- n = int(input())
- books = []
- for i in range(n):
- books.append([int(i) for i in input().split(' ')][1])
- k = int(input())
- read = 0
- for i in range(n):
- if books[i] >= k:
- return n - read
- read += 1
- print(go())
- #######
- # 5.2 #
- #######
- #
- # https://codeforces.com/contest/1136/problem/B
- def go():
- n, k = map(int, input().split(' '))
- return min(n - k, k - 1) + 3 * n
- print(go())
- #######
- # 5.1 #
- #######
- #
- # https://codeforces.com/contest/1136/problem/C - this one is failing on a test. I have no idea why that would be, since my reasoning seems legit. (You can switch any elements, given enough transposes on different submatrices. The only 2 that won't ever switch are the diagonal heads (0, 0), (n, m). Yet, this fails. I could use some help with understanding why this is wrong or why this assumption is invalid.
- def go():
- n, m = map(int, input().split(' '))
- a = []
- b = []
- for l in [a, b]:
- for i in range(n):
- l.append([int(i) for i in input().split(' ')])
- if a[0][0] != b[0][0] or a[-1][-1] != b[-1][-1]:
- return "NO"
- counter_a = {}
- counter_b = {}
- for l, c in [(a, counter_a), (b, counter_b)]:
- for i in range(n):
- for j in range(m):
- c.setdefault(l[i][j], 0)
- c[l[i][j]] += 1
- if counter_a != counter_b:
- return "NO"
- return "YES"
- print(go())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement