problem_id
stringlengths
3
7
contestId
stringclasses
660 values
problem_index
stringclasses
27 values
programmingLanguage
stringclasses
3 values
testset
stringclasses
5 values
incorrect_passedTestCount
float64
0
146
incorrect_timeConsumedMillis
float64
15
4.26k
incorrect_memoryConsumedBytes
float64
0
271M
incorrect_submission_id
stringlengths
7
9
incorrect_source
stringlengths
10
27.7k
correct_passedTestCount
float64
2
360
correct_timeConsumedMillis
int64
30
8.06k
correct_memoryConsumedBytes
int64
0
475M
correct_submission_id
stringlengths
7
9
correct_source
stringlengths
28
21.2k
contest_name
stringclasses
664 values
contest_type
stringclasses
3 values
contest_start_year
int64
2.01k
2.02k
time_limit
float64
0.5
15
memory_limit
float64
64
1.02k
title
stringlengths
2
54
description
stringlengths
35
3.16k
input_format
stringlengths
67
1.76k
output_format
stringlengths
18
1.06k
interaction_format
null
note
stringclasses
840 values
examples
stringlengths
34
1.16k
rating
int64
800
3.4k
tags
stringclasses
533 values
testset_size
int64
2
360
official_tests
stringlengths
44
19.7M
official_tests_complete
bool
1 class
input_mode
stringclasses
1 value
generated_checker
stringclasses
231 values
executable
bool
1 class
529/B
529
B
PyPy 3-64
TESTS
0
46
0
203130215
n = int(input()) friends = [] area_sum = 0 for i in range(n): w, h = map(int, input().split()) friends.append((w, h)) area_sum += w * h friends.sort(key=lambda x: x[1]) if area_sum < (n//2) * friends[-1][1]: print(area_sum) else: left, right = 1, 10**12 while left < right: mid = (left + right) // 2 min_h = float('inf') candidates = [] for w, h in friends: if w * h >= mid / 2: min_h = min(min_h, w) else: candidates.append(h) candidates.sort(reverse=True) count = sum(candidates[:n//2]) if count >= mid // 2: left = mid + 1 else: right = mid print(left)
58
842
6,348,800
10391728
from operator import neg n = int(input()) a = [tuple(map(int, input().split())) for i in range(n)] def check(max_h): k = n // 2 b = [] for w, h in a: if h > max_h: if k <= 0 or w > max_h: return 1 << 60 b.append((h, w)) k -= 1 else: b.append((w, h)) b.sort(key=lambda t: t[1] - t[0]) r = 0 for w, h in b: if k > 0 and w <= max_h and h < w: r += h k -= 1 else: r += w return r * max_h print(min(check(h) for h in range(1, 1001)))
VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)
CF
2,015
2
256
Group Photo 2 (online mirror version)
Many years have passed, and n friends met at a party again. Technologies have leaped forward since the last meeting, cameras with timer appeared and now it is not obligatory for one of the friends to stand with a camera, and, thus, being absent on the photo. Simply speaking, the process of photographing can be described as follows. Each friend occupies a rectangle of pixels on the photo: the i-th of them in a standing state occupies a wi pixels wide and a hi pixels high rectangle. But also, each person can lie down for the photo, and then he will occupy a hi pixels wide and a wi pixels high rectangle. The total photo will have size W × H, where W is the total width of all the people rectangles, and H is the maximum of the heights. The friends want to determine what minimum area the group photo can they obtain if no more than n / 2 of them can lie on the ground (it would be strange if more than n / 2 gentlemen lie on the ground together, isn't it?..) Help them to achieve this goal.
The first line contains integer n (1 ≤ n ≤ 1000) — the number of friends. The next n lines have two integers wi, hi (1 ≤ wi, hi ≤ 1000) each, representing the size of the rectangle, corresponding to the i-th friend.
Print a single integer equal to the minimum possible area of the photo containing all friends if no more than n / 2 of them can lie on the ground.
null
null
[{"input": "3\n10 1\n20 2\n30 3", "output": "180"}, {"input": "3\n3 1\n2 2\n4 3", "output": "21"}, {"input": "1\n5 10", "output": "50"}]
1,900
["brute force", "greedy", "sortings"]
58
[{"input": "3\r\n10 1\r\n20 2\r\n30 3\r\n", "output": "180\r\n"}, {"input": "3\r\n3 1\r\n2 2\r\n4 3\r\n", "output": "21\r\n"}, {"input": "1\r\n5 10\r\n", "output": "50\r\n"}, {"input": "1\r\n1 1\r\n", "output": "1\r\n"}, {"input": "1\r\n1000 1000\r\n", "output": "1000000\r\n"}, {"input": "1\r\n1 1000\r\n", "output": "1000\r\n"}, {"input": "2\r\n1 1000\r\n1000 1\r\n", "output": "2000\r\n"}, {"input": "2\r\n1 1\r\n1000 1000\r\n", "output": "1001000\r\n"}, {"input": "1\r\n1000 1\r\n", "output": "1000\r\n"}, {"input": "5\r\n407 830\r\n12 199\r\n768 215\r\n668 28\r\n628 310\r\n", "output": "1070700\r\n"}, {"input": "3\r\n379 820\r\n923 407\r\n916 853\r\n", "output": "1570946\r\n"}, {"input": "3\r\n99 768\r\n477 885\r\n169 118\r\n", "output": "614190\r\n"}, {"input": "3\r\n227 612\r\n223 259\r\n423 895\r\n", "output": "781335\r\n"}, {"input": "3\r\n651 161\r\n480 32\r\n485 672\r\n", "output": "756672\r\n"}, {"input": "3\r\n779 301\r\n34 214\r\n442 937\r\n", "output": "728049\r\n"}, {"input": "3\r\n203 145\r\n780 692\r\n992 713\r\n", "output": "1366821\r\n"}, {"input": "3\r\n627 286\r\n37 65\r\n53 490\r\n", "output": "235752\r\n"}, {"input": "3\r\n755 938\r\n487 543\r\n307 459\r\n", "output": "1307660\r\n"}, {"input": "3\r\n475 487\r\n41 20\r\n368 236\r\n", "output": "366224\r\n"}, {"input": "3\r\n922 71\r\n719 26\r\n462 700\r\n", "output": "1013790\r\n"}, {"input": "2\r\n881 4\r\n788 2\r\n", "output": "6676\r\n"}, {"input": "2\r\n1 304\r\n8 892\r\n", "output": "8028\r\n"}, {"input": "3\r\n227 2\r\n223 9\r\n423 5\r\n", "output": "7857\r\n"}, {"input": "3\r\n7 612\r\n3 259\r\n3 895\r\n", "output": "11635\r\n"}, {"input": "4\r\n573 7\r\n169 9\r\n447 7\r\n947 3\r\n", "output": "19224\r\n"}, {"input": "10\r\n864 874\r\n534 702\r\n73 363\r\n856 895\r\n827 72\r\n435 468\r\n888 921\r\n814 703\r\n648 715\r\n384 781\r\n", "output": "4909752\r\n"}, {"input": "10\r\n489 685\r\n857 870\r\n736 221\r\n687 697\r\n166 360\r\n265 200\r\n738 519\r\n393 760\r\n66 176\r\n798 160\r\n", "output": "3231747\r\n"}, {"input": "1\r\n1 1\r\n", "output": "1\r\n"}, {"input": "1\r\n1000 1000\r\n", "output": "1000000\r\n"}, {"input": "1\r\n1 1000\r\n", "output": "1000\r\n"}, {"input": "2\r\n1 1000\r\n1000 1\r\n", "output": "2000\r\n"}, {"input": "2\r\n1 1\r\n1000 1000\r\n", "output": "1001000\r\n"}, {"input": "1\r\n1000 1\r\n", "output": "1000\r\n"}, {"input": "2\r\n1 1\r\n1 1\r\n", "output": "2\r\n"}, {"input": "4\r\n3 817\r\n9 729\r\n7 407\r\n7 433\r\n", "output": "21242\r\n"}, {"input": "3\r\n500 500\r\n2 1\r\n10 5\r\n", "output": "253500\r\n"}]
false
stdio
null
true
311/B
311
B
Python 3
TESTS
1
46
204,800
22298692
n,m,p = [int(i) for i in input().split(' ')] d = [0]+[int(i) for i in input().split(' ')]#del dp = [[0]*(m+1) for _ in range(p+1)] oo=10*10 pos = [0]*(n+1) a=[0]*(m+1) s=[0]*(m+1) st=[0]*10000 def g(i,k): return dp[i-1][k]+s[k] def main(): global a, s, st, dp for i in range(1,n+1): pos[i] = sum(d[1:i]) for i in range(1,m+1): h,t = [int(j) for j in input().split(' ')] a[i] = t-pos[h] a=sorted(a) for i in range(1,m+1): s[i] = sum(a[1:i+1]) for i in range(1,m+1): dp[0][i]= oo for i in range(1,p+1): hd=1 tl=0 for j in range(0,m+1): while hd<tl and g(i,st[hd+1])-g(i,st[hd])<=a[j]*(st[hd+1]-st[hd]): hd+=1 dp[i][j]=g(i,st[hd])+a[j]*(j-st[hd])-s[j] while hd<tl and (g(i,j)-g(i,st[tl]))*(st[tl]-st[tl-1])<=(g(i,st[tl])-g(i,st[tl-1]))*(j-st[tl]): tl-=1 tl+=1 st[tl]=j print(dp[p][m]) main()
30
1,216
27,340,800
211419212
def solve(A, B, C): N = len(B) + 1 M = len(C) a = [0] * N for i in range(1, N): a[i] += a[i - 1] + B[i - 1] b = [0] * M s = [0] * M dp = [0] * M k = [0] * M for i in range(M): b[i] = C[i][1] - a[C[i][0] - 1] b.sort() for i in range(M - 1, -1, -1): b[i] -= b[0] for i in range(M): s[i] = s[i - 1] + b[i] if i > 0 else 0 for i in range(M): dp[i] = b[i] * (i + 1) - s[i] q = [0] * (10**5 + 9) for i in range(1, A): for j in range(M): k[j] = dp[j] + s[j] H = 0 T = -1 for j in range(M): while H < T and (k[q[H + 1]] - k[q[H]]) < b[j] * (q[H + 1] - q[H]): H += 1 while H < T and (k[q[T]] - k[q[T - 1]]) * (j - q[T]) > (k[j] - k[q[T]]) * (q[T] - q[T - 1]): T -= 1 T += 1 q[T] = j dp[j] = k[q[H]] + b[j] * (j - q[H]) - s[j] return str(dp[M - 1]) t = 1 # t = int(input()) # Uncomment this line to take input for 't' while t > 0: n, m, p = map(int, input().split()) arr = list(map(int, input().split())) brr = [] for _ in range(m): brr.append(list(map(int, input().split()))) print(solve(p, arr, brr)) t -= 1
Codeforces Round 185 (Div. 1)
CF
2,013
2
256
Cats Transport
Zxr960115 is owner of a large farm. He feeds m cute cats and employs p feeders. There's a straight road across the farm and n hills along the road, numbered from 1 to n from left to right. The distance between hill i and (i - 1) is di meters. The feeders live in hill 1. One day, the cats went out to play. Cat i went on a trip to hill hi, finished its trip at time ti, and then waited at hill hi for a feeder. The feeders must take all the cats. Each feeder goes straightly from hill 1 to n without waiting at a hill and takes all the waiting cats at each hill away. Feeders walk at a speed of 1 meter per unit time and are strong enough to take as many cats as they want. For example, suppose we have two hills (d2 = 1) and one cat that finished its trip at time 3 at hill 2 (h1 = 2). Then if the feeder leaves hill 1 at time 2 or at time 3, he can take this cat, but if he leaves hill 1 at time 1 he can't take it. If the feeder leaves hill 1 at time 2, the cat waits him for 0 time units, if the feeder leaves hill 1 at time 3, the cat waits him for 1 time units. Your task is to schedule the time leaving from hill 1 for each feeder so that the sum of the waiting time of all cats is minimized.
The first line of the input contains three integers n, m, p (2 ≤ n ≤ 105, 1 ≤ m ≤ 105, 1 ≤ p ≤ 100). The second line contains n - 1 positive integers d2, d3, ..., dn (1 ≤ di < 104). Each of the next m lines contains two integers hi and ti (1 ≤ hi ≤ n, 0 ≤ ti ≤ 109).
Output an integer, the minimum sum of waiting time of all cats. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
null
null
[{"input": "4 6 2\n1 3 5\n1 0\n2 1\n4 9\n1 10\n2 10\n3 12", "output": "3"}]
2,400
["data structures", "dp"]
30
[{"input": "4 6 2\r\n1 3 5\r\n1 0\r\n2 1\r\n4 9\r\n1 10\r\n2 10\r\n3 12\r\n", "output": "3\r\n"}]
false
stdio
null
true
13/B
13
B
Python 3
TESTS
1
434
716,800
69700947
""" A : Determine if three line segments form A """ def distance(a, b, c, d): return (d - b) ** 2 + (c - a) ** 2 def cross(vecA, vecB): return vecA[0] * vecB[1] - vecA[1] * vecB[0] def dot(vecA, vecB): return vecA[0] * vecB[0] + vecA[1] * vecB[1] def slope(x1, y1, x2, y2): if x1 != x2: return (y2 - y1) / (x2 - x1) return float('inf') def div_ratio(line, point): # Simple way to approximately check if the point lies on the line mx, my = point (a, b), (c, d) = line if not (min(a, c) <= mx <= max(a, c) and min(b, d) <= my <= max(b, d)): return -1 # Again make sure the point lies on the line vecA = (a - mx, b - my) vecB = (a - c, b - d) if cross(vecA, vecB) != 0: return -1 # Finally compute the ratio if c == a and d == b: return -1 if d != b: ratio = (my - b) / (d - b) if ratio > 1: ratio = 1 / ratio if a != c: ratio = (mx - a) / (c - a) if ratio > 1: ratio = 1 / ratio if ratio < 0.2 or ratio > 0.8: ratio = -1 # print("LPR : ", line, point, ratio) return ratio def isA(lines): """ Given three line segments check if they form A of not""" # Find the two slanted line segments k, l = -1, -1 lines_found = False for i in range(3): temp = set(lines[i]) for j in range(i + 1, 3): # Check which of the two points is the common point if lines[j][0] in temp: common = lines[j][0] k, l = i, j lines_found = True break if lines[j][1] in temp: common = lines[j][1] k, l = i, j lines_found = True break else: return False # Cause no common point was found if lines_found: break # Process the lines lineA = lines[k] if lineA[0] != common: lineA = [lineA[1], lineA[0]] lineB = lines[l] if lineB[0] != common: lineB = [lineB[1], lineB[0]] # The third is the line connecting two slanted line segments divider = lines[3 - k - l] # If first point of divider does not divide (or lie on) either of the line segments, it does not form A thresh = 0.0625 # 0.25 * 0.25 = 0.0625 thresh = 0.2 r1 = div_ratio(lineA, divider[0]) if r1 == -1: r2 = div_ratio(lineB, divider[0]) # div[0] lies on lineB if r2 == -1: return False r1 = div_ratio(lineA, divider[1]) # div[1] lies on lineA if r1 == -1: return False else: r2 = div_ratio(lineB, divider[1]) if r2 == -1: return False # For everything else return True def CF14C(): N = int(input()) result = [] for _ in range(N): lines = [] for _ in range(3): a, b, c, d = map(int, input().split()) lines.append(((a, b), (c, d))) res = isA(lines) result.append("YES" if res else "NO") return result res = CF14C() print("\n".join(res))
2
530
0
178586732
import random from math import sqrt as s def dist(x1, y1, x2, y2): return s((x2 - x1) ** 2 + (y2 - y1) ** 2) def is_dot_on_line(c1, c2, dot): A = c1[1] - c2[1] B = c2[0] - c1[0] C = c1[0] * c2[1] - c2[0] * c1[1] maxx, minx = max(c1[0], c2[0]), min(c1[0], c2[0]) maxy, miny = max(c1[1], c2[1]), min(c1[1], c2[1]) res = A * dot[0] + B * dot[1] + C if res == 0 and minx <= dot[0] <= maxx and miny <= dot[1] <= maxy: return True return False def cosangle(x1, y1, x2, y2): return x1 * x2 + y1 * y2 def same(k11, k12, k21, k22, k31, k32): if k11 == k21 or k11 == k22 or k12 == k21 or k12 == k22: return 0, 1, 2 if k11 == k31 or k11 == k32 or k12 == k31 or k12 == k32: return 0, 2, 1 if k21 == k31 or k21 == k32 or k22 == k31 or k22 == k32: return 1, 2, 0 return False def is_a(c1, c2, c3, debug=-1): al = [c1, c2, c3] lines = same(*c1, *c2, *c3) if not lines: return False c1, c2, c3 = al[lines[0]], al[lines[1]], al[lines[2]] if c1[0] == c2[1]: c2[0], c2[1] = c2[1], c2[0] if c1[1] == c2[0]: c1[0], c1[1] = c1[1], c1[0] if not (is_dot_on_line(c1[0], c1[1], c3[0]) and is_dot_on_line(c2[0], c2[1], c3[1])): if not (is_dot_on_line(c1[0], c1[1], c3[1]) and is_dot_on_line(c2[0], c2[1], c3[0])): return False c3[0], c3[1] = c3[1], c3[0] cosa = cosangle(c1[0][0] - c1[1][0], c1[0][1] - c1[1][1], c2[0][0] - c2[1][0], c2[0][1] - c2[1][1]) if cosa < 0: return False l1 = dist(*c1[0], *c3[0]), dist(*c1[1], *c3[0]) l2 = dist(*c2[0], *c3[1]), dist(*c2[1], *c3[1]) if min(l1) / max(l1) < 1/4 or min(l2) / max(l2) < 1/4: return False return True n = int(input()) for i in range(n): c1 = list(map(int, input().split())) c2 = list(map(int, input().split())) c3 = list(map(int, input().split())) if is_a([c1[:2], c1[2:]], [c2[:2], c2[2:]], [c3[:2], c3[2:]], debug=i): print("YES") else: print("NO")
Codeforces Beta Round 13
ICPC
2,010
1
64
Letter A
Little Petya learns how to write. The teacher gave pupils the task to write the letter A on the sheet of paper. It is required to check whether Petya really had written the letter A. You are given three segments on the plane. They form the letter A if the following conditions hold: - Two segments have common endpoint (lets call these segments first and second), while the third segment connects two points on the different segments. - The angle between the first and the second segments is greater than 0 and do not exceed 90 degrees. - The third segment divides each of the first two segments in proportion not less than 1 / 4 (i.e. the ratio of the length of the shortest part to the length of the longest part is not less than 1 / 4).
The first line contains one integer t (1 ≤ t ≤ 10000) — the number of test cases to solve. Each case consists of three lines. Each of these three lines contains four space-separated integers — coordinates of the endpoints of one of the segments. All coordinates do not exceed 108 by absolute value. All segments have positive length.
Output one line for each test case. Print «YES» (without quotes), if the segments form the letter A and «NO» otherwise.
null
null
[{"input": "3\n4 4 6 0\n4 1 5 2\n4 0 4 4\n0 0 0 6\n0 6 2 -4\n1 1 0 1\n0 0 0 5\n0 5 2 -1\n1 2 0 1", "output": "YES\nNO\nYES"}]
2,000
["geometry", "implementation"]
2
[{"input": "3\r\n4 4 6 0\r\n4 1 5 2\r\n4 0 4 4\r\n0 0 0 6\r\n0 6 2 -4\r\n1 1 0 1\r\n0 0 0 5\r\n0 5 2 -1\r\n1 2 0 1\r\n", "output": "YES\r\nNO\r\nYES\r\n"}]
false
stdio
null
true
1298/E
978
F
Python 3
TESTS
0
31
0
190728825
def inputs(): return map(int, input().split()) n,q = inputs() p = list(inputs()) ans = [0 for _ in range(n)] class fen: def __init__(self): self.BIT = [0 for _ in range(n + 1)] def upd(self, idx, val): while idx<=n: self.BIT[idx] += val idx += (idx&-idx) def prefsum(self, idx): tot = 0 while idx>=1: tot += self.BIT[idx] idx -= (idx&-idx) return tot def RQ(self, l, r): return self.prefsum(r) - self.prefsum(l-1) for _ in range(q): a,b = inputs() a-=1 b-=1 if p[a] < p[b]: ans[b]-=1 else: ans[a]-=1 comp = dict() idx = 1 wick = fen() for pp in p: ppp = -1 try: ppp = comp[pp] except: comp[pp] = idx ppp = idx idx += 1 wick.upd(ppp, 1) for i, pp in enumerate(p): ans[i] += wick.RQ(1, comp[pp] - 1) print(*ans, sep = ' ')
41
872
56,934,400
201985672
import sys input = lambda: sys.stdin.readline().rstrip() from bisect import * N,K = map(int, input().split()) R = list(map(int, input().split())) A = sorted(R) P = [[] for _ in range(N)] for _ in range(K): u,v = map(int, input().split()) u-=1;v-=1 P[u].append(R[v]) P[v].append(R[u]) ans = [] for i in range(N): p = P[i] p.sort() idx = bisect_left(p, R[i]) tmp = bisect_left(A, R[i]) ans.append(tmp-idx) print(*ans)
Kotlin Heroes: Practice 3
ICPC
2,020
3
256
Mentors
In BerSoft $$$n$$$ programmers work, the programmer $$$i$$$ is characterized by a skill $$$r_i$$$. A programmer $$$a$$$ can be a mentor of a programmer $$$b$$$ if and only if the skill of the programmer $$$a$$$ is strictly greater than the skill of the programmer $$$b$$$ $$$(r_a > r_b)$$$ and programmers $$$a$$$ and $$$b$$$ are not in a quarrel. You are given the skills of each programmers and a list of $$$k$$$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $$$i$$$, find the number of programmers, for which the programmer $$$i$$$ can be a mentor.
The first line contains two integers $$$n$$$ and $$$k$$$ $$$(2 \le n \le 2 \cdot 10^5$$$, $$$0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$$$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $$$r_1, r_2, \dots, r_n$$$ $$$(1 \le r_i \le 10^{9})$$$, where $$$r_i$$$ equals to the skill of the $$$i$$$-th programmer. Each of the following $$$k$$$ lines contains two distinct integers $$$x$$$, $$$y$$$ $$$(1 \le x, y \le n$$$, $$$x \ne y)$$$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $$$x$$$ is in a quarrel with $$$y$$$ then $$$y$$$ is in a quarrel with $$$x$$$. Guaranteed, that for each pair $$$(x, y)$$$ there are no other pairs $$$(x, y)$$$ and $$$(y, x)$$$ in the input.
Print $$$n$$$ integers, the $$$i$$$-th number should be equal to the number of programmers, for which the $$$i$$$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input.
null
In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
[{"input": "4 2\n10 4 10 15\n1 2\n4 3", "output": "0 0 1 2"}, {"input": "10 4\n5 4 1 5 4 3 7 1 2 5\n4 6\n2 1\n10 8\n3 5", "output": "5 4 0 5 3 3 9 0 2 5"}]
null
["*special", "data structures", "implementation"]
41
[{"input": "4 2\r\n10 4 10 15\r\n1 2\r\n4 3\r\n", "output": "0 0 1 2 \r\n"}, {"input": "10 4\r\n5 4 1 5 4 3 7 1 2 5\r\n4 6\r\n2 1\r\n10 8\r\n3 5\r\n", "output": "5 4 0 5 3 3 9 0 2 5 \r\n"}, {"input": "2 0\r\n3 1\r\n", "output": "1 0 \r\n"}, {"input": "2 0\r\n1 1\r\n", "output": "0 0 \r\n"}, {"input": "10 35\r\n322022227 751269818 629795150 369443545 344607287 250044294 476897672 184054549 986884572 917181121\r\n6 3\r\n7 3\r\n1 9\r\n7 9\r\n10 7\r\n3 4\r\n8 6\r\n7 4\r\n6 10\r\n7 2\r\n3 5\r\n6 9\r\n3 10\r\n8 7\r\n6 5\r\n8 1\r\n8 5\r\n1 7\r\n8 10\r\n8 2\r\n1 5\r\n10 4\r\n6 7\r\n4 6\r\n2 6\r\n5 4\r\n9 10\r\n9 2\r\n4 8\r\n5 9\r\n4 1\r\n3 2\r\n2 1\r\n4 2\r\n9 8\r\n", "output": "1 1 2 0 0 0 1 0 2 3 \r\n"}]
false
stdio
null
true
1298/E
978
F
Python 3
TESTS
0
30
0
195402570
n, m = map(int, input().split()) ability = list(map(int, input().split())) # создаем пустой массив с количеством менторов, равным количеству учеников num_mentors = [0] * n # пройдемся по каждому запросу и обновим значения массива num_mentors for i in range(m): x, y = map(int, input().split()) num_mentors[x - 1] += (ability[x - 1] > ability[y - 1]) num_mentors[y - 1] += (ability[y - 1] > ability[x - 1]) # выведем ответ print(' '.join(str(num_mentors[i]) for i in range(n)))
41
1,013
40,652,800
172377597
import sys from math import sqrt, gcd, factorial, ceil, floor, pi, inf from collections import deque, Counter, OrderedDict from heapq import heapify, heappush, heappop #sys.setrecursionlimit(10**6) #from functools import lru_cache #@lru_cache(None) #======================================================# input = lambda: sys.stdin.readline() I = lambda: int(input().strip()) S = lambda: input().strip() M = lambda: map(int,input().strip().split()) L = lambda: list(map(int,input().strip().split())) #======================================================# #======================================================# def primelist(): L = [False for i in range((10**10)+1)] primes = [False for i in range((10**10)+1)] for i in range(2,(10**10)+1): if not L[i]: primes[i]=True for j in range(i,(10**10)+1,i): L[j]=True return primes def isPrime(n): p = primelist() return p[n] #======================================================# def bst(arr,x): low,high = 0,len(arr)-1 ans = -1 while low<=high: mid = (low+high)//2 if arr[mid]==x: return mid elif arr[mid]<x: ans = mid low = mid+1 else: high = mid-1 return ans def factors(x): l1 = [] l2 = [] for i in range(1,int(sqrt(x))+1): if x%i==0: if (i*i)==x: l1.append(i) else: l1.append(i) l2.append(x//i) for i in range(len(l2)//2): l2[i],l2[len(l2)-1-i]=l2[len(l2)-1-i],l2[i] return l1+l2 #======================================================# n,k =M() a = L() b = [0 for i in range(n)] for _ in range(k): x,y = M() if a[x-1]<a[y-1]: b[y-1]-=1 elif a[x-1]>a[y-1]: b[x-1]-=1 p = [[a[i],i] for i in range(n)] p.sort() i=1 while i<n: if p[i][0]==p[i-1][0]: i+=1 else: break j=i-1 while i<n: if p[i][0]!=p[i-1][0]: j=i-1 b[p[i][1]]+=j+1 i+=1 print(*b)
Kotlin Heroes: Practice 3
ICPC
2,020
3
256
Mentors
In BerSoft $$$n$$$ programmers work, the programmer $$$i$$$ is characterized by a skill $$$r_i$$$. A programmer $$$a$$$ can be a mentor of a programmer $$$b$$$ if and only if the skill of the programmer $$$a$$$ is strictly greater than the skill of the programmer $$$b$$$ $$$(r_a > r_b)$$$ and programmers $$$a$$$ and $$$b$$$ are not in a quarrel. You are given the skills of each programmers and a list of $$$k$$$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $$$i$$$, find the number of programmers, for which the programmer $$$i$$$ can be a mentor.
The first line contains two integers $$$n$$$ and $$$k$$$ $$$(2 \le n \le 2 \cdot 10^5$$$, $$$0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$$$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $$$r_1, r_2, \dots, r_n$$$ $$$(1 \le r_i \le 10^{9})$$$, where $$$r_i$$$ equals to the skill of the $$$i$$$-th programmer. Each of the following $$$k$$$ lines contains two distinct integers $$$x$$$, $$$y$$$ $$$(1 \le x, y \le n$$$, $$$x \ne y)$$$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $$$x$$$ is in a quarrel with $$$y$$$ then $$$y$$$ is in a quarrel with $$$x$$$. Guaranteed, that for each pair $$$(x, y)$$$ there are no other pairs $$$(x, y)$$$ and $$$(y, x)$$$ in the input.
Print $$$n$$$ integers, the $$$i$$$-th number should be equal to the number of programmers, for which the $$$i$$$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input.
null
In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
[{"input": "4 2\n10 4 10 15\n1 2\n4 3", "output": "0 0 1 2"}, {"input": "10 4\n5 4 1 5 4 3 7 1 2 5\n4 6\n2 1\n10 8\n3 5", "output": "5 4 0 5 3 3 9 0 2 5"}]
null
["*special", "data structures", "implementation"]
41
[{"input": "4 2\r\n10 4 10 15\r\n1 2\r\n4 3\r\n", "output": "0 0 1 2 \r\n"}, {"input": "10 4\r\n5 4 1 5 4 3 7 1 2 5\r\n4 6\r\n2 1\r\n10 8\r\n3 5\r\n", "output": "5 4 0 5 3 3 9 0 2 5 \r\n"}, {"input": "2 0\r\n3 1\r\n", "output": "1 0 \r\n"}, {"input": "2 0\r\n1 1\r\n", "output": "0 0 \r\n"}, {"input": "10 35\r\n322022227 751269818 629795150 369443545 344607287 250044294 476897672 184054549 986884572 917181121\r\n6 3\r\n7 3\r\n1 9\r\n7 9\r\n10 7\r\n3 4\r\n8 6\r\n7 4\r\n6 10\r\n7 2\r\n3 5\r\n6 9\r\n3 10\r\n8 7\r\n6 5\r\n8 1\r\n8 5\r\n1 7\r\n8 10\r\n8 2\r\n1 5\r\n10 4\r\n6 7\r\n4 6\r\n2 6\r\n5 4\r\n9 10\r\n9 2\r\n4 8\r\n5 9\r\n4 1\r\n3 2\r\n2 1\r\n4 2\r\n9 8\r\n", "output": "1 1 2 0 0 0 1 0 2 3 \r\n"}]
false
stdio
null
true
311/B
311
B
Python 3
TESTS
1
46
102,400
224545646
n, m, p = map(int, input().split()) arr = [0, 0] + list(map(int, input().split())) for i in range(2, n+1): arr[i] += arr[i-1] ts = [] for i in range(m): h, t = map(int, input().split()) ts.append(t-arr[h]) ts.sort() tm = [0]*(m+1) for i in range(1, m+1): tm[i] = tm[i-1]+ts[i-1] dp = [float("inf")]*(m+1) def GetY(i, j): return dp[i] + tm[i]-dp[j]-tm[j] def GetX(i, j): return i-j def GetV(i, j): return dp[j]+(i-j)*ts[i-1]-(tm[i]-tm[j]) for j in range(p): dp[0] = 0 ndp = [float('inf')]*(m+1) dq = [0]*(m+2) h = t = 0 for i in range(1, m+1): while h > t and GetY(dq[t+1], dq[t]) <= GetX(dq[t+1], dq[t])*ts[i-1]: t += 1 ndp[i] = GetV(i, dq[t]) while h > t and GetY(i, dq[h])*GetX(dq[h-1], dq[h]) <= GetY(dq[h-1], dq[h])*GetX(i, dq[h]): h -= 1 h += 1 dq[h] = i dp = ndp[:] print(dp[m])
30
1,216
27,340,800
211419212
def solve(A, B, C): N = len(B) + 1 M = len(C) a = [0] * N for i in range(1, N): a[i] += a[i - 1] + B[i - 1] b = [0] * M s = [0] * M dp = [0] * M k = [0] * M for i in range(M): b[i] = C[i][1] - a[C[i][0] - 1] b.sort() for i in range(M - 1, -1, -1): b[i] -= b[0] for i in range(M): s[i] = s[i - 1] + b[i] if i > 0 else 0 for i in range(M): dp[i] = b[i] * (i + 1) - s[i] q = [0] * (10**5 + 9) for i in range(1, A): for j in range(M): k[j] = dp[j] + s[j] H = 0 T = -1 for j in range(M): while H < T and (k[q[H + 1]] - k[q[H]]) < b[j] * (q[H + 1] - q[H]): H += 1 while H < T and (k[q[T]] - k[q[T - 1]]) * (j - q[T]) > (k[j] - k[q[T]]) * (q[T] - q[T - 1]): T -= 1 T += 1 q[T] = j dp[j] = k[q[H]] + b[j] * (j - q[H]) - s[j] return str(dp[M - 1]) t = 1 # t = int(input()) # Uncomment this line to take input for 't' while t > 0: n, m, p = map(int, input().split()) arr = list(map(int, input().split())) brr = [] for _ in range(m): brr.append(list(map(int, input().split()))) print(solve(p, arr, brr)) t -= 1
Codeforces Round 185 (Div. 1)
CF
2,013
2
256
Cats Transport
Zxr960115 is owner of a large farm. He feeds m cute cats and employs p feeders. There's a straight road across the farm and n hills along the road, numbered from 1 to n from left to right. The distance between hill i and (i - 1) is di meters. The feeders live in hill 1. One day, the cats went out to play. Cat i went on a trip to hill hi, finished its trip at time ti, and then waited at hill hi for a feeder. The feeders must take all the cats. Each feeder goes straightly from hill 1 to n without waiting at a hill and takes all the waiting cats at each hill away. Feeders walk at a speed of 1 meter per unit time and are strong enough to take as many cats as they want. For example, suppose we have two hills (d2 = 1) and one cat that finished its trip at time 3 at hill 2 (h1 = 2). Then if the feeder leaves hill 1 at time 2 or at time 3, he can take this cat, but if he leaves hill 1 at time 1 he can't take it. If the feeder leaves hill 1 at time 2, the cat waits him for 0 time units, if the feeder leaves hill 1 at time 3, the cat waits him for 1 time units. Your task is to schedule the time leaving from hill 1 for each feeder so that the sum of the waiting time of all cats is minimized.
The first line of the input contains three integers n, m, p (2 ≤ n ≤ 105, 1 ≤ m ≤ 105, 1 ≤ p ≤ 100). The second line contains n - 1 positive integers d2, d3, ..., dn (1 ≤ di < 104). Each of the next m lines contains two integers hi and ti (1 ≤ hi ≤ n, 0 ≤ ti ≤ 109).
Output an integer, the minimum sum of waiting time of all cats. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
null
null
[{"input": "4 6 2\n1 3 5\n1 0\n2 1\n4 9\n1 10\n2 10\n3 12", "output": "3"}]
2,400
["data structures", "dp"]
30
[{"input": "4 6 2\r\n1 3 5\r\n1 0\r\n2 1\r\n4 9\r\n1 10\r\n2 10\r\n3 12\r\n", "output": "3\r\n"}]
false
stdio
null
true
311/B
311
B
Python 3
TESTS
1
61
204,800
22308495
n,m,p = [int(i) for i in input().split(' ')] d = [0]+[int(i) for i in input().split(' ')]#del dp = [0]*(m+1) pos = [0]*(n+1) a = [0]*(m+1) s = [0]*(m+1) M,B=[],[] def bad(l1,l2,l3): return (B[l3]-B[l1])*(M[l1]-M[l2])<=(B[l2]-B[l1])*(M[l1]-M[l3]) def addline(m,b): M.append(m) B.append(b) while len(M)>=3 and bad(len(M)-3,len(M)-2,len(M)-1): M.pop(-2) B.pop(-2) def query(x,ptr): if ptr>=len(M):p=len(M)-1 while ptr<len(M)-1 and M[ptr+1]*x+B[ptr+1]<=M[ptr]*x+B[ptr]: ptr+=1 return M[ptr]*x+B[ptr] def main(): global a, s, st, dp for i in range(1,n+1): pos[i] = sum(d[1:i]) for i in range(1,m+1): h,t = [int(j) for j in input().split(' ')] a[i] = t-pos[h] a=sorted(a) for i in range(1,m+1): s[i] = sum(a[1:i+1]) for i in range(1, m+1): dp[i]=i*(s[i]-s[i-1])-s[i] for i in range(2,p+1): ptr=0 for j in range(1,m+1):addline(-j,dp[j]+s[j]) for j in range(1,m+1): dp[j]=query(s[j]-s[j-1],ptr)+j*(s[j]-s[j-1])-s[j] print(dp[m]) main()
30
1,216
27,340,800
211419212
def solve(A, B, C): N = len(B) + 1 M = len(C) a = [0] * N for i in range(1, N): a[i] += a[i - 1] + B[i - 1] b = [0] * M s = [0] * M dp = [0] * M k = [0] * M for i in range(M): b[i] = C[i][1] - a[C[i][0] - 1] b.sort() for i in range(M - 1, -1, -1): b[i] -= b[0] for i in range(M): s[i] = s[i - 1] + b[i] if i > 0 else 0 for i in range(M): dp[i] = b[i] * (i + 1) - s[i] q = [0] * (10**5 + 9) for i in range(1, A): for j in range(M): k[j] = dp[j] + s[j] H = 0 T = -1 for j in range(M): while H < T and (k[q[H + 1]] - k[q[H]]) < b[j] * (q[H + 1] - q[H]): H += 1 while H < T and (k[q[T]] - k[q[T - 1]]) * (j - q[T]) > (k[j] - k[q[T]]) * (q[T] - q[T - 1]): T -= 1 T += 1 q[T] = j dp[j] = k[q[H]] + b[j] * (j - q[H]) - s[j] return str(dp[M - 1]) t = 1 # t = int(input()) # Uncomment this line to take input for 't' while t > 0: n, m, p = map(int, input().split()) arr = list(map(int, input().split())) brr = [] for _ in range(m): brr.append(list(map(int, input().split()))) print(solve(p, arr, brr)) t -= 1
Codeforces Round 185 (Div. 1)
CF
2,013
2
256
Cats Transport
Zxr960115 is owner of a large farm. He feeds m cute cats and employs p feeders. There's a straight road across the farm and n hills along the road, numbered from 1 to n from left to right. The distance between hill i and (i - 1) is di meters. The feeders live in hill 1. One day, the cats went out to play. Cat i went on a trip to hill hi, finished its trip at time ti, and then waited at hill hi for a feeder. The feeders must take all the cats. Each feeder goes straightly from hill 1 to n without waiting at a hill and takes all the waiting cats at each hill away. Feeders walk at a speed of 1 meter per unit time and are strong enough to take as many cats as they want. For example, suppose we have two hills (d2 = 1) and one cat that finished its trip at time 3 at hill 2 (h1 = 2). Then if the feeder leaves hill 1 at time 2 or at time 3, he can take this cat, but if he leaves hill 1 at time 1 he can't take it. If the feeder leaves hill 1 at time 2, the cat waits him for 0 time units, if the feeder leaves hill 1 at time 3, the cat waits him for 1 time units. Your task is to schedule the time leaving from hill 1 for each feeder so that the sum of the waiting time of all cats is minimized.
The first line of the input contains three integers n, m, p (2 ≤ n ≤ 105, 1 ≤ m ≤ 105, 1 ≤ p ≤ 100). The second line contains n - 1 positive integers d2, d3, ..., dn (1 ≤ di < 104). Each of the next m lines contains two integers hi and ti (1 ≤ hi ≤ n, 0 ≤ ti ≤ 109).
Output an integer, the minimum sum of waiting time of all cats. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
null
null
[{"input": "4 6 2\n1 3 5\n1 0\n2 1\n4 9\n1 10\n2 10\n3 12", "output": "3"}]
2,400
["data structures", "dp"]
30
[{"input": "4 6 2\r\n1 3 5\r\n1 0\r\n2 1\r\n4 9\r\n1 10\r\n2 10\r\n3 12\r\n", "output": "3\r\n"}]
false
stdio
null
true
13/B
13
B
PyPy 3
TESTS
1
842
9,523,200
109222376
import sys from collections import defaultdict from math import hypot # import logging # logging.root.setLevel(level=logging.INFO) def gcd(bigger, smaller): if smaller == 0: return bigger return gcd(smaller,bigger%smaller) cases = int(sys.stdin.readline().strip()) coordinates = list(sys.stdin.read().strip().split('\n')) for i in range(cases): points = defaultdict(list) vectors = [] length = [] for side_id,s in enumerate(coordinates[i:i+3]): x1,y1,x2,y2 = map(int,s.split()) points[(x1,y1)].append((side_id,1)) # 1 means the begin points points[(x2,y2)].append((side_id,-1)) v = (x2-x1,y2-y1) vectors.append(v) length.append(hypot(*v)) two_sides = None for p,side in points.items(): if len(side) == 2: two_sides = side common = p # no common points if two_sides is None: print("NO") continue # for side_id,is_begin in two_sides: v1 = vectors[two_sides[0][0]] v2 = vectors[two_sides[1][0]] cross_product = v1[0] * v2[0] + v1[1] * v2[1] # the angle > 90 if cross_product*two_sides[0][1] * two_sides[1][1] <= 0: print("NO") continue cut_side = [0,1,2] cut_side.remove(two_sides[0][0]) cut_side.remove(two_sides[1][0]) cut_side = cut_side[0] print("YES")
2
560
0
179032385
import math r = lambda: map(int,input().split()) def cross(a, b): return a[0] * b[1] - a[1] * b[0] def dot(a, b): return a[0] * b[0] + a[1] * b[1] def on_line(a, b): return dot(a, b) > 0 and cross(a, b) == 0 and abs(b[0]) <= abs(5 * a[0]) <= abs(4 * b[0]) and abs(b[1]) <= abs(5 * a[1]) <= abs(4 * b[1]) def is_A(a, b, c): a, b = set(a), set(b) if len(a & b) != 1: return False p1, = a & b p2, p3 = a ^ b v1 = (p2[0] - p1[0], p2[1] - p1[1]) v2 = (p3[0] - p1[0], p3[1] - p1[1]) if dot(v1, v2) < 0 or abs(cross(v1, v2)) == 0: return False v3 = (c[0][0] - p1[0], c[0][1] - p1[1]) v4 = (c[1][0] - p1[0], c[1][1] - p1[1]) return (on_line(v3, v1) and on_line(v4, v2)) or (on_line(v3, v2) and on_line(v4, v1)) for i in range(int(input())): xa1, ya1, xa2, ya2 = r() xb1, yb1, xb2, yb2 = r() xc1, yc1, xc2, yc2 = r() a, b, c = [(xa1, ya1), (xa2, ya2)], [(xb1, yb1), (xb2, yb2)], [(xc1, yc1), (xc2, yc2)] print ("YES" if is_A(a, b, c) or is_A(a, c, b) or is_A(b, c, a) else "NO")
Codeforces Beta Round 13
ICPC
2,010
1
64
Letter A
Little Petya learns how to write. The teacher gave pupils the task to write the letter A on the sheet of paper. It is required to check whether Petya really had written the letter A. You are given three segments on the plane. They form the letter A if the following conditions hold: - Two segments have common endpoint (lets call these segments first and second), while the third segment connects two points on the different segments. - The angle between the first and the second segments is greater than 0 and do not exceed 90 degrees. - The third segment divides each of the first two segments in proportion not less than 1 / 4 (i.e. the ratio of the length of the shortest part to the length of the longest part is not less than 1 / 4).
The first line contains one integer t (1 ≤ t ≤ 10000) — the number of test cases to solve. Each case consists of three lines. Each of these three lines contains four space-separated integers — coordinates of the endpoints of one of the segments. All coordinates do not exceed 108 by absolute value. All segments have positive length.
Output one line for each test case. Print «YES» (without quotes), if the segments form the letter A and «NO» otherwise.
null
null
[{"input": "3\n4 4 6 0\n4 1 5 2\n4 0 4 4\n0 0 0 6\n0 6 2 -4\n1 1 0 1\n0 0 0 5\n0 5 2 -1\n1 2 0 1", "output": "YES\nNO\nYES"}]
2,000
["geometry", "implementation"]
2
[{"input": "3\r\n4 4 6 0\r\n4 1 5 2\r\n4 0 4 4\r\n0 0 0 6\r\n0 6 2 -4\r\n1 1 0 1\r\n0 0 0 5\r\n0 5 2 -1\r\n1 2 0 1\r\n", "output": "YES\r\nNO\r\nYES\r\n"}]
false
stdio
null
true
13/B
13
B
Python 3
TESTS
1
686
6,963,200
85126177
n=int(input()) dct={} def check(): for i in dct[1]: for j in dct[2]: if(i==j): return [1,2] for i in dct[1]: for j in dct[3]: if(i==j): return [1,3] for i in dct[2]: for j in dct[3]: if (i == j): return [1, 3] else: return [] def prop(a,b,id1): sm=0 (x1, y1), (x2, y2)=dct[a] for i in dct[b]: if(x2!=x1): z=((y2-y1)/(x2-x1))*(i[0]-x1) z1=i[1]-y1 if(z==z1): x3,y3=i sm+=1 else: if(i[0]==x1): x3, y3 = i sm += 1 if(sm!=1): return False p1=(dct[a][id1][0]-x3)**2+(dct[a][id1][1]-y3)**2 p2=(dct[a][1-id1][0]-x3)**2+(dct[a][1-id1][1]-y3)**2 if((min(p1,p2)/max(p1,p2))**(.5)<.25 ): return False return True def angle(a,b,id1,id2): sm1=1-id1 sm2=1-id2 x1=dct[a][sm1][0]-dct[a][id1][0] y1=dct[a][sm1][1]-dct[a][id1][1] x2 = dct[b][sm2][0] - dct[b][id2][0] y2 = dct[b][sm2][1] - dct[b][id2][1] if(x1<x2): p1=x1*y2-x2*y1 else: p1 = x2 * y1 - x1 * y2 p2=((x1**2+y1**2)*(x2**2+y2**2))**(.5) if(p1/p2<=0 or p1/p2 >=1 ): # print("p1/p2" ,p1/p2) return False else: return True for _ in range(n): x1,y1,x2,y2=map(int,input().split()) dct[1]=[[x1,y1],[x2,y2]] x1, y1, x2, y2 = map(int, input().split()) dct[2] = [[x1, y1], [x2, y2]] x1, y1, x2, y2 = map(int, input().split()) dct[3] = [[x1, y1], [x2, y2]] # print(dct) x=check() if(x==[]): print("NO") continue if(dct[x[0]][0]==dct[x[1]][0]): id1=0;id2=0 elif(dct[x[0]][1]==dct[x[1]][1]): id1 = 1 id2 = 1 elif (dct[x[0]][1] == dct[x[1]][0]): id1 = 1 id2 = 0 else: id1=0 id2=1 z=6-x[0]-x[1] if(prop(x[0],z,id1)): if(prop(x[1],z,id2)): if(angle(x[0],x[1],id1,id2)): print('YES') else: print("NO") else: print("NO") else: print("NO")
2
716
307,200
78333419
#13B - Letter A import math r = lambda: map(int,input().split()) #Function to check if the lines cross def cross(a, b): return a[0] * b[1] - a[1] * b[0] def dot(a, b): return a[0] * b[0] + a[1] * b[1] # def l(v): # return math.hypot(*v) def on_line(a, b): return dot(a, b) > 0 and cross(a, b) == 0 and abs(b[0]) <= abs(5 * a[0]) <= abs(4 * b[0]) and abs(b[1]) <= abs(5 * a[1]) <= abs(4 * b[1]) def is_A(a, b, c): a, b = set(a), set(b) if len(a & b) != 1: return False p1, = a & b p2, p3 = a ^ b v1 = (p2[0] - p1[0], p2[1] - p1[1]) v2 = (p3[0] - p1[0], p3[1] - p1[1]) if dot(v1, v2) < 0 or abs(cross(v1, v2)) == 0: return False v3 = (c[0][0] - p1[0], c[0][1] - p1[1]) v4 = (c[1][0] - p1[0], c[1][1] - p1[1]) return (on_line(v3, v1) and on_line(v4, v2)) or (on_line(v3, v2) and on_line(v4, v1)) for i in range(int(input())): xa1, ya1, xa2, ya2 = r() xb1, yb1, xb2, yb2 = r() xc1, yc1, xc2, yc2 = r() a, b, c = [(xa1, ya1), (xa2, ya2)], [(xb1, yb1), (xb2, yb2)], [(xc1, yc1), (xc2, yc2)] print ("YES" if is_A(a, b, c) or is_A(a, c, b) or is_A(b, c, a) else "NO")
Codeforces Beta Round 13
ICPC
2,010
1
64
Letter A
Little Petya learns how to write. The teacher gave pupils the task to write the letter A on the sheet of paper. It is required to check whether Petya really had written the letter A. You are given three segments on the plane. They form the letter A if the following conditions hold: - Two segments have common endpoint (lets call these segments first and second), while the third segment connects two points on the different segments. - The angle between the first and the second segments is greater than 0 and do not exceed 90 degrees. - The third segment divides each of the first two segments in proportion not less than 1 / 4 (i.e. the ratio of the length of the shortest part to the length of the longest part is not less than 1 / 4).
The first line contains one integer t (1 ≤ t ≤ 10000) — the number of test cases to solve. Each case consists of three lines. Each of these three lines contains four space-separated integers — coordinates of the endpoints of one of the segments. All coordinates do not exceed 108 by absolute value. All segments have positive length.
Output one line for each test case. Print «YES» (without quotes), if the segments form the letter A and «NO» otherwise.
null
null
[{"input": "3\n4 4 6 0\n4 1 5 2\n4 0 4 4\n0 0 0 6\n0 6 2 -4\n1 1 0 1\n0 0 0 5\n0 5 2 -1\n1 2 0 1", "output": "YES\nNO\nYES"}]
2,000
["geometry", "implementation"]
2
[{"input": "3\r\n4 4 6 0\r\n4 1 5 2\r\n4 0 4 4\r\n0 0 0 6\r\n0 6 2 -4\r\n1 1 0 1\r\n0 0 0 5\r\n0 5 2 -1\r\n1 2 0 1\r\n", "output": "YES\r\nNO\r\nYES\r\n"}]
false
stdio
null
true
958/C1
958
C1
Python 3
TESTS
8
77
1,740,800
120957273
# Problem: C1. Encryption (easy) # Contest: Codeforces - Helvetic Coding Contest 2018 online mirror (teams allowed, unrated) # URL: https://codeforces.com/contest/958/problem/C1 # Memory Limit: 256 MB # Time Limit: 1000 ms # Powered by CP Editor (https://github.com/cpeditor/cpeditor) from sys import stdin def get_ints(): return list(map(int, stdin.readline().strip().split())) n,p = get_ints() ar = get_ints() k = 0 s = 0 mx = 0 for i in range(n-1): s+=ar[i] # print(s%p) if s%p > mx: k = i mxx = 0 ss = 0 j = 0 for i in range(n-1,0,-1): ss+=ar[i] # print(s%p) if ss%p > mxx: j = i # print(k)/ a = sum(ar[:k])%p b=sum(ar[k:])%p c = sum(ar[:j])%p d=sum(ar[j:])%p print(max(a+b,c+d))
27
77
11,468,800
198899912
n, p = map(int, input().split()) a = [int(x) for x in input().split()] s = sum(a) ans = -1; psum = 0 for i in a: psum += i ans = max(ans, psum % p + (s - psum) % p) print(ans)
Helvetic Coding Contest 2018 online mirror (teams allowed, unrated)
ICPC
2,018
1
256
Encryption (easy)
Rebel spy Heidi has just obtained the plans for the Death Star from the Empire and, now on her way to safety, she is trying to break the encryption of the plans (of course they are encrypted – the Empire may be evil, but it is not stupid!). The encryption has several levels of security, and here is how the first one looks. Heidi is presented with a screen that shows her a sequence of integers A and a positive integer p. She knows that the encryption code is a single number S, which is defined as follows: Define the score of X to be the sum of the elements of X modulo p. Heidi is given a sequence A that consists of N integers, and also given an integer p. She needs to split A into 2 parts such that: - Each part contains at least 1 element of A, and each part consists of contiguous elements of A. - The two parts do not overlap. - The total sum S of the scores of those two parts is maximized. This is the encryption code. Output the sum S, which is the encryption code.
The first line of the input contains two space-separated integer N and p (2 ≤ N ≤ 100 000, 2 ≤ p ≤ 10 000) – the number of elements in A, and the modulo for computing scores, respectively. The second line contains N space-separated integers which are the elements of A. Each integer is from the interval [1, 1 000 000].
Output the number S as described in the problem statement.
null
In the first example, the score is maximized if the input sequence is split into two parts as (3, 4), (7, 2). It gives the total score of $$(3+4\mod10)+(7+2\mod10)=16$$. In the second example, the score is maximized if the first part consists of the first three elements, and the second part consists of the rest. Then, the score is $$( 1 6 + 3 + 2 4 \mod 1 2 ) + ( 1 3 + 9 + 8 + 7 + 5 + 1 2 \mod 1 2 ) = 7 + 6 = 1 3$$.
[{"input": "4 10\n3 4 7 2", "output": "16"}, {"input": "10 12\n16 3 24 13 9 8 7 5 12 12", "output": "13"}]
1,200
["brute force"]
27
[{"input": "4 10\r\n3 4 7 2\r\n", "output": "16\r\n"}, {"input": "10 12\r\n16 3 24 13 9 8 7 5 12 12\r\n", "output": "13\r\n"}, {"input": "2 2\r\n9 9\r\n", "output": "2\r\n"}, {"input": "2 2\r\n8 8\r\n", "output": "0\r\n"}, {"input": "5 50\r\n1 1 1 1 1\r\n", "output": "5\r\n"}, {"input": "5 50\r\n100 150 200 100 50\r\n", "output": "0\r\n"}]
false
stdio
null
true
311/B
311
B
Python 3
TESTS
1
78
409,600
104983919
import sys import math from collections import deque class line: def __init__ (self , a , b): self.a = a self.b = b def inter (self , other): return math.ceil((self.b - other.b) / (other.a - self.a)) def getval(self , x): return self.a * x + self.b class pair: def __init__ (self , first , second): self.first = first self.second = second n,m,p = map(int , sys.stdin.readline().strip().split()) h = list(map(int , sys.stdin.readline().strip().split())) for i in range(1 , n - 1): h[i] += h[i - 1] cat = [] for i in range(m): hill,t = map(int , sys.stdin.readline().strip().split()) if hill == 1: cat.append(t) else: cat.append(t - h[hill - 2]) cat.sort() #print("h : ") #for i in h: # print(i) #print("cat : ") #for i in cat: # print(i) #print("pref_sum : ") #for i in pref_sum: # print(i) pref_sum = [cat[0]] for i in range(m - 1): pref_sum.append(cat[i + 1] + pref_sum[i]) dp = [(10 ** 18 if i > 0 else 0) for i in range(m)] for t in range(p): dq = deque([]) lanjut = list([]) for i in range (m): if i == 0: dq.append(pair(line(0 , 0) , 0)) else: now = line(-i , dp[i - 1] + pref_sum[i - 1]) while len(dq) > 0 and dq[-1].first.inter(now) <= dq[-1].second: dq.pop() if len(dq) > 0: dq.append(pair(now , dq[-1].first.inter(now))) else: dq.append(pair(now , 0)) while len(dq) > 1 and dq[1].second <= cat[i]: dq.popleft() lanjut.append(dq[0].first.getval(cat[i]) - (0 if i == 0 else pref_sum[i - 1]) + cat[i] * i) dp = lanjut print(dp[-1])
30
1,216
27,340,800
211419212
def solve(A, B, C): N = len(B) + 1 M = len(C) a = [0] * N for i in range(1, N): a[i] += a[i - 1] + B[i - 1] b = [0] * M s = [0] * M dp = [0] * M k = [0] * M for i in range(M): b[i] = C[i][1] - a[C[i][0] - 1] b.sort() for i in range(M - 1, -1, -1): b[i] -= b[0] for i in range(M): s[i] = s[i - 1] + b[i] if i > 0 else 0 for i in range(M): dp[i] = b[i] * (i + 1) - s[i] q = [0] * (10**5 + 9) for i in range(1, A): for j in range(M): k[j] = dp[j] + s[j] H = 0 T = -1 for j in range(M): while H < T and (k[q[H + 1]] - k[q[H]]) < b[j] * (q[H + 1] - q[H]): H += 1 while H < T and (k[q[T]] - k[q[T - 1]]) * (j - q[T]) > (k[j] - k[q[T]]) * (q[T] - q[T - 1]): T -= 1 T += 1 q[T] = j dp[j] = k[q[H]] + b[j] * (j - q[H]) - s[j] return str(dp[M - 1]) t = 1 # t = int(input()) # Uncomment this line to take input for 't' while t > 0: n, m, p = map(int, input().split()) arr = list(map(int, input().split())) brr = [] for _ in range(m): brr.append(list(map(int, input().split()))) print(solve(p, arr, brr)) t -= 1
Codeforces Round 185 (Div. 1)
CF
2,013
2
256
Cats Transport
Zxr960115 is owner of a large farm. He feeds m cute cats and employs p feeders. There's a straight road across the farm and n hills along the road, numbered from 1 to n from left to right. The distance between hill i and (i - 1) is di meters. The feeders live in hill 1. One day, the cats went out to play. Cat i went on a trip to hill hi, finished its trip at time ti, and then waited at hill hi for a feeder. The feeders must take all the cats. Each feeder goes straightly from hill 1 to n without waiting at a hill and takes all the waiting cats at each hill away. Feeders walk at a speed of 1 meter per unit time and are strong enough to take as many cats as they want. For example, suppose we have two hills (d2 = 1) and one cat that finished its trip at time 3 at hill 2 (h1 = 2). Then if the feeder leaves hill 1 at time 2 or at time 3, he can take this cat, but if he leaves hill 1 at time 1 he can't take it. If the feeder leaves hill 1 at time 2, the cat waits him for 0 time units, if the feeder leaves hill 1 at time 3, the cat waits him for 1 time units. Your task is to schedule the time leaving from hill 1 for each feeder so that the sum of the waiting time of all cats is minimized.
The first line of the input contains three integers n, m, p (2 ≤ n ≤ 105, 1 ≤ m ≤ 105, 1 ≤ p ≤ 100). The second line contains n - 1 positive integers d2, d3, ..., dn (1 ≤ di < 104). Each of the next m lines contains two integers hi and ti (1 ≤ hi ≤ n, 0 ≤ ti ≤ 109).
Output an integer, the minimum sum of waiting time of all cats. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
null
null
[{"input": "4 6 2\n1 3 5\n1 0\n2 1\n4 9\n1 10\n2 10\n3 12", "output": "3"}]
2,400
["data structures", "dp"]
30
[{"input": "4 6 2\r\n1 3 5\r\n1 0\r\n2 1\r\n4 9\r\n1 10\r\n2 10\r\n3 12\r\n", "output": "3\r\n"}]
false
stdio
null
true
311/B
311
B
Python 3
TESTS
1
92
0
39120539
n, m, p = map(int, input().split()) d = [0, 0] sum = 0 for x in input().split() : d.append(int(x) + d[-1]) v = [-10000000000] for i in range (m) : h, t = map(int, input().split()) v.append(t - d[h]) v = sorted(v) pre = [0] for i in range (1, m + 1) : pre.append(pre[-1] + (v[i] - v[i - 1]) * (i - 1)) dp = [i for i in pre] tmp = [0 for i in pre] print(3)
30
1,216
27,340,800
211419212
def solve(A, B, C): N = len(B) + 1 M = len(C) a = [0] * N for i in range(1, N): a[i] += a[i - 1] + B[i - 1] b = [0] * M s = [0] * M dp = [0] * M k = [0] * M for i in range(M): b[i] = C[i][1] - a[C[i][0] - 1] b.sort() for i in range(M - 1, -1, -1): b[i] -= b[0] for i in range(M): s[i] = s[i - 1] + b[i] if i > 0 else 0 for i in range(M): dp[i] = b[i] * (i + 1) - s[i] q = [0] * (10**5 + 9) for i in range(1, A): for j in range(M): k[j] = dp[j] + s[j] H = 0 T = -1 for j in range(M): while H < T and (k[q[H + 1]] - k[q[H]]) < b[j] * (q[H + 1] - q[H]): H += 1 while H < T and (k[q[T]] - k[q[T - 1]]) * (j - q[T]) > (k[j] - k[q[T]]) * (q[T] - q[T - 1]): T -= 1 T += 1 q[T] = j dp[j] = k[q[H]] + b[j] * (j - q[H]) - s[j] return str(dp[M - 1]) t = 1 # t = int(input()) # Uncomment this line to take input for 't' while t > 0: n, m, p = map(int, input().split()) arr = list(map(int, input().split())) brr = [] for _ in range(m): brr.append(list(map(int, input().split()))) print(solve(p, arr, brr)) t -= 1
Codeforces Round 185 (Div. 1)
CF
2,013
2
256
Cats Transport
Zxr960115 is owner of a large farm. He feeds m cute cats and employs p feeders. There's a straight road across the farm and n hills along the road, numbered from 1 to n from left to right. The distance between hill i and (i - 1) is di meters. The feeders live in hill 1. One day, the cats went out to play. Cat i went on a trip to hill hi, finished its trip at time ti, and then waited at hill hi for a feeder. The feeders must take all the cats. Each feeder goes straightly from hill 1 to n without waiting at a hill and takes all the waiting cats at each hill away. Feeders walk at a speed of 1 meter per unit time and are strong enough to take as many cats as they want. For example, suppose we have two hills (d2 = 1) and one cat that finished its trip at time 3 at hill 2 (h1 = 2). Then if the feeder leaves hill 1 at time 2 or at time 3, he can take this cat, but if he leaves hill 1 at time 1 he can't take it. If the feeder leaves hill 1 at time 2, the cat waits him for 0 time units, if the feeder leaves hill 1 at time 3, the cat waits him for 1 time units. Your task is to schedule the time leaving from hill 1 for each feeder so that the sum of the waiting time of all cats is minimized.
The first line of the input contains three integers n, m, p (2 ≤ n ≤ 105, 1 ≤ m ≤ 105, 1 ≤ p ≤ 100). The second line contains n - 1 positive integers d2, d3, ..., dn (1 ≤ di < 104). Each of the next m lines contains two integers hi and ti (1 ≤ hi ≤ n, 0 ≤ ti ≤ 109).
Output an integer, the minimum sum of waiting time of all cats. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
null
null
[{"input": "4 6 2\n1 3 5\n1 0\n2 1\n4 9\n1 10\n2 10\n3 12", "output": "3"}]
2,400
["data structures", "dp"]
30
[{"input": "4 6 2\r\n1 3 5\r\n1 0\r\n2 1\r\n4 9\r\n1 10\r\n2 10\r\n3 12\r\n", "output": "3\r\n"}]
false
stdio
null
true
237/B
237
B
PyPy 3-64
TESTS
1
92
0
175841068
import sys input = sys.stdin.readline n = int(input()) w = list(map(int, input().split())) g = [list(map(int, input().split())) for _ in range(n)] s = dict() d = [[0 for j in range(w[i])] for i in range(n)] c = 1 for i in range(n): for j in range(w[i]): s[g[i][j]] = (i+1, j+1) d[i][j] = c c += 1 print(sum(w)) for i in range(n): for j in range(w[i]): print(*s[d[i][j]], i+1, j+1) s[g[i][j]] = s[d[i][j]]
55
186
6,963,200
216770114
n = int(input()) c = list(map(int, input().split())) a = [] for i in range(n): a.append(list(map(int, input().split()))) d = dict() for i in range(n): for j in range(c[i]): d[a[i][j]] = [i + 1, j + 1] ans = [] tek = 0 for i in range(n): for j in range(c[i]): tek += 1 if a[i][j] != tek: ans.append([i + 1, j + 1, d[tek][0], d[tek][1]]) d[a[i][j]] = d[tek] a[i][j], a[d[tek][0] - 1][d[tek][1] - 1] = a[d[tek][0] - 1][d[tek][1] - 1], a[i][j] print(len(ans)) for i in ans: print(*i)# 1690927075.7984898
Codeforces Round 147 (Div. 2)
CF
2,012
2
256
Young Table
You've got table a, consisting of n rows, numbered from 1 to n. The i-th line of table a contains ci cells, at that for all i (1 < i ≤ n) holds ci ≤ ci - 1. Let's denote s as the total number of cells of table a, that is, $$s = \sum_{i=1}^{n} c_i$$. We know that each cell of the table contains a single integer from 1 to s, at that all written integers are distinct. Let's assume that the cells of the i-th row of table a are numbered from 1 to ci, then let's denote the number written in the j-th cell of the i-th row as ai, j. Your task is to perform several swap operations to rearrange the numbers in the table so as to fulfill the following conditions: 1. for all i, j (1 < i ≤ n; 1 ≤ j ≤ ci) holds ai, j > ai - 1, j; 2. for all i, j (1 ≤ i ≤ n; 1 < j ≤ ci) holds ai, j > ai, j - 1. In one swap operation you are allowed to choose two different cells of the table and swap the recorded there numbers, that is the number that was recorded in the first of the selected cells before the swap, is written in the second cell after it. Similarly, the number that was recorded in the second of the selected cells, is written in the first cell after the swap. Rearrange the numbers in the required manner. Note that you are allowed to perform any number of operations, but not more than s. You do not have to minimize the number of operations.
The first line contains a single integer n (1 ≤ n ≤ 50) that shows the number of rows in the table. The second line contains n space-separated integers ci (1 ≤ ci ≤ 50; ci ≤ ci - 1) — the numbers of cells on the corresponding rows. Next n lines contain table а. The i-th of them contains ci space-separated integers: the j-th integer in this line represents ai, j. It is guaranteed that all the given numbers ai, j are positive and do not exceed s. It is guaranteed that all ai, j are distinct.
In the first line print a single integer m (0 ≤ m ≤ s), representing the number of performed swaps. In the next m lines print the description of these swap operations. In the i-th line print four space-separated integers xi, yi, pi, qi (1 ≤ xi, pi ≤ n; 1 ≤ yi ≤ cxi; 1 ≤ qi ≤ cpi). The printed numbers denote swapping the contents of cells axi, yi and api, qi. Note that a swap operation can change the contents of distinct table cells. Print the swaps in the order, in which they should be executed.
null
null
[{"input": "3\n3 2 1\n4 3 5\n6 1\n2", "output": "2\n1 1 2 2\n2 1 3 1"}, {"input": "1\n4\n4 3 2 1", "output": "2\n1 1 1 4\n1 2 1 3"}]
1,500
["implementation", "sortings"]
55
[{"input": "3\r\n3 2 1\r\n4 3 5\r\n6 1\r\n2\r\n", "output": "2\r\n1 1 2 2\r\n2 1 3 1\r\n"}, {"input": "1\r\n4\r\n4 3 2 1\r\n", "output": "2\r\n1 1 1 4\r\n1 2 1 3\r\n"}, {"input": "5\r\n4 4 3 3 1\r\n14 13 4 15\r\n11 1 2 5\r\n7 6 10\r\n8 9 3\r\n12\r\n", "output": "13\r\n1 1 2 2\r\n1 2 2 3\r\n1 3 4 3\r\n1 4 4 3\r\n2 1 2 4\r\n2 2 3 2\r\n2 3 3 1\r\n2 4 4 1\r\n3 1 4 2\r\n3 2 3 3\r\n3 3 4 1\r\n4 1 5 1\r\n4 3 5 1\r\n"}, {"input": "2\r\n8 6\r\n1 2 3 13 10 4 11 7\r\n9 12 8 5 14 6\r\n", "output": "7\r\n1 4 1 6\r\n1 5 2 4\r\n1 6 2 6\r\n1 7 1 8\r\n1 8 2 3\r\n2 2 2 4\r\n2 5 2 6\r\n"}, {"input": "6\r\n10 9 7 6 4 3\r\n18 20 29 19 5 28 31 30 32 15\r\n38 33 11 8 39 2 6 9 3\r\n13 37 27 24 26 1 17\r\n36 10 35 21 7 16\r\n22 23 4 12\r\n34 25 14\r\n", "output": "33\n1 1 3 6\n1 2 2 6\n1 3 2 9\n1 4 5 3\n1 6 2 7\n1 7 4 5\n1 8 2 4\n1 9 2 8\n1 10 4 2\n2 1 2 3\n2 2 5 4\n2 3 3 1\n2 4 6 3\n2 5 4 2\n2 6 4 6\n2 7 3 7\n2 8 3 6\n2 9 5 3\n3 1 4 6\n3 2 4 4\n3 3 5 1\n3 4 5 2\n3 5 5 2\n3 6 6 2\n3 7 5 2\n4 1 5 1\n4 2 5 2\n4 3 5 3\n4 4 6 3\n4 6 6 2\n5 1 5 4\n5 2 6 1\n6 1 6 3\n"}, {"input": "8\r\n2 2 2 2 1 1 1 1\r\n10 9\r\n11 5\r\n7 3\r\n2 6\r\n12\r\n1\r\n8\r\n4\r\n", "output": "9\r\n1 1 6 1\r\n1 2 4 1\r\n2 1 3 2\r\n2 2 8 1\r\n3 1 8 1\r\n3 2 4 2\r\n4 1 8 1\r\n4 2 7 1\r\n5 1 8 1\r\n"}, {"input": "4\r\n3 3 3 2\r\n6 3 11\r\n10 7 1\r\n9 4 5\r\n2 8\r\n", "output": "8\r\n1 1 2 3\r\n1 2 4 1\r\n1 3 4 1\r\n2 1 3 2\r\n2 2 3 3\r\n3 1 3 3\r\n3 2 4 2\r\n4 1 4 2\r\n"}, {"input": "1\r\n1\r\n1\r\n", "output": "0\r\n"}, {"input": "2\r\n35 7\r\n6 8 35 9 28 25 10 41 33 39 19 24 5 12 30 40 18 2 4 11 32 13 31 21 14 27 3 34 37 16 17 29 1 42 36\r\n20 23 38 15 26 7 22\r\n", "output": "39\n1 1 1 33\n1 2 1 18\n1 3 1 27\n1 4 1 19\n1 5 1 13\n1 6 1 33\n1 7 2 6\n1 8 1 18\n1 9 1 19\n1 10 2 6\n1 11 1 20\n1 12 1 14\n1 13 1 22\n1 14 1 25\n1 15 2 4\n1 16 1 30\n1 17 1 31\n1 18 1 31\n1 19 1 20\n1 20 2 1\n1 21 1 24\n1 22 2 7\n1 23 2 2\n1 24 1 25\n1 25 1 33\n1 26 2 5\n1 27 2 5\n1 28 2 7\n1 29 1 32\n1 30 2 4\n1 31 2 2\n1 32 1 33\n1 33 2 1\n1 34 2 7\n1 35 2 5\n2 1 2 5\n2 2 2 5\n2 4 2 6\n2 5 2 6\n"}, {"input": "3\r\n36 28 14\r\n46 15 35 60 41 65 73 33 18 20 68 22 28 23 67 44 2 24 21 51 37 3 48 69 12 50 32 72 45 53 17 47 56 52 29 57\r\n8 62 10 19 26 64 7 49 6 25 34 63 74 31 14 43 30 4 11 76 16 55 36 5 70 61 77 27\r\n38 40 1 78 58 42 66 71 75 59 54 9 39 13\r\n", "output": "73\n1 1 3 3\n1 2 1 17\n1 3 1 22\n1 4 2 18\n1 5 2 24\n1 6 2 9\n1 7 2 7\n1 8 2 1\n1 9 3 12\n1 10 2 3\n1 11 2 19\n1 12 1 25\n1 13 3 14\n1 14 2 15\n1 15 1 17\n1 16 2 21\n1 17 1 31\n1 18 3 12\n1 19 2 4\n1 20 2 3\n1 21 2 4\n1 22 1 25\n1 23 2 15\n1 24 3 12\n1 25 2 10\n1 26 2 5\n1 27 2 28\n1 28 3 14\n1 29 1 35\n1 30 2 17\n1 31 2 14\n1 32 2 28\n1 33 2 1\n1 34 2 11\n1 35 2 10\n1 36 2 23\n2 1 2 4\n2 2 3 1\n2 3 3 13\n2 4 3 2\n2 5 2 24\n2 6 3 6\n2 7 2 16\n2 8 2 21\n2 9 2 10\n2 10 3 3\n2 11 2 28\n2 12 2 15\n2 13 2 21\n2 14 2 24\n2 15 3 13\n2 16 2 28\n2 18 3 11\n2 19 2 22\n2 20 3 2\n2 21 2 23\n2 22 3 5\n2 23 3 10\n2 24 3 11\n2 25 2 26\n2 26 3 1\n2 27 3 13\n2 28 3 6\n3 1 3 3\n3 2 3 7\n3 3 3 11\n3 4 3 5\n3 5 3 12\n3 6 3 11\n3 7 3 8\n3 8 3 14\n3 9 3 11\n3 12 3 14\n"}, {"input": "5\r\n5 2 2 2 1\r\n1 3 4 5 12\r\n2 6\r\n8 9\r\n7 10\r\n11\r\n", "output": "8\r\n1 2 2 1\r\n1 3 2 1\r\n1 4 2 1\r\n1 5 2 1\r\n2 1 2 2\r\n2 2 4 1\r\n4 1 4 2\r\n4 2 5 1\r\n"}, {"input": "5\r\n5 4 3 2 1\r\n1 2 3 4 5\r\n6 7 8 9\r\n10 11 12\r\n13 14\r\n15\r\n", "output": "0\r\n"}, {"input": "1\r\n1\r\n1\r\n", "output": "0\r\n"}, {"input": "4\r\n4 4 2 2\r\n1 2 3 4\r\n5 6 7 8\r\n9 10\r\n11 12\r\n", "output": "0\r\n"}, {"input": "1\r\n50\r\n1 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\r\n", "output": "0\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path) as f: lines = f.read().splitlines() ptr = 0 n = int(lines[ptr]) ptr += 1 c = list(map(int, lines[ptr].split())) ptr += 1 s = sum(c) table = [] for i in range(n): row = list(map(int, lines[ptr].split())) table.append(row) ptr += 1 original_numbers = set() for row in table: for num in row: original_numbers.add(num) with open(submission_path) as f: sub_lines = f.read().splitlines() if not sub_lines: print(0) return try: m = int(sub_lines[0].strip()) except: print(0) return if m < 0 or m > s: print(0) return if len(sub_lines) < m + 1: print(0) return swaps = [] for line in sub_lines[1:1 + m]: parts = line.strip().split() if len(parts) != 4: print(0) return try: xi, yi, pi, qi = map(int, parts) xi -= 1 yi -= 1 pi -= 1 qi -= 1 except: print(0) return swaps.append((xi, yi, pi, qi)) current_table = [row.copy() for row in table] for swap in swaps: xi, yi, pi, qi = swap if xi < 0 or xi >= n or pi < 0 or pi >= n: print(0) return if yi < 0 or yi >= c[xi] or qi < 0 or qi >= c[pi]: print(0) return if (xi, yi) == (pi, qi): print(0) return current_table[xi][yi], current_table[pi][qi] = current_table[pi][qi], current_table[xi][yi] for row in current_table: for j in range(1, len(row)): if row[j] <= row[j - 1]: print(0) return for i in range(1, n): for j in range(len(current_table[i])): if current_table[i][j] <= current_table[i - 1][j]: print(0) return current_numbers = set() for row in current_table: for num in row: current_numbers.add(num) if current_numbers != original_numbers: print(0) return print(1) if __name__ == "__main__": main()
true
58/D
58
D
PyPy 3-64
TESTS
0
92
0
186355070
# Read input n = int(input()) cities = [] for i in range(n): cities.append(input()) d = input() # Sort the list of cities cities.sort() # Divide the list into two equal halves half = n // 2 cities1 = cities[:half] cities2 = cities[half:] # Zip the two halves together to create a list of pairs of cities calendar = list(zip(cities1, cities2)) # Join each pair of cities with the separator character 'd' and store the resulting calendar lines in a new list calendar_lines = [] for line in calendar: calendar_lines.append(line[0] + d + line[1]) # Sort the list of calendar lines lexicographically calendar_lines.sort() # Print the sorted list of calendar lines for line in calendar_lines: print(line)
46
810
10,137,600
67845539
import heapq n = int(input()) totLength = 0 allNames = [] used = {} namesOfLength = [[] for i in range(11)] for o in range(n): s = input() totLength += len(s) allNames.append(s) namesOfLength[len(s)].append(s) d = input() for i in range(n): allNames[i] += d allNames.sort() charPerLine = 2*totLength/n for i in range(11): heapq.heapify(namesOfLength[i]) for i in range(n): if used.get(allNames[i],False) == True: continue length = len(allNames[i])-1 used[allNames[i]] = True otherLength = int(charPerLine - length) heapq.heappop(namesOfLength[length]) line = "" line += allNames[i] otherWord = heapq.heappop(namesOfLength[otherLength]) used[otherWord+d] = True line += otherWord print(line)
Codeforces Beta Round 54 (Div. 2)
CF
2,011
2
256
Calendar
BerOilGasDiamondBank has branches in n cities, at that n is an even number. The bank management wants to publish a calendar with the names of all those cities written in two columns: the calendar should consist of exactly n / 2 lines of strictly equal length, each of which contains exactly two names and exactly one separator character between them. The name of every city should be used in the calendar exactly once. For historical reasons the symbol d is used as the separator of words in the calendar. The BerOilGasDiamondBank management wants to show that all its branches are equally important to it, that's why the order of their appearance in the calendar should be following: if we "glue"(concatinate) all the n / 2 calendar lines (from top to bottom) to make a single line, then the lexicographically minimal line is obtained. No separator character will be used to separate calendar lines. For example, if the lines are "bertown!berville", "newberville!bera", then the resulting line is "bertown!bervillenewberville!bera". In some sense one has to find the lexicographically minimal calendar, where the comparison of calendars happens line by line. Help BerOilGasDiamondBank and construct the required calendar.
The first line contains an integer n (1 ≤ n ≤ 104, n is even) which is the number of branches. Then follow n lines which are the names of the cities. All the names consist of lowercase Latin letters; their lengths are no less than 1 and no more than 10 symbols. The next line contains a single symbol d (d has an ASCII-code from 33 to 126 inclusively, excluding lowercase Latin letters) which is the separator between words in the calendar lines. It is guaranteed that the calendar is possible to be constructed and all the names are different.
Print n / 2 lines of similar length which are the required calendar. Every line should contain exactly two words and exactly one separator between them. If there are several solutions, print the lexicographically minimal one. The lexicographical comparison of lines is realized by the "<" operator in the modern programming languages.
null
null
[{"input": "4\nb\naa\nhg\nc\n.", "output": "aa.b\nc.hg"}, {"input": "2\naa\na\n!", "output": "a!aa"}, {"input": "2\naa\na\n|", "output": "aa|a"}]
2,000
["greedy", "strings"]
46
[{"input": "4\r\nb\r\naa\r\nhg\r\nc\r\n.\r\n", "output": "aa.b\r\nc.hg\r\n"}, {"input": "2\r\naa\r\na\r\n!\r\n", "output": "a!aa\r\n"}, {"input": "2\r\naa\r\na\r\n|\r\n", "output": "aa|a\r\n"}, {"input": "4\r\nqhcivbxotj\r\nirgxzzxvw\r\npxdmcyszvk\r\nyyaevcdal\r\n~\r\n", "output": "irgxzzxvw~pxdmcyszvk\r\nqhcivbxotj~yyaevcdal\r\n"}, {"input": "8\r\nbad\r\nrnnpg\r\njvcjsxfob\r\nad\r\nairnnpg\r\nqury\r\njvcjsxfo\r\nquryai\r\n6\r\n", "output": "ad6jvcjsxfob\r\nairnnpg6qury\r\nbad6jvcjsxfo\r\nquryai6rnnpg\r\n"}, {"input": "6\r\neh\r\nehkhdp\r\ngque\r\nkhdptvgque\r\ntvgque\r\nehkhdptv\r\n}\r\n", "output": "ehkhdptv}gque\r\nehkhdp}tvgque\r\neh}khdptvgque\r\n"}, {"input": "10\r\ndoecgzo\r\ntjptpqp\r\noitegxzwlp\r\nmwsrwmeyeg\r\nsmapaqanak\r\nsmapaqa\r\nqghrydm\r\nnakqghrydm\r\nmedoraus\r\nnyvgozjdf\r\n|\r\n", "output": "doecgzo|mwsrwmeyeg\r\nmedoraus|nyvgozjdf\r\nnakqghrydm|qghrydm\r\noitegxzwlp|smapaqa\r\nsmapaqanak|tjptpqp\r\n"}, {"input": "30\r\nd\r\nahx\r\nr\r\nyd\r\np\r\nnhy\r\na\r\ntqt\r\nctp\r\ntp\r\nho\r\nry\r\nm\r\ng\r\ns\r\nn\r\nct\r\nsc\r\nqr\r\nrry\r\ny\r\nhxm\r\nqrr\r\nsct\r\ncwu\r\nq\r\ndk\r\nrf\r\nhyd\r\nnh\r\n$\r\n", "output": "a$ahx\r\nct$dk\r\nctp$d\r\ncwu$g\r\nho$nh\r\nhxm$m\r\nhyd$n\r\nnhy$p\r\nq$qrr\r\nqr$rf\r\nr$rry\r\nry$sc\r\ns$sct\r\ntp$yd\r\ntqt$y\r\n"}, {"input": "14\r\neskrrgzq\r\nxbmynhxfg\r\nwwffafny\r\nfaxcnrqkkb\r\nfaxcnrqk\r\nkbwwffafny\r\nmnborvqeae\r\nranfahuebj\r\neskrrgzqk\r\nfaxcnrqkk\r\ncznaycxe\r\nrnkgfgyq\r\nkxbmynhxfg\r\nbwwffafny\r\n}\r\n", "output": "bwwffafny}eskrrgzqk\r\ncznaycxe}faxcnrqkkb\r\neskrrgzq}kbwwffafny\r\nfaxcnrqkk}xbmynhxfg\r\nfaxcnrqk}kxbmynhxfg\r\nmnborvqeae}rnkgfgyq\r\nranfahuebj}wwffafny\r\n"}, {"input": "34\r\nobseknnnqk\r\ncvyvvbcgb\r\nxvmhfzfl\r\ngrtp\r\nhbcbhj\r\nknnnqk\r\ncyud\r\nkuaeui\r\naeui\r\nlhpdobsekn\r\ncxmigej\r\ncvyvvbcgbs\r\nuwuu\r\nnnqk\r\npzcftfrrqp\r\nnwsyrgqa\r\nxvmhfzflku\r\nndcis\r\nxhaznwqsgk\r\ncftfrrqp\r\nkakdggegew\r\njjzvokhh\r\nlhpdobse\r\nxjjzvokhh\r\nlhpd\r\nsuwuu\r\ntuatbwof\r\nvpsuday\r\nndcisx\r\nfggxici\r\nbfnipz\r\nknzjio\r\noirksxb\r\nbfni\r\n~\r\n", "output": "aeui~cvyvvbcgbs\r\nbfnipz~cftfrrqp\r\nbfni~kakdggegew\r\ncvyvvbcgb~ndcis\r\ncxmigej~fggxici\r\ncyud~lhpdobsekn\r\ngrtp~obseknnnqk\r\nhbcbhj~jjzvokhh\r\nknnnqk~lhpdobse\r\nknzjio~nwsyrgqa\r\nkuaeui~tuatbwof\r\nlhpd~pzcftfrrqp\r\nndcisx~xvmhfzfl\r\nnnqk~xhaznwqsgk\r\noirksxb~vpsuday\r\nsuwuu~xjjzvokhh\r\nuwuu~xvmhfzflku\r\n"}, {"input": "58\r\nesgdfmf\r\nxfkluadj\r\nqhvh\r\njwhuyhm\r\nmgi\r\nysgc\r\nvhhenku\r\npb\r\ntr\r\nu\r\njyrpjnpd\r\nkluadjo\r\nopb\r\ncopb\r\ngcyhceo\r\nr\r\ndjo\r\nxfklu\r\neo\r\nadjo\r\nfkluadjo\r\nybe\r\nwljwh\r\nqhvhh\r\nrhgotp\r\nyhceo\r\nuyhm\r\nvdd\r\nyhm\r\nysgcyhc\r\nvddrhg\r\nril\r\nwljwhu\r\nx\r\nqh\r\nhceo\r\ntfcopb\r\nmgitfc\r\nvddrh\r\nmgitfco\r\nxf\r\nmgitf\r\ncyoybe\r\notp\r\no\r\nljwhuyhm\r\nysgcy\r\nhhenku\r\nwl\r\ngotp\r\nqhv\r\nw\r\nhenku\r\nenku\r\nys\r\nrilcyo\r\nxfklua\r\nqhvhhenk\r\n|\r\n", "output": "adjo|henku\r\ncopb|mgitf\r\ncyoybe|djo\r\nenku|qhvhh\r\neo|esgdfmf\r\nfkluadjo|o\r\ngcyhceo|pb\r\ngotp|vddrh\r\nhceo|wljwh\r\nhhenku|mgi\r\njwhuyhm|qh\r\njyrpjnpd|r\r\nkluadjo|tr\r\nljwhuyhm|u\r\nmgitfco|wl\r\nmgitfc|opb\r\notp|rhgotp\r\nqhvhhenk|w\r\nqhvh|xfklu\r\nqhv|rilcyo\r\nril|tfcopb\r\nuyhm|yhceo\r\nvddrhg|vdd\r\nvhhenku|xf\r\nwljwhu|ybe\r\nxfkluadj|x\r\nxfklua|yhm\r\nysgcyhc|ys\r\nysgcy|ysgc\r\n"}, {"input": "76\r\nsd\r\nwhx\r\nk\r\nce\r\nthm\r\nbyfi\r\npju\r\nbn\r\ndz\r\non\r\nizr\r\niswh\r\nl\r\nwig\r\ns\r\nju\r\nsr\r\nie\r\nx\r\nbth\r\nzvi\r\nlxth\r\ndmzz\r\nbnqq\r\nan\r\ny\r\ng\r\nvlj\r\nc\r\nhdu\r\nlx\r\nwkyd\r\ndb\r\nrmr\r\nrv\r\nis\r\ngv\r\nu\r\nbyf\r\nm\r\nqqb\r\nwe\r\nb\r\ne\r\nnioo\r\niek\r\no\r\nymk\r\nifpw\r\nisw\r\nammm\r\ncgk\r\ncq\r\nhhv\r\nq\r\nat\r\nd\r\ney\r\nn\r\nrhq\r\ncecg\r\nqsh\r\nak\r\nhx\r\nrve\r\nlaly\r\ni\r\nbnsa\r\nioou\r\nsk\r\nkg\r\nqshs\r\nwzmn\r\nupt\r\nvwvr\r\nyjj\r\nN\r\n", "output": "akNbth\r\nammmNb\r\nanNbyf\r\natNcgk\r\nbnNhdu\r\nbnqqNc\r\nbnsaNd\r\nbyfiNe\r\nceNhhv\r\ncecgNg\r\ncqNiek\r\ndbNisw\r\ndmzzNi\r\ndzNizr\r\neyNpju\r\ngvNqqb\r\nhxNqsh\r\nieNrhq\r\nifpwNk\r\nioouNl\r\nisNrmr\r\niswhNm\r\njuNrve\r\nkgNthm\r\nlalyNn\r\nlxNupt\r\nlxthNo\r\nniooNq\r\nonNvlj\r\nqshsNs\r\nrvNwhx\r\nsdNwig\r\nskNyjj\r\nsrNymk\r\nuNvwvr\r\nweNzvi\r\nwkydNx\r\nwzmnNy\r\n"}, {"input": "10\r\npo\r\negf\r\ne\r\ngfuzaqsi\r\nsi\r\nhpo\r\nuldiig\r\negfuzaq\r\nuldiigh\r\nuzaqsi\r\n{\r\n", "output": "egfuzaq{po\r\negf{uldiig\r\ne{gfuzaqsi\r\nhpo{uzaqsi\r\nsi{uldiigh\r\n"}, {"input": "4\r\na\r\nf\r\nz\r\nh\r\n!\r\n", "output": "a!f\r\nh!z\r\n"}]
false
stdio
null
true
831/B
831
B
Python 3
TESTS
0
30
0
215367358
normal = input() different = input() text = input() new = "" for char in text: if char.isalpha(): for i in range(len(normal)): if char.isupper() and char.lower() == normal[i]: new += different[i].upper() else: new += different[i] else: new += char print(new)
19
31
0
147677202
k1=str(input()[:26]) k2=str(input()[:26]) tx=str(input()) newStr = "" for j in tx: for i in range(len(k1)): if j.lower() == k1[i] and j.islower(): newStr += k2[i] elif j.lower() == k1[i] and j.isupper(): newStr += k2[i].upper() elif j.isdigit(): newStr += j break print(newStr)
Codeforces Round 424 (Div. 2, rated, based on VK Cup Finals)
CF
2,017
1
256
Keyboard Layouts
There are two popular keyboard layouts in Berland, they differ only in letters positions. All the other keys are the same. In Berland they use alphabet with 26 letters which coincides with English alphabet. You are given two strings consisting of 26 distinct letters each: all keys of the first and the second layouts in the same order. You are also given some text consisting of small and capital English letters and digits. It is known that it was typed in the first layout, but the writer intended to type it in the second layout. Print the text if the same keys were pressed in the second layout. Since all keys but letters are the same in both layouts, the capitalization of the letters should remain the same, as well as all other characters.
The first line contains a string of length 26 consisting of distinct lowercase English letters. This is the first layout. The second line contains a string of length 26 consisting of distinct lowercase English letters. This is the second layout. The third line contains a non-empty string s consisting of lowercase and uppercase English letters and digits. This is the text typed in the first layout. The length of s does not exceed 1000.
Print the text if the same keys were pressed in the second layout.
null
null
[{"input": "qwertyuiopasdfghjklzxcvbnm\nveamhjsgqocnrbfxdtwkylupzi\nTwccpQZAvb2017", "output": "HelloVKCup2017"}, {"input": "mnbvcxzlkjhgfdsapoiuytrewq\nasdfghjklqwertyuiopzxcvbnm\n7abaCABAABAcaba7", "output": "7uduGUDUUDUgudu7"}]
800
["implementation", "strings"]
19
[{"input": "qwertyuiopasdfghjklzxcvbnm\r\nveamhjsgqocnrbfxdtwkylupzi\r\nTwccpQZAvb2017\r\n", "output": "HelloVKCup2017\r\n"}, {"input": "mnbvcxzlkjhgfdsapoiuytrewq\r\nasdfghjklqwertyuiopzxcvbnm\r\n7abaCABAABAcaba7\r\n", "output": "7uduGUDUUDUgudu7\r\n"}, {"input": "ayvguplhjsoiencbkxdrfwmqtz\r\nkhzvtbspcndierqumlojyagfwx\r\n3\r\n", "output": "3\r\n"}, {"input": "oaihbljgekzsxucwnqyrvfdtmp\r\nwznqcfvrthjibokeglmudpayxs\r\ntZ8WI33UZZytE8A99EvJjck228LxUQtL5A8q7O217KrmdhpmdhN7JEdVXc8CRm07TFidlIou9AKW9cCl1c4289rfU87oXoSCwHpZO7ggC2GmmDl0KGuA2IimDco2iKaBKl46H089r2tw16mhzI44d2X6g3cnoD0OU5GvA8l89nhNpzTbY9FtZ2wE3Y2a5EC7zXryudTZhXFr9EEcX8P71fp6694aa02B4T0w1pDaVml8FM3N2qB78DBrS723Vpku105sbTJEdBpZu77b1C47DujdoR7rjm5k2nsaPBqX93EfhW95Mm0sBnFtgo12gS87jegSR5u88tM5l420dkt1l1b18UjatzU7P2i9KNJA528caiEpE3JtRw4m4TJ7M1zchxO53skt3Fqvxk2C51gD8XEY7YJC2xmTUqyEUFmPX581Gow2HWq4jaP8FK87\r\n", "output": "yJ8EN33OJJmyT8Z99TdVvkh228FbOLyF5Z8l7W217HuxaqsxaqG7VTaDBk8KUx07YPnafNwo9ZHE9kKf1k4289upO87wBwIKeQsJW7rrK2RxxAf0HRoZ2NnxAkw2nHzCHf46Q089u2ye16xqjN44a2B6r3kgwA0WO5RdZ8f89gqGsjYcM9PyJ2eT3M2z5TK7jBumoaYJqBPu9TTkB8S71ps6694zz02C4Y0e1sAzDxf8PX3G2lC78ACuI723Dsho105icYVTaCsJo77c1K47AovawU7uvx5h2gizSClB93TpqE95Xx0iCgPyrw12rI87vtrIU5o88yX5f420ahy1f1c18OvzyjO7S2n9HGVZ528kznTsT3VyUe4x4YV7X1jkqbW53ihy3Pldbh2K51rA8BTM7MVK2bxYOlmTOPxSB581Rwe2QEl4vzS8PH87\r\n"}, {"input": "aymrnptzhklcbuxfdvjsgqweio\r\nwzsavqryltmjnfgcedxpiokbuh\r\nB5\r\n", "output": "N5\r\n"}, {"input": "unbclszprgiqjodxeawkymvfth\r\ncxfwbdvuqlotkgparmhsyinjze\r\nk081O\r\n", "output": "s081G\r\n"}, {"input": "evfsnczuiodgbhqmlypkjatxrw\r\nhvsockwjxtgreqmyanlzidpbuf\r\n306QMPpaqZ\r\n", "output": "306MYLldmW\r\n"}, {"input": "pbfjtvryklwmuhxnqsoceiadgz\r\ntaipfdvlzemhjsnkwyocqgrxbu\r\nTm9H66Ux59PuGe3lEG94q18u11Dda6w59q1hAAIvHR1qquKI2Xf5ZFdKAPhcEnqKT6BF6Oh16P48YvrIKWGDlRcx9BZwwEF64o0As\r\n", "output": "Fh9S66Jn59TjBq3eQB94w18j11Xxr6m59w1sRRGdSV1wwjZG2Ni5UIxZRTscQkwZF6AI6Os16T48LdvGZMBXeVcn9AUmmQI64o0Ry\r\n"}, {"input": "rtqgahmkeoldsiynjbuwpvcxfz\r\noxqiuwflvebnapyrmcghtkdjzs\r\nJqNskelr3FNjbDhfKPfPXxlqOw72p9BVBwf0tN8Ucs48Vlfjxqo9V3ruU5205UgTYi3JKFbW91NLQ1683315VJ4RSLFW7s26s6uZKs5cO2wAT4JS8rCytZVlPWXdNXaCTq06F1v1Fj2zq7DeJbBSfM5Eko6vBndR75d46mf5Pq7Ark9NARTtQ176ukljBdaqXRsYxrBYl7hda1V7sy38hfbjz59HYM9U55P9eh1CX7tUE44NFlQu7zSjSBHyS3Tte2XaXD3O470Q8U20p8W5rViIh8lsn2TvmcdFdxrF3Ye26J2ZK0BR3KShN597WSJmHJTl4ZZ88IMhzHi6vFyr7MuGYNFGebTB573e6Crwj8P18h344yd8sR2NPge36Y3QC8Y2uW577CO2w4fz\r\n", "output": "MqRalvbo3ZRmcNwzLTzTJjbqEh72t9CKChz0xR8Gda48Kbzmjqe9K3ogG5205GiXYp3MLZcH91RBQ1683315KM4OABZH7a26a6gSLa5dE2hUX4MA8oDyxSKbTHJnRJuDXq06Z1k1Zm2sq7NvMcCAzF5Vle6kCrnO75n46fz5Tq7Uol9RUOXxQ176glbmCnuqJOaYjoCYb7wnu1K7ay38wzcms59WYF9G55T9vw1DJ7xGV44RZbQg7sAmACWyA3Xxv2JuJN3E470Q8G20t8H5oKpPw8bar2XkfdnZnjoZ3Yv26M2SL0CO3LAwR597HAMfWMXb4SS88PFwsWp6kZyo7FgIYRZIvcXC573v6Dohm8T18w344yn8aO2RTiv36Y3QD8Y2gH577DE2h4zs\r\n"}, {"input": "buneohqdgxjsafrmwtzickvlpy\r\nzblwamjxifyuqtnrgdkchpoves\r\n4RZf8YivG6414X1GdDfcCbc10GA0Wz8514LI9D647XzPb66UNh7lX1rDQv0hQvJ7aqhyh1Z39yABGKn24g185Y85ER5q9UqPFaQ2JeK97wHZ78CMSuU8Zf091mePl2OX61BLe5KdmUWodt4BXPiseOZkZ4SZ27qtBM4hT499mCirjy6nB0ZqjQie4Wr3uhW2mGqBlHyEZbW7A6QnsNX9d3j5aHQN0H6GF8J0365KWuAmcroutnJD6l6HI3kSSq17Sdo2htt9y967y8sc98ZAHbutH1m9MOVT1E9Mb5UIK3qNatk9A0m2i1fQl9A65204Q4z4O4rQf374YEq0s2sfmQNW9K7E1zSbj51sGINJVr5736Gw8aW6u9Cjr0sjffXctLopJ0YQ47xD1yEP6bB3odG7slgiM8hJ9BuwfGUwN8tbAgJU8wMI2L0P446MO\r\n", "output": "4NKt8ScoI6414F1IxXthHzh10IQ0Gk8514VC9X647FkEz66BLm7vF1nXJo0mJoY7qjmsm1K39sQZIPl24i185S85WN5j9BjETqJ2YwP97gMK78HRUbB8Kt091rwEv2AF61ZVw5PxrBGaxd4ZFEcuwAKpK4UK27jdZR4mD499rHcnys6lZ0KjyJcw4Gn3bmG2rIjZvMsWKzG7Q6JluLF9x3y5qMJL0M6IT8Y0365PGbQrhnabdlYX6v6MC3pUUj17Uxa2mdd9s967s8uh98KQMzbdM1r9RAOD1W9Rz5BCP3jLqdp9Q0r2c1tJv9Q65204J4k4A4nJt374SWj0u2utrJLG9P7W1kUzy51uICLYOn5736Ig8qG6b9Hyn0uyttFhdVaeY0SJ47fX1sWE6zZ3axI7uvicR8mY9ZbgtIBgL8dzQiYB8gRC2V0E446RA\r\n"}, {"input": "qwertyuiopasdfghjklzxcvbnm\r\nqwertyuiopasdfghjklzxcvbnm\r\nqwertyuiopasdfghjklzxcvbnmPOIUYTREWQLKJHGFDSAMNBVCXZ12345678900987654321ASDFGHJKLqwertyuiopZXCVBNM\r\n", "output": "qwertyuiopasdfghjklzxcvbnmPOIUYTREWQLKJHGFDSAMNBVCXZ12345678900987654321ASDFGHJKLqwertyuiopZXCVBNM\r\n"}, {"input": "qwertyuiopasdfghjklzxcvbnm\r\nmnbvcxzlkjhgfdsapoiuytrewq\r\nasdfghjklzxcvbnmqwertyuiopASDFGHJKLQWERTYUIOPZXCVBNM12345678900987654321QWSDFGVBNxcvghjkoWQEDFGHNJMzxcfghjkl\r\n", "output": "hgfdsapoiuytrewqmnbvcxzlkjHGFDSAPOIMNBVCXZLKJUYTREWQ12345678900987654321MNGFDSREWytrsapokNMBFDSAWPQuytdsapoi\r\n"}]
false
stdio
null
true
311/B
311
B
Python 3
TESTS
1
92
7,065,600
38941287
oo = int(1e20) f = lambda : map(int, input().split()) d, dp, suffix, a = [0, 0], [], [], [-oo] n, m, k = f() for i in range (m + 10) : dp.append(0) suffix.append(0) tp = [int(x) for x in input().split()] for i in range (2, n + 1) : d.append(tp[i - 2] + d[i - 1]) for i in range (m) : h, t = f() a.append(t - d[h]) a = sorted(a) for i in range (m, 0, -1) : suffix[i] = suffix[i + 1] + oo - a[i] def check(l, r, p) : return suffix[l] - suffix[r + 1] - (oo - p) * (r + 1 - l) for i in range (1, m + 1) : dp[i] = check(1, i, a[i]) for i in range (k - 1) : tmp = [] for j in range (m + 10) : tmp.append(0) fr = 0 for j in range (1, m + 1) : while(fr < j - 1 and dp[fr] + check(fr + 1, j, a[j]) > dp[fr + 1] + check(fr + 2, j, a[j])) : fr += 1 tmp[j] = dp[fr] + check(fr + 1, j, a[j]) for j in range (1, m + 1) : dp[j] = tmp[j] print(dp[m])
30
1,216
27,340,800
211419212
def solve(A, B, C): N = len(B) + 1 M = len(C) a = [0] * N for i in range(1, N): a[i] += a[i - 1] + B[i - 1] b = [0] * M s = [0] * M dp = [0] * M k = [0] * M for i in range(M): b[i] = C[i][1] - a[C[i][0] - 1] b.sort() for i in range(M - 1, -1, -1): b[i] -= b[0] for i in range(M): s[i] = s[i - 1] + b[i] if i > 0 else 0 for i in range(M): dp[i] = b[i] * (i + 1) - s[i] q = [0] * (10**5 + 9) for i in range(1, A): for j in range(M): k[j] = dp[j] + s[j] H = 0 T = -1 for j in range(M): while H < T and (k[q[H + 1]] - k[q[H]]) < b[j] * (q[H + 1] - q[H]): H += 1 while H < T and (k[q[T]] - k[q[T - 1]]) * (j - q[T]) > (k[j] - k[q[T]]) * (q[T] - q[T - 1]): T -= 1 T += 1 q[T] = j dp[j] = k[q[H]] + b[j] * (j - q[H]) - s[j] return str(dp[M - 1]) t = 1 # t = int(input()) # Uncomment this line to take input for 't' while t > 0: n, m, p = map(int, input().split()) arr = list(map(int, input().split())) brr = [] for _ in range(m): brr.append(list(map(int, input().split()))) print(solve(p, arr, brr)) t -= 1
Codeforces Round 185 (Div. 1)
CF
2,013
2
256
Cats Transport
Zxr960115 is owner of a large farm. He feeds m cute cats and employs p feeders. There's a straight road across the farm and n hills along the road, numbered from 1 to n from left to right. The distance between hill i and (i - 1) is di meters. The feeders live in hill 1. One day, the cats went out to play. Cat i went on a trip to hill hi, finished its trip at time ti, and then waited at hill hi for a feeder. The feeders must take all the cats. Each feeder goes straightly from hill 1 to n without waiting at a hill and takes all the waiting cats at each hill away. Feeders walk at a speed of 1 meter per unit time and are strong enough to take as many cats as they want. For example, suppose we have two hills (d2 = 1) and one cat that finished its trip at time 3 at hill 2 (h1 = 2). Then if the feeder leaves hill 1 at time 2 or at time 3, he can take this cat, but if he leaves hill 1 at time 1 he can't take it. If the feeder leaves hill 1 at time 2, the cat waits him for 0 time units, if the feeder leaves hill 1 at time 3, the cat waits him for 1 time units. Your task is to schedule the time leaving from hill 1 for each feeder so that the sum of the waiting time of all cats is minimized.
The first line of the input contains three integers n, m, p (2 ≤ n ≤ 105, 1 ≤ m ≤ 105, 1 ≤ p ≤ 100). The second line contains n - 1 positive integers d2, d3, ..., dn (1 ≤ di < 104). Each of the next m lines contains two integers hi and ti (1 ≤ hi ≤ n, 0 ≤ ti ≤ 109).
Output an integer, the minimum sum of waiting time of all cats. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
null
null
[{"input": "4 6 2\n1 3 5\n1 0\n2 1\n4 9\n1 10\n2 10\n3 12", "output": "3"}]
2,400
["data structures", "dp"]
30
[{"input": "4 6 2\r\n1 3 5\r\n1 0\r\n2 1\r\n4 9\r\n1 10\r\n2 10\r\n3 12\r\n", "output": "3\r\n"}]
false
stdio
null
true
457/B
457
B
Python 3
TESTS
2
109
0
67588331
mn = [int(i) for i in input().split()] a = [int(j) for j in input().split()] b = [int(k) for k in input().split()] c = a+b print(c.count(max(c))*(sum(c)-(c.count(max(c))*max(c))))
77
296
11,264,000
162480494
n, m = map(int, input().split()) sum1 = 0 sum2 = 0 a = list(map(int, input().strip().split())) b = list(map(int, input().strip().split())) for i in range (0, n) : sum1 += a[i] for i in range (0, m) : sum2 += b[i] ans1 = sum2 ans2 = sum1 a.sort() b.sort() for i in range (0, n-1) : ans1 += min(a[i], sum2) for i in range (0, m-1) : ans2 += min(b[i], sum1) res = min(ans1, ans2) print(res)
MemSQL Start[c]UP 2.0 - Round 2
CF
2,014
1
256
Distributed Join
Piegirl was asked to implement two table join operation for distributed database system, minimizing the network traffic. Suppose she wants to join two tables, A and B. Each of them has certain number of rows which are distributed on different number of partitions. Table A is distributed on the first cluster consisting of m partitions. Partition with index i has ai rows from A. Similarly, second cluster containing table B has n partitions, i-th one having bi rows from B. In one network operation she can copy one row from any partition to any other partition. At the end, for each row from A and each row from B there should be a partition that has both rows. Determine the minimal number of network operations to achieve this.
First line contains two integer numbers, m and n (1 ≤ m, n ≤ 105). Second line contains description of the first cluster with m space separated integers, ai (1 ≤ ai ≤ 109). Similarly, third line describes second cluster with n space separated integers, bi (1 ≤ bi ≤ 109).
Print one integer — minimal number of copy operations.
null
In the first example it makes sense to move all the rows to the second partition of the second cluster which is achieved in 2 + 6 + 3 = 11 operations In the second example Piegirl can copy each row from B to the both partitions of the first cluster which needs 2·3 = 6 copy operations.
[{"input": "2 2\n2 6\n3 100", "output": "11"}, {"input": "2 3\n10 10\n1 1 1", "output": "6"}]
1,900
["greedy"]
77
[{"input": "2 2\r\n2 6\r\n3 100\r\n", "output": "11\r\n"}, {"input": "2 3\r\n10 10\r\n1 1 1\r\n", "output": "6\r\n"}, {"input": "2 2\r\n888381664 866366630\r\n170399907 404233949\r\n", "output": "1149267712\r\n"}, {"input": "3 4\r\n337369924 278848730 654933675\r\n866361693 732544605 890800310 350303294\r\n", "output": "3220361921\r\n"}, {"input": "10 10\r\n510955240 684852706 455356451 284505713 595775142 646334608 563116339 941123613 818750895 516673753\r\n382626402 204542396 341363992 234231105 75079663 683639780 624391764 265169060 686304227 280991725\r\n", "output": "8854660961\r\n"}, {"input": "6 5\r\n45936257 8169878 14134346 26323055 65863745 50728147\r\n997339869 2970526 163305525 839524148 193404120\r\n", "output": "781991027\r\n"}, {"input": "5 4\r\n556840201 669601415 674042771 93322040 157280418\r\n253115131 933556803 294280580 169051325\r\n", "output": "2867533881\r\n"}, {"input": "5 7\r\n473347111 640932948 320036306 595696211 365475226\r\n347859328 553364017 687935743 145411543 689180757 696504973 783694820\r\n", "output": "5515744163\r\n"}, {"input": "8 8\r\n808147225 623333304 535665685 469385259 122918604 200681823 800211367 286974812\r\n85215517 983921829 274028967 567054904 144473212 964018990 177471567 73882806\r\n", "output": "6133464042\r\n"}, {"input": "10 10\r\n326151338 981287141 830123412 482457331 77554645 351237238 663827505 549778905 967488359 954617100\r\n238752550 787656851 393452025 837732771 522417885 876998499 195063055 325140429 546151936 403260186\r\n", "output": "10329862020\r\n"}, {"input": "10 10\r\n933168403 835157665 823216696 818565876 448948583 884328249 809244579 473034231 407137956 871269623\r\n653126539 145998557 644003076 138712151 839886312 479712343 709513279 138285801 858528549 643830064\r\n", "output": "11622500129\r\n"}, {"input": "10 10\r\n269584761 865524829 265226347 963092340 261501474 16861445 221090297 746538035 842020225 649641719\r\n49728483 423679832 107851851 179960003 345895125 400584885 460489835 377856735 506736683 676996548\r\n", "output": "7667769112\r\n"}, {"input": "10 10\r\n458278487 288667180 648471199 581765640 758405216 589361337 319325955 938498114 249892107 138299026\r\n57775135 470751607 454623764 556600014 141039336 225043834 692497485 517610562 635337211 56258907\r\n", "output": "7840004002\r\n"}, {"input": "5 6\r\n7780674 1861750 4491902 10256124 14362475\r\n1809567 5616386 1771573 2099536 1113026 3938402\r\n", "output": "40738940\r\n"}, {"input": "6 5\r\n40192277 37957130 22509015 95257198 6210193 16850057\r\n76289330 265203569 184343840 163207736 126924648\r\n", "output": "769741424\r\n"}, {"input": "6 5\r\n4689556 6609945 15705705 10301912 11245669 5844638\r\n440894622 898226832 22060902 222576920 53133033\r\n", "output": "238386210\r\n"}, {"input": "5 6\r\n284534195 993347068 628813225 512761241 835859363\r\n61567950 7311163 14322159 100466429 66443161 48573213\r\n", "output": "1479270495\r\n"}, {"input": "5 6\r\n574664105 497253985 200935113 926362846 223381305\r\n34188719 14075259 27219005 9682257 14352213 11696423\r\n", "output": "556069380\r\n"}, {"input": "1 1\r\n1889\r\n2867\r\n", "output": "1889\r\n"}, {"input": "20 30\r\n81 67 100 83 97 97 58 54 72 78 59 64 55 85 75 58 79 91 64 84\r\n116 13 114 180 17 123 64 185 170 54 138 142 89 191 78 152 49 5 121 66 163 171 64 170 143 143 126 12 175 84\r\n", "output": "4628\r\n"}]
false
stdio
null
true
982/D
982
D
PyPy 3
TESTS
2
108
20,172,800
127577116
#brute hai def brute(a): l=list(zip(a,range(len(a)))) l=sorted(l,key=lambda s:s[0]) ans=-1 id=float("inf") for i in range(len(l)): res=l[i:] f=True cnt=1 if ans<=1 and i>=1: id=min(id,res[0][0]) ans=1 if len(res)>=2: temp=res[0][1] for j in range(len(res)-1): if res[j+1][1]-res[j][1]-1==temp: cnt+=1 continue else: f=False if f==True: if cnt>=ans: if ans==cnt: id=min(id,res[0][0]) else: ans=cnt id=res[0][0] return id n=input() l=list(map(int,input().strip().split())) print(brute(l))
50
342
26,214,400
139935576
import sys input = sys.stdin.buffer.readline def process(A): n = len(A) A2 = sorted(A) inverse = {} cell_sizes = [0 for i in range(n+3)] cell_size_count = 0 left_borders = [0 for i in range(n+3)] right_borders = [0 for i in range(n+3)] answer = [0, None] for i in range(n): ai = A2[i] inverse[ai] = [None, i+1] for i in range(n): ai = A[i] inverse[ai][0] = i+1 #for each element, inverse = #index in unsorted, index = sorted for i in range(n): ai = A2[i] I = inverse[ai][0] size1 = left_borders[I] #size of the cell that ai has to the left, if any size2 = right_borders[I] #size of the cell that ai has to the right, if any new_size = size1+size2+1 #size of the new cell ai will form cell_sizes[size1]-=1 if cell_sizes[size1]==0: cell_size_count-=1 cell_sizes[size2]-=1 if cell_sizes[size2]==0: cell_size_count-=1 cell_sizes[new_size]+=1 if cell_sizes[new_size]==1: cell_size_count+=1 left_borders[I] = 0 right_borders[I] = 0 right_borders[I+size1+1] = new_size left_borders[I-size2-1] = new_size if cell_size_count==1: if cell_sizes[new_size] > answer[0]: answer = [cell_sizes[new_size], ai+1] return answer[1] n = int(input()) A = [int(x) for x in input().split()] print(process(A))
Codeforces Round 484 (Div. 2)
CF
2,018
1
256
Shark
For long time scientists study the behavior of sharks. Sharks, as many other species, alternate short movements in a certain location and long movements between locations. Max is a young biologist. For $$$n$$$ days he watched a specific shark, and now he knows the distance the shark traveled in each of the days. All the distances are distinct. Max wants to know now how many locations the shark visited. He assumed there is such an integer $$$k$$$ that if the shark in some day traveled the distance strictly less than $$$k$$$, then it didn't change the location; otherwise, if in one day the shark traveled the distance greater than or equal to $$$k$$$; then it was changing a location in that day. Note that it is possible that the shark changed a location for several consecutive days, in each of them the shark traveled the distance at least $$$k$$$. The shark never returned to the same location after it has moved from it. Thus, in the sequence of $$$n$$$ days we can find consecutive nonempty segments when the shark traveled the distance less than $$$k$$$ in each of the days: each such segment corresponds to one location. Max wants to choose such $$$k$$$ that the lengths of all such segments are equal. Find such integer $$$k$$$, that the number of locations is as large as possible. If there are several such $$$k$$$, print the smallest one.
The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of days. The second line contains $$$n$$$ distinct positive integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the distance traveled in each of the day.
Print a single integer $$$k$$$, such that 1. the shark was in each location the same number of days, 2. the number of locations is maximum possible satisfying the first condition, 3. $$$k$$$ is smallest possible satisfying the first and second conditions.
null
In the first example the shark travels inside a location on days $$$1$$$ and $$$2$$$ (first location), then on $$$4$$$-th and $$$5$$$-th days (second location), then on $$$7$$$-th and $$$8$$$-th days (third location). There are three locations in total. In the second example the shark only moves inside a location on the $$$2$$$-nd day, so there is only one location.
[{"input": "8\n1 2 7 3 4 8 5 6", "output": "7"}, {"input": "6\n25 1 2 3 14 36", "output": "2"}]
1,900
["brute force", "data structures", "dsu", "trees"]
50
[{"input": "8\r\n1 2 7 3 4 8 5 6\r\n", "output": "7"}, {"input": "6\r\n25 1 2 3 14 36\r\n", "output": "2"}, {"input": "20\r\n1 20 2 19 3 18 4 17 5 16 6 15 7 14 8 13 9 12 10 11\r\n", "output": "11"}, {"input": "7\r\n1 2 5 7 3 4 6\r\n", "output": "5"}, {"input": "1\r\n1000000000\r\n", "output": "1000000001"}, {"input": "1\r\n1\r\n", "output": "2"}, {"input": "2\r\n1 2\r\n", "output": "2"}, {"input": "2\r\n2 1\r\n", "output": "2"}, {"input": "22\r\n22 1 20 2 19 3 18 4 17 5 16 6 15 7 14 8 13 9 12 10 11 21\r\n", "output": "11"}, {"input": "63\r\n32 48 31 56 30 47 29 60 28 46 27 55 26 45 25 62 24 44 23 54 22 43 21 59 20 42 19 53 18 41 17 63 16 40 15 52 14 39 13 58 12 38 11 51 10 37 9 61 8 36 7 50 6 35 5 57 4 34 3 49 2 33 1\r\n", "output": "33"}, {"input": "127\r\n64 96 63 112 62 95 61 120 60 94 59 111 58 93 57 124 56 92 55 110 54 91 53 119 52 90 51 109 50 89 49 126 48 88 47 108 46 87 45 118 44 86 43 107 42 85 41 123 40 84 39 106 38 83 37 117 36 82 35 105 34 81 33 127 32 80 31 104 30 79 29 116 28 78 27 103 26 77 25 122 24 76 23 102 22 75 21 115 20 74 19 101 18 73 17 125 16 72 15 100 14 71 13 114 12 70 11 99 10 69 9 121 8 68 7 98 6 67 5 113 4 66 3 97 2 65 1\r\n", "output": "65"}, {"input": "100\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 100 99 98 97 96 95 94 93 92 91 90 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 73 72 71 70 69 68 67 66 65 64 63 62 61\r\n", "output": "61"}, {"input": "10\r\n1 2 3 10 9 8 4 5 6 7\r\n", "output": "7"}]
false
stdio
null
true
58/D
58
D
PyPy 3-64
TESTS
0
60
0
186355239
# Read input n = int(input()) cities = [] for i in range(n): cities.append(input()) d = input() # Sort the list of cities cities.sort() # Divide the list into two equal halves half = n // 2 cities1 = cities[:half] cities2 = cities[half:] # Zip the two halves together to create a list of pairs of cities calendar = list(zip(cities1, cities2)) # Sort the list of pairs lexicographically by comparing the first element of each pair calendar.sort(key=lambda pair: pair[0]) # Join each pair of cities with the separator character 'd' and print the resulting calendar lines for line in calendar: print(line[0] + d + line[1])
46
1,622
1,228,800
18768735
n = int(input()) // 2 a = sorted([input() for i in range(n * 2)], reverse = 1) d = input() L = sum(len(i) for i in a) // n ans = [] for i in range(n): x = a.pop() for y in a[::-1]: if len(x) + len(y) == L: ans.append(min(x + d + y, y + d + x)) a.remove(y) break print('\n'.join(sorted(ans)))
Codeforces Beta Round 54 (Div. 2)
CF
2,011
2
256
Calendar
BerOilGasDiamondBank has branches in n cities, at that n is an even number. The bank management wants to publish a calendar with the names of all those cities written in two columns: the calendar should consist of exactly n / 2 lines of strictly equal length, each of which contains exactly two names and exactly one separator character between them. The name of every city should be used in the calendar exactly once. For historical reasons the symbol d is used as the separator of words in the calendar. The BerOilGasDiamondBank management wants to show that all its branches are equally important to it, that's why the order of their appearance in the calendar should be following: if we "glue"(concatinate) all the n / 2 calendar lines (from top to bottom) to make a single line, then the lexicographically minimal line is obtained. No separator character will be used to separate calendar lines. For example, if the lines are "bertown!berville", "newberville!bera", then the resulting line is "bertown!bervillenewberville!bera". In some sense one has to find the lexicographically minimal calendar, where the comparison of calendars happens line by line. Help BerOilGasDiamondBank and construct the required calendar.
The first line contains an integer n (1 ≤ n ≤ 104, n is even) which is the number of branches. Then follow n lines which are the names of the cities. All the names consist of lowercase Latin letters; their lengths are no less than 1 and no more than 10 symbols. The next line contains a single symbol d (d has an ASCII-code from 33 to 126 inclusively, excluding lowercase Latin letters) which is the separator between words in the calendar lines. It is guaranteed that the calendar is possible to be constructed and all the names are different.
Print n / 2 lines of similar length which are the required calendar. Every line should contain exactly two words and exactly one separator between them. If there are several solutions, print the lexicographically minimal one. The lexicographical comparison of lines is realized by the "<" operator in the modern programming languages.
null
null
[{"input": "4\nb\naa\nhg\nc\n.", "output": "aa.b\nc.hg"}, {"input": "2\naa\na\n!", "output": "a!aa"}, {"input": "2\naa\na\n|", "output": "aa|a"}]
2,000
["greedy", "strings"]
46
[{"input": "4\r\nb\r\naa\r\nhg\r\nc\r\n.\r\n", "output": "aa.b\r\nc.hg\r\n"}, {"input": "2\r\naa\r\na\r\n!\r\n", "output": "a!aa\r\n"}, {"input": "2\r\naa\r\na\r\n|\r\n", "output": "aa|a\r\n"}, {"input": "4\r\nqhcivbxotj\r\nirgxzzxvw\r\npxdmcyszvk\r\nyyaevcdal\r\n~\r\n", "output": "irgxzzxvw~pxdmcyszvk\r\nqhcivbxotj~yyaevcdal\r\n"}, {"input": "8\r\nbad\r\nrnnpg\r\njvcjsxfob\r\nad\r\nairnnpg\r\nqury\r\njvcjsxfo\r\nquryai\r\n6\r\n", "output": "ad6jvcjsxfob\r\nairnnpg6qury\r\nbad6jvcjsxfo\r\nquryai6rnnpg\r\n"}, {"input": "6\r\neh\r\nehkhdp\r\ngque\r\nkhdptvgque\r\ntvgque\r\nehkhdptv\r\n}\r\n", "output": "ehkhdptv}gque\r\nehkhdp}tvgque\r\neh}khdptvgque\r\n"}, {"input": "10\r\ndoecgzo\r\ntjptpqp\r\noitegxzwlp\r\nmwsrwmeyeg\r\nsmapaqanak\r\nsmapaqa\r\nqghrydm\r\nnakqghrydm\r\nmedoraus\r\nnyvgozjdf\r\n|\r\n", "output": "doecgzo|mwsrwmeyeg\r\nmedoraus|nyvgozjdf\r\nnakqghrydm|qghrydm\r\noitegxzwlp|smapaqa\r\nsmapaqanak|tjptpqp\r\n"}, {"input": "30\r\nd\r\nahx\r\nr\r\nyd\r\np\r\nnhy\r\na\r\ntqt\r\nctp\r\ntp\r\nho\r\nry\r\nm\r\ng\r\ns\r\nn\r\nct\r\nsc\r\nqr\r\nrry\r\ny\r\nhxm\r\nqrr\r\nsct\r\ncwu\r\nq\r\ndk\r\nrf\r\nhyd\r\nnh\r\n$\r\n", "output": "a$ahx\r\nct$dk\r\nctp$d\r\ncwu$g\r\nho$nh\r\nhxm$m\r\nhyd$n\r\nnhy$p\r\nq$qrr\r\nqr$rf\r\nr$rry\r\nry$sc\r\ns$sct\r\ntp$yd\r\ntqt$y\r\n"}, {"input": "14\r\neskrrgzq\r\nxbmynhxfg\r\nwwffafny\r\nfaxcnrqkkb\r\nfaxcnrqk\r\nkbwwffafny\r\nmnborvqeae\r\nranfahuebj\r\neskrrgzqk\r\nfaxcnrqkk\r\ncznaycxe\r\nrnkgfgyq\r\nkxbmynhxfg\r\nbwwffafny\r\n}\r\n", "output": "bwwffafny}eskrrgzqk\r\ncznaycxe}faxcnrqkkb\r\neskrrgzq}kbwwffafny\r\nfaxcnrqkk}xbmynhxfg\r\nfaxcnrqk}kxbmynhxfg\r\nmnborvqeae}rnkgfgyq\r\nranfahuebj}wwffafny\r\n"}, {"input": "34\r\nobseknnnqk\r\ncvyvvbcgb\r\nxvmhfzfl\r\ngrtp\r\nhbcbhj\r\nknnnqk\r\ncyud\r\nkuaeui\r\naeui\r\nlhpdobsekn\r\ncxmigej\r\ncvyvvbcgbs\r\nuwuu\r\nnnqk\r\npzcftfrrqp\r\nnwsyrgqa\r\nxvmhfzflku\r\nndcis\r\nxhaznwqsgk\r\ncftfrrqp\r\nkakdggegew\r\njjzvokhh\r\nlhpdobse\r\nxjjzvokhh\r\nlhpd\r\nsuwuu\r\ntuatbwof\r\nvpsuday\r\nndcisx\r\nfggxici\r\nbfnipz\r\nknzjio\r\noirksxb\r\nbfni\r\n~\r\n", "output": "aeui~cvyvvbcgbs\r\nbfnipz~cftfrrqp\r\nbfni~kakdggegew\r\ncvyvvbcgb~ndcis\r\ncxmigej~fggxici\r\ncyud~lhpdobsekn\r\ngrtp~obseknnnqk\r\nhbcbhj~jjzvokhh\r\nknnnqk~lhpdobse\r\nknzjio~nwsyrgqa\r\nkuaeui~tuatbwof\r\nlhpd~pzcftfrrqp\r\nndcisx~xvmhfzfl\r\nnnqk~xhaznwqsgk\r\noirksxb~vpsuday\r\nsuwuu~xjjzvokhh\r\nuwuu~xvmhfzflku\r\n"}, {"input": "58\r\nesgdfmf\r\nxfkluadj\r\nqhvh\r\njwhuyhm\r\nmgi\r\nysgc\r\nvhhenku\r\npb\r\ntr\r\nu\r\njyrpjnpd\r\nkluadjo\r\nopb\r\ncopb\r\ngcyhceo\r\nr\r\ndjo\r\nxfklu\r\neo\r\nadjo\r\nfkluadjo\r\nybe\r\nwljwh\r\nqhvhh\r\nrhgotp\r\nyhceo\r\nuyhm\r\nvdd\r\nyhm\r\nysgcyhc\r\nvddrhg\r\nril\r\nwljwhu\r\nx\r\nqh\r\nhceo\r\ntfcopb\r\nmgitfc\r\nvddrh\r\nmgitfco\r\nxf\r\nmgitf\r\ncyoybe\r\notp\r\no\r\nljwhuyhm\r\nysgcy\r\nhhenku\r\nwl\r\ngotp\r\nqhv\r\nw\r\nhenku\r\nenku\r\nys\r\nrilcyo\r\nxfklua\r\nqhvhhenk\r\n|\r\n", "output": "adjo|henku\r\ncopb|mgitf\r\ncyoybe|djo\r\nenku|qhvhh\r\neo|esgdfmf\r\nfkluadjo|o\r\ngcyhceo|pb\r\ngotp|vddrh\r\nhceo|wljwh\r\nhhenku|mgi\r\njwhuyhm|qh\r\njyrpjnpd|r\r\nkluadjo|tr\r\nljwhuyhm|u\r\nmgitfco|wl\r\nmgitfc|opb\r\notp|rhgotp\r\nqhvhhenk|w\r\nqhvh|xfklu\r\nqhv|rilcyo\r\nril|tfcopb\r\nuyhm|yhceo\r\nvddrhg|vdd\r\nvhhenku|xf\r\nwljwhu|ybe\r\nxfkluadj|x\r\nxfklua|yhm\r\nysgcyhc|ys\r\nysgcy|ysgc\r\n"}, {"input": "76\r\nsd\r\nwhx\r\nk\r\nce\r\nthm\r\nbyfi\r\npju\r\nbn\r\ndz\r\non\r\nizr\r\niswh\r\nl\r\nwig\r\ns\r\nju\r\nsr\r\nie\r\nx\r\nbth\r\nzvi\r\nlxth\r\ndmzz\r\nbnqq\r\nan\r\ny\r\ng\r\nvlj\r\nc\r\nhdu\r\nlx\r\nwkyd\r\ndb\r\nrmr\r\nrv\r\nis\r\ngv\r\nu\r\nbyf\r\nm\r\nqqb\r\nwe\r\nb\r\ne\r\nnioo\r\niek\r\no\r\nymk\r\nifpw\r\nisw\r\nammm\r\ncgk\r\ncq\r\nhhv\r\nq\r\nat\r\nd\r\ney\r\nn\r\nrhq\r\ncecg\r\nqsh\r\nak\r\nhx\r\nrve\r\nlaly\r\ni\r\nbnsa\r\nioou\r\nsk\r\nkg\r\nqshs\r\nwzmn\r\nupt\r\nvwvr\r\nyjj\r\nN\r\n", "output": "akNbth\r\nammmNb\r\nanNbyf\r\natNcgk\r\nbnNhdu\r\nbnqqNc\r\nbnsaNd\r\nbyfiNe\r\nceNhhv\r\ncecgNg\r\ncqNiek\r\ndbNisw\r\ndmzzNi\r\ndzNizr\r\neyNpju\r\ngvNqqb\r\nhxNqsh\r\nieNrhq\r\nifpwNk\r\nioouNl\r\nisNrmr\r\niswhNm\r\njuNrve\r\nkgNthm\r\nlalyNn\r\nlxNupt\r\nlxthNo\r\nniooNq\r\nonNvlj\r\nqshsNs\r\nrvNwhx\r\nsdNwig\r\nskNyjj\r\nsrNymk\r\nuNvwvr\r\nweNzvi\r\nwkydNx\r\nwzmnNy\r\n"}, {"input": "10\r\npo\r\negf\r\ne\r\ngfuzaqsi\r\nsi\r\nhpo\r\nuldiig\r\negfuzaq\r\nuldiigh\r\nuzaqsi\r\n{\r\n", "output": "egfuzaq{po\r\negf{uldiig\r\ne{gfuzaqsi\r\nhpo{uzaqsi\r\nsi{uldiigh\r\n"}, {"input": "4\r\na\r\nf\r\nz\r\nh\r\n!\r\n", "output": "a!f\r\nh!z\r\n"}]
false
stdio
null
true
960/F
960
F
Python 3
TESTS
1
77
7,065,600
37092866
n,m = map(int, input().split()) dp = [set() for _ in range(n)] def pixat(to, path): to_remove = [] for p in dp[to]: if p[1] >= path[1] and p[0] <= path[0]: return if p[1] <= path[1] and p[0] >= path[0]: to_remove.append(p) for r in to_remove: dp[to].remove(r) dp[to].add(path) for _ in range(m): a, b, w = map(lambda x: int(x)-1, input().split()) to_pixat = [1] max_w = (-1, -1) for p in dp[a]: if p[0] <= w: max_w = max(max_w, p) if max_w[0] == -1: pixat(b, (w, 1)) else: pixat(b, (w, max_w[1])) ans = -1 for to in dp: for l, w in to: ans = max(ans, l) print(ans)
63
685
44,236,800
206868890
import bisect import sys, os, io input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline def f(u, v): return u * z + v def update(i, x): i += l1 tree[i] = x i //= 2 while i: tree[i] = max(tree[2 * i], tree[2 * i + 1]) i //= 2 return def get_max(s, t): s, t = s + l1, t + l1 ans = 0 while s <= t: if s % 2 == 0: s //= 2 else: ans = max(ans, tree[s]) s = (s + 1) // 2 if t % 2 == 1: t //= 2 else: ans = max(ans, tree[t]) t = (t - 1) // 2 return ans n, m = map(int, input().split()) abw = [tuple(map(int, input().split())) for _ in range(m)] z = pow(10, 5) + 5 s = [] for a, b, w in abw: s.append(f(b, w)) s = list(set(sorted(s))) s.sort() l = len(s) d = dict() for i in range(l): d[s[i]] = i l1 = pow(2, (l + 1).bit_length()) l2 = 2 * l1 tree = [0] * l2 for a, b, w in abw: l0 = bisect.bisect_left(s, f(a, 0)) r0 = bisect.bisect_left(s, f(a, w)) - 1 update(d[f(b, w)], max(get_max(l0, r0) + 1, tree[d[f(b, w)] + l1])) ans = tree[1] print(ans)
Divide by Zero 2018 and Codeforces Round 474 (Div. 1 + Div. 2, combined)
CF
2,018
1
256
Pathwalks
You are given a directed graph with n nodes and m edges, with all edges having a certain weight. There might be multiple edges and self loops, and the graph can also be disconnected. You need to choose a path (possibly passing through same vertices multiple times) in the graph such that the weights of the edges are in strictly increasing order, and these edges come in the order of input. Among all such paths, you need to find the the path that has the maximum possible number of edges, and report this value. Please note that the edges picked don't have to be consecutive in the input.
The first line contains two integers n and m (1 ≤ n ≤ 100000,1 ≤ m ≤ 100000) — the number of vertices and edges in the graph, respectively. m lines follows. The i-th of these lines contains three space separated integers ai, bi and wi (1 ≤ ai, bi ≤ n, 0 ≤ wi ≤ 100000), denoting an edge from vertex ai to vertex bi having weight wi
Print one integer in a single line — the maximum number of edges in the path.
null
The answer for the first sample input is 2: $$1 \rightarrow 2 \rightarrow 3$$. Note that you cannot traverse $$1 \rightarrow 2 \rightarrow 3 \rightarrow 1$$ because edge $$\overset{3}{\rightarrow}1$$ appears earlier in the input than the other two edges and hence cannot be picked/traversed after either of the other two edges. In the second sample, it's optimal to pick 1-st, 3-rd and 5-th edges to get the optimal answer: $$1 \rightarrow 3 \rightarrow 4 \rightarrow 5$$.
[{"input": "3 3\n3 1 3\n1 2 1\n2 3 2", "output": "2"}, {"input": "5 5\n1 3 2\n3 2 3\n3 4 5\n5 4 0\n4 5 8", "output": "3"}]
2,100
["data structures", "dp", "graphs"]
63
[{"input": "3 3\r\n3 1 3\r\n1 2 1\r\n2 3 2\r\n", "output": "2"}, {"input": "5 5\r\n1 3 2\r\n3 2 3\r\n3 4 5\r\n5 4 0\r\n4 5 8\r\n", "output": "3"}, {"input": "5 10\r\n3 4 8366\r\n5 1 6059\r\n2 1 72369\r\n2 2 35472\r\n5 3 50268\r\n2 4 98054\r\n5 1 26220\r\n2 3 24841\r\n1 3 42450\r\n3 1 59590\r\n", "output": "3"}, {"input": "1000 10\r\n11 368 48256\r\n192 176 81266\r\n236 360 25346\r\n377 962 3089\r\n486 176 49857\r\n693 810 36660\r\n692 698 35141\r\n879 822 10964\r\n974 439 31998\r\n364 142 62668\r\n", "output": "1"}, {"input": "1 1\r\n1 1 1000\r\n", "output": "1"}, {"input": "6 5\r\n1 2 1\r\n2 3 2\r\n3 4 3\r\n5 4 10\r\n4 6 11\r\n", "output": "4"}]
false
stdio
null
true
397/B
397
B
Python 3
TESTS
2
62
0
6759060
t = int(input()) for _ in range(t): n, l, r = map(int, input().split()) if n < l: print("No") elif n <= r: print("Yes") else: d = n // l if d * l <= n <= (d - 1) * l + r: print("Yes") else: print("No")
6
108
204,800
63120826
for i in range(int(input())): n,l,r = map(int,input().split()) print('No') if (n//l)<n/r else print('Yes')
Codeforces Round 232 (Div. 2)
CF
2,014
1
256
On Corruption and Numbers
Alexey, a merry Berland entrant, got sick of the gray reality and he zealously wants to go to university. There are a lot of universities nowadays, so Alexey is getting lost in the diversity — he has not yet decided what profession he wants to get. At school, he had bad grades in all subjects, and it's only thanks to wealthy parents that he was able to obtain the graduation certificate. The situation is complicated by the fact that each high education institution has the determined amount of voluntary donations, paid by the new students for admission — ni berubleys. He cannot pay more than ni, because then the difference between the paid amount and ni can be regarded as a bribe! Each rector is wearing the distinctive uniform of his university. Therefore, the uniform's pockets cannot contain coins of denomination more than ri. The rector also does not carry coins of denomination less than li in his pocket — because if everyone pays him with so small coins, they gather a lot of weight and the pocket tears. Therefore, a donation can be paid only by coins of denomination x berubleys, where li ≤ x ≤ ri (Berland uses coins of any positive integer denomination). Alexey can use the coins of different denominations and he can use the coins of the same denomination any number of times. When Alexey was first confronted with such orders, he was puzzled because it turned out that not all universities can accept him! Alexey is very afraid of going into the army (even though he had long wanted to get the green uniform, but his dad says that the army bullies will beat his son and he cannot pay to ensure the boy's safety). So, Alexey wants to know for sure which universities he can enter so that he could quickly choose his alma mater. Thanks to the parents, Alexey is not limited in money and we can assume that he has an unlimited number of coins of each type. In other words, you are given t requests, each of them contains numbers ni, li, ri. For each query you need to answer, whether it is possible to gather the sum of exactly ni berubleys using only coins with an integer denomination from li to ri berubleys. You can use coins of different denominations. Coins of each denomination can be used any number of times.
The first line contains the number of universities t, (1 ≤ t ≤ 1000) Each of the next t lines contain three space-separated integers: ni, li, ri (1 ≤ ni, li, ri ≤ 109; li ≤ ri).
For each query print on a single line: either "Yes", if Alexey can enter the university, or "No" otherwise.
null
You can pay the donation to the first university with two coins: one of denomination 2 and one of denomination 3 berubleys. The donation to the second university cannot be paid.
[{"input": "2\n5 2 3\n6 4 5", "output": "Yes\nNo"}]
null
["constructive algorithms", "implementation", "math"]
6
[{"input": "2\r\n5 2 3\r\n6 4 5\r\n", "output": "Yes\r\nNo\r\n"}, {"input": "50\r\n69 6 6\r\n22 1 1\r\n23 3 3\r\n60 13 13\r\n13 3 3\r\n7 4 7\r\n6 1 1\r\n49 7 9\r\n68 8 8\r\n20 2 2\r\n34 1 1\r\n79 5 5\r\n22 1 1\r\n77 58 65\r\n10 3 3\r\n72 5 5\r\n47 1 1\r\n82 3 3\r\n92 8 8\r\n34 1 1\r\n42 9 10\r\n63 14 14\r\n10 3 3\r\n38 2 2\r\n80 6 6\r\n79 5 5\r\n53 5 5\r\n44 7 7\r\n85 2 2\r\n24 2 2\r\n57 3 3\r\n95 29 81\r\n77 6 6\r\n24 1 1\r\n33 4 4\r\n93 6 6\r\n55 22 28\r\n91 14 14\r\n7 1 1\r\n16 1 1\r\n20 3 3\r\n43 3 3\r\n53 3 3\r\n49 3 3\r\n52 5 5\r\n2 1 1\r\n60 5 5\r\n76 57 68\r\n67 3 3\r\n61 52 61\r\n", "output": "No\r\nYes\r\nNo\r\nNo\r\nNo\r\nYes\r\nYes\r\nYes\r\nNo\r\nYes\r\nYes\r\nNo\r\nYes\r\nNo\r\nNo\r\nNo\r\nYes\r\nNo\r\nNo\r\nYes\r\nNo\r\nNo\r\nNo\r\nYes\r\nNo\r\nNo\r\nNo\r\nNo\r\nNo\r\nYes\r\nYes\r\nYes\r\nNo\r\nYes\r\nNo\r\nNo\r\nYes\r\nNo\r\nYes\r\nYes\r\nNo\r\nNo\r\nNo\r\nNo\r\nNo\r\nYes\r\nYes\r\nNo\r\nNo\r\nYes\r\n"}]
false
stdio
null
true
982/D
982
D
Python 3
TESTS
4
46
0
154419850
days = int(input()) segments = list(map(int,input().split())) sorted_arr = segments.copy() sorted_arr.sort() sorted_dict = {x:[[]] for x in sorted_arr[1:]} res = [0,0] for minimum in sorted_arr[1:]: for k in segments: if k < minimum: if sum(sorted_dict[minimum][-1]) >= minimum: sorted_dict[minimum].append([k]) else: sorted_dict[minimum][-1].append(k) else: if sorted_dict[minimum][-1]: sorted_dict[minimum].append([]) if not(sorted_dict[minimum][-1]): del sorted_dict[minimum][-1] for resp, li in sorted_dict.items(): if len(li) == 1 and resp < res[0]: res[0] = resp res[1] = len(li) else: it = iter(li) the_len = len(next(it)) if all(len(l) == the_len for l in it): if len(li) > res[1]: res[0] = resp res[1] = len(li) elif resp < res[0]: res[0] = resp res[1] = len(li) print(res[0])
50
389
22,016,000
164651035
def main(): n = int(input()) ori_a = [int(x) for x in input().split()] pos = {ori_a[i]: i for i in range(len(ori_a))} seg = [dict(left=None, right=None) for i in range(len(ori_a))] a = sorted(ori_a) seg_size = 0 seg_num = 0 fi_k = a[0] fi_seg_num = 0 for n_items, item in enumerate(a, 1): i = pos[item] seg[i]["left"], seg[i]["right"] = i, i seg_num += 1 size = seg[i]["right"] - seg[i]["left"] + 1 if size > seg_size: seg_size = size li = pos[item] - 1 if 0 <= li: if seg[li]["right"] == i - 1: seg[i]["left"] = seg[li]["left"] seg[li]["right"] = seg[i]["left"] seg_num -= 1 size = seg[i]["right"] - seg[i]["left"] + 1 if size > seg_size: seg_size = size ri = pos[item] + 1 if ri < n: if seg[ri]["left"] == i + 1: seg[i]["right"] = seg[ri]["right"] seg[ri]["left"] = seg[i]["left"] seg_num -= 1 size = seg[i]["right"] - seg[i]["left"] + 1 if size > seg_size: seg_size = size if seg_size * seg_num == n_items and seg_num > fi_seg_num: fi_seg_num = seg_num fi_k = item + 1 print(fi_k) if __name__ == '__main__': main()
Codeforces Round 484 (Div. 2)
CF
2,018
1
256
Shark
For long time scientists study the behavior of sharks. Sharks, as many other species, alternate short movements in a certain location and long movements between locations. Max is a young biologist. For $$$n$$$ days he watched a specific shark, and now he knows the distance the shark traveled in each of the days. All the distances are distinct. Max wants to know now how many locations the shark visited. He assumed there is such an integer $$$k$$$ that if the shark in some day traveled the distance strictly less than $$$k$$$, then it didn't change the location; otherwise, if in one day the shark traveled the distance greater than or equal to $$$k$$$; then it was changing a location in that day. Note that it is possible that the shark changed a location for several consecutive days, in each of them the shark traveled the distance at least $$$k$$$. The shark never returned to the same location after it has moved from it. Thus, in the sequence of $$$n$$$ days we can find consecutive nonempty segments when the shark traveled the distance less than $$$k$$$ in each of the days: each such segment corresponds to one location. Max wants to choose such $$$k$$$ that the lengths of all such segments are equal. Find such integer $$$k$$$, that the number of locations is as large as possible. If there are several such $$$k$$$, print the smallest one.
The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of days. The second line contains $$$n$$$ distinct positive integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the distance traveled in each of the day.
Print a single integer $$$k$$$, such that 1. the shark was in each location the same number of days, 2. the number of locations is maximum possible satisfying the first condition, 3. $$$k$$$ is smallest possible satisfying the first and second conditions.
null
In the first example the shark travels inside a location on days $$$1$$$ and $$$2$$$ (first location), then on $$$4$$$-th and $$$5$$$-th days (second location), then on $$$7$$$-th and $$$8$$$-th days (third location). There are three locations in total. In the second example the shark only moves inside a location on the $$$2$$$-nd day, so there is only one location.
[{"input": "8\n1 2 7 3 4 8 5 6", "output": "7"}, {"input": "6\n25 1 2 3 14 36", "output": "2"}]
1,900
["brute force", "data structures", "dsu", "trees"]
50
[{"input": "8\r\n1 2 7 3 4 8 5 6\r\n", "output": "7"}, {"input": "6\r\n25 1 2 3 14 36\r\n", "output": "2"}, {"input": "20\r\n1 20 2 19 3 18 4 17 5 16 6 15 7 14 8 13 9 12 10 11\r\n", "output": "11"}, {"input": "7\r\n1 2 5 7 3 4 6\r\n", "output": "5"}, {"input": "1\r\n1000000000\r\n", "output": "1000000001"}, {"input": "1\r\n1\r\n", "output": "2"}, {"input": "2\r\n1 2\r\n", "output": "2"}, {"input": "2\r\n2 1\r\n", "output": "2"}, {"input": "22\r\n22 1 20 2 19 3 18 4 17 5 16 6 15 7 14 8 13 9 12 10 11 21\r\n", "output": "11"}, {"input": "63\r\n32 48 31 56 30 47 29 60 28 46 27 55 26 45 25 62 24 44 23 54 22 43 21 59 20 42 19 53 18 41 17 63 16 40 15 52 14 39 13 58 12 38 11 51 10 37 9 61 8 36 7 50 6 35 5 57 4 34 3 49 2 33 1\r\n", "output": "33"}, {"input": "127\r\n64 96 63 112 62 95 61 120 60 94 59 111 58 93 57 124 56 92 55 110 54 91 53 119 52 90 51 109 50 89 49 126 48 88 47 108 46 87 45 118 44 86 43 107 42 85 41 123 40 84 39 106 38 83 37 117 36 82 35 105 34 81 33 127 32 80 31 104 30 79 29 116 28 78 27 103 26 77 25 122 24 76 23 102 22 75 21 115 20 74 19 101 18 73 17 125 16 72 15 100 14 71 13 114 12 70 11 99 10 69 9 121 8 68 7 98 6 67 5 113 4 66 3 97 2 65 1\r\n", "output": "65"}, {"input": "100\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 100 99 98 97 96 95 94 93 92 91 90 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 73 72 71 70 69 68 67 66 65 64 63 62 61\r\n", "output": "61"}, {"input": "10\r\n1 2 3 10 9 8 4 5 6 7\r\n", "output": "7"}]
false
stdio
null
true
831/B
831
B
Python 3
TESTS
0
31
0
205456250
a = input().lower() b = input().lower() c = input().lower() new_text = '' for i in c: if i.isalpha(): if i.islower(): index = a.find(i) new_text += b[index] else: index = c.find(i) new_text += c[index] print(new_text)
19
31
0
147782656
s1 = input() s2 = input() s = input() o = '' for alpha in s: if alpha.lower() in s1: if alpha.isupper(): o += s2[s1.index(alpha.lower())].upper() else: o += s2[s1.index(alpha.lower())] else: o += alpha print(o)
Codeforces Round 424 (Div. 2, rated, based on VK Cup Finals)
CF
2,017
1
256
Keyboard Layouts
There are two popular keyboard layouts in Berland, they differ only in letters positions. All the other keys are the same. In Berland they use alphabet with 26 letters which coincides with English alphabet. You are given two strings consisting of 26 distinct letters each: all keys of the first and the second layouts in the same order. You are also given some text consisting of small and capital English letters and digits. It is known that it was typed in the first layout, but the writer intended to type it in the second layout. Print the text if the same keys were pressed in the second layout. Since all keys but letters are the same in both layouts, the capitalization of the letters should remain the same, as well as all other characters.
The first line contains a string of length 26 consisting of distinct lowercase English letters. This is the first layout. The second line contains a string of length 26 consisting of distinct lowercase English letters. This is the second layout. The third line contains a non-empty string s consisting of lowercase and uppercase English letters and digits. This is the text typed in the first layout. The length of s does not exceed 1000.
Print the text if the same keys were pressed in the second layout.
null
null
[{"input": "qwertyuiopasdfghjklzxcvbnm\nveamhjsgqocnrbfxdtwkylupzi\nTwccpQZAvb2017", "output": "HelloVKCup2017"}, {"input": "mnbvcxzlkjhgfdsapoiuytrewq\nasdfghjklqwertyuiopzxcvbnm\n7abaCABAABAcaba7", "output": "7uduGUDUUDUgudu7"}]
800
["implementation", "strings"]
19
[{"input": "qwertyuiopasdfghjklzxcvbnm\r\nveamhjsgqocnrbfxdtwkylupzi\r\nTwccpQZAvb2017\r\n", "output": "HelloVKCup2017\r\n"}, {"input": "mnbvcxzlkjhgfdsapoiuytrewq\r\nasdfghjklqwertyuiopzxcvbnm\r\n7abaCABAABAcaba7\r\n", "output": "7uduGUDUUDUgudu7\r\n"}, {"input": "ayvguplhjsoiencbkxdrfwmqtz\r\nkhzvtbspcndierqumlojyagfwx\r\n3\r\n", "output": "3\r\n"}, {"input": "oaihbljgekzsxucwnqyrvfdtmp\r\nwznqcfvrthjibokeglmudpayxs\r\ntZ8WI33UZZytE8A99EvJjck228LxUQtL5A8q7O217KrmdhpmdhN7JEdVXc8CRm07TFidlIou9AKW9cCl1c4289rfU87oXoSCwHpZO7ggC2GmmDl0KGuA2IimDco2iKaBKl46H089r2tw16mhzI44d2X6g3cnoD0OU5GvA8l89nhNpzTbY9FtZ2wE3Y2a5EC7zXryudTZhXFr9EEcX8P71fp6694aa02B4T0w1pDaVml8FM3N2qB78DBrS723Vpku105sbTJEdBpZu77b1C47DujdoR7rjm5k2nsaPBqX93EfhW95Mm0sBnFtgo12gS87jegSR5u88tM5l420dkt1l1b18UjatzU7P2i9KNJA528caiEpE3JtRw4m4TJ7M1zchxO53skt3Fqvxk2C51gD8XEY7YJC2xmTUqyEUFmPX581Gow2HWq4jaP8FK87\r\n", "output": "yJ8EN33OJJmyT8Z99TdVvkh228FbOLyF5Z8l7W217HuxaqsxaqG7VTaDBk8KUx07YPnafNwo9ZHE9kKf1k4289upO87wBwIKeQsJW7rrK2RxxAf0HRoZ2NnxAkw2nHzCHf46Q089u2ye16xqjN44a2B6r3kgwA0WO5RdZ8f89gqGsjYcM9PyJ2eT3M2z5TK7jBumoaYJqBPu9TTkB8S71ps6694zz02C4Y0e1sAzDxf8PX3G2lC78ACuI723Dsho105icYVTaCsJo77c1K47AovawU7uvx5h2gizSClB93TpqE95Xx0iCgPyrw12rI87vtrIU5o88yX5f420ahy1f1c18OvzyjO7S2n9HGVZ528kznTsT3VyUe4x4YV7X1jkqbW53ihy3Pldbh2K51rA8BTM7MVK2bxYOlmTOPxSB581Rwe2QEl4vzS8PH87\r\n"}, {"input": "aymrnptzhklcbuxfdvjsgqweio\r\nwzsavqryltmjnfgcedxpiokbuh\r\nB5\r\n", "output": "N5\r\n"}, {"input": "unbclszprgiqjodxeawkymvfth\r\ncxfwbdvuqlotkgparmhsyinjze\r\nk081O\r\n", "output": "s081G\r\n"}, {"input": "evfsnczuiodgbhqmlypkjatxrw\r\nhvsockwjxtgreqmyanlzidpbuf\r\n306QMPpaqZ\r\n", "output": "306MYLldmW\r\n"}, {"input": "pbfjtvryklwmuhxnqsoceiadgz\r\ntaipfdvlzemhjsnkwyocqgrxbu\r\nTm9H66Ux59PuGe3lEG94q18u11Dda6w59q1hAAIvHR1qquKI2Xf5ZFdKAPhcEnqKT6BF6Oh16P48YvrIKWGDlRcx9BZwwEF64o0As\r\n", "output": "Fh9S66Jn59TjBq3eQB94w18j11Xxr6m59w1sRRGdSV1wwjZG2Ni5UIxZRTscQkwZF6AI6Os16T48LdvGZMBXeVcn9AUmmQI64o0Ry\r\n"}, {"input": "rtqgahmkeoldsiynjbuwpvcxfz\r\noxqiuwflvebnapyrmcghtkdjzs\r\nJqNskelr3FNjbDhfKPfPXxlqOw72p9BVBwf0tN8Ucs48Vlfjxqo9V3ruU5205UgTYi3JKFbW91NLQ1683315VJ4RSLFW7s26s6uZKs5cO2wAT4JS8rCytZVlPWXdNXaCTq06F1v1Fj2zq7DeJbBSfM5Eko6vBndR75d46mf5Pq7Ark9NARTtQ176ukljBdaqXRsYxrBYl7hda1V7sy38hfbjz59HYM9U55P9eh1CX7tUE44NFlQu7zSjSBHyS3Tte2XaXD3O470Q8U20p8W5rViIh8lsn2TvmcdFdxrF3Ye26J2ZK0BR3KShN597WSJmHJTl4ZZ88IMhzHi6vFyr7MuGYNFGebTB573e6Crwj8P18h344yd8sR2NPge36Y3QC8Y2uW577CO2w4fz\r\n", "output": "MqRalvbo3ZRmcNwzLTzTJjbqEh72t9CKChz0xR8Gda48Kbzmjqe9K3ogG5205GiXYp3MLZcH91RBQ1683315KM4OABZH7a26a6gSLa5dE2hUX4MA8oDyxSKbTHJnRJuDXq06Z1k1Zm2sq7NvMcCAzF5Vle6kCrnO75n46fz5Tq7Uol9RUOXxQ176glbmCnuqJOaYjoCYb7wnu1K7ay38wzcms59WYF9G55T9vw1DJ7xGV44RZbQg7sAmACWyA3Xxv2JuJN3E470Q8G20t8H5oKpPw8bar2XkfdnZnjoZ3Yv26M2SL0CO3LAwR597HAMfWMXb4SS88PFwsWp6kZyo7FgIYRZIvcXC573v6Dohm8T18w344yn8aO2RTiv36Y3QD8Y2gH577DE2h4zs\r\n"}, {"input": "buneohqdgxjsafrmwtzickvlpy\r\nzblwamjxifyuqtnrgdkchpoves\r\n4RZf8YivG6414X1GdDfcCbc10GA0Wz8514LI9D647XzPb66UNh7lX1rDQv0hQvJ7aqhyh1Z39yABGKn24g185Y85ER5q9UqPFaQ2JeK97wHZ78CMSuU8Zf091mePl2OX61BLe5KdmUWodt4BXPiseOZkZ4SZ27qtBM4hT499mCirjy6nB0ZqjQie4Wr3uhW2mGqBlHyEZbW7A6QnsNX9d3j5aHQN0H6GF8J0365KWuAmcroutnJD6l6HI3kSSq17Sdo2htt9y967y8sc98ZAHbutH1m9MOVT1E9Mb5UIK3qNatk9A0m2i1fQl9A65204Q4z4O4rQf374YEq0s2sfmQNW9K7E1zSbj51sGINJVr5736Gw8aW6u9Cjr0sjffXctLopJ0YQ47xD1yEP6bB3odG7slgiM8hJ9BuwfGUwN8tbAgJU8wMI2L0P446MO\r\n", "output": "4NKt8ScoI6414F1IxXthHzh10IQ0Gk8514VC9X647FkEz66BLm7vF1nXJo0mJoY7qjmsm1K39sQZIPl24i185S85WN5j9BjETqJ2YwP97gMK78HRUbB8Kt091rwEv2AF61ZVw5PxrBGaxd4ZFEcuwAKpK4UK27jdZR4mD499rHcnys6lZ0KjyJcw4Gn3bmG2rIjZvMsWKzG7Q6JluLF9x3y5qMJL0M6IT8Y0365PGbQrhnabdlYX6v6MC3pUUj17Uxa2mdd9s967s8uh98KQMzbdM1r9RAOD1W9Rz5BCP3jLqdp9Q0r2c1tJv9Q65204J4k4A4nJt374SWj0u2utrJLG9P7W1kUzy51uICLYOn5736Ig8qG6b9Hyn0uyttFhdVaeY0SJ47fX1sWE6zZ3axI7uvicR8mY9ZbgtIBgL8dzQiYB8gRC2V0E446RA\r\n"}, {"input": "qwertyuiopasdfghjklzxcvbnm\r\nqwertyuiopasdfghjklzxcvbnm\r\nqwertyuiopasdfghjklzxcvbnmPOIUYTREWQLKJHGFDSAMNBVCXZ12345678900987654321ASDFGHJKLqwertyuiopZXCVBNM\r\n", "output": "qwertyuiopasdfghjklzxcvbnmPOIUYTREWQLKJHGFDSAMNBVCXZ12345678900987654321ASDFGHJKLqwertyuiopZXCVBNM\r\n"}, {"input": "qwertyuiopasdfghjklzxcvbnm\r\nmnbvcxzlkjhgfdsapoiuytrewq\r\nasdfghjklzxcvbnmqwertyuiopASDFGHJKLQWERTYUIOPZXCVBNM12345678900987654321QWSDFGVBNxcvghjkoWQEDFGHNJMzxcfghjkl\r\n", "output": "hgfdsapoiuytrewqmnbvcxzlkjHGFDSAPOIMNBVCXZLKJUYTREWQ12345678900987654321MNGFDSREWytrsapokNMBFDSAWPQuytdsapoi\r\n"}]
false
stdio
null
true
461/B
461
B
PyPy 3
TESTS
0
77
20,172,800
122320886
n = int(input()) t = [int(x) for x in input().split()] color = [int(x) for x in input().split()] edge = [[] for _ in range(n)] dp = [[1, 0] for _ in range(n)] for i in range(n - 1): edge[t[i]].append(i + 1) def dfs(root, parent): for x in edge[root]: if x != parent: dfs(x, root) dp[root][0] *= dp[x][0] if color[root]: dp[root][1] = dp[root][0] return for x in edge[root]: if x != parent: dp[root][1] += dp[root][0] // dp[x][0] * dp[x][1] dp[root][0] += dp[root][1] print(root, dp[root][0], dp[root][1]) dfs(0, -1) print(dp[0][1])
23
358
18,432,000
100683256
mod=10**9+7 n=int(input()) edges=list(map(int,input().split())) colored=list(map(int,input().split())) childs=[[] for i in range(n)] for i in range(1,n): childs[edges[i-1]].append(i) dp = [[0,0] for i in range(n)] for i in range(n-1,-1,-1): prod=1 for child in childs[i]: prod*=sum(dp[child]) if colored[i]: dp[i]=[0,prod%mod] else: sec=0 for child in childs[i]: now=dp[child][1]*prod//sum(dp[child]) sec+=now dp[i]=[prod%mod,sec%mod] print(dp[0][1])
Codeforces Round 263 (Div. 1)
CF
2,014
2
256
Appleman and Tree
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white. Consider a set consisting of k (0 ≤ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices. Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
The first line contains an integer n (2 ≤ n ≤ 105) — the number of tree vertices. The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 ≤ pi ≤ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1. The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output a single integer — the number of ways to split the tree modulo 1000000007 (109 + 7).
null
null
[{"input": "3\n0 0\n0 1 1", "output": "2"}, {"input": "6\n0 1 1 0 4\n1 1 0 0 1 0", "output": "1"}, {"input": "10\n0 1 2 1 4 4 4 0 8\n0 0 0 1 0 1 1 0 0 1", "output": "27"}]
2,000
["dfs and similar", "dp", "trees"]
23
[{"input": "3\r\n0 0\r\n0 1 1\r\n", "output": "2\r\n"}, {"input": "6\r\n0 1 1 0 4\r\n1 1 0 0 1 0\r\n", "output": "1\r\n"}, {"input": "10\r\n0 1 2 1 4 4 4 0 8\r\n0 0 0 1 0 1 1 0 0 1\r\n", "output": "27\r\n"}, {"input": "5\r\n0 1 1 3\r\n0 0 0 1 1\r\n", "output": "1\r\n"}, {"input": "10\r\n0 1 1 2 4 3 3 3 2\r\n1 0 1 1 1 0 0 1 1 0\r\n", "output": "3\r\n"}, {"input": "100\r\n0 0 2 2 0 3 5 0 6 2 0 4 0 2 3 7 8 3 15 19 13 8 18 19 3 14 23 9 6 3 6 17 26 24 20 6 4 27 8 5 14 5 35 31 27 3 41 25 20 14 25 31 49 40 0 1 10 5 50 13 29 58 1 6 8 1 40 52 30 15 50 8 66 52 29 71 25 68 36 7 80 60 6 2 11 43 62 27 84 86 71 38 14 50 88 4 8 95 53\r\n1 0 0 1 0 0 1 0 1 0 0 0 1 0 1 1 0 1 1 1 1 0 0 0 0 1 1 0 1 0 0 0 0 1 1 0 1 1 1 0 0 0 0 1 0 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 1 0 1 0 1 0 0 0 0 1 0 1 0 1 0 1 0 1 1 1 1 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 0 0 1\r\n", "output": "9523200\r\n"}, {"input": "2\r\n0\r\n1 0\r\n", "output": "1\r\n"}, {"input": "115\r\n0 0 1 2 0 4 1 3 4 1 4 5 4 5 0 0 3 1 2 3 3 0 5 1 3 4 1 5 2 0 1 3 3 1 3 5 0 4 5 1 3 0 0 1 3 1 1 3 3 3 2 3 1 3 0 2 5 5 1 1 2 2 1 1 3 2 1 2 3 1 5 4 2 1 2 1 1 2 3 4 3 1 5 0 2 4 4 5 2 5 0 2 4 5 5 5 5 0 3 1 1 4 2 2 4 3 3 0 3 3 0 2 0 0\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "1\r\n"}]
false
stdio
null
true
652/F
652
F
Python 3
TESTS
1
93
307,200
58373924
from bisect import bisect n, l, t = map(int, input().split()) ants = [] for _ in range(n): x, y = input().split() ants.append([int(x), y]) bnts = [] diff = [] for i in range(n): x, w = ants[i][0], 3 - 2*(ants[i][1]=="L") bnts.append((x + t*w) % l) if i == 0: zero = bnts[-1] if ants[i][1] != ants[0][1]: if ants[0][1] == "L": diff.append(x - ants[0][0]) else: diff.append(l - (x - ants[0][0])) bnts.sort() num = 0 quo, mod = t//l, t%l num += quo * len(diff) * 2 diff.sort() diff += [d+l for d in diff] num += bisect(diff, mod*2) num %= n for i in range(n): if bnts[i] == zero: if ants[0][1] == "L": true_zero = (i-num) % n else: if i < n-1: if bnts[i+1] == zero: num -= 1 true_zero = (i+num) % n ans = bnts[true_zero:] + bnts[:true_zero] print(*ans)
30
1,419
45,772,800
144096513
I = lambda: [int(i) for i in input().split()] import sys input = sys.stdin.readline n, L, T = I() a, b = [0]*n, [0]*n ans = [0] * n cnt = 0 for i in range(n): x, ch = input().split() a[i] = int(x) - 1 b[i] = (a[i], i) dr = 1 if ch == "R" else -1 w = a[i] + T * dr a[i] = (a[i] + T * dr) % L cnt += w // L a, b = sorted(a), sorted(b) for i in range(n): ans[b[i][1]] = a[(i + cnt) % n]+1 print(" ".join(str(x) for x in ans))
Educational Codeforces Round 10
ICPC
2,016
2
256
Ants on a Circle
n ants are on a circle of length m. An ant travels one unit of distance per one unit of time. Initially, the ant number i is located at the position si and is facing in the direction di (which is either L or R). Positions are numbered in counterclockwise order starting from some point. Positions of the all ants are distinct. All the ants move simultaneously, and whenever two ants touch, they will both switch their directions. Note that it is possible for an ant to move in some direction for a half of a unit of time and in opposite direction for another half of a unit of time. Print the positions of the ants after t time units.
The first line contains three integers n, m and t (2 ≤ n ≤ 3·105, 2 ≤ m ≤ 109, 0 ≤ t ≤ 1018) — the number of ants, the length of the circle and the number of time units. Each of the next n lines contains integer si and symbol di (1 ≤ si ≤ m and di is either L or R) — the position and the direction of the i-th ant at the start. The directions L and R corresponds to the clockwise and counterclockwise directions, respectively. It is guaranteed that all positions si are distinct.
Print n integers xj — the position of the j-th ant after t units of time. The ants are numbered from 1 to n in order of their appearing in input.
null
null
[{"input": "2 4 8\n1 R\n3 L", "output": "1 3"}, {"input": "4 8 6\n6 R\n5 L\n1 R\n8 L", "output": "7 4 2 7"}, {"input": "4 8 2\n1 R\n5 L\n6 L\n8 R", "output": "3 3 4 2"}]
2,800
["constructive algorithms", "math"]
30
[{"input": "2 4 8\r\n1 R\r\n3 L\r\n", "output": "1 3\r\n"}, {"input": "4 8 6\r\n6 R\r\n5 L\r\n1 R\r\n8 L\r\n", "output": "7 4 2 7\r\n"}, {"input": "4 8 2\r\n1 R\r\n5 L\r\n6 L\r\n8 R\r\n", "output": "3 3 4 2\r\n"}, {"input": "10 10 90\r\n2 R\r\n1 R\r\n3 L\r\n4 R\r\n7 L\r\n8 L\r\n6 R\r\n9 R\r\n5 R\r\n10 L\r\n", "output": "10 9 1 2 5 6 4 7 3 8\r\n"}, {"input": "10 20 85\r\n6 L\r\n12 R\r\n2 L\r\n20 R\r\n18 L\r\n8 R\r\n16 R\r\n14 L\r\n10 L\r\n4 R\r\n", "output": "5 13 1 1 17 9 17 13 9 5\r\n"}, {"input": "10 20 59\r\n1 R\r\n15 L\r\n7 L\r\n13 R\r\n5 R\r\n17 R\r\n3 L\r\n9 R\r\n11 L\r\n19 L\r\n", "output": "20 16 8 12 4 16 4 8 12 20\r\n"}, {"input": "2 2 0\r\n2 L\r\n1 R\r\n", "output": "2 1\r\n"}, {"input": "2 2 0\r\n2 L\r\n1 R\r\n", "output": "2 1\r\n"}, {"input": "4 8 6\r\n6 R\r\n5 L\r\n1 R\r\n8 R\r\n", "output": "7 7 6 4\r\n"}]
false
stdio
null
true
23/C
23
C
Python 3
TESTS
3
935
1,843,200
78436980
t=int(input()) for z in range(t): n=int(input()) s1=0 s2=0 l1=[] l2=[] l3=[] for i in range(2*n-1): a,b=map(int,input().split()) s1+=a/2 s2+=b/2 l1.append(a) l2.append(b) l3.append(((a+b)*a*b,i)) l4=l3[:] l4.sort() l4.reverse() d1=0 d2=0 for i in range(n): d1+=l1[l4[i][1]] d2+=l2[l4[i][1]] if d1<s1 or d2<s2: print("NO") else: print("YES") for i in range(n): print(l4[i][1]+1,end=" ") print()
25
950
42,905,600
212533744
from sys import stdin # read = sys.stdin.readline T = int(stdin.readline()) for _ in range(T): N = int(stdin.readline()) M = N + N - 1 data = [ ] for i in range(M): data.append([int(w) for w in stdin.readline().split()] + [i + 1]) data.sort(reverse=True) # print(data) total = sum(d[1] for d in data) half = sum(d[1] for d in data[0::2]) print('YES') result = [] if half + half >= total: result.extend([d[2] for d in data[0::2]]) else: result.append(data[0][2]) result.extend([d[2] for d in data[1::2]]) print(*result)
Codeforces Beta Round 23
ICPC
2,010
1.5
256
Oranges and Apples
In 2N - 1 boxes there are apples and oranges. Your task is to choose N boxes so, that they will contain not less than half of all the apples and not less than half of all the oranges.
The first input line contains one number T — amount of tests. The description of each test starts with a natural number N — amount of boxes. Each of the following 2N - 1 lines contains numbers ai and oi — amount of apples and oranges in the i-th box (0 ≤ ai, oi ≤ 109). The sum of N in all the tests in the input doesn't exceed 105. All the input numbers are integer.
For each test output two lines. In the first line output YES, if it's possible to choose N boxes, or NO otherwise. If the answer is positive output in the second line N numbers — indexes of the chosen boxes. Boxes are numbered from 1 in the input order. Otherwise leave the second line empty. Separate the numbers with one space.
null
null
[{"input": "2\n2\n10 15\n5 7\n20 18\n1\n0 0", "output": "YES\n1 3\nYES\n1"}]
2,500
["constructive algorithms", "sortings"]
25
[{"input": "2\r\n2\r\n10 15\r\n5 7\r\n20 18\r\n1\r\n0 0\r\n", "output": "YES\r\n3 1\r\nYES\r\n1\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path, 'r') as f_in, open(submission_path, 'r') as f_sub: input_data = f_in.read().split() sub_lines = [line.strip() for line in f_sub.readlines()] ptr = 0 T = int(input_data[ptr]) ptr +=1 sub_ptr = 0 for _ in range(T): if sub_ptr >= len(sub_lines): print(0) return res_line = sub_lines[sub_ptr].strip() sub_ptr +=1 if res_line != 'YES': print(0) return if sub_ptr >= len(sub_lines): print(0) return boxes_line = sub_lines[sub_ptr].strip() sub_ptr +=1 if not boxes_line: print(0) return selected = list(map(int, boxes_line.split())) N = int(input_data[ptr]) ptr +=1 total_apples = 0 total_oranges = 0 boxes = [] for _ in range(2*N -1): a = int(input_data[ptr]) o = int(input_data[ptr+1]) ptr +=2 total_apples += a total_oranges += o boxes.append( (a, o) ) if len(selected) != N: print(0) return seen = set() sum_a = 0 sum_o = 0 for idx in selected: if idx <1 or idx > 2*N -1: print(0) return if idx in seen: print(0) return seen.add(idx) a, o = boxes[idx-1] sum_a += a sum_o += o req_a = (total_apples +1) //2 req_o = (total_oranges +1) //2 if sum_a < req_a or sum_o < req_o: print(0) return print(1) if __name__ == '__main__': main()
true
23/C
23
C
Python 3
TESTS
3
1,856
2,560,000
78440501
t=int(input()) for z in range(t): n=int(input()) s1=0 s2=0 l1=[] l2=[] for i in range(2*n-1): a,b=map(int,input().split()) s1+=a/2 s2+=b/2 l1.append((a,i)) l2.append((b,i)) l3=l1[:] l4=l2[:] l3.sort(key=lambda x: x[0]) l4.sort(key=lambda x: x[0]) l3.reverse() l4.reverse() l5=[] l6=[] for i in range(2*n-1): l5.append(l3[i][1]) l6.append(l4[i][1]) l7=[] i=0 while i<n: l7.append(l5[0]) l6.remove(l5[0]) l5.pop(0) i+=1 if i<n: l7.append(l6[0]) l5.remove(l6[0]) l6.pop(0) i+=1 d1=0 d2=0 for i in range(n): d1+=l1[l7[i]][0] d2+=l2[l7[i]][0] if d1<s1 or d2<s2: print("NO") else: print("YES") for i in range(n): print(l7[i]+1,end=" ") print()
25
1,045
29,593,600
154131565
# 解説を見た # 一見天才だが、「半分以上」を考えるとき、「ソートして隣同士を比較」は定石の一つ。 import sys input = sys.stdin.readline from operator import itemgetter T=int(input()) for tests in range(T): N=int(input()) X=[] for i in range(2*N-1): a,b=map(int,input().split()) X.append((a,b,i+1)) X.sort(key=itemgetter(0)) ANS=[] for i in range(N-1): if X[2*i][1]<X[2*i+1][1]: ANS.append(X[2*i+1][2]) else: ANS.append(X[2*i][2]) ANS.append(X[2*N-2][2]) print("YES") print(*ANS)
Codeforces Beta Round 23
ICPC
2,010
1.5
256
Oranges and Apples
In 2N - 1 boxes there are apples and oranges. Your task is to choose N boxes so, that they will contain not less than half of all the apples and not less than half of all the oranges.
The first input line contains one number T — amount of tests. The description of each test starts with a natural number N — amount of boxes. Each of the following 2N - 1 lines contains numbers ai and oi — amount of apples and oranges in the i-th box (0 ≤ ai, oi ≤ 109). The sum of N in all the tests in the input doesn't exceed 105. All the input numbers are integer.
For each test output two lines. In the first line output YES, if it's possible to choose N boxes, or NO otherwise. If the answer is positive output in the second line N numbers — indexes of the chosen boxes. Boxes are numbered from 1 in the input order. Otherwise leave the second line empty. Separate the numbers with one space.
null
null
[{"input": "2\n2\n10 15\n5 7\n20 18\n1\n0 0", "output": "YES\n1 3\nYES\n1"}]
2,500
["constructive algorithms", "sortings"]
25
[{"input": "2\r\n2\r\n10 15\r\n5 7\r\n20 18\r\n1\r\n0 0\r\n", "output": "YES\r\n3 1\r\nYES\r\n1\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path, 'r') as f_in, open(submission_path, 'r') as f_sub: input_data = f_in.read().split() sub_lines = [line.strip() for line in f_sub.readlines()] ptr = 0 T = int(input_data[ptr]) ptr +=1 sub_ptr = 0 for _ in range(T): if sub_ptr >= len(sub_lines): print(0) return res_line = sub_lines[sub_ptr].strip() sub_ptr +=1 if res_line != 'YES': print(0) return if sub_ptr >= len(sub_lines): print(0) return boxes_line = sub_lines[sub_ptr].strip() sub_ptr +=1 if not boxes_line: print(0) return selected = list(map(int, boxes_line.split())) N = int(input_data[ptr]) ptr +=1 total_apples = 0 total_oranges = 0 boxes = [] for _ in range(2*N -1): a = int(input_data[ptr]) o = int(input_data[ptr+1]) ptr +=2 total_apples += a total_oranges += o boxes.append( (a, o) ) if len(selected) != N: print(0) return seen = set() sum_a = 0 sum_o = 0 for idx in selected: if idx <1 or idx > 2*N -1: print(0) return if idx in seen: print(0) return seen.add(idx) a, o = boxes[idx-1] sum_a += a sum_o += o req_a = (total_apples +1) //2 req_o = (total_oranges +1) //2 if sum_a < req_a or sum_o < req_o: print(0) return print(1) if __name__ == '__main__': main()
true
23/B
23
B
Python 3
TESTS
1
62
0
138008389
a = int(input()) b = int(input()) if a >= 2: print(1) elif b >= 2: print(1) elif a >= 2 and b >= 2: print(2)
2
436
9,523,200
153665974
import sys input = sys.stdin.readline t=int(input()) for tests in range(t): n=int(input()) print(max(0,n-2))
Codeforces Beta Round 23
ICPC
2,010
2
256
Party
n people came to a party. Then those, who had no friends among people at the party, left. Then those, who had exactly 1 friend among those who stayed, left as well. Then those, who had exactly 2, 3, ..., n - 1 friends among those who stayed by the moment of their leaving, did the same. What is the maximum amount of people that could stay at the party in the end?
The first input line contains one number t — amount of tests (1 ≤ t ≤ 105). Each of the following t lines contains one integer number n (1 ≤ n ≤ 105).
For each test output in a separate line one number — the maximum amount of people that could stay in the end.
null
null
[{"input": "1\n3", "output": "1"}]
1,600
["constructive algorithms", "graphs", "math"]
2
[{"input": "1\r\n3\r\n", "output": "1\r\n"}]
false
stdio
null
true
95/C
95
C
Python 3
TESTS
1
92
0
226705762
# LUOGU_RID: 127785605 #aaaaaaaaaaaaaaaaaaaaaaaaaaaaa print(9)
65
1,590
30,105,600
153724036
import heapq import sys, os, io input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline def dijkstra(s): dist = [inf] * (n + 1) dist[s] = 0 p = [] heapq.heappush(p, (dist[s], s)) while p: d, u = heapq.heappop(p) if dist[u] < d: continue for v, c in G[u]: nd = dist[u] + c if nd < dist[v]: dist[v] = nd heapq.heappush(p, (dist[v], v)) return dist n, m = map(int, input().split()) x, y = map(int, input().split()) G = [[] for _ in range(n + 1)] for _ in range(m): u, v, w = map(int, input().split()) G[u].append((v, w)) G[v].append((u, w)) tc = [[0, 0]] + [list(map(int, input().split())) for _ in range(n)] h = [] visit = [0] * (n + 1) heapq.heappush(h, (0, x)) inf = pow(10, 15) + 1 dist = [inf] * (n + 1) while h: d, s = heapq.heappop(h) if visit[s]: continue dist[s] = d visit[s] = 1 dist0 = dijkstra(s) t, c = tc[s] for i in range(1, n + 1): if not visit[i] and 0 < dist0[i] <= t: heapq.heappush(h, (d + c, i)) ans = (dist[y] + 1) % (inf + 1) - 1 print(ans)
Codeforces Beta Round 77 (Div. 1 Only)
CF
2,011
2
256
Volleyball
Petya loves volleyball very much. One day he was running late for a volleyball match. Petya hasn't bought his own car yet, that's why he had to take a taxi. The city has n junctions, some of which are connected by two-way roads. The length of each road is defined by some positive integer number of meters; the roads can have different lengths. Initially each junction has exactly one taxi standing there. The taxi driver from the i-th junction agrees to drive Petya (perhaps through several intermediate junctions) to some other junction if the travel distance is not more than ti meters. Also, the cost of the ride doesn't depend on the distance and is equal to ci bourles. Taxis can't stop in the middle of a road. Each taxi can be used no more than once. Petya can catch taxi only in the junction, where it stands initially. At the moment Petya is located on the junction x and the volleyball stadium is on the junction y. Determine the minimum amount of money Petya will need to drive to the stadium.
The first line contains two integers n and m (1 ≤ n ≤ 1000, 0 ≤ m ≤ 1000). They are the number of junctions and roads in the city correspondingly. The junctions are numbered from 1 to n, inclusive. The next line contains two integers x and y (1 ≤ x, y ≤ n). They are the numbers of the initial and final junctions correspondingly. Next m lines contain the roads' description. Each road is described by a group of three integers ui, vi, wi (1 ≤ ui, vi ≤ n, 1 ≤ wi ≤ 109) — they are the numbers of the junctions connected by the road and the length of the road, correspondingly. The next n lines contain n pairs of integers ti and ci (1 ≤ ti, ci ≤ 109), which describe the taxi driver that waits at the i-th junction — the maximum distance he can drive and the drive's cost. The road can't connect the junction with itself, but between a pair of junctions there can be more than one road. All consecutive numbers in each line are separated by exactly one space character.
If taxis can't drive Petya to the destination point, print "-1" (without the quotes). Otherwise, print the drive's minimum cost. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator.
null
An optimal way — ride from the junction 1 to 2 (via junction 4), then from 2 to 3. It costs 7+2=9 bourles.
[{"input": "4 4\n1 3\n1 2 3\n1 4 1\n2 4 1\n2 3 5\n2 7\n7 2\n1 2\n7 7", "output": "9"}]
1,900
["shortest paths"]
65
[{"input": "4 4\r\n1 3\r\n1 2 3\r\n1 4 1\r\n2 4 1\r\n2 3 5\r\n2 7\r\n7 2\r\n1 2\r\n7 7\r\n", "output": "9\r\n"}, {"input": "3 3\r\n1 3\r\n1 2 2\r\n1 3 3\r\n3 2 1\r\n2 7\r\n2 7\r\n3 6\r\n", "output": "14\r\n"}, {"input": "3 1\r\n1 3\r\n1 2 2\r\n2 7\r\n2 7\r\n3 6\r\n", "output": "-1\r\n"}, {"input": "3 2\r\n3 3\r\n1 2 2\r\n1 3 3\r\n2 7\r\n2 7\r\n3 7\r\n", "output": "0\r\n"}, {"input": "2 2\r\n1 2\r\n1 2 3\r\n1 2 2\r\n2 7\r\n3 2\r\n", "output": "7\r\n"}, {"input": "1 0\r\n1 1\r\n74 47\r\n", "output": "0\r\n"}, {"input": "5 5\r\n1 3\r\n1 3 3\r\n5 1 6\r\n4 3 8\r\n1 3 3\r\n5 2 4\r\n1 2\r\n4 1\r\n2 5\r\n5 2\r\n1 2\r\n", "output": "-1\r\n"}, {"input": "7 4\r\n3 4\r\n6 2 7\r\n6 1 4\r\n4 3 5\r\n3 6 4\r\n1 6\r\n7 3\r\n3 6\r\n6 5\r\n3 7\r\n4 4\r\n1 4\r\n", "output": "-1\r\n"}, {"input": "5 5\r\n4 5\r\n1 4 8\r\n4 2 4\r\n4 1 3\r\n3 1 9\r\n4 5 4\r\n2 7\r\n2 7\r\n5 1\r\n6 3\r\n3 2\r\n", "output": "3\r\n"}, {"input": "5 5\r\n5 4\r\n2 4 10\r\n2 4 7\r\n3 1 7\r\n2 4 2\r\n5 3 9\r\n6 17\r\n2 4\r\n3 12\r\n7 18\r\n2 5\r\n", "output": "-1\r\n"}, {"input": "4 7\r\n3 4\r\n2 3 5\r\n3 2 9\r\n4 1 9\r\n3 2 1\r\n3 1 2\r\n2 3 6\r\n1 2 8\r\n2 2\r\n5 3\r\n2 1\r\n1 5\r\n", "output": "-1\r\n"}, {"input": "7 14\r\n7 5\r\n1 3 15\r\n2 1 10\r\n1 3 5\r\n2 4 9\r\n5 4 19\r\n1 3 11\r\n5 1 1\r\n2 4 5\r\n2 3 11\r\n3 2 10\r\n3 4 18\r\n5 1 18\r\n6 2 5\r\n5 6 2\r\n3 6\r\n6 7\r\n9 1\r\n3 6\r\n1 1\r\n9 4\r\n9 8\r\n", "output": "-1\r\n"}, {"input": "7 15\r\n5 5\r\n3 4 6\r\n7 4 3\r\n7 2 8\r\n2 5 2\r\n7 2 8\r\n5 2 9\r\n3 1 7\r\n1 2 4\r\n7 1 8\r\n7 5 7\r\n2 4 2\r\n4 3 9\r\n7 4 2\r\n5 4 8\r\n7 2 8\r\n15 4\r\n18 18\r\n6 8\r\n16 5\r\n11 1\r\n5 3\r\n18 4\r\n", "output": "0\r\n"}, {"input": "8 20\r\n8 4\r\n6 3 1\r\n3 4 4\r\n5 2 2\r\n3 6 3\r\n5 8 7\r\n6 2 7\r\n8 6 4\r\n6 4 5\r\n4 2 5\r\n5 3 3\r\n5 7 3\r\n8 1 6\r\n2 4 3\r\n6 8 5\r\n1 8 6\r\n8 2 7\r\n8 2 3\r\n1 6 7\r\n8 7 3\r\n6 3 6\r\n2 2\r\n5 9\r\n1 9\r\n4 7\r\n1 8\r\n4 8\r\n9 7\r\n9 3\r\n", "output": "3\r\n"}, {"input": "8 20\r\n8 2\r\n1 7 5\r\n3 2 3\r\n2 7 6\r\n6 5 6\r\n4 8 5\r\n7 8 4\r\n1 6 2\r\n7 4 3\r\n4 3 1\r\n6 7 5\r\n4 2 4\r\n2 8 7\r\n6 2 2\r\n2 3 4\r\n3 7 3\r\n7 8 4\r\n5 4 2\r\n7 1 1\r\n5 7 3\r\n4 3 7\r\n4 4\r\n2 7\r\n3 5\r\n3 1\r\n3 5\r\n1 5\r\n11 4\r\n10 5\r\n", "output": "5\r\n"}, {"input": "9 20\r\n5 1\r\n8 9 3\r\n1 8 6\r\n5 6 3\r\n2 1 4\r\n7 1 6\r\n1 4 4\r\n3 2 4\r\n5 6 4\r\n3 9 6\r\n6 2 3\r\n9 1 7\r\n1 7 1\r\n1 3 3\r\n8 4 7\r\n7 1 7\r\n6 9 3\r\n5 8 3\r\n9 4 5\r\n6 9 1\r\n6 2 6\r\n1 7\r\n1 3\r\n6 1\r\n1 2\r\n6 1\r\n2 2\r\n4 7\r\n4 5\r\n4 1\r\n", "output": "-1\r\n"}, {"input": "10 21\r\n9 5\r\n5 2 6\r\n1 9 7\r\n6 2 7\r\n8 10 2\r\n7 2 1\r\n6 9 6\r\n10 9 4\r\n2 10 2\r\n10 8 4\r\n10 1 7\r\n9 1 7\r\n1 8 5\r\n10 9 7\r\n7 5 3\r\n2 10 6\r\n4 7 3\r\n10 5 6\r\n5 10 4\r\n6 9 2\r\n2 3 6\r\n1 9 3\r\n10 6\r\n7 12\r\n13 3\r\n17 4\r\n18 17\r\n1 9\r\n16 16\r\n12 13\r\n1 10\r\n6 15\r\n", "output": "-1\r\n"}, {"input": "14 20\r\n7 2\r\n12 2 17\r\n13 3 8\r\n6 8 3\r\n14 4 16\r\n13 5 17\r\n7 14 7\r\n11 10 6\r\n12 4 16\r\n6 11 7\r\n2 13 12\r\n13 2 18\r\n2 10 12\r\n13 12 1\r\n12 5 4\r\n9 4 16\r\n7 6 7\r\n2 3 15\r\n4 14 1\r\n13 5 3\r\n10 9 3\r\n4 6\r\n4 5\r\n5 3\r\n2 6\r\n2 4\r\n1 2\r\n4 7\r\n2 2\r\n6 1\r\n1 1\r\n6 5\r\n7 7\r\n3 4\r\n2 6\r\n", "output": "-1\r\n"}, {"input": "5 0\r\n2 4\r\n1 2\r\n6 9\r\n4 585\r\n6 9\r\n7 4\r\n", "output": "-1\r\n"}, {"input": "5 8\r\n1 3\r\n1 2 1\r\n1 3 3\r\n1 5 1\r\n1 4 2\r\n5 4 3\r\n3 5 2\r\n2 3 8\r\n1 3 2\r\n2 7\r\n10 3\r\n4 7\r\n2 1\r\n1 1\r\n", "output": "7\r\n"}, {"input": "5 7\r\n1 3\r\n1 2 1\r\n1 3 3\r\n1 5 1\r\n1 4 2\r\n5 4 3\r\n3 5 2\r\n2 3 8\r\n2 7\r\n10 3\r\n4 7\r\n2 1\r\n1 1\r\n", "output": "10\r\n"}, {"input": "7 5\r\n6 7\r\n1 3 1000000000\r\n2 7 999999999\r\n5 7 123456789\r\n1 5 148879589\r\n5 4 1000000000\r\n1000000000 1000000000\r\n1000000000 1000000000\r\n999999999 145785965\r\n1000000000 1000000000\r\n1000000000 1\r\n123456789 123568591\r\n1000000000 1000000000\r\n", "output": "-1\r\n"}, {"input": "2 1\r\n1 2\r\n1 2 1\r\n1 999999998\r\n1 999999998\r\n", "output": "999999998\r\n"}, {"input": "3 3\r\n1 3\r\n2 1 1\r\n1 2 1000000000\r\n1 2 1000000000\r\n1000000000 1000000000\r\n1000000000 1000000000\r\n1000000000 1000000000\r\n", "output": "-1\r\n"}, {"input": "3 3\r\n1 2\r\n2 1 1\r\n1 2 1000000000\r\n1 2 1000000000\r\n10000000 1000000000\r\n10000000 1000000000\r\n10000000 1000000000\r\n", "output": "1000000000\r\n"}, {"input": "3 3\r\n1 2\r\n2 1 1000000000\r\n1 2 1000000000\r\n1 2 1000000000\r\n10000000 1000000000\r\n10000000 1000000000\r\n10000000 1000000000\r\n", "output": "-1\r\n"}]
false
stdio
null
true
23/C
23
C
PyPy 3-64
TESTS
1
467
10,649,600
212532745
from sys import stdin # read = sys.stdin.readline T = int(stdin.readline()) for _ in range(T): N = int(stdin.readline()) M = N + N - 1 data = [ ] for i in range(M): data.append([int(w) for w in stdin.readline().split()]) data.sort(reverse=True) total = sum(d[1] for d in data) half = sum(d[1] for d in data[0::2]) print('YES') if half + half >= total: print(*[i + i + 1 for i in range(N)]) else: print(1, end=' ') print(*[i + i for i in range(1, N)])
25
1,294
24,371,200
170146783
t=int(input()) for _ in range(t): n=int(input()) nums=[] for i in range(2*n-1): a,b=map(int,input().split()) nums.append([b,a,i+1]) nums=sorted(nums)[::-1] vals=[nums[0][2]] i=1 while(i+1<len(nums)): c=i if(nums[i][1]<nums[i+1][1]): c=i+1 vals.append(nums[c][2]) i+=2 print("YES") print(*vals)
Codeforces Beta Round 23
ICPC
2,010
1.5
256
Oranges and Apples
In 2N - 1 boxes there are apples and oranges. Your task is to choose N boxes so, that they will contain not less than half of all the apples and not less than half of all the oranges.
The first input line contains one number T — amount of tests. The description of each test starts with a natural number N — amount of boxes. Each of the following 2N - 1 lines contains numbers ai and oi — amount of apples and oranges in the i-th box (0 ≤ ai, oi ≤ 109). The sum of N in all the tests in the input doesn't exceed 105. All the input numbers are integer.
For each test output two lines. In the first line output YES, if it's possible to choose N boxes, or NO otherwise. If the answer is positive output in the second line N numbers — indexes of the chosen boxes. Boxes are numbered from 1 in the input order. Otherwise leave the second line empty. Separate the numbers with one space.
null
null
[{"input": "2\n2\n10 15\n5 7\n20 18\n1\n0 0", "output": "YES\n1 3\nYES\n1"}]
2,500
["constructive algorithms", "sortings"]
25
[{"input": "2\r\n2\r\n10 15\r\n5 7\r\n20 18\r\n1\r\n0 0\r\n", "output": "YES\r\n3 1\r\nYES\r\n1\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path, 'r') as f_in, open(submission_path, 'r') as f_sub: input_data = f_in.read().split() sub_lines = [line.strip() for line in f_sub.readlines()] ptr = 0 T = int(input_data[ptr]) ptr +=1 sub_ptr = 0 for _ in range(T): if sub_ptr >= len(sub_lines): print(0) return res_line = sub_lines[sub_ptr].strip() sub_ptr +=1 if res_line != 'YES': print(0) return if sub_ptr >= len(sub_lines): print(0) return boxes_line = sub_lines[sub_ptr].strip() sub_ptr +=1 if not boxes_line: print(0) return selected = list(map(int, boxes_line.split())) N = int(input_data[ptr]) ptr +=1 total_apples = 0 total_oranges = 0 boxes = [] for _ in range(2*N -1): a = int(input_data[ptr]) o = int(input_data[ptr+1]) ptr +=2 total_apples += a total_oranges += o boxes.append( (a, o) ) if len(selected) != N: print(0) return seen = set() sum_a = 0 sum_o = 0 for idx in selected: if idx <1 or idx > 2*N -1: print(0) return if idx in seen: print(0) return seen.add(idx) a, o = boxes[idx-1] sum_a += a sum_o += o req_a = (total_apples +1) //2 req_o = (total_oranges +1) //2 if sum_a < req_a or sum_o < req_o: print(0) return print(1) if __name__ == '__main__': main()
true
23/B
23
B
Python 3
TESTS
1
92
409,600
167609995
# Coded By Block_Cipher import math import os import random import re import sys from math import gcd from math import sqrt from collections import Counter n = int(input()) if n%2!=0: print(1) else: print(0)
2
498
9,625,600
206004920
import sys import math input = sys.stdin.readline def fg(): return int(input()) def fgh(): return [int(xx) for xx in input().split()] M = 10 ** 9 + 7 for _____ in range(fg()): n = fg() print(max(0, n - 2))
Codeforces Beta Round 23
ICPC
2,010
2
256
Party
n people came to a party. Then those, who had no friends among people at the party, left. Then those, who had exactly 1 friend among those who stayed, left as well. Then those, who had exactly 2, 3, ..., n - 1 friends among those who stayed by the moment of their leaving, did the same. What is the maximum amount of people that could stay at the party in the end?
The first input line contains one number t — amount of tests (1 ≤ t ≤ 105). Each of the following t lines contains one integer number n (1 ≤ n ≤ 105).
For each test output in a separate line one number — the maximum amount of people that could stay in the end.
null
null
[{"input": "1\n3", "output": "1"}]
1,600
["constructive algorithms", "graphs", "math"]
2
[{"input": "1\r\n3\r\n", "output": "1\r\n"}]
false
stdio
null
true
739/A
739
A
Python 3
TESTS
1
62
0
22901893
[n, m] = map(int, input().split()) ans = 1e9 for i in range(m): [l, r] = map(int, input().split()) ans = min(ans, r - l + 1) print(ans) for i in range(n): print(i % (1 + ans))
69
405
16,179,200
22624691
def main(): n, m = map(int, input().split()) a = [list(map(int, input().split())) for i in range(m)] z = min(y - x + 1 for x, y in a) print (z) print( ' '.join([str(i % z) for i in range(n)])) main()
Codeforces Round 381 (Div. 1)
CF
2,016
2
256
Alyona and mex
Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special. Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri, and its elements are a[li], a[li + 1], ..., a[ri]. Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible. You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible. The mex of a set S is a minimum possible non-negative integer that is not in S.
The first line contains two integers n and m (1 ≤ n, m ≤ 105). The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), that describe the subarray a[li], a[li + 1], ..., a[ri].
In the first line print single integer — the maximum possible minimum mex. In the second line print n integers — the array a. All the elements in a should be between 0 and 109. It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109. If there are multiple solutions, print any of them.
null
The first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2.
[{"input": "5 3\n1 3\n2 5\n4 5", "output": "2\n1 0 2 1 0"}, {"input": "4 2\n1 4\n2 4", "output": "3\n5 2 0 1"}]
1,700
["constructive algorithms", "greedy"]
69
[{"input": "5 3\r\n1 3\r\n2 5\r\n4 5\r\n", "output": "2\r\n0 1 0 1 0\r\n"}, {"input": "4 2\r\n1 4\r\n2 4\r\n", "output": "3\r\n0 1 2 0\r\n"}, {"input": "1 1\r\n1 1\r\n", "output": "1\r\n0\r\n"}, {"input": "2 1\r\n2 2\r\n", "output": "1\r\n0 0\r\n"}, {"input": "5 6\r\n2 4\r\n2 3\r\n1 4\r\n3 4\r\n2 5\r\n1 3\r\n", "output": "2\r\n0 1 0 1 0\r\n"}, {"input": "8 3\r\n2 3\r\n2 8\r\n3 6\r\n", "output": "2\r\n0 1 0 1 0 1 0 1\r\n"}, {"input": "10 10\r\n1 9\r\n4 8\r\n4 8\r\n5 9\r\n1 9\r\n3 8\r\n1 6\r\n1 9\r\n1 6\r\n6 9\r\n", "output": "4\r\n0 1 2 3 0 1 2 3 0 1\r\n"}, {"input": "3 6\r\n1 3\r\n1 3\r\n1 1\r\n1 1\r\n3 3\r\n3 3\r\n", "output": "1\r\n0 0 0\r\n"}, {"input": "3 3\r\n1 3\r\n2 2\r\n1 3\r\n", "output": "1\r\n0 0 0\r\n"}, {"input": "6 8\r\n3 5\r\n3 6\r\n4 6\r\n2 5\r\n2 5\r\n1 3\r\n3 6\r\n3 5\r\n", "output": "3\r\n0 1 2 0 1 2\r\n"}, {"input": "10 4\r\n4 10\r\n4 6\r\n6 8\r\n1 10\r\n", "output": "3\r\n0 1 2 0 1 2 0 1 2 0\r\n"}, {"input": "9 1\r\n1 1\r\n", "output": "1\r\n0 0 0 0 0 0 0 0 0\r\n"}, {"input": "3 8\r\n2 3\r\n1 3\r\n1 2\r\n2 3\r\n1 3\r\n2 2\r\n1 2\r\n1 2\r\n", "output": "1\r\n0 0 0\r\n"}, {"input": "3 8\r\n1 2\r\n1 2\r\n1 1\r\n2 3\r\n2 3\r\n1 1\r\n1 3\r\n1 3\r\n", "output": "1\r\n0 0 0\r\n"}, {"input": "7 3\r\n7 7\r\n3 7\r\n5 7\r\n", "output": "1\r\n0 0 0 0 0 0 0\r\n"}, {"input": "9 9\r\n4 5\r\n5 8\r\n1 8\r\n4 8\r\n3 4\r\n7 8\r\n1 4\r\n7 8\r\n6 7\r\n", "output": "2\r\n0 1 0 1 0 1 0 1 0\r\n"}, {"input": "10 10\r\n1 5\r\n7 10\r\n2 10\r\n2 5\r\n2 9\r\n5 9\r\n3 10\r\n4 9\r\n6 9\r\n2 7\r\n", "output": "4\r\n0 1 2 3 0 1 2 3 0 1\r\n"}, {"input": "8 7\r\n5 8\r\n3 7\r\n1 8\r\n3 4\r\n2 8\r\n2 7\r\n4 6\r\n", "output": "2\r\n0 1 0 1 0 1 0 1\r\n"}, {"input": "10 3\r\n2 4\r\n8 10\r\n4 6\r\n", "output": "3\r\n0 1 2 0 1 2 0 1 2 0\r\n"}, {"input": "5 8\r\n3 4\r\n1 5\r\n3 4\r\n3 5\r\n3 4\r\n1 4\r\n1 5\r\n2 5\r\n", "output": "2\r\n0 1 0 1 0\r\n"}, {"input": "9 4\r\n5 9\r\n3 8\r\n2 8\r\n1 4\r\n", "output": "4\r\n0 1 2 3 0 1 2 3 0\r\n"}, {"input": "7 7\r\n2 4\r\n1 4\r\n3 6\r\n2 6\r\n5 7\r\n3 6\r\n1 4\r\n", "output": "3\r\n0 1 2 0 1 2 0\r\n"}, {"input": "6 2\r\n4 6\r\n2 5\r\n", "output": "3\r\n0 1 2 0 1 2\r\n"}, {"input": "7 9\r\n6 7\r\n1 2\r\n2 5\r\n4 7\r\n1 7\r\n5 6\r\n2 3\r\n6 7\r\n1 2\r\n", "output": "2\r\n0 1 0 1 0 1 0\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] # Read input with open(input_path, 'r') as f: n, m = map(int, f.readline().split()) subarrays = [tuple(map(int, line.strip().split())) for line in f] # Read reference output (k_ref) with open(output_path, 'r') as f: k_ref = int(f.readline().strip()) # Read submission output with open(submission_path, 'r') as f: lines = f.readlines() if len(lines) < 2: print(0) return try: k_sub = int(lines[0].strip()) array_sub = list(map(int, lines[1].strip().split())) except: print(0) return # Check k_sub equals k_ref if k_sub != k_ref: print(0) return # Check array validity if len(array_sub) != n: print(0) return for num in array_sub: if not (0 <= num <= 10**9): print(0) return required = set(range(k_sub)) found_k_sub = False for l, r in subarrays: start = l - 1 end = r sub = array_sub[start:end] elements = set(sub) # Check all required elements are present if not required.issubset(elements): print(0) return # Check if current subarray has mex exactly k_sub if k_sub not in elements: found_k_sub = True if not found_k_sub: print(0) return print(100) if __name__ == "__main__": main()
true
792/F
792
F
Python 3
TESTS
0
46
4,812,800
26330414
million = 10**6 def vova_fight(input_string): input_list = input_string.split('\n') init_condition = input_list.pop(0).split(' ') queries = int(init_condition[0]) mana_total = int(init_condition[1]) spells = [] j = 0 for i in range(0,queries): current_query = input_list[i].split(' ') query_type = int(current_query[0]) print('current_curry', current_query) if query_type == 1: spell = {} spell['dps'] = (int(current_query[1]) + j) % million + 1 spell['mps'] = (int(current_query[2]) + j) % million + 1 spell['dpm'] = spell['dps'] / spell['mps'] spells.append(spell) else: # print('h', mana_total, spells, current_query, input_list) success = fight(mana_total, spells, current_query, j) if success: j = i # print(success) def fight(mana_total, spells, query, j): # print(mana_total, spells, query) spells = sorted(spells, key=lambda k: k['dpm']) time_limit = (int(query[1]) + j) % million + 1 hp = (int(query[2]) + j) % million + 1 candidate = {} damage = 0 for ind, spell in enumerate(spells): if not candidate: damage = spell['dpm'] * mana_total if (spell['mps'] * time_limit) < mana_total: break else: candidate = spell continue else: if candidate['dps'] > spell['dps']: continue # set candidate if mana used is less than mana_total if (spell['mps'] * time_limit) < mana_total: candidate = spell continue # consider the share percentage for maximizing dps time_share1 = (mana_limit - time_limit*candidate['mps']) / (spell['mps'] - candidate['mps']) timeshare0 = time_limit - time_share1 damage = time_share1 * spell['dps'] + time_share0 * candidate['dps'] break if damage >= hp: print('YES') return True else: print('NO') return False
36
873
15,564,800
37535361
#!/usr/bin/env python3 # solution after hint # (instead of best hit/mana spell store convex hull of spells) # O(n^2) instead of O(n log n) [q, m] = map(int, input().strip().split()) qis = [tuple(map(int, input().strip().split())) for _ in range(q)] mod = 10**6 j = 0 spell_chull = [(0, 0)] # lower hull _/ def is_right(xy0, xy1, xy): (x0, y0) = xy0 (x1, y1) = xy1 (x, y) = xy return (x0 - x) * (y1 - y) >= (x1 - x) * (y0 - y) def in_chull(x, y): i = 0 if x > spell_chull[-1][0]: return False while spell_chull[i][0] < x: i += 1 if spell_chull[i][0] == x: return spell_chull[i][1] <= y else: return is_right(spell_chull[i - 1], spell_chull[i], (x, y)) def add_spell(x, y): global spell_chull if in_chull(x, y): return i_left = 0 while i_left < len(spell_chull) - 1 and not is_right(spell_chull[i_left + 1], spell_chull[i_left], (x, y)): i_left += 1 i_right = i_left + 1 while i_right < len(spell_chull) - 1 and is_right(spell_chull[i_right + 1], spell_chull[i_right], (x, y)): i_right += 1 if i_right == len(spell_chull) - 1 and x >= spell_chull[-1][0]: i_right += 1 spell_chull = spell_chull[:i_left + 1] + [(x, y)] + spell_chull[i_right:] for i, qi in enumerate(qis): (k, a, b) = qi x = (a + j) % mod + 1 y = (b + j) % mod + 1 if k == 1: add_spell(x, y) else: #2 if in_chull(y / x, m / x): print ('YES') j = i + 1 else: print ('NO')
Educational Codeforces Round 18
ICPC
2,017
2
256
Mages and Monsters
Vova plays a computer game known as Mages and Monsters. Vova's character is a mage. Though as he has just started, his character knows no spells. Vova's character can learn new spells during the game. Every spell is characterized by two values xi and yi — damage per second and mana cost per second, respectively. Vova doesn't have to use a spell for an integer amount of seconds. More formally, if he uses a spell with damage x and mana cost y for z seconds, then he will deal x·z damage and spend y·z mana (no rounding). If there is no mana left (mana amount is set in the start of the game and it remains the same at the beginning of every fight), then character won't be able to use any spells. It is prohibited to use multiple spells simultaneously. Also Vova can fight monsters. Every monster is characterized by two values tj and hj — monster kills Vova's character in tj seconds and has hj health points. Mana refills after every fight (or Vova's character revives with full mana reserve), so previous fights have no influence on further ones. Vova's character kills a monster, if he deals hj damage to it in no more than tj seconds using his spells (it is allowed to use more than one spell in a fight) and spending no more mana than he had at the beginning of the fight. If monster's health becomes zero exactly in tj seconds (it means that the monster and Vova's character kill each other at the same time), then Vova wins the fight. You have to write a program which can answer two types of queries: - 1 x y — Vova's character learns new spell which deals x damage per second and costs y mana per second. - 2 t h — Vova fights the monster which kills his character in t seconds and has h health points. Note that queries are given in a different form. Also remember that Vova's character knows no spells at the beginning of the game. For every query of second type you have to determine if Vova is able to win the fight with corresponding monster.
The first line contains two integer numbers q and m (2 ≤ q ≤ 105, 1 ≤ m ≤ 1012) — the number of queries and the amount of mana at the beginning of every fight. i-th of each next q lines contains three numbers ki, ai and bi (1 ≤ ki ≤ 2, 1 ≤ ai, bi ≤ 106). Using them you can restore queries this way: let j be the index of the last query of second type with positive answer (j = 0 if there were none of these). - If ki = 1, then character learns spell with x = (ai + j) mod 106 + 1, y = (bi + j) mod 106 + 1. - If ki = 2, then you have to determine if Vova is able to win the fight against monster with t = (ai + j) mod 106 + 1, h = (bi + j) mod 106 + 1.
For every query of second type print YES if Vova is able to win the fight with corresponding monster and NO otherwise.
null
In first example Vova's character at first learns the spell with 5 damage and 10 mana cost per second. Next query is a fight with monster which can kill character in 20 seconds and has 50 health points. Vova kills it in 10 seconds (spending 100 mana). Next monster has 52 health, so Vova can't deal that much damage with only 100 mana.
[{"input": "3 100\n1 4 9\n2 19 49\n2 19 49", "output": "YES\nNO"}]
3,100
["data structures", "geometry"]
36
[{"input": "3 100\r\n1 4 9\r\n2 19 49\r\n2 19 49\r\n", "output": "YES\r\nNO\r\n"}, {"input": "10 442006988299\r\n2 10 47\r\n1 9 83\r\n1 15 24\r\n2 19 47\r\n2 75 99\r\n2 85 23\r\n2 8 33\r\n2 9 82\r\n1 86 49\r\n2 71 49\r\n", "output": "NO\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\n"}, {"input": "2 424978864039\r\n2 7 3\r\n2 10 8\r\n", "output": "NO\r\nNO\r\n"}, {"input": "2 1000000000000\r\n1 235726 255577\r\n1 959304 206090\r\n", "output": ""}, {"input": "3 10\r\n1 1 1\r\n2 1 1\r\n2 999999 999999\r\n", "output": "YES\r\nYES\r\n"}, {"input": "12 100\r\n1 8 8\r\n2 200 101\r\n2 10 99\r\n1 9 9\r\n2 10 99\r\n2 200 101\r\n1 14 4\r\n2 194 195\r\n2 194 194\r\n2 990 290\r\n2 999991 11\r\n2 999991 10\r\n", "output": "NO\r\nNO\r\nYES\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "15 100\r\n1 8 8\r\n2 200 101\r\n2 10 99\r\n1 9 9\r\n2 10 99\r\n2 200 101\r\n1 14 4\r\n2 194 195\r\n2 194 194\r\n2 990 290\r\n1 2 999992\r\n2 6 256\r\n2 7 256\r\n1 2 999988\r\n2 2 252\r\n", "output": "NO\r\nNO\r\nYES\r\nNO\r\nNO\r\nYES\r\nNO\r\nNO\r\nYES\r\nYES\r\n"}, {"input": "3 12\r\n1 99 9\r\n1 49 1\r\n2 1 149\r\n", "output": "YES\r\n"}]
false
stdio
null
true
23/B
23
B
Python 3
TESTS
1
434
5,632,000
34057632
a=int(input()) s=0 for i in range(0,a): c=int(input()) if(c>1): s=s+1 print(s)
2
528
9,830,400
45194826
import sys inputs,first = sys.stdin.readlines(),True for i in inputs: if first: first = False else: print(max(0,int(i)-2))
Codeforces Beta Round 23
ICPC
2,010
2
256
Party
n people came to a party. Then those, who had no friends among people at the party, left. Then those, who had exactly 1 friend among those who stayed, left as well. Then those, who had exactly 2, 3, ..., n - 1 friends among those who stayed by the moment of their leaving, did the same. What is the maximum amount of people that could stay at the party in the end?
The first input line contains one number t — amount of tests (1 ≤ t ≤ 105). Each of the following t lines contains one integer number n (1 ≤ n ≤ 105).
For each test output in a separate line one number — the maximum amount of people that could stay in the end.
null
null
[{"input": "1\n3", "output": "1"}]
1,600
["constructive algorithms", "graphs", "math"]
2
[{"input": "1\r\n3\r\n", "output": "1\r\n"}]
false
stdio
null
true
23/C
23
C
PyPy 3
TESTS
1
1,855
12,083,200
197190305
for _ in range(int(input())): N = int(input()) a = [] o = [] apple = 0 orange = 0 answers = [] A = 0 O = 0 for _ in range(2*N-1): x,y = map(int,input().split()) a.append(x) o.append(y) A += x O += y A /= 2*N O /= 2*N x = [] y = [] for i in range(2*N-1): a[i] -= A o[i] -= O if a[i] < 0 and o[i] < 0: continue if a[i] < 0: x.append((a[i],o[i],i)) elif o[i] < 0: y.append((o[i],a[i],i)) else: answers.append(i+1) apple += a[i] orange += o[i] x = sorted(x,key=lambda x: x[0]) y = sorted(y,key=lambda x: x[0]) apple1 = [0]*(len(x)+1) orange1 = [0]*(len(x)+1) apple2 = [0]*(len(y)+1) orange2 = [0]*(len(y)+1) for i in range(1,len(x)+1): apple1[i] = apple1[i-1]+x[i-1][0] orange1[i] = orange1[i-1]+x[i-1][1] for i in range(1,len(y)+1): apple2[i] = apple2[i-1]+y[i-1][1] orange2[i] = orange2[i-1]+y[i-1][0] for i in range(min(N-len(answers),len(x))+1): m = N-len(answers)-i if 0 <= m <= len(y) and apple1[i]+apple2[m]+apple > 0 and orange+orange2[m]+orange1[i] > 0: for j in range(i): answers.append(x[j][2]) for j in range(m): answers.append(y[j][2]) break if len(answers) < N: print('NO') else: print('YES\n'+' '.join(map(str,answers[:N])))
25
1,309
24,371,200
170149156
t = int(input()) for _ in range(t): n = int(input()) nums = [] for i in range(2*n-1): a, b = map(int, input().split()) nums.append([b, a, i+1]) s=set() nums = sorted(nums)[::-1] vals=[] vals.append(nums[0][2]) i=1 while len(vals)<n: c=i if nums[i][1] <nums[i+1][1]:c+=1 vals.append(nums[c][2]) i += 2 print("YES") print(*vals)
Codeforces Beta Round 23
ICPC
2,010
1.5
256
Oranges and Apples
In 2N - 1 boxes there are apples and oranges. Your task is to choose N boxes so, that they will contain not less than half of all the apples and not less than half of all the oranges.
The first input line contains one number T — amount of tests. The description of each test starts with a natural number N — amount of boxes. Each of the following 2N - 1 lines contains numbers ai and oi — amount of apples and oranges in the i-th box (0 ≤ ai, oi ≤ 109). The sum of N in all the tests in the input doesn't exceed 105. All the input numbers are integer.
For each test output two lines. In the first line output YES, if it's possible to choose N boxes, or NO otherwise. If the answer is positive output in the second line N numbers — indexes of the chosen boxes. Boxes are numbered from 1 in the input order. Otherwise leave the second line empty. Separate the numbers with one space.
null
null
[{"input": "2\n2\n10 15\n5 7\n20 18\n1\n0 0", "output": "YES\n1 3\nYES\n1"}]
2,500
["constructive algorithms", "sortings"]
25
[{"input": "2\r\n2\r\n10 15\r\n5 7\r\n20 18\r\n1\r\n0 0\r\n", "output": "YES\r\n3 1\r\nYES\r\n1\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path, 'r') as f_in, open(submission_path, 'r') as f_sub: input_data = f_in.read().split() sub_lines = [line.strip() for line in f_sub.readlines()] ptr = 0 T = int(input_data[ptr]) ptr +=1 sub_ptr = 0 for _ in range(T): if sub_ptr >= len(sub_lines): print(0) return res_line = sub_lines[sub_ptr].strip() sub_ptr +=1 if res_line != 'YES': print(0) return if sub_ptr >= len(sub_lines): print(0) return boxes_line = sub_lines[sub_ptr].strip() sub_ptr +=1 if not boxes_line: print(0) return selected = list(map(int, boxes_line.split())) N = int(input_data[ptr]) ptr +=1 total_apples = 0 total_oranges = 0 boxes = [] for _ in range(2*N -1): a = int(input_data[ptr]) o = int(input_data[ptr+1]) ptr +=2 total_apples += a total_oranges += o boxes.append( (a, o) ) if len(selected) != N: print(0) return seen = set() sum_a = 0 sum_o = 0 for idx in selected: if idx <1 or idx > 2*N -1: print(0) return if idx in seen: print(0) return seen.add(idx) a, o = boxes[idx-1] sum_a += a sum_o += o req_a = (total_apples +1) //2 req_o = (total_oranges +1) //2 if sum_a < req_a or sum_o < req_o: print(0) return print(1) if __name__ == '__main__': main()
true
23/B
23
B
Python 3
TESTS
1
466
7,168,000
11601107
import sys from itertools import * from math import * def solve(): t = int(input()) res = list() for test in range(t): val = int(input()) res.append(val // 2) print(' '.join(map(str, res))) if sys.hexversion == 50594544 : sys.stdin = open("test.txt") solve()
2
530
6,860,800
11923591
num_tests = int(input()) results = num_tests * [None] for i in range(num_tests): n = int(input()) results[i] = max(0, n-2) print('\n'.join(map(str, results)))
Codeforces Beta Round 23
ICPC
2,010
2
256
Party
n people came to a party. Then those, who had no friends among people at the party, left. Then those, who had exactly 1 friend among those who stayed, left as well. Then those, who had exactly 2, 3, ..., n - 1 friends among those who stayed by the moment of their leaving, did the same. What is the maximum amount of people that could stay at the party in the end?
The first input line contains one number t — amount of tests (1 ≤ t ≤ 105). Each of the following t lines contains one integer number n (1 ≤ n ≤ 105).
For each test output in a separate line one number — the maximum amount of people that could stay in the end.
null
null
[{"input": "1\n3", "output": "1"}]
1,600
["constructive algorithms", "graphs", "math"]
2
[{"input": "1\r\n3\r\n", "output": "1\r\n"}]
false
stdio
null
true
23/C
23
C
PyPy 3
TESTS
1
654
15,052,800
85923068
import os,io from sys import stdout import collections # import random import math from operator import itemgetter input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline from collections import Counter # from decimal import Decimal # import heapq # from functools import lru_cache # import sys # sys.setrecursionlimit(10**6) # from functools import lru_cache # @lru_cache(maxsize=None) def primes(n): sieve = [True] * n for i in range(3,int(n**0.5)+1,2): if sieve[i]: sieve[i*i::2*i]=[False]*((n-i*i-1)//(2*i)+1) return [2] + [i for i in range(3,n,2) if sieve[i]] def binomial_coefficient(n, k): if 0 <= k <= n: ntok = 1 ktok = 1 for t in range(1, min(k, n - k) + 1): ntok *= n ktok *= t n -= 1 return ntok // ktok else: return 0 def powerOfK(k, max): if k == 1: return [1] if k == -1: return [-1, 1] result = [] n = 1 while n <= max: result.append(n) n *= k return result def prefixSum(arr): for i in range(1, len(arr)): arr[i] = arr[i] + arr[i-1] return arr def divisors(n): i = 1 result = [] while i*i <= n: if n%i == 0: if n/i == i: result.append(i) else: result.append(i) result.append(n/i) i+=1 return result def kadane(a,size): max_so_far = 0 max_ending_here = 0 for i in range(0, size): max_ending_here = max_ending_here + a[i] if (max_so_far < max_ending_here): max_so_far = max_ending_here if max_ending_here < 0: max_ending_here = 0 return max_so_far # @lru_cache(maxsize=None) def digitsSum(n): if n == 0: return 0 r = 0 while n > 0: r += n % 10 n //= 10 return r def print_grid(grid): for line in grid: print("".join(line)) # INPUTS -------------------------- # s = input().decode('utf-8').strip() # n = int(input()) # l = list(map(int, input().split())) # t = int(input()) # for _ in range(t): # for _ in range(t): t = int(input()) for _ in range(t): n = int(input()) boxes = [] i = 0 for _ in range(2*n-1): boxes.append(list(map(int, input().split()))+[i+1]) i+=1 boxes = sorted(boxes, key=itemgetter(0, 1)) even = boxes[::2] odd = boxes[1::2] oranges = int(math.ceil(sum([e[0] for e in boxes])/2)) apples = int(math.ceil(sum([e[1] for e in boxes])/2)) oeven = sum([e[0] for e in even]) aeven = sum([e[1] for e in even]) oodd = sum([e[0] for e in odd]) aodd = sum([e[1] for e in odd]) if oeven >= oranges and aeven >= apples: print('YES') res = [] for e in even: res.append(e[2]) print(" ".join([str(e) for e in sorted(res)])) elif oodd >= oranges and aodd >= apples: print('YES') res = [] for e in odd: res.append(e[2]) print(" ".join([str(e) for e in sorted(res)])) else: print('NO')
25
1,918
44,953,600
229815706
def II(): return(int(input())) def LMI(): return(list(map(int,input().split()))) def I(): return(input()) def MII(): return(map(int,input().split())) # import sys # input=sys.stdin.readline # import io,os # input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline # from collections import Counter # int(math.log(len(L))) # import math # from collections import defaultdict # mod=10**9+7 from collections import deque # import math def half(a): if a%2==0: return a//2 return a//2+1 for _ in range(II()): n=II() X=[] tot=0 tot1=0 tot2=0 for i in range(2*n-1): a,o=MII() X.append((a,o,i+1)) tot+=o print("YES") X.sort() for i in range(2*n-1): if i%2: tot1+=X[i][1] else: tot2+=X[i][1] if tot1>=half(tot): ans=[] for i in range(1,2*n-1,2): ans.append(X[i][2]) ans.append(X[-1][2]) print(*ans) else: ans=[] for i in range(0,2*n-1,2): ans.append(X[i][2]) print(*ans) # return # if __name__=="__main__": # for _ in range(II()): # t() # t()
Codeforces Beta Round 23
ICPC
2,010
1.5
256
Oranges and Apples
In 2N - 1 boxes there are apples and oranges. Your task is to choose N boxes so, that they will contain not less than half of all the apples and not less than half of all the oranges.
The first input line contains one number T — amount of tests. The description of each test starts with a natural number N — amount of boxes. Each of the following 2N - 1 lines contains numbers ai and oi — amount of apples and oranges in the i-th box (0 ≤ ai, oi ≤ 109). The sum of N in all the tests in the input doesn't exceed 105. All the input numbers are integer.
For each test output two lines. In the first line output YES, if it's possible to choose N boxes, or NO otherwise. If the answer is positive output in the second line N numbers — indexes of the chosen boxes. Boxes are numbered from 1 in the input order. Otherwise leave the second line empty. Separate the numbers with one space.
null
null
[{"input": "2\n2\n10 15\n5 7\n20 18\n1\n0 0", "output": "YES\n1 3\nYES\n1"}]
2,500
["constructive algorithms", "sortings"]
25
[{"input": "2\r\n2\r\n10 15\r\n5 7\r\n20 18\r\n1\r\n0 0\r\n", "output": "YES\r\n3 1\r\nYES\r\n1\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path, 'r') as f_in, open(submission_path, 'r') as f_sub: input_data = f_in.read().split() sub_lines = [line.strip() for line in f_sub.readlines()] ptr = 0 T = int(input_data[ptr]) ptr +=1 sub_ptr = 0 for _ in range(T): if sub_ptr >= len(sub_lines): print(0) return res_line = sub_lines[sub_ptr].strip() sub_ptr +=1 if res_line != 'YES': print(0) return if sub_ptr >= len(sub_lines): print(0) return boxes_line = sub_lines[sub_ptr].strip() sub_ptr +=1 if not boxes_line: print(0) return selected = list(map(int, boxes_line.split())) N = int(input_data[ptr]) ptr +=1 total_apples = 0 total_oranges = 0 boxes = [] for _ in range(2*N -1): a = int(input_data[ptr]) o = int(input_data[ptr+1]) ptr +=2 total_apples += a total_oranges += o boxes.append( (a, o) ) if len(selected) != N: print(0) return seen = set() sum_a = 0 sum_o = 0 for idx in selected: if idx <1 or idx > 2*N -1: print(0) return if idx in seen: print(0) return seen.add(idx) a, o = boxes[idx-1] sum_a += a sum_o += o req_a = (total_apples +1) //2 req_o = (total_oranges +1) //2 if sum_a < req_a or sum_o < req_o: print(0) return print(1) if __name__ == '__main__': main()
true
23/C
23
C
Python 3
TESTS
1
826
1,638,400
170147767
t = int(input()) for _ in range(t): n = int(input()) nums = [] for i in range(2*n-1): a, b = map(int, input().split()) nums.append([b, a, i+1]) s=set() nums = sorted(nums)[::-1] vals = [nums[0][2]] s.add(nums[0][2]) i = 1 while(i+1 < len(nums)//2): c = i # if(nums[i][1]<nums[i+1][1]): # c=i+1 vals.append(nums[c][2]) s.add(nums[c][2]) i += 1 nums = sorted(nums, key=lambda x: x[1])[::-1] i = 0 cnt=0 while cnt<n//2: # if(nums[i][1]<nums[i+1][1]): # c=i+1 if nums[i][2] not in s: vals.append(nums[i][2]) cnt+=1 i += 1 print("YES") print(*vals)
25
950
42,905,600
212533744
from sys import stdin # read = sys.stdin.readline T = int(stdin.readline()) for _ in range(T): N = int(stdin.readline()) M = N + N - 1 data = [ ] for i in range(M): data.append([int(w) for w in stdin.readline().split()] + [i + 1]) data.sort(reverse=True) # print(data) total = sum(d[1] for d in data) half = sum(d[1] for d in data[0::2]) print('YES') result = [] if half + half >= total: result.extend([d[2] for d in data[0::2]]) else: result.append(data[0][2]) result.extend([d[2] for d in data[1::2]]) print(*result)
Codeforces Beta Round 23
ICPC
2,010
1.5
256
Oranges and Apples
In 2N - 1 boxes there are apples and oranges. Your task is to choose N boxes so, that they will contain not less than half of all the apples and not less than half of all the oranges.
The first input line contains one number T — amount of tests. The description of each test starts with a natural number N — amount of boxes. Each of the following 2N - 1 lines contains numbers ai and oi — amount of apples and oranges in the i-th box (0 ≤ ai, oi ≤ 109). The sum of N in all the tests in the input doesn't exceed 105. All the input numbers are integer.
For each test output two lines. In the first line output YES, if it's possible to choose N boxes, or NO otherwise. If the answer is positive output in the second line N numbers — indexes of the chosen boxes. Boxes are numbered from 1 in the input order. Otherwise leave the second line empty. Separate the numbers with one space.
null
null
[{"input": "2\n2\n10 15\n5 7\n20 18\n1\n0 0", "output": "YES\n1 3\nYES\n1"}]
2,500
["constructive algorithms", "sortings"]
25
[{"input": "2\r\n2\r\n10 15\r\n5 7\r\n20 18\r\n1\r\n0 0\r\n", "output": "YES\r\n3 1\r\nYES\r\n1\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path, 'r') as f_in, open(submission_path, 'r') as f_sub: input_data = f_in.read().split() sub_lines = [line.strip() for line in f_sub.readlines()] ptr = 0 T = int(input_data[ptr]) ptr +=1 sub_ptr = 0 for _ in range(T): if sub_ptr >= len(sub_lines): print(0) return res_line = sub_lines[sub_ptr].strip() sub_ptr +=1 if res_line != 'YES': print(0) return if sub_ptr >= len(sub_lines): print(0) return boxes_line = sub_lines[sub_ptr].strip() sub_ptr +=1 if not boxes_line: print(0) return selected = list(map(int, boxes_line.split())) N = int(input_data[ptr]) ptr +=1 total_apples = 0 total_oranges = 0 boxes = [] for _ in range(2*N -1): a = int(input_data[ptr]) o = int(input_data[ptr+1]) ptr +=2 total_apples += a total_oranges += o boxes.append( (a, o) ) if len(selected) != N: print(0) return seen = set() sum_a = 0 sum_o = 0 for idx in selected: if idx <1 or idx > 2*N -1: print(0) return if idx in seen: print(0) return seen.add(idx) a, o = boxes[idx-1] sum_a += a sum_o += o req_a = (total_apples +1) //2 req_o = (total_oranges +1) //2 if sum_a < req_a or sum_o < req_o: print(0) return print(1) if __name__ == '__main__': main()
true
23/C
23
C
Python 3
TESTS
1
857
2,150,400
29838881
#! python3 T = int(input()) while T > 0: N = int(input()) boxes = [] apples = 0 oranges = 0 for i in range(2*N-1): boxes.append([int(x) for x in input().strip().split(' ')]) apples += boxes[-1][0] oranges += boxes[-1][1] boxes.sort(key=lambda x: x[0]) odd_apples = 0 for i in range(0, 2*N-1, 2): odd_apples += boxes[i][1] print('YES') if odd_apples * 2 >= apples: print(' '.join([str(x+1) for x in range(0, 2*N-1, 2)])) else: print(' '.join([str(x+1) for x in range(0, 2*N-1, 2)]) + ' ' + str(2*N-1)) T -= 1
25
1,045
29,593,600
154131565
# 解説を見た # 一見天才だが、「半分以上」を考えるとき、「ソートして隣同士を比較」は定石の一つ。 import sys input = sys.stdin.readline from operator import itemgetter T=int(input()) for tests in range(T): N=int(input()) X=[] for i in range(2*N-1): a,b=map(int,input().split()) X.append((a,b,i+1)) X.sort(key=itemgetter(0)) ANS=[] for i in range(N-1): if X[2*i][1]<X[2*i+1][1]: ANS.append(X[2*i+1][2]) else: ANS.append(X[2*i][2]) ANS.append(X[2*N-2][2]) print("YES") print(*ANS)
Codeforces Beta Round 23
ICPC
2,010
1.5
256
Oranges and Apples
In 2N - 1 boxes there are apples and oranges. Your task is to choose N boxes so, that they will contain not less than half of all the apples and not less than half of all the oranges.
The first input line contains one number T — amount of tests. The description of each test starts with a natural number N — amount of boxes. Each of the following 2N - 1 lines contains numbers ai and oi — amount of apples and oranges in the i-th box (0 ≤ ai, oi ≤ 109). The sum of N in all the tests in the input doesn't exceed 105. All the input numbers are integer.
For each test output two lines. In the first line output YES, if it's possible to choose N boxes, or NO otherwise. If the answer is positive output in the second line N numbers — indexes of the chosen boxes. Boxes are numbered from 1 in the input order. Otherwise leave the second line empty. Separate the numbers with one space.
null
null
[{"input": "2\n2\n10 15\n5 7\n20 18\n1\n0 0", "output": "YES\n1 3\nYES\n1"}]
2,500
["constructive algorithms", "sortings"]
25
[{"input": "2\r\n2\r\n10 15\r\n5 7\r\n20 18\r\n1\r\n0 0\r\n", "output": "YES\r\n3 1\r\nYES\r\n1\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path, 'r') as f_in, open(submission_path, 'r') as f_sub: input_data = f_in.read().split() sub_lines = [line.strip() for line in f_sub.readlines()] ptr = 0 T = int(input_data[ptr]) ptr +=1 sub_ptr = 0 for _ in range(T): if sub_ptr >= len(sub_lines): print(0) return res_line = sub_lines[sub_ptr].strip() sub_ptr +=1 if res_line != 'YES': print(0) return if sub_ptr >= len(sub_lines): print(0) return boxes_line = sub_lines[sub_ptr].strip() sub_ptr +=1 if not boxes_line: print(0) return selected = list(map(int, boxes_line.split())) N = int(input_data[ptr]) ptr +=1 total_apples = 0 total_oranges = 0 boxes = [] for _ in range(2*N -1): a = int(input_data[ptr]) o = int(input_data[ptr+1]) ptr +=2 total_apples += a total_oranges += o boxes.append( (a, o) ) if len(selected) != N: print(0) return seen = set() sum_a = 0 sum_o = 0 for idx in selected: if idx <1 or idx > 2*N -1: print(0) return if idx in seen: print(0) return seen.add(idx) a, o = boxes[idx-1] sum_a += a sum_o += o req_a = (total_apples +1) //2 req_o = (total_oranges +1) //2 if sum_a < req_a or sum_o < req_o: print(0) return print(1) if __name__ == '__main__': main()
true
612/E
612
E
Python 3
TESTS
0
31
0
15155208
n=int(input()) q=list(map(int, input().split())) p="" for x in range(n): if q[x] <= n : if q[x]!= q[q[x]-1]: p = p + ' '+ str(q[q[x]-1]) else: p='-1' else: break print(p.strip())
18
1,730
96,358,400
116414526
import sys import bisect from bisect import bisect_left as lb input_=lambda: sys.stdin.readline().strip("\r\n") from math import log from math import gcd from math import atan2,acos from random import randint sa=lambda :input_() sb=lambda:int(input_()) sc=lambda:input_().split() sd=lambda:list(map(int,input_().split())) se=lambda:float(input_()) sf=lambda:list(input_()) flsh=lambda: sys.stdout.flush() #sys.setrecursionlimit(10**6) mod=10**9+7 gp=[] cost=[] dp=[] mx=[] ans1=[] ans2=[] special=[] specnode=[] a=0 kthpar=[] def dfs(root,par): if par!=-1: dp[root]=dp[par]+1 for i in range(1,20): if kthpar[root][i-1]!=-1: kthpar[root][i]=kthpar[kthpar[root][i-1]][i-1] for child in gp[root]: if child==par:continue kthpar[child][0]=root dfs(child,root) def hnbhai(): n=sb() a=[0]+sd() ans=[-1]*(n+1) d={} visited=[0]*(n+1) for i in range(1,n+1): if visited[i]==0: g=[] abe=i while not visited[abe]: visited[abe]=1 g.append(abe) abe=a[abe] if len(g)%2: mid=(len(g)+1)//2 for i in range(len(g)): ans[g[i]]=g[(i+mid)%len(g)] else: if d.get(len(g)): temp=d[len(g)] for i in range(len(g)): ans[g[i]]=temp[(i+1)%len(g)] ans[temp[i]]=g[i] del d[len(g)] else: d[len(g)]=g if len(d): print(-1) return print(*ans[1:]) for _ in range(1): hnbhai()
Educational Codeforces Round 4
ICPC
2,015
2
256
Square Root of Permutation
A permutation of length n is an array containing each integer from 1 to n exactly once. For example, q = [4, 5, 1, 2, 3] is a permutation. For the permutation q the square of permutation is the permutation p that p[i] = q[q[i]] for each i = 1... n. For example, the square of q = [4, 5, 1, 2, 3] is p = q2 = [2, 3, 4, 5, 1]. This problem is about the inverse operation: given the permutation p you task is to find such permutation q that q2 = p. If there are several such q find any of them.
The first line contains integer n (1 ≤ n ≤ 106) — the number of elements in permutation p. The second line contains n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the elements of permutation p.
If there is no permutation q such that q2 = p print the number "-1". If the answer exists print it. The only line should contain n different integers qi (1 ≤ qi ≤ n) — the elements of the permutation q. If there are several solutions print any of them.
null
null
[{"input": "4\n2 1 4 3", "output": "3 4 2 1"}, {"input": "4\n2 1 3 4", "output": "-1"}, {"input": "5\n2 3 4 5 1", "output": "4 5 1 2 3"}]
2,200
["combinatorics", "constructive algorithms", "dfs and similar", "graphs", "math"]
18
[{"input": "4\r\n2 1 4 3\r\n", "output": "3 4 2 1\r\n"}, {"input": "4\r\n2 1 3 4\r\n", "output": "-1\r\n"}, {"input": "5\r\n2 3 4 5 1\r\n", "output": "4 5 1 2 3\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "10\r\n8 2 10 3 4 6 1 7 9 5\r\n", "output": "-1\r\n"}, {"input": "10\r\n3 5 1 2 10 8 7 6 4 9\r\n", "output": "6 9 8 10 4 3 7 1 5 2\r\n"}, {"input": "100\r\n11 9 35 34 51 74 16 67 26 21 14 80 84 79 7 61 28 3 53 43 42 5 56 36 69 30 22 88 1 27 65 91 46 31 59 50 17 96 25 18 64 55 78 2 63 24 95 48 93 13 38 76 89 94 15 90 45 81 52 87 83 73 44 49 23 82 85 75 86 33 47 19 58 97 37 20 40 10 92 4 6 68 77 54 71 12 62 60 100 39 41 99 72 29 57 8 70 32 66 98\r\n", "output": "-1\r\n"}, {"input": "100\r\n94 22 24 99 58 97 20 29 67 30 38 64 77 50 15 44 92 88 39 42 25 70 2 76 84 6 37 49 17 71 31 19 26 79 10 35 65 63 32 95 5 8 52 27 83 18 53 93 13 81 48 68 54 82 34 60 87 23 16 86 55 40 61 45 28 7 74 41 14 91 3 72 33 11 98 89 90 69 78 36 80 59 56 21 43 1 75 46 47 12 96 73 57 51 4 85 9 100 66 62\r\n", "output": "78 52 95 76 96 49 53 59 77 100 64 11 9 48 15 17 44 46 32 54 84 68 43 4 21 28 73 6 16 62 31 39 65 86 98 75 33 45 19 3 91 82 2 92 63 88 7 50 97 93 14 22 20 42 60 55 80 85 29 34 56 71 83 38 26 47 90 70 51 41 40 72 37 12 35 99 67 94 1 87 57 8 61 25 23 79 36 18 66 74 5 27 81 69 24 58 13 10 89 30\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] sub_path = sys.argv[3] with open(input_path, 'r') as f: n = int(f.readline()) p = list(map(int, f.readline().split())) with open(sub_path, 'r') as f: lines = f.readlines() sub_output = [] for line in lines: line = line.strip() if line == '-1': sub_output = [-1] break sub_output.extend(list(map(int, line.split()))) if len(sub_output) == 1 and sub_output[0] == -1: visited = [False] * (n + 1) cycle_counts = {} for i in range(1, n + 1): if not visited[i]: current = i cycle = [] while not visited[current]: visited[current] = True cycle.append(current) current = p[current - 1] cycle_length = len(cycle) if cycle_length % 2 == 0: cycle_counts[cycle_length] = cycle_counts.get(cycle_length, 0) + 1 valid = True for length, count in cycle_counts.items(): if count % 2 != 0: valid = False break print(1 if not valid else 0) else: if len(sub_output) != n: print(0) return seen = [False] * (n + 1) valid_perm = True for num in sub_output: if not (1 <= num <= n) or seen[num]: valid_perm = False break seen[num] = True if not valid_perm: print(0) return q = sub_output q_squared = [] for i in range(n): first = q[i] second = q[first - 1] q_squared.append(second) print(1 if q_squared == p else 0) if __name__ == "__main__": main()
true
613/B
613
B
PyPy 3
TESTS
0
124
0
91054884
n,A,cf,cm,mN = map(int,input().split()) a = list(map(int,input().split())) aCOPY = [] for elem in a: aCOPY.append(elem) a.sort() aPartialSum = [0] for elem in a: aPartialSum.append(aPartialSum[-1] + elem) maxScore = 0 ansMAXIBound = 0 ansMAXI = 0 ansMIN = 0 for MAXI in range(n + 1): currentScore = cf * MAXI if MAXI >= 1: mN -= (A - a[-MAXI]) if mN < 0: break if MAXI == n: maxScore = currentScore ansMAXIBound = 0 ansMAXI = 10 ** 10 ansMIN = 10 ** 10 # Find the maximum of minimum l = a[0] r = A while l < r: m = (l + r + 1) // 2 lA = 0 rA = n - MAXI - 1 while lA < rA: mA = (lA + rA) // 2 if a[mA] > m: rA = mA - 1 if a[mA] < m: lA = mA + 1 if a[mA] == m: lA = mA rA = mA break lA = min(lA,n - MAXI - 1) if a[lA] > m: lA -= 1 expenditure = (lA + 1) * m - aPartialSum[lA + 1] if MAXI == 1: print(expenditure) print(mN) if expenditure > mN: r = m - 1 else: l = m currentScore += l * cm if currentScore > maxScore: maxScore = currentScore ansMAXIBound = a[-MAXI] ansMAXI = MAXI ansMIN = l print(maxScore) inclCount = 0 for i in range(n): if aCOPY[i] > ansMAXIBound: aCOPY[i] = A inclCount += 1 for i in range(n): if aCOPY[i] == ansMAXIBound and inclCount < MAXI: aCOPY[i] = A inclCount += 1 if aCOPY[i] < ansMIN: aCOPY[i] = ansMIN print(" ".join(map(str,aCOPY)))
35
1,045
16,486,400
15377652
import itertools import bisect n, A, cf, cm, m = [int(x) for x in input().split()] skills = [int(x) for x in input().split()] sorted_skills = list(sorted((k, i) for i, k in enumerate(skills))) bottom_lift = [0 for i in range(n)] for i in range(1, n): bottom_lift[i] = bottom_lift[i-1] + i * (sorted_skills[i][0] - sorted_skills[i-1][0]) root_lift = [0 for i in range(n+1)] for i in range(1, n+1): root_lift[i] = root_lift[i-1] + A - sorted_skills[n-i][0] max_level = -1 for i in range(n+1): money_left = m - root_lift[i] if money_left < 0: break k = min(bisect.bisect(bottom_lift, money_left), n-i) money_left -= bottom_lift[k-1] min_level = min(A, sorted_skills[k-1][0] + money_left//k) if k > 0 else A level = cf*i + cm*min_level if max_level < level: max_level = level argmax = i argmax_min_level = min_level argmax_k = k ans = [0 for i in range(n)] for i, skill in enumerate(sorted_skills): if i < argmax_k: ans[skill[1]] = argmax_min_level elif i >= n - argmax: ans[skill[1]] = A else: ans[skill[1]] = skill[0] print(max_level) for a in ans: print(a, end = ' ')
Codeforces Round 339 (Div. 1)
CF
2,016
2
256
Skills
Lesha plays the recently published new version of the legendary game hacknet. In this version character skill mechanism was introduced. Now, each player character has exactly n skills. Each skill is represented by a non-negative integer ai — the current skill level. All skills have the same maximum level A. Along with the skills, global ranking of all players was added. Players are ranked according to the so-called Force. The Force of a player is the sum of the following values: - The number of skills that a character has perfected (i.e., such that ai = A), multiplied by coefficient cf. - The minimum skill level among all skills (min ai), multiplied by coefficient cm. Now Lesha has m hacknetian currency units, which he is willing to spend. Each currency unit can increase the current level of any skill by 1 (if it's not equal to A yet). Help him spend his money in order to achieve the maximum possible value of the Force.
The first line of the input contains five space-separated integers n, A, cf, cm and m (1 ≤ n ≤ 100 000, 1 ≤ A ≤ 109, 0 ≤ cf, cm ≤ 1000, 0 ≤ m ≤ 1015). The second line contains exactly n integers ai (0 ≤ ai ≤ A), separated by spaces, — the current levels of skills.
On the first line print the maximum value of the Force that the character can achieve using no more than m currency units. On the second line print n integers a'i (ai ≤ a'i ≤ A), skill levels which one must achieve in order to reach the specified value of the Force, while using no more than m currency units. Numbers should be separated by spaces.
null
In the first test the optimal strategy is to increase the second skill to its maximum, and increase the two others by 1. In the second test one should increase all skills to maximum.
[{"input": "3 5 10 1 5\n1 3 1", "output": "12\n2 5 2"}, {"input": "3 5 10 1 339\n1 3 1", "output": "35\n5 5 5"}]
1,900
["binary search", "brute force", "dp", "greedy", "sortings", "two pointers"]
35
[{"input": "3 5 10 1 5\r\n1 3 1\r\n", "output": "12\r\n2 5 2 \r\n"}, {"input": "3 5 10 1 339\r\n1 3 1\r\n", "output": "35\r\n5 5 5 \r\n"}, {"input": "2 6 0 1 4\r\n5 1\r\n", "output": "5\r\n5 5 \r\n"}, {"input": "1 1000000000 1000 1000 1000000000000000\r\n0\r\n", "output": "1000000001000\r\n1000000000 \r\n"}, {"input": "1 100 1 2 30\r\n1\r\n", "output": "62\r\n31 \r\n"}, {"input": "1 100 1 2 30\r\n71\r\n", "output": "201\r\n100 \r\n"}, {"input": "1 1000000000 1000 1000 1000000000000000\r\n1000000000\r\n", "output": "1000000001000\r\n1000000000 \r\n"}, {"input": "5 5 10 20 50\r\n0 0 0 0 0\r\n", "output": "150\r\n5 5 5 5 5 \r\n"}, {"input": "5 5 10 20 50\r\n3 3 3 3 3\r\n", "output": "150\r\n5 5 5 5 5 \r\n"}, {"input": "4 5 3 7 15\r\n4 3 3 1\r\n", "output": "47\r\n5 5 5 5 \r\n"}, {"input": "3 6 4 6 8\r\n6 4 5\r\n", "output": "48\r\n6 6 6 \r\n"}]
false
stdio
null
true
893/E
893
E
PyPy 3-64
TESTS
4
1,918
14,745,600
150336846
mod = 1e9+7; from functools import lru_cache def modexp(a, n): ans = 1 while (n): if (n&1): ans = (ans*a)%mod a = (a*a)%mod n = n>>1 return ans @lru_cache(100000) def rec(x, y): if (y==0): return 0 if (y==1): return 1 # calculate factors O(sqrt(n)) ans = 0 f1 = 1 while (f1*f1 <= x): if (x%f1==0): ans = (ans + rec(x/f1, y-1))%mod f2 = x/f1 if (f2!=f1): ans = (ans + rec(x/f2, y-1))%mod f1 += 1 return ans for i in range(int(input())): x,y = list(map(int, input().split(' '))) print(int((rec(x,y) * modexp(2, y-1))%mod))
45
1,029
135,782,400
93454490
import sys mod = 10**9 + 7 max_n = 10**6 + 100 fac, inv = [1]*max_n, [1]*max_n for i in range(2, max_n): fac[i] = fac[i-1] * i % mod inv[-1] = pow(fac[-1], mod-2, mod) for i in range(max_n-1, 0, -1): inv[i-1] = inv[i] * i % mod def get_primes(n: int): from itertools import chain from array import array primes = [2, 3] is_prime = (array('b', (0, 0, 1, 1, 0, 1, 0)) + array('b', (1, 0, 0, 0, 1, 0))*((n-1)//6)) for i in chain.from_iterable((range(5, n+1, 6), range(7, n+1, 6))): if is_prime[i]: primes.append(i) for j in range(i*3, n+1, i*2): is_prime[j] = 0 return is_prime, primes _, primes = get_primes(10**3) q = int(sys.stdin.buffer.readline().decode('utf-8')) ans = [0]*q for i in range(q): x, y = map(int, sys.stdin.buffer.readline().decode('utf-8').split()) pfac = [] for p in primes: if x % p == 0: pfac.append(0) while x % p == 0: pfac[-1] += 1 x //= p if x > 1: pfac.append(1) res = 1 for c in pfac: res = (res * fac[c+y-1] * inv[y-1] * inv[c]) % mod res = res * pow(2, y-1, mod) % mod ans[i] = res sys.stdout.buffer.write('\n'.join(map(str, ans)).encode('utf-8'))
Educational Codeforces Round 33 (Rated for Div. 2)
ICPC
2,017
3
256
Counting Arrays
You are given two positive integer numbers x and y. An array F is called an y-factorization of x iff the following conditions are met: - There are y elements in F, and all of them are integer numbers; - $$\prod_{i=1}^{y} F_i = x$$. You have to count the number of pairwise distinct arrays that are y-factorizations of x. Two arrays A and B are considered different iff there exists at least one index i (1 ≤ i ≤ y) such that Ai ≠ Bi. Since the answer can be very large, print it modulo 109 + 7.
The first line contains one integer q (1 ≤ q ≤ 105) — the number of testcases to solve. Then q lines follow, each containing two integers xi and yi (1 ≤ xi, yi ≤ 106). Each of these lines represents a testcase.
Print q integers. i-th integer has to be equal to the number of yi-factorizations of xi modulo 109 + 7.
null
In the second testcase of the example there are six y-factorizations: - { - 4,  - 1}; - { - 2,  - 2}; - { - 1,  - 4}; - {1, 4}; - {2, 2}; - {4, 1}.
[{"input": "2\n6 3\n4 2", "output": "36\n6"}]
2,000
["combinatorics", "dp", "math", "number theory"]
45
[{"input": "2\r\n6 3\r\n4 2\r\n", "output": "36\r\n6\r\n"}, {"input": "1\r\n524288 1000000\r\n", "output": "645043186\r\n"}, {"input": "1\r\n65536 1000000\r\n", "output": "928522471\r\n"}, {"input": "1\r\n5612 11399\r\n", "output": "215664246\r\n"}]
false
stdio
null
true