code
stringlengths
46
24k
language
stringclasses
6 values
AST_depth
int64
3
30
alphanumeric_fraction
float64
0.2
0.75
max_line_length
int64
13
399
avg_line_length
float64
5.01
139
num_lines
int64
7
299
task
stringlengths
151
14k
source
stringclasses
3 values
n = int(input()) s = list(input()) cnt = 0 ans_list = [0] e_cnt = 0 for i in range(1, n): if s[i - 1] == 'E' and s[i] == 'E': cnt -= 1 e_cnt += 1 if s[i - 1] == 'E' and s[i] == 'W': cnt -= 0 if s[i - 1] == 'W' and s[i] == 'E': cnt += 0 e_cnt += 1 if s[i - 1] == 'W' and s[i] == 'W': cnt += 1 ans_list.append(cnt) print(min(ans_list) + e_cnt)
python
8
0.440111
36
18.944444
18
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
from collections import Counter n = int(input()) s = input() sn = Counter(s) l = 0 r = sn['E'] ans = r for i in range(n): if s[i] == 'W': l += 1 else: r -= 1 ans = min(ans, l + r) print(ans)
python
8
0.540404
31
13.142857
14
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
n = int(input()) s = input() ans = [0] * (n + 1) for i in range(n): if s[i] == 'E': ans[i + 1] = ans[i] + 1 else: ans[i + 1] = ans[i] print(min((i - ans[i] + ans[n] - ans[i + 1] for i in range(n))))
python
11
0.463415
64
21.777778
9
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
def count(left_w, right_e): return left_w + right_e def main(): n = int(input()) s = str(input()) w_count = s.count('W') e_count = n - w_count lst = [] left = 0 left_w = 0 right = n right_e = e_count for i in range(n): s_alp = s[i] right -= 1 if s_alp == 'E': right_e -= 1 lst.append(count(left_w, right_e)) left += 1 if s_alp == 'W': left_w += 1 minimum = min(lst) print(minimum) def __starting_point(): main() __starting_point()
python
10
0.554839
36
15.607143
28
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
n = int(input()) s = input() e = s.count('E') w = n - e ans = 10 ** 9 if s[0] == 'W': cnt = e else: cnt = e - 1 num = cnt for i in range(n - 1): if s[i] == s[i + 1] and s[i] == 'E': cnt -= 1 elif s[i] == s[i + 1]: cnt += 1 ans = min(ans, cnt, num) ans = max(ans, 0) print(max(ans, 0))
python
8
0.461017
37
15.388889
18
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
n = int(input()) s = list(input()) ans = [0] * n ans[0] += list(s[1:]).count('E') for i in range(1, n): if s[i] == 'E' and s[i - 1] == 'E': ans[i] = ans[i - 1] - 1 elif s[i] == 'E' and s[i - 1] == 'W' or (s[i - 1] == 'E' and s[i] == 'W'): ans[i] = ans[i - 1] else: ans[i] = ans[i - 1] + 1 print(min(ans))
python
11
0.429936
75
25.166667
12
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
N = int(input()) S = list(input()) W = [0] * N E = [0] * N W_pre = E_pre = 0 for i in range(N): if S[i] == 'W': W[i] = W_pre + 1 E[i] = E_pre else: W[i] = W_pre E[i] = E_pre + 1 W_pre = W[i] E_pre = E[i] ans = N for i in range(N): if i > 0: res1 = W[i - 1] else: res1 = 0 res2 = E[N - 1] - E[i] ans = min(ans, res1 + res2) print(ans)
python
9
0.460452
28
14.391304
23
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
N = int(input()) S = input() l = 0 r = S[1:N].count('E') ans = l + r for n in range(1, N): if S[n - 1] == 'W': l += 1 if S[n] == 'E': r -= 1 ans = min(ans, l + r) print(ans)
python
8
0.436464
22
14.083333
12
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
n = int(input()) s = input() count = s.count('E') ans = s.count('E') for i in range(len(s)): if s[i] == 'E': count -= 1 else: count += 1 ans = min(count, ans) print(ans)
python
8
0.536723
23
15.090909
11
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
import sys, re, os from collections import deque, defaultdict, Counter from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd from itertools import permutations, combinations, product, accumulate from operator import itemgetter, mul from copy import deepcopy from string import ascii_lowercase, ascii_uppercase, digits from functools import reduce from bisect import bisect_left, insort_left from heapq import heapify, heappush, heappop INPUT = lambda : sys.stdin.readline().rstrip() INT = lambda : int(INPUT()) MAP = lambda : list(map(int, INPUT().split())) S_MAP = lambda : list(map(str, INPUT().split())) LIST = lambda : list(map(int, INPUT().split())) S_LIST = lambda : list(map(str, INPUT().split())) sys.setrecursionlimit(10 ** 9) INF = float('inf') mod = 10 ** 9 + 7 def main(): N = INT() S = INPUT() (left, right) = (0, S[1:].count('E')) ans = left + right for i in range(N - 1): if S[i] == 'W': left += 1 if S[i + 1] == 'E': right -= 1 ans = min(ans, left + right) print(ans) def __starting_point(): main() __starting_point()
python
12
0.673488
73
28.861111
36
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
def solve(N, S): W_sum = [0] E_sum = [0] for s in list(S): if s == 'W': W_sum.append(W_sum[-1] + 1) E_sum.append(E_sum[-1]) else: W_sum.append(W_sum[-1]) E_sum.append(E_sum[-1] + 1) ans = N + 1 for i in range(1, N + 1): ans = min(W_sum[i - 1] + (E_sum[-1] - E_sum[i]), ans) print(ans) def __starting_point(): N = int(input()) S = input() solve(N, S) __starting_point()
python
14
0.513854
55
18.85
20
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
n = int(input()) s = list(input()) E = s.count('E') W = s.count('W') if s[0] == 'E': ans = E - 1 else: ans = E ans1 = [] ans1.append(ans) for x in range(1, n): if s[x] == 'E': if s[x - 1] == 'E': ans -= 1 elif s[x - 1] == 'W': ans += 1 ans1.append(ans) print(min(ans1))
python
9
0.478723
22
14.666667
18
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
def main(): N = int(input()) S = input() lst = [0] * N ans = N if S[0] == 'E': lst[0] = 1 for i in range(1, N): if S[i] == 'E': lst[i] = lst[i - 1] + 1 else: lst[i] = lst[i - 1] for i in range(N): if i == 0: left = 0 else: left = i - lst[i - 1] right = lst[N - 1] - lst[i] if left + right < ans: ans = left + right print(ans) def __starting_point(): main() __starting_point()
python
13
0.477108
29
15.6
25
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
N = int(input()) S = list(input()) E = S[1:].count('E') W = 0 mi = E + W for i in range(1, N): if S[i] == 'E': E -= 1 if S[i - 1] == 'W': W += 1 if E + W <= mi: mi = E + W print(mi)
python
8
0.413613
21
13.692308
13
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
N = int(input()) S = input() west = [0] * N east = [0] * N tmp = 0 for i in range(1, N): if S[i - 1] == 'W': tmp += 1 west[i] = tmp else: west[i] = tmp tmp = 0 for i in range(N - 2, -1, -1): if S[i + 1] == 'E': tmp += 1 east[i] = tmp else: east[i] = tmp ans = float('inf') for i in range(N): ans = min(ans, west[i] + east[i]) print(ans)
python
9
0.488701
34
15.090909
22
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
import sys N = int(input()) S = input() W = [0] * N E = [0] * N ans = N for i in range(1, N): W[i] = W[i - 1] + 1 if S[i - 1] == 'W' else W[i - 1] E[-i - 1] = E[-i] + 1 if S[-i] == 'E' else E[-i] for i in range(N): ans = min(W[i] + E[i], ans) print(ans)
python
9
0.451362
53
20.416667
12
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
n = int(input()) S = list(input()) W = [0] * (n + 1) E = [0] * (n + 1) for i in range(n): W[i] = W[i - 1] + S[i].count('W') E[n - i - 1] = E[n - i] + S[n - i - 1].count('E') W = W[:n] E = E[:n] ans = 1000000 for i in range(n): ans = min(ans, W[i] + E[i] - 1) print(ans)
python
11
0.435897
50
20
13
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
n = int(input()) s = input() W = 0 E = s[1:].count('E') ans = float('inf') for i in range(n): ans = min(ans, W + E) if i == n - 1: print(ans) return if s[i] == 'W': W += 1 if s[i + 1] == 'E': E -= 1
python
8
0.445498
22
14.071429
14
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
N = int(input()) S = input() e_all = S.count('E') w_cnt = 0 e_cnt = 0 ans = float('inf') for i in range(N): if S[i] == 'W': w_cnt += 1 ans = min(ans, w_cnt - 1 + e_all - e_cnt) else: e_cnt += 1 ans = min(ans, w_cnt + e_all - e_cnt) print(ans)
python
12
0.509881
43
17.071429
14
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
n = int(input()) s = input() from collections import Counter count = Counter(s[1:]) x = count['E'] temp = x for i in range(1, n): if s[i - 1] == 'W': x += 1 if s[i] == 'E': x -= 1 temp = min(temp, x) print(temp)
python
7
0.538813
31
15.846154
13
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
N = int(input()) S = input() west = S.count('W') east = N - west Q = [0, 0, west, east] ans = N - 1 for i in range(N): if S[i] == 'W': Q[2] -= 1 cnt = Q[0] + Q[3] Q[0] += 1 else: Q[3] -= 1 cnt = Q[0] + Q[3] Q[1] += 1 ans = min(ans, cnt) print(ans)
python
10
0.448669
22
14.470588
17
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
N = int(input()) r = input() E_sum = sum((x == 'E' for x in r)) W_sum = N - E_sum left_sum = 0 right_sum = E_sum min_sum = E_sum for i in range(N): if i == 0: if r[i] == 'E': min_sum -= 1 prev_sum = min_sum elif (r[i - 1], r[i]) == ('W', 'W'): prev_sum += 1 elif (r[i - 1], r[i]) == ('E', 'W'): pass elif (r[i - 1], r[i]) == ('E', 'E'): prev_sum -= 1 elif (r[i - 1], r[i]) == ('W', 'E'): pass if min_sum > prev_sum: min_sum = prev_sum print(min_sum)
python
9
0.460888
37
19.565217
23
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
n = int(input()) s = input() total_E = s.count('E') total_W = n - total_E right_E = [] tmp_E = total_E tmp_W = 0 for i in range(n): if s[i] == 'E': tmp_E -= 1 right_E.append(tmp_E) left_W = [0] for i in range(1, n): if s[i - 1] == 'W': tmp_W += 1 left_W.append(tmp_W) candidates = [] for i in range(n): candidates.append(right_E[i] + left_W[i]) ans = min(candidates) print(ans)
python
8
0.568475
42
17.428571
21
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
n = int(input()) s = [(i == 'W') * 1 for i in list(input())] c = [0] * (n + 1) for i in range(n): c[i + 1] = c[i] + s[i] ans = float('inf') for i in range(n): t = c[i] + (n - i - 1 - c[-1] + c[i + 1]) ans = min(ans, t) print(ans)
python
11
0.44206
43
22.3
10
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
inf = float('inf') n = int(input()) s = input() right_e = s.count('E') left_w = 0 cnt = inf for e in s: if e == 'W': cnt = min(cnt, right_e + left_w) left_w += 1 else: right_e -= 1 cnt = min(cnt, right_e + left_w) print(cnt)
python
11
0.540426
34
15.785714
14
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
N = int(input()) S = input() a = [0] for i in range(N): a.append(a[-1]) if S[i] == 'W': a[-1] += 1 ans = N for i in range(N): ans = min(ans, a[i] + N - (i + 1) - (a[N] - a[i + 1])) print(ans)
python
12
0.451777
55
16.909091
11
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
n = int(input()) s = input() wcnt = [0] * n ecnt = [0] * n enum = 0 wnum = 0 for i in range(n): if s[i] == 'W': wnum += 1 wcnt[i] += wnum if s[-i - 1] == 'E': enum += 1 ecnt[-i - 1] += enum ans = 10 ** 10 for i in range(n): temp = 0 if i - 1 >= 0: temp += wcnt[i - 1] if i + 1 < n: temp += ecnt[i + 1] ans = min(ans, temp) print(ans)
python
9
0.468571
21
14.909091
22
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
N = int(input()) S = input() ans = S.count('W') ans1 = -10 for i in range(N): if S[i] == 'W': ans -= 1 if i != 0: if S[i - 1] == 'E': ans += 1 ans1 = max(ans, ans1) print(N - 1 - ans1)
python
9
0.466667
22
15.25
12
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
n = int(input()) s = input() (l, r) = ([], []) ans = min(s.count('W'), s.count('E')) k = s.count('E') for i in range(n): if s[i] == 'W': k += 1 else: k -= 1 ans = min(ans, k) print(ans)
python
8
0.455959
37
15.083333
12
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
def main(): n = int(input()) ss = list(input()) num_sum = [0] * n west_sum = 0 east_sum = 0 for i in range(1, n): if ss[i - 1] == 'W': west_sum += 1 num_sum[i] += west_sum for i in range(n - 2, -1, -1): if ss[i + 1] == 'E': east_sum += 1 num_sum[i] += east_sum ans = min(num_sum) print(ans) def __starting_point(): main() __starting_point()
python
9
0.519126
31
17.3
20
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
from itertools import accumulate n = int(input()) s = list(input()) l = [1 if s[i] == 'E' else 0 for i in range(n)] l = list(accumulate(l)) ans = l[-1] - l[0] for i in range(1, n): ans = min(ans, i - l[i - 1] + l[-1] - l[i]) print(ans)
python
12
0.561181
47
25.333333
9
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
def schange(s): if s == 'E': return 1 else: return 0 n = int(input()) s = list(map(schange, input())) ssum = [0] * (n + 1) for i in range(n): ssum[i + 1] = ssum[i] + s[i] ans = float('inf') for i in range(1, n + 1): turnE = i - 1 - ssum[i - 1] turnW = ssum[-1] - ssum[i] ans = min(ans, turnE + turnW) print(ans)
python
9
0.540373
31
19.125
16
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
N = int(input()) S = input() oE = S.count('E') Wo = 0 L = [] for i in range(N): if i == 0: if S[i] == 'E': oE -= 1 else: if S[i - 1] == 'W': Wo += 1 if S[i] == 'E': oE -= 1 L.append(Wo + oE) if Wo + oE == 0: break print(min(L))
python
10
0.421687
21
12.833333
18
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
n = int(input()) s = input() li = [] for i in range(n): if s[i] == 'W': li.append(-1) else: li.append(1) cumsum = [li[0]] for i in range(n - 1): cumsum.append(cumsum[i] + li[i + 1]) l = cumsum.index(max(cumsum)) w_to_e = 0 e_to_w = 0 for i in range(n): if i < l: if li[i] == -1: w_to_e += 1 elif i > l: if li[i] == 1: e_to_w += 1 print(e_to_w + w_to_e)
python
10
0.508065
37
15.909091
22
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
from collections import deque N = int(input()) S = input() left = deque([0]) right = [0] for i in range(N): if S[i] == 'W': plus = 1 else: plus = 0 left.append(left[-1] + plus) if S[-i - 1] == 'E': plus = 1 else: plus = 0 right.append(right[-1] + plus) left.popleft() right = right[::-1] right.pop() print(min([left[i] + right[i] for i in range(N)]) - 1)
python
11
0.569106
54
17.45
20
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
N = int(input()) S = list(str(input())) east = S[1:].count('E') ans_min = east ans = east for l in range(1, N): if S[l - 1] == 'W': ans += 1 if S[l] == 'E': ans -= 1 if ans <= 0: ans_min = 0 break if ans < ans_min: ans_min = ans print(ans_min)
python
9
0.511628
23
15.125
16
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
N = int(input()) S = list(input()) dp_f = [0 for _ in range(N)] dp_b = [0 for _ in range(N)] for n in range(1, N): if S[n - 1] == 'W': tmp = 1 else: tmp = 0 dp_f[n] = dp_f[n - 1] + tmp for n in reversed(range(N - 1)): if S[n + 1] == 'E': tmp = 1 else: tmp = 0 dp_b[n] = dp_b[n + 1] + tmp MIN = N for n in range(N): if n == N - 1: MIN = min(MIN, dp_f[N - 1]) elif n == 0: MIN = min(MIN, dp_b[0]) else: MIN = min(MIN, dp_f[n] + dp_b[n]) print(MIN)
python
12
0.481876
35
17.76
25
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
n = int(input()) s = input() cnt_e = [0] * n cnt_w = [0] * n for i in range(n): if i == 0: if s[i] == 'E': cnt_e[i] = 1 else: cnt_w[i] = 1 elif s[i] == 'E': cnt_e[i] = cnt_e[i - 1] + 1 cnt_w[i] = cnt_w[i - 1] else: cnt_e[i] = cnt_e[i - 1] cnt_w[i] = cnt_w[i - 1] + 1 ans = 3 * 10 ** 5 + 1 for i in range(n): if s[i] == 'E': e = cnt_e[-1] - cnt_e[i] w = cnt_w[i] else: e = cnt_e[-1] - cnt_e[i] w = cnt_w[i] - 1 ans = min(ans, e + w) print(ans)
python
11
0.429474
29
17.269231
26
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
N = int(input()) S = input() S = 'N' + S + 'N' ans = 0 E = 0 W = S.count('W') for i in range(1, N + 1): if S[i - 1] == 'E': E += 1 if S[i] == 'W': W -= 1 tmp = E + W if tmp > ans: ans = tmp ans = N - 1 - ans print(ans)
python
7
0.423581
25
13.3125
16
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
n = int(input()) s = input() west_back = s.count('W') east_back = n - west_back west_front = 0 east_front = 0 ans = 10 ** 6 for i in range(n): if s[i] == 'W': west_front += 1 west_back -= 1 key = west_front + east_back - 1 else: east_front += 1 east_back -= 1 key = west_front + east_back ans = min(ans, key) print(ans)
python
9
0.576119
34
17.611111
18
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
N = int(input()) N_List = str(input()) C_Ans = N_List[1:].count('E') Ans = C_Ans for i in range(1, N): C_Ans = C_Ans + (0, 1)[N_List[i - 1] == 'W'] - (0, 1)[N_List[i] == 'E'] if C_Ans < Ans: Ans = C_Ans print(Ans)
python
11
0.5
72
23.222222
9
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
n = int(input()) s = input() cnt = s.count('E') ans = cnt for i in range(n): if s[i] == 'E': cnt -= 1 if ans > cnt: ans = cnt else: cnt += 1 print(ans)
python
9
0.503067
18
12.583333
12
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
import sys import numpy as np stdin = sys.stdin def ns(): return stdin.readline().rstrip() def ni(): return int(stdin.readline().rstrip()) def nm(): return list(map(int, stdin.readline().split())) def nl(): return list(map(int, stdin.readline().split())) def main(): n = ni() S = ns() E = np.cumsum([1 if s == 'E' else 0 for s in S][::-1])[::-1] W = np.cumsum([1 if s == 'W' else 0 for s in S]) ans = E + W print(np.min(ans) - 1) def __starting_point(): main() __starting_point()
python
13
0.597586
61
17.407407
27
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
N = int(input()) S = input() L = [0] * (N + 1) for i in range(N): if S[i] == 'E': L[i + 1] = L[i] + 1 else: L[i + 1] = L[i] print(min((i - L[i] + L[N] - L[i + 1] for i in range(N))))
python
11
0.417989
58
20
9
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
n = int(input()) s = input() dp = [0] * n for i in range(1, n): if s[i] == 'E': dp[0] += 1 for i in range(1, n): dp[i] = dp[i - 1] if s[i - 1] == 'W': dp[i] += 1 if s[i] == 'E': dp[i] -= 1 print(min(dp))
python
8
0.420561
21
15.461538
13
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
import sys import re from collections import deque, defaultdict, Counter from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd from itertools import accumulate, permutations, combinations, product from operator import itemgetter, mul from copy import deepcopy from string import ascii_lowercase, ascii_uppercase, digits from bisect import bisect, bisect_left from heapq import heappush, heappop from functools import reduce def input(): return sys.stdin.readline().strip() def INT(): return int(input()) def MAP(): return list(map(str, input().split())) def LIST(): return list(map(int, input().split())) sys.setrecursionlimit(10 ** 9) INF = float('inf') mod = 10 ** 9 + 7 N = INT() S = [_ for _ in input()] (left, right) = (0, S[1:].count('E')) ans = left + right for i in range(N - 1): if S[i] == 'W': left += 1 if S[i + 1] == 'E': right -= 1 ans = min(ans, left + right) print(ans)
python
12
0.686957
73
23.864865
37
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W. You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing. The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions. -----Constraints----- - 2 \leq N \leq 3 \times 10^5 - |S| = N - S_i is E or W. -----Input----- Input is given from Standard Input in the following format: N S -----Output----- Print the minimum number of people who have to change their directions. -----Sample Input----- 5 WEEWW -----Sample Output----- 1 Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.
taco
class Solution: def minFlipsMonoIncr(self, S: str) -> int: onesSoFar = 0 partial = 0 for n in S: if n == '0': partial = min(onesSoFar, partial + 1) else: onesSoFar += 1 return partial
python
14
0.605769
43
17.909091
11
A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), followed by some number of '1's (also possibly 0.) We are given a string S of '0's and '1's, and we may flip any '0' to a '1' or a '1' to a '0'. Return the minimum number of flips to make SΒ monotone increasing. Β  Example 1: Input: "00110" Output: 1 Explanation: We flip the last digit to get 00111. Example 2: Input: "010110" Output: 2 Explanation: We flip to get 011111, or alternatively 000111. Example 3: Input: "00011000" Output: 2 Explanation: We flip to get 00000000. Β  Note: 1 <= S.length <= 20000 S only consists of '0' and '1' characters.
taco
class Solution: def minFlipsMonoIncr(self, S: str) -> int: if not S: return 0 n = len(S) if n == 1: return 0 total_1s = 0 total_0s = 0 for char in S: if char == '1': total_1s += 1 else: total_0s += 1 if total_1s == 0 or total_0s == 0: return 0 prefix_sum = 0 ans = float('inf') for i in range(n): prefix_sum += 1 if S[i] == '1' else 0 ans = min(ans, prefix_sum + (n - i - 1 - (total_1s - prefix_sum))) return min(ans, total_0s, total_1s)
python
16
0.545825
69
20.347826
23
A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), followed by some number of '1's (also possibly 0.) We are given a string S of '0's and '1's, and we may flip any '0' to a '1' or a '1' to a '0'. Return the minimum number of flips to make SΒ monotone increasing. Β  Example 1: Input: "00110" Output: 1 Explanation: We flip the last digit to get 00111. Example 2: Input: "010110" Output: 2 Explanation: We flip to get 011111, or alternatively 000111. Example 3: Input: "00011000" Output: 2 Explanation: We flip to get 00000000. Β  Note: 1 <= S.length <= 20000 S only consists of '0' and '1' characters.
taco
class Solution: def minFlipsMonoIncr(self, S: str) -> int: at0 = 0 at1 = 0 num0 = 0 for a in S: if a == '0': at1 = min(at1, at0) + 1 else: at1 = min(at1, at0) at0 += 1 return min(at1, at0)
python
14
0.525114
43
15.846154
13
A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), followed by some number of '1's (also possibly 0.) We are given a string S of '0's and '1's, and we may flip any '0' to a '1' or a '1' to a '0'. Return the minimum number of flips to make SΒ monotone increasing. Β  Example 1: Input: "00110" Output: 1 Explanation: We flip the last digit to get 00111. Example 2: Input: "010110" Output: 2 Explanation: We flip to get 011111, or alternatively 000111. Example 3: Input: "00011000" Output: 2 Explanation: We flip to get 00000000. Β  Note: 1 <= S.length <= 20000 S only consists of '0' and '1' characters.
taco
class Solution: def minFlipsMonoIncr(self, S: str) -> int: str_len = len(S) count_arr = [[0, 0] for x in range(str_len)] one_start_idx = -1 for i in range(len(S)): if S[i] == '0': if i == 0: count_arr[i][0] += 1 count_arr[i][1] = 0 else: count_arr[i][0] = count_arr[i - 1][0] + 1 count_arr[i][1] = count_arr[i - 1][1] else: if i == 0: count_arr[i][1] += 1 else: count_arr[i][1] = count_arr[i - 1][1] + 1 count_arr[i][0] = count_arr[i - 1][0] if one_start_idx == -1: one_start_idx = i total_flips = [] total_flips.append(min(count_arr[str_len - 1][0], count_arr[str_len - 1][1])) for i in range(one_start_idx, str_len): if i == 0: total_flips.append(count_arr[str_len - 1][0] - count_arr[i][0]) elif i == str_len - 1: total_flips.append(count_arr[str_len - 1][0]) else: total_flips.append(count_arr[i - 1][1] + count_arr[str_len - 1][0] - count_arr[i][0]) return min(total_flips)
python
19
0.54065
89
29.75
32
A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), followed by some number of '1's (also possibly 0.) We are given a string S of '0's and '1's, and we may flip any '0' to a '1' or a '1' to a '0'. Return the minimum number of flips to make SΒ monotone increasing. Β  Example 1: Input: "00110" Output: 1 Explanation: We flip the last digit to get 00111. Example 2: Input: "010110" Output: 2 Explanation: We flip to get 011111, or alternatively 000111. Example 3: Input: "00011000" Output: 2 Explanation: We flip to get 00000000. Β  Note: 1 <= S.length <= 20000 S only consists of '0' and '1' characters.
taco
class Solution: def minFlipsMonoIncr(self, S: str) -> int: dp = 0 ones = 0 for c in S: if c == '0': dp = min(1 + dp, ones) else: dp = min(dp, 1 + ones) ones += 1 return dp
python
15
0.515
43
15.666667
12
A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), followed by some number of '1's (also possibly 0.) We are given a string S of '0's and '1's, and we may flip any '0' to a '1' or a '1' to a '0'. Return the minimum number of flips to make SΒ monotone increasing. Β  Example 1: Input: "00110" Output: 1 Explanation: We flip the last digit to get 00111. Example 2: Input: "010110" Output: 2 Explanation: We flip to get 011111, or alternatively 000111. Example 3: Input: "00011000" Output: 2 Explanation: We flip to get 00000000. Β  Note: 1 <= S.length <= 20000 S only consists of '0' and '1' characters.
taco
class Solution: def minFlipsMonoIncr(self, S: str) -> int: (flip, one) = (0, 0) for i in S: if i == '1': one += 1 else: flip += 1 flip = min(one, flip) return flip
python
12
0.529101
43
16.181818
11
A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), followed by some number of '1's (also possibly 0.) We are given a string S of '0's and '1's, and we may flip any '0' to a '1' or a '1' to a '0'. Return the minimum number of flips to make SΒ monotone increasing. Β  Example 1: Input: "00110" Output: 1 Explanation: We flip the last digit to get 00111. Example 2: Input: "010110" Output: 2 Explanation: We flip to get 011111, or alternatively 000111. Example 3: Input: "00011000" Output: 2 Explanation: We flip to get 00000000. Β  Note: 1 <= S.length <= 20000 S only consists of '0' and '1' characters.
taco
class Solution: def minFlipsMonoIncr(self, S: str) -> int: (n, prefix, total, res) = (len(S), 0, S.count('1'), sys.maxsize) for i in range(n + 1): res = min(res, prefix + len(S) - i - total + prefix) if i < n: prefix += 1 if S[i] == '1' else 0 return res
python
17
0.564103
66
29.333333
9
A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), followed by some number of '1's (also possibly 0.) We are given a string S of '0's and '1's, and we may flip any '0' to a '1' or a '1' to a '0'. Return the minimum number of flips to make SΒ monotone increasing. Β  Example 1: Input: "00110" Output: 1 Explanation: We flip the last digit to get 00111. Example 2: Input: "010110" Output: 2 Explanation: We flip to get 011111, or alternatively 000111. Example 3: Input: "00011000" Output: 2 Explanation: We flip to get 00000000. Β  Note: 1 <= S.length <= 20000 S only consists of '0' and '1' characters.
taco
class Solution: def findXY(self, a, b): import math n = math.gcd(a, b) x = a / n y = b / n if b / a == y / x: return [int(y), int(x)]
python
11
0.496644
26
15.555556
9
Given two values β€˜a’ and β€˜b’ that represent coefficients in β€œax – by = 0”, find the smallest values of x and y that satisfy the equation. It may also be assumed that x > 0, y > 0, a > 0 and b > 0. Example 1: Input: a = 25, b = 35 Output: 7 5 Explaination: 25*7 - 35*5 = 0. And x = 7 and y = 5 are the least possible values of x and y to get the equation solved. Example 2: Input: a = 3, b = 7 Output: 7 3 Explaination: For this case x = 7 and y = 3 are the least values of x and y to satisfy the equation. Your Task: You do not need to read input or print anything. Your task is to complete the function findXY() which takes a and b as input parameters and returns the least possible values of x and y to satisfy the equation. Expected Time Complexity: O(log(max(a, b))) Expected Auxiliary Space: O(1) Constraints: 1 ≀ a, b ≀ 10^{4}
taco
class Solution: def findXY(self, a, b): i = 2 n = min(a, b) while i != n + 1: if a % i == 0 and b % i == 0: a = a // i b = b // i else: i += 1 return (b, a)
python
12
0.405405
32
14.416667
12
Given two values β€˜a’ and β€˜b’ that represent coefficients in β€œax – by = 0”, find the smallest values of x and y that satisfy the equation. It may also be assumed that x > 0, y > 0, a > 0 and b > 0. Example 1: Input: a = 25, b = 35 Output: 7 5 Explaination: 25*7 - 35*5 = 0. And x = 7 and y = 5 are the least possible values of x and y to get the equation solved. Example 2: Input: a = 3, b = 7 Output: 7 3 Explaination: For this case x = 7 and y = 3 are the least values of x and y to satisfy the equation. Your Task: You do not need to read input or print anything. Your task is to complete the function findXY() which takes a and b as input parameters and returns the least possible values of x and y to satisfy the equation. Expected Time Complexity: O(log(max(a, b))) Expected Auxiliary Space: O(1) Constraints: 1 ≀ a, b ≀ 10^{4}
taco
class Solution: def findXY(self, a, b): def gcd(m, n): if n == 0: return m return gcd(n, m % n) result = a * b // gcd(a, b) x = result // a y = result // b return (x, y)
python
11
0.497409
29
15.083333
12
Given two values β€˜a’ and β€˜b’ that represent coefficients in β€œax – by = 0”, find the smallest values of x and y that satisfy the equation. It may also be assumed that x > 0, y > 0, a > 0 and b > 0. Example 1: Input: a = 25, b = 35 Output: 7 5 Explaination: 25*7 - 35*5 = 0. And x = 7 and y = 5 are the least possible values of x and y to get the equation solved. Example 2: Input: a = 3, b = 7 Output: 7 3 Explaination: For this case x = 7 and y = 3 are the least values of x and y to satisfy the equation. Your Task: You do not need to read input or print anything. Your task is to complete the function findXY() which takes a and b as input parameters and returns the least possible values of x and y to satisfy the equation. Expected Time Complexity: O(log(max(a, b))) Expected Auxiliary Space: O(1) Constraints: 1 ≀ a, b ≀ 10^{4}
taco
import math class Solution: def findXY(self, a, b): l = math.gcd(a, b) return [b // l, a // l]
python
9
0.568627
25
13.571429
7
Given two values β€˜a’ and β€˜b’ that represent coefficients in β€œax – by = 0”, find the smallest values of x and y that satisfy the equation. It may also be assumed that x > 0, y > 0, a > 0 and b > 0. Example 1: Input: a = 25, b = 35 Output: 7 5 Explaination: 25*7 - 35*5 = 0. And x = 7 and y = 5 are the least possible values of x and y to get the equation solved. Example 2: Input: a = 3, b = 7 Output: 7 3 Explaination: For this case x = 7 and y = 3 are the least values of x and y to satisfy the equation. Your Task: You do not need to read input or print anything. Your task is to complete the function findXY() which takes a and b as input parameters and returns the least possible values of x and y to satisfy the equation. Expected Time Complexity: O(log(max(a, b))) Expected Auxiliary Space: O(1) Constraints: 1 ≀ a, b ≀ 10^{4}
taco
class Solution: def findXY(self, a, b): if a > b: greater = a else: greater = b while True: if greater % a == 0 and greater % b == 0: lcm = greater break greater += 1 return (lcm // a, lcm // b)
python
11
0.537778
44
16.307692
13
Given two values β€˜a’ and β€˜b’ that represent coefficients in β€œax – by = 0”, find the smallest values of x and y that satisfy the equation. It may also be assumed that x > 0, y > 0, a > 0 and b > 0. Example 1: Input: a = 25, b = 35 Output: 7 5 Explaination: 25*7 - 35*5 = 0. And x = 7 and y = 5 are the least possible values of x and y to get the equation solved. Example 2: Input: a = 3, b = 7 Output: 7 3 Explaination: For this case x = 7 and y = 3 are the least values of x and y to satisfy the equation. Your Task: You do not need to read input or print anything. Your task is to complete the function findXY() which takes a and b as input parameters and returns the least possible values of x and y to satisfy the equation. Expected Time Complexity: O(log(max(a, b))) Expected Auxiliary Space: O(1) Constraints: 1 ≀ a, b ≀ 10^{4}
taco
from math import gcd class Solution: def findXY(self, a, b): m = gcd(a, b) l = [] l.append(b // m) l.append(a // m) return l
python
9
0.557971
24
12.8
10
Given two values β€˜a’ and β€˜b’ that represent coefficients in β€œax – by = 0”, find the smallest values of x and y that satisfy the equation. It may also be assumed that x > 0, y > 0, a > 0 and b > 0. Example 1: Input: a = 25, b = 35 Output: 7 5 Explaination: 25*7 - 35*5 = 0. And x = 7 and y = 5 are the least possible values of x and y to get the equation solved. Example 2: Input: a = 3, b = 7 Output: 7 3 Explaination: For this case x = 7 and y = 3 are the least values of x and y to satisfy the equation. Your Task: You do not need to read input or print anything. Your task is to complete the function findXY() which takes a and b as input parameters and returns the least possible values of x and y to satisfy the equation. Expected Time Complexity: O(log(max(a, b))) Expected Auxiliary Space: O(1) Constraints: 1 ≀ a, b ≀ 10^{4}
taco
class Solution: def findXY(self, a, b): p = max(a, b) q = min(a, b) for i in range(1, q + 1): if p * i % q == 0: break j = int(p * i / q) if p == a: return [i, j] else: return [j, i]
python
11
0.457143
27
15.153846
13
Given two values β€˜a’ and β€˜b’ that represent coefficients in β€œax – by = 0”, find the smallest values of x and y that satisfy the equation. It may also be assumed that x > 0, y > 0, a > 0 and b > 0. Example 1: Input: a = 25, b = 35 Output: 7 5 Explaination: 25*7 - 35*5 = 0. And x = 7 and y = 5 are the least possible values of x and y to get the equation solved. Example 2: Input: a = 3, b = 7 Output: 7 3 Explaination: For this case x = 7 and y = 3 are the least values of x and y to satisfy the equation. Your Task: You do not need to read input or print anything. Your task is to complete the function findXY() which takes a and b as input parameters and returns the least possible values of x and y to satisfy the equation. Expected Time Complexity: O(log(max(a, b))) Expected Auxiliary Space: O(1) Constraints: 1 ≀ a, b ≀ 10^{4}
taco
class Solution: def findXY(self, a, b): r = [0] * 2 if a > b: small = b else: small = a for i in range(1, small + 1): if a % i == 0 and b % i == 0: gcd = i lcm = a * b // gcd r[0] = lcm // a r[1] = lcm // b return r
python
11
0.449393
32
15.466667
15
Given two values β€˜a’ and β€˜b’ that represent coefficients in β€œax – by = 0”, find the smallest values of x and y that satisfy the equation. It may also be assumed that x > 0, y > 0, a > 0 and b > 0. Example 1: Input: a = 25, b = 35 Output: 7 5 Explaination: 25*7 - 35*5 = 0. And x = 7 and y = 5 are the least possible values of x and y to get the equation solved. Example 2: Input: a = 3, b = 7 Output: 7 3 Explaination: For this case x = 7 and y = 3 are the least values of x and y to satisfy the equation. Your Task: You do not need to read input or print anything. Your task is to complete the function findXY() which takes a and b as input parameters and returns the least possible values of x and y to satisfy the equation. Expected Time Complexity: O(log(max(a, b))) Expected Auxiliary Space: O(1) Constraints: 1 ≀ a, b ≀ 10^{4}
taco
import math class Solution: def findXY(self, a, b): h = math.gcd(a, b) l = a / h * b arr = [] a1 = l / a arr.append(int(a1)) a2 = l / b arr.append(int(a2)) return arr
python
10
0.537634
24
13.307692
13
Given two values β€˜a’ and β€˜b’ that represent coefficients in β€œax – by = 0”, find the smallest values of x and y that satisfy the equation. It may also be assumed that x > 0, y > 0, a > 0 and b > 0. Example 1: Input: a = 25, b = 35 Output: 7 5 Explaination: 25*7 - 35*5 = 0. And x = 7 and y = 5 are the least possible values of x and y to get the equation solved. Example 2: Input: a = 3, b = 7 Output: 7 3 Explaination: For this case x = 7 and y = 3 are the least values of x and y to satisfy the equation. Your Task: You do not need to read input or print anything. Your task is to complete the function findXY() which takes a and b as input parameters and returns the least possible values of x and y to satisfy the equation. Expected Time Complexity: O(log(max(a, b))) Expected Auxiliary Space: O(1) Constraints: 1 ≀ a, b ≀ 10^{4}
taco
from sys import stdin, stdout from collections import deque import sys from copy import deepcopy import math import collections from itertools import combinations def check(temp): for i in range(len(temp)): for j in range(i + 1, len(temp)): if temp[i] in enemy: if temp[j] in enemy[temp[i]]: return False if temp[j] in enemy: if temp[i] in enemy[temp[j]]: return False return True (n, m) = list(map(int, stdin.readline().split())) name = dict() back_name = dict() arr = [] for i in range(n): string = stdin.readline().strip() name[string] = i back_name[i] = string enemy = collections.defaultdict(dict) for i in range(m): (first, second) = list(stdin.readline().split()) enemy[name[first]][name[second]] = True enemy[name[second]][name[first]] = True arr = [x for x in range(n)] ans = [] num = 0 for i in range(1, n + 1): comb = combinations(arr, i) for i in comb: temp = list(i) if check(temp): if len(temp) > num: ans = temp num = len(temp) print(len(ans)) ans2 = [] for i in ans: ans2.append(back_name[i]) ans2.sort() for i in ans2: print(i)
python
13
0.652411
49
21.428571
49
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
from itertools import combinations def solve(): (n, m) = map(int, input().split()) name = sorted([input().strip() for i in range(n)]) bad = sorted([sorted(input().strip().split()) for i in range(m)]) for i in range(n, -1, -1): temp = sorted(map(sorted, combinations(name, i))) for k in temp: flag = 1 for j in map(sorted, combinations(k, 2)): if j in bad: flag = 0 break if flag: return k x = solve() print(len(x)) print(*x, sep='\n')
python
16
0.596603
66
23.789474
19
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
from itertools import product (n, m) = map(int, input().split()) vl = sorted([input() for i in range(n)]) cm = {vl[i]: i for i in range(n)} cl = [] for i in range(m): (a, b) = input().split() cl += [[cm[a], cm[b]]] ans = [(), 0] for x in list(product([1, 0], repeat=n)): flag = 0 for c in cl: if x[c[0]] == 1 == x[c[1]]: flag = 1 break if flag == 0 and ans[1] < x.count(1): ans = [x, x.count(1)] print(ans[1]) print('\n'.join([vl[i] for i in range(n) if ans[0][i] == 1]))
python
11
0.527721
61
24.631579
19
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
tmp = input().split(' ') N_ppl = int(tmp[0]) N_cstrt = int(tmp[1]) ppl_to_id = {} id_ppl = [0] * N_ppl cstrt = [0] * N_ppl for i in range(N_ppl): tmp = input() id_ppl[i] = tmp ppl_to_id[tmp] = i cstrt[i] = [] for i in range(N_cstrt): tmp = input().split() cstrt[ppl_to_id[tmp[0]]].append(ppl_to_id[tmp[1]]) how_many_ones = [0] * 2 ** N_ppl for N in range(2 ** N_ppl): i = 0 n = N while n > 0: i += n % 2 n //= 2 how_many_ones[N] = i def test_team(n): global N_ppl, cstrt Team = [0] * N_ppl for k in range(N_ppl): Team[k] = n % 2 n //= 2 for k in range(N_ppl): if Team[k]: for l in cstrt[k]: if Team[l]: return False return Team def main(): global N_ppl, how_many_ones for i in range(N_ppl, 0, -1): for j in range(0, 2 ** N_ppl): if how_many_ones[j] != i: continue T = test_team(j) if T: return T T = main() R = [] for i in range(len(T)): if T[i]: R.append(i) print(len(R)) for i in range(len(R)): R[i] = id_ppl[R[i]] R.sort() print('\n'.join(R))
python
12
0.541584
51
17.363636
55
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
(n, m) = map(int, input().split()) names = [] mp = {} for i in range(n): name = input() mp[name] = i names.append(name) mask = [0] * n for i in range(m): (a, b) = map(mp.get, input().split()) mask[a] |= 1 << b mask[b] |= 1 << a ans = 0 result = 0 def bcnt(x): return 0 if x == 0 else bcnt(x >> 1) + (x & 1) for val in range(1 << n): if bcnt(val) <= ans: continue valid = True for i in range(n): if 1 << i & val and val & mask[i]: valid = False break if valid: ans = bcnt(val) result = val print(ans) out = [] for i in range(n): if 1 << i & result: out.append(names[i]) for s in sorted(out): print(s)
python
11
0.549206
47
17
35
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
nums = input().split() numPeople = int(nums[0]) numPairs = int(nums[1]) names = [] for i in range(numPeople): names.append(input()) pair1 = [] pair2 = [] for i in range(numPairs): pair = input().split() pair1.append(pair[0]) pair2.append(pair[1]) numSets = pow(2, numPeople) bestSet = [] for i in range(1, numSets): currSet = [] for j in range(numPeople): if i & 1 << j > 0: currSet.append(names[j]) validSet = True for j in range(numPairs): person1 = pair1[j] person2 = pair2[j] if person1 in currSet and person2 in currSet: validSet = False break if validSet: if len(currSet) > len(bestSet): bestSet = currSet bestSet.sort() print(len(bestSet)) for i in bestSet: print(i)
python
11
0.657224
47
20.393939
33
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
from collections import defaultdict graph = defaultdict(list) (n, m) = list(map(int, input().split())) d = {} cnt = 0 for i in range(n): x = input() d[x] = cnt cnt += 1 arr = [] for i in range(m): (u, v) = list(map(str, input().split())) arr.append([u, v]) possibilities = [] for i in range(2 ** n): x = bin(i).split('b')[1] x = '0' * (n - len(x)) + x possibilities.append(x) ans = [] for i in possibilities: f = 0 for j in arr: if i[d[j[0]]] == '1' and i[d[j[1]]] == '1': f = 1 break if f == 0: ans.append(i) k = -1 u = -1 mat = [] for i in ans: y = i.count('1') if k < y: k = y u = i for i in range(len(u)): if u[i] == '1': for j in d: if d[j] == i: mat.append(j) break mat.sort() print(len(mat)) for i in mat: print(i)
python
13
0.524869
45
15.977778
45
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
def get_subsets(elements): if not elements: yield tuple() else: for subset_rec in get_subsets(elements[1:]): yield subset_rec yield ((elements[0],) + subset_rec) def is_valid_set(subset, pairs): subset = set(subset) ok = True for pair in pairs: ok = ok and (not (pair[0] in subset and pair[1] in subset)) return ok (n, m) = map(int, input().split()) names = tuple((input() for _ in range(n))) pairs = [input().split() for _ in range(m)] ok = lambda subset: is_valid_set(subset, pairs) team = max(filter(ok, get_subsets(names)), key=len) print(len(team)) for name in sorted(team): print(name)
python
14
0.661765
61
26.818182
22
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
f_in = input() f_in = f_in.split(' ') num_members = int(f_in[0]) num_bad_pairs = int(f_in[1]) members = [] bad_pair = [] bad_pair_dic = {} for i in range(num_members): members.append(input()) for i in range(num_bad_pairs): bad_pair = input().split(' ') if bad_pair[0] not in bad_pair_dic: bad_pair_dic[bad_pair[0]] = [] bad_pair_dic[bad_pair[0]].append(bad_pair[1]) if bad_pair[1] not in bad_pair_dic: bad_pair_dic[bad_pair[1]] = [] bad_pair_dic[bad_pair[1]].append(bad_pair[0]) answer = [] max_answer = [] max_iterations = num_members ** 2 num_iterations = 0 members.sort() def recursive_brute_force(members, answer): global max_iterations global num_iterations global max_answer num_iterations += 1 if num_iterations >= max_iterations: return if members == []: if len(answer) > len(max_answer): max_answer = answer[:] return else: new_members = members[:] for member in members: new_members.remove(member) flag = False for key in answer: if key in bad_pair_dic: if member in bad_pair_dic[key]: flag = True break else: continue if flag: continue new_answer = answer[:] new_answer.append(member) if len(new_answer) > len(max_answer): max_answer = new_answer[:] recursive_brute_force(new_members, new_answer) return if num_bad_pairs != 0: recursive_brute_force(members, answer) else: max_answer = members[:] max_answer.sort() print(len(max_answer)) for member in max_answer: print(member)
python
16
0.654338
49
22.983871
62
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
import sys from functools import lru_cache, cmp_to_key from heapq import merge, heapify, heappop, heappush from math import ceil, floor, gcd, sqrt, trunc, inf from collections import defaultdict as dd, deque, Counter as C from itertools import combinations as comb, permutations as perm from bisect import bisect_left as bl, bisect_right as br, bisect from time import perf_counter from fractions import Fraction mod = pow(10, 9) + 7 mod2 = 998244353 def data(): return sys.stdin.readline().strip() def out(*var, end='\n'): sys.stdout.write(' '.join(map(str, var)) + end) def l(): return list(sp()) def sl(): return list(ssp()) def sp(): return map(int, data().split()) def ssp(): return map(str, data().split()) def l1d(n, val=0): return [val for i in range(n)] def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)] def on(a): i = n cnt = [] while i >= 0: if a & 1 << i: cnt.append(i) i -= 1 return cnt def check(arr, index): for i in arr: if names[i] in graph[names[index]] or names[index] in graph[names[i]]: return False return True def recur(i=0, mask=0): global answer if i == n: temp = on(mask) if len(temp) > len(answer): answer = temp return if mask & 1 << i: return temp = on(mask) if check(temp, i): recur(i + 1, mask | 1 << i) recur(i + 1, mask) (n, m) = sp() graph = dd(set) names = [] for i in range(n): names.append(data()) for i in range(m): (u, v) = ssp() graph[u].add(v) graph[v].add(u) answer = [] recur() out(len(answer)) for i in range(len(answer)): answer[i] = names[answer[i]] for i in sorted(answer): out(i)
python
11
0.643973
72
19.0125
80
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
ii = lambda : int(input()) kk = lambda : map(int, input().split()) ll = lambda : list(kk()) (n, m) = kk() ppl = [input() for _ in range(n)] others = [set() for _ in range(n)] for _ in range(m): (a, b) = input().split() a = ppl.index(a) b = ppl.index(b) others[a].add(b) others[b].add(a) largest = set() for i in range(2 ** n): s = set() for j in range(n): if i & 2 ** j: if others[j] & s: break s.add(j) else: if len(s) > len(largest): largest = s print(len(largest)) print('\n'.join(sorted([ppl[x] for x in largest])))
python
10
0.554128
51
20.8
25
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
def bitmask(m, array): return [array[i] for i in range(len(array)) if m & 1 << i] (n, k) = map(int, input().split()) names = [input() for _ in range(n)] names.sort() bads = [input().split() for _ in range(k)] (m_count, bb) = (-1, []) for m in range(2 ** n): if bin(m).count('1') < m_count: continue b = bitmask(m, names) f = True for (x, y) in bads: if x in b and y in b: f = False if f: (m_count, bb) = (len(b), b) print(m_count) print('\n'.join(bb))
python
10
0.558887
59
23.578947
19
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
def good(a, bad): for i in bad: if i[0] in a and i[1] in a: return False return True def f(arr, bad, d): n = len(arr) mask = 0 ans = [] while mask < 1 << n: temp = [] for i in range(len(arr)): if mask & 1 << i: temp.append(i) if good(temp, bad): ans.append(temp) mask += 1 mx = max(ans, key=lambda s: len(s)) print(len(mx)) for i in mx: print(arr[i]) return '' (a, b) = map(int, input().strip().split()) blanck = [] for i in range(a): blanck.append(input()) d = {} blanck = sorted(blanck) for i in range(len(blanck)): d[blanck[i]] = i bad = [] for i in range(b): (x, y) = map(str, input().strip().split()) k = sorted((d[x], d[y])) bad.append(k) print(f(blanck, bad, d))
python
13
0.562588
43
18.216216
37
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
def GSB(x): counter = 0 while x != 0: counter += 1 x = x >> 1 return counter def friends(checker, enemy): for i in range(len(enemy)): if enemy[i][0] in checker and enemy[i][1] in checker: return 0 return 1 (people, conflict) = [int(x) for x in input().split()] person = [] enemy = [] highest = -1 highestarray = [] for i in range(people): person.append(input()) for j in range(conflict): enemy.append(input().split()) combinations = [int(x) for x in range(2 ** people)] for i in combinations: checker = [] j = 0 z = GSB(i) while j != z and i != 0: if i & 1 == 1: checker.append(person[j]) i = i >> 1 j += 1 if friends(checker, enemy): if len(checker) > highest: highest = len(checker) highestarray = checker print(highest) highestarray.sort() for i in range(highest): print(highestarray[i])
python
11
0.631769
55
20.307692
39
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
import itertools as it (n, m) = map(int, input().split()) edges = set() friends_M = {} for i in range(n): friends_M[input()] = i for _ in range(m): (a, b) = input().split() (a, b) = (friends_M[a], friends_M[b]) edges.add((a, b)) edges.add((b, a)) best = 0 best_vals = [] for subset in it.product([0, 1], repeat=n): ss = list(it.compress(range(n), subset)) good = True for i in range(len(ss)): for j in range(i + 1, len(ss)): if (ss[i], ss[j]) in edges: good = False if good: if len(ss) > best: best = len(ss) best_vals = ss print(best) res = [] for i in range(len(best_vals)): for (j, k) in friends_M.items(): if k == best_vals[i]: res += [j] break for name in sorted(res): print(name)
python
11
0.576177
43
20.878788
33
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
(n, m) = map(int, input().split()) N = [n] Names = {} Numbers = {} Enemies = [] for i in range(n): s = input() Names[s] = i Enemies.append([]) Numbers[i] = str(s) for i in range(m): (a, b) = input().split() Enemies[Names[a]].append(Names[b]) Enemies[Names[b]].append(Names[a]) Ans = [[]] def dp(N, i, Taken, Forbidden): if i == N: if len(Taken) > len(Ans[0]): Ans[0] = list(Taken) return if i not in Forbidden: dp(N, i + 1, Taken, Forbidden) dp(N, i + 1, Taken + [i], Forbidden + Enemies[i]) else: dp(N, i + 1, Taken, Forbidden) return dp(n, 0, [], []) print(len(Ans[0])) Ans = Ans[0] for i in range(len(Ans)): Ans[i] = Numbers[Ans[i]] Ans.sort() for item in Ans: print(item)
python
11
0.573257
51
19.085714
35
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
(n, m) = map(int, input().split()) ID = {} name_from_ID = {} for i in range(n): name = input() ID[name] = i name_from_ID[i] = name people_I_hate = [set() for i in range(n)] for j in range(m): (name1, name2) = input().split() people_I_hate[ID[name1]].add(ID[name2]) people_I_hate[ID[name2]].add(ID[name1]) def conflict(team, people_I_hate): for guy1 in team: for guy2 in team: if guy1 in people_I_hate[guy2] or guy2 in people_I_hate: return True return False ans = [] for mask in range(1 << n): team = [] for i in range(n): if mask & 1 << i: team.append(i) if not conflict(team, people_I_hate) and len(team) > len(ans): ans = [name_from_ID[x] for x in team] print(len(ans)) for name in sorted(ans): print(name)
python
11
0.625169
63
23.633333
30
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
def moins(M, N): L = [] for i in M: if i not in N: L += [i] return L S = str(input()) l = S.split(' ') (n, m) = (int(l[0]), int(l[1])) d = {} S = str(input()) L = [S] for i in range(n - 1): S = str(input()) (j, f) = (0, 0) while f == 0: if j == len(L): L += [S] f = 1 elif L[j] > S: L = L[:j] + [S] + L[j:] f = 1 else: j += 1 T = list(L) P = [] for m in range(m): S = str(input()) l = S.split(' ') (S1, S2) = (l[0], l[1]) if S1 in d: d[S1][0] += 1 d[S1] += [S2] else: d[S1] = [1, S2] T.remove(S1) P += [S1] if S2 in d: d[S2][0] += 1 d[S2] += [S1] else: d[S2] = [1, S1] T.remove(S2) P += [S2] m = [] O = [] k = 0 for i in d: m += [[i, moins(P, d[i][1:] + [i])]] for i in m: if i[-1] == []: if len(i[:-1]) > k: O = i[:-1] k = len(i[:-1]) for j in i[-1]: m += [i[:-1] + [j] + [moins(i[-1], d[j][1:] + [j])]] for i in O: if T == []: T = [i] else: for j in range(len(T)): if T[j] > i: T = T[:j] + [i] + T[j:] break if j == len(T) - 1: T += [i] print(len(T)) for i in T: print(i)
python
16
0.387488
54
14.521739
69
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
n_m = input() (n, m) = [int(s) for s in n_m.split()] names = [] reverse = dict() entente = [[True] * n for _ in range(n)] for i in range(n): name = input() names.append(name) reverse[name] = i entente[i][i] = False for i in range(m): (i1, i2) = [reverse[s] for s in input().split()] entente[i2][i1] = False entente[i1][i2] = False def rec(valides): best = [[False] * n, 0] participants_restants = sum(valides) for i in range(n): if valides[i] and participants_restants != sum(best[0]): res_temp = rec([valides[j] and entente[i][j] and (j > i) for j in range(n)]) if best[1] <= res_temp[1]: best = res_temp best[0][i] = True best[1] += 1 return best res = [names[i] for (i, b) in enumerate(rec([True] * n)[0]) if b] res.sort() print(len(res)) for s in res: print(s)
python
16
0.595238
79
24.741935
31
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
(n, m) = map(int, input().split()) p = {} for i in range(n): p[input()] = [] for i in range(m): (a, b) = input().split() p[a].append(b) p[b].append(a) (t, r, k) = ([], [], 0) for a in p: d = set(p[a]) for i in range(k): if a in t[i]: continue q = t[i] | d if len(q) == len(t[i]): r[i].append(a) else: t.append(q) r.append(r[i] + [a]) t.append(d) r.append([a]) k = len(t) (k, j) = (0, 0) for (i, x) in enumerate(r): if len(x) > k: (k, j) = (len(x), i) print(k) print('\n'.join(sorted(r[j])))
python
13
0.476099
34
17.034483
29
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
(n, m) = map(int, input().split()) names = dict() ind = dict() enemies = list() for i in range(n): cur = input() names[i] = cur ind[cur] = i enemies.append(set()) for i in range(m): (nm1, nm2) = input().split() enemies[ind[nm1]].add(ind[nm2]) enemies[ind[nm2]].add(ind[nm1]) ans = 0 for i in range(2 ** n): cur = set() for j in range(n): if i & 1 << j: cur.add(j) f = True for j in cur: for jj in enemies[j]: if jj in cur: f = False break if not f: break else: if len(cur) > ans: ans = len(cur) curans = cur ansarr = sorted([names[_] for _ in curans]) print(ans) for i in ansarr: print(i)
python
12
0.57346
43
17.085714
35
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
def check(b): for i in b: for j in b: if (a[i], a[j]) in pair or (a[j], a[i]) in pair: return False return True (n, m) = map(int, input().split()) a = [] ind = {} for i in range(n): a.append(input()) ind[a[-1]] = i graph = [[] for i in range(n)] pair = {} for i in range(m): (x, y) = input().split() pair[x, y] = 1 graph[ind[x]].append(ind[y]) graph[ind[y]].append(ind[x]) if m == 0: print(n) a.sort() for i in a: print(i) exit() ans = 0 string = '' for i in range(1, 2 ** n): b = bin(i)[2:] b = '0' * (n - len(b)) + b temp = [] for j in range(n): if b[j] == '1': temp.append(ind[a[j]]) if check(temp): if len(temp) > ans: ans = len(temp) string = b print(ans) ansl = [] for i in range(n): if string[i] == '1': ansl.append(a[i]) ansl.sort() for i in ansl: print(i)
python
12
0.525926
51
16.608696
46
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
(n, m) = map(int, input().split()) d1 = {} d2 = {} arr = [] for i in range(n): s = input() d1[s] = i arr.append([]) d2[i] = str(s) for i in range(m): (a, b) = input().split() arr[d1[a]].append(d1[b]) arr[d1[b]].append(d1[a]) from copy import deepcopy ans = [] def dp(n, i, f, e): global ans if i == n: if len(f) > len(ans): ans = list(f) return dp(n, i + 1, f, e) if i not in e: dp(n, i + 1, f + [i], e + arr[i]) dp(n, 0, [], []) print(len(ans)) for i in range(len(ans)): ans[i] = d2[ans[i]] ans.sort() for i in ans: print(i)
python
11
0.513661
35
16.15625
32
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
import sys, math def input(): return sys.stdin.readline().strip() def iinput(): return int(input()) def tinput(): return input().split() def rinput(): return map(int, tinput()) def rlinput(): return list(rinput()) (n, m) = rinput() names = [] for _ in range(n): names.append(input()) pairs = [] for _ in range(m): pairs.append(list(input().split())) total_sets = int(math.pow(2, n)) max_set = [] for i in range(1, total_sets): cur_set = [] for j in range(n): if i & 1 << j: cur_set.append(names[j]) for p in pairs: if p[0] in cur_set and p[1] in cur_set: break else: if len(cur_set) > len(max_set): max_set = cur_set max_set.sort() print(len(max_set)) print(*max_set, sep='\n')
python
12
0.61354
41
17.179487
39
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
import math from operator import itemgetter var = input('') var = var.split(' ') n = int(var[0]) m = int(var[1]) L = [] for i in range(n): name = input('') L.append(name) if m == 0: L.sort() print(len(L)) for i in range(len(L)): print(L[i]) else: def diffc(A, B): L = [] for x in A: if x not in B: L.append(x) return L d = {} K = [] L1 = [] for i in range(m): pair = input('') pair = pair.split(' ') K.append(pair) A = pair[0] B = pair[1] if A in d.keys(): d[A][0] += 1 d[A] += [B] else: d[A] = [1, B] if B in d.keys(): d[B][0] += 1 d[B] += [A] else: d[B] = [1, A] L1 = list(set(L) - set(list(d.keys()))) (T, R, S, i) = (list(d.keys()), [], [], 0) for x in T: R.append([x, diffc(T, [x] + d[x][1:])]) for y in R: for f in y[len(y) - 1]: R += [y[:len(y) - 1] + [f] + [diffc(y[len(y) - 1], [f] + d[f][1:])]] if y[len(y) - 1] == []: if len(y[:len(y) - 1]) > i: (i, S) = (len(y[:len(y) - 1]), y[:len(y) - 1]) L1 = L1 + S L1.sort() print(len(L1)) for i in L1: print(i)
python
20
0.454805
71
17.438596
57
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
import sys from math import ceil, floor team = [] result = [] nok = [[0 for j in range(17)] for i in range(17)] def check(x, sz): comb = [0] * sz p = sz - 1 while x: comb[p] = x & 1 p -= 1 x >>= 1 for i in range(sz): for j in range(i + 1, sz): if comb[i] == 1 and comb[j] == 1 and (nok[i][j] == 1): return return comb def main(): (n, m) = [int(i) for i in input().split()] for i in range(n): team.append(input()) for i in range(m): (a, b) = [i for i in input().split()] u = team.index(a) v = team.index(b) nok[u][v] = 1 nok[v][u] = 1 mx = 1 << n for k in range(mx): possible = check(k, n) if possible != None: result.append(possible) ans = [] comb_ans = [] ln = -1 for comb in result: cnt = comb.count(1) if cnt > ln: ln = cnt comb_ans = comb print(ln) for num in range(len(comb)): if comb_ans[num] == 1: ans.append(team[num]) print('\n'.join(sorted(ans))) main()
python
13
0.548283
57
18.416667
48
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
def do(): (n, m) = map(int, input().split(' ')) ban = [] names = [] res = [] for _ in range(n): names.append(input()) d = {name: i for (i, name) in enumerate(names)} for _ in range(m): (x, y) = input().split(' ') ban.append(1 << d[x] | 1 << d[y]) for state in range(1 << n): can = True for bs in ban: if state & bs == bs: can = False break if can: res.append(state) if not res: print(0) return 0 res.sort(key=lambda x: -bin(x).count('1')) rt = [] for i in range(n): if res[0] & 1 << i: rt.append(names[i]) rt.sort() print(len(rt)) for w in rt: print(w) return 0 do()
python
13
0.530645
48
17.787879
33
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
(cant_integrantes, cant_lleva_mal) = map(int, input().split(' ')) integrantes = [] lleva_mal = [] for i in range(cant_integrantes): integrantes.append(input()) for i in range(cant_lleva_mal): (n1, n2) = map(str, input().split(' ')) lista = [n1, n2] lista.sort() lleva_mal.append(lista) if cant_lleva_mal == 0: integrantes.sort() print(cant_integrantes) for nombre in integrantes: print(nombre) exit() bit = 1 << cant_integrantes total_combinaciones = [] for i in range(bit): combinacion = [] for j in range(cant_integrantes): if 1 << j & i: combinacion.append(integrantes[j]) combinacion.sort() total_combinaciones.append(tuple(combinacion)) total_combinaciones.sort(key=len, reverse=True) for tupla in total_combinaciones: flag = True for (n1, n2) in lleva_mal: if n1 in tupla and n2 in tupla: flag = False break if flag: break print(len(tupla)) for elemento in tupla: print(elemento)
python
11
0.693478
65
23.864865
37
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
global res, maxi res = [] maxi = 0 def check(op): global maxi, res flag = 1 if len(op) > maxi: d = {} for i in op: d[i] = None for i in range(m): if v[i][0] in d and v[i][1] in d: flag = 0 break if flag: maxi = len(op) res = op def func(ip, op): if len(ip) == 0: check(op) return op1 = op op2 = op + [ip[-1]] d = ip[:len(ip) - 1] func(d, op1) func(d, op2) (n, m) = map(int, input().split()) c = [] for i in range(n): c.append(input()) v = [] for i in range(m): v.append(list(map(str, input().split()))) func(c, []) print(maxi) res.sort() for i in res: print(i)
python
14
0.532125
42
14.175
40
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
(n, m) = map(int, input().split()) names = [] for i in range(n): names.append(input()) hate = [] for i in range(m): hate.append(list(input().split())) ans = set() for x in range(1, (1 << n) + 1): a = set() for i in range(n): if x & 1 << i: a.add(names[i]) flag = True for b in hate: if b[0] in a and b[1] in a: flag = False break if flag: if len(a) > len(ans): ans = a print(len(ans)) print(*sorted(list(ans)), sep='\n')
python
12
0.549327
35
18.391304
23
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
(n, m) = map(int, input().split()) l = [] for i in range(n): l.append(input()) c = [] for i in range(m): (s1, s2) = input().split() c.append([1 << l.index(s1), 1 << l.index(s2)]) ans = [] for i in range(1, 2 ** n): flag = 0 for k in c: if i & k[0] and i & k[1]: flag = 1 break if flag: continue j = 0 t = i d = [] while t > 0: if t & 1: d.append(l[j]) j += 1 t = t >> 1 if len(ans) < len(d): ans = d print(len(ans)) print(*sorted(ans), sep='\n')
python
11
0.48954
47
15.482759
29
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
def s(): [n, m] = list(map(int, input().split())) a = [input() for _ in range(n)] b = {input() for _ in range(m)} res = [[]] aa = len(a) * [False] def r(x=0): if x == n: p = [a[i] for i in range(n) if aa[i]] if len(p) <= len(res[0]): return for i in p: for j in p: if i + ' ' + j in b or j + ' ' + i in b: return res[0] = p return x else: aa[x] = True r(x + 1) aa[x] = False r(x + 1) r() res = res[0] res.sort() print(len(res)) print(*res, sep='\n') s()
python
17
0.45155
45
16.793103
29
When little Petya grew up and entered the university, he started to take part in АБМ contests. Later he realized that he doesn't like how the АБМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β€” Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members. To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. Input The first line contains two integer numbers n (1 ≀ n ≀ 16) β€” the number of volunteers, and m (<image>) β€” the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names β€” the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct. Output The first output line should contain the single number k β€” the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. Examples Input 3 1 Petya Vasya Masha Petya Vasya Output 2 Masha Petya Input 3 0 Pasha Lesha Vanya Output 3 Lesha Pasha Vanya
taco
class Solution: def maximumScore(self, n, x, y, a, b, p, q): max = 0 lst = [0, 0] for i in range(0, a + 1): for j in range(0, b + 1): if i * x + j * y > max and i * p + j * q <= n: max = i * x + j * y lst[0] = i lst[1] = j elif i * x + j * y == max and i * p + j * q <= n and (lst[0] < i): lst[0] = i lst[1] = j else: pass return lst
python
16
0.421594
70
21.882353
17
Nikit has to give a short contest of duration "n" minutes. The contest is divided into 2 sections-Easy and Hard. x and y marks will be awarded per problem for Easy and Hard respectively. Assume that he will take p minutes to solve an Easy problem and q minutes to solve a Hard problem successfully. There are a and b number of Easy and Hard problems respectively. Calculate how many problems of a particular section should he perform to get the maximum score in time. Note: Assume he will always try to solve the easiest problem. Example 1: Input: n = 180, x = 2, y = 5, a = 4 b = 6,p = 20, q = 40 Output: 1 4 Explanation: Maximum marks get scored when he solves 1 easy and 4 hard problems. Example 2: Input: n = 50, x = 5, y = 10, a = 5 b = 3, p = 10, q = 20 Output: 5 0 Explanation : Maximum marks gets scored when he solves 5 easy problems or 1 easy and 2 hard problems or 3 easy and 1 hard problem. But he always try to solve the easiest problem therefore solves 5 easy problems. Your Task: You don't need to read or print anything. Your task is to complete the function maximumScore() which takes n, x, y, a, b, p, and q as input parameter and returns a list which contains the total number of easy problems and hard problems required to solve to get the maximum score. Expected Time Complexity: O(a * b) Expected Space Complexity: O(1) Constraints: 1 <= n <= 1000 1 <= x < y <= 100 1 <= a, b <= 100 1 <= p < q <= 100
taco
class Solution: def maximumScore(self, n, x, y, a, b, p, q): m = x c = d = 0 for i in range(0, a + 1): for j in range(0, b + 1): if p * i + q * j <= n: if x * i + y * j >= m: m = x * i + y * j c = i d = j return (c, d)
python
17
0.397683
45
18.923077
13
Nikit has to give a short contest of duration "n" minutes. The contest is divided into 2 sections-Easy and Hard. x and y marks will be awarded per problem for Easy and Hard respectively. Assume that he will take p minutes to solve an Easy problem and q minutes to solve a Hard problem successfully. There are a and b number of Easy and Hard problems respectively. Calculate how many problems of a particular section should he perform to get the maximum score in time. Note: Assume he will always try to solve the easiest problem. Example 1: Input: n = 180, x = 2, y = 5, a = 4 b = 6,p = 20, q = 40 Output: 1 4 Explanation: Maximum marks get scored when he solves 1 easy and 4 hard problems. Example 2: Input: n = 50, x = 5, y = 10, a = 5 b = 3, p = 10, q = 20 Output: 5 0 Explanation : Maximum marks gets scored when he solves 5 easy problems or 1 easy and 2 hard problems or 3 easy and 1 hard problem. But he always try to solve the easiest problem therefore solves 5 easy problems. Your Task: You don't need to read or print anything. Your task is to complete the function maximumScore() which takes n, x, y, a, b, p, and q as input parameter and returns a list which contains the total number of easy problems and hard problems required to solve to get the maximum score. Expected Time Complexity: O(a * b) Expected Space Complexity: O(1) Constraints: 1 <= n <= 1000 1 <= x < y <= 100 1 <= a, b <= 100 1 <= p < q <= 100
taco
class Solution: def maximumScore(self, n, x, y, a, b, p, q): max_easy = min(n // p, a) max_hard = min((n - max_easy * p) // q, b) max_score = max_easy * x + max_hard * y for i in range(max_easy + 1): j = min((n - i * p) // q, b) score = i * x + j * y if score > max_score: max_score = score max_easy = i max_hard = j return [max_easy, max_hard]
python
15
0.52381
45
26
14
Nikit has to give a short contest of duration "n" minutes. The contest is divided into 2 sections-Easy and Hard. x and y marks will be awarded per problem for Easy and Hard respectively. Assume that he will take p minutes to solve an Easy problem and q minutes to solve a Hard problem successfully. There are a and b number of Easy and Hard problems respectively. Calculate how many problems of a particular section should he perform to get the maximum score in time. Note: Assume he will always try to solve the easiest problem. Example 1: Input: n = 180, x = 2, y = 5, a = 4 b = 6,p = 20, q = 40 Output: 1 4 Explanation: Maximum marks get scored when he solves 1 easy and 4 hard problems. Example 2: Input: n = 50, x = 5, y = 10, a = 5 b = 3, p = 10, q = 20 Output: 5 0 Explanation : Maximum marks gets scored when he solves 5 easy problems or 1 easy and 2 hard problems or 3 easy and 1 hard problem. But he always try to solve the easiest problem therefore solves 5 easy problems. Your Task: You don't need to read or print anything. Your task is to complete the function maximumScore() which takes n, x, y, a, b, p, and q as input parameter and returns a list which contains the total number of easy problems and hard problems required to solve to get the maximum score. Expected Time Complexity: O(a * b) Expected Space Complexity: O(1) Constraints: 1 <= n <= 1000 1 <= x < y <= 100 1 <= a, b <= 100 1 <= p < q <= 100
taco
class Solution: def maximumScore(self, n, x, y, a, b, p, q): max = 0 easy = 0 hard = 0 for i in range(a, -1, -1): for j in range(b + 1): if p * i + q * j <= n: if x * i + y * j > max: easy = i hard = j max = x * i + y * j return [easy, hard]
python
17
0.447183
45
19.285714
14
Nikit has to give a short contest of duration "n" minutes. The contest is divided into 2 sections-Easy and Hard. x and y marks will be awarded per problem for Easy and Hard respectively. Assume that he will take p minutes to solve an Easy problem and q minutes to solve a Hard problem successfully. There are a and b number of Easy and Hard problems respectively. Calculate how many problems of a particular section should he perform to get the maximum score in time. Note: Assume he will always try to solve the easiest problem. Example 1: Input: n = 180, x = 2, y = 5, a = 4 b = 6,p = 20, q = 40 Output: 1 4 Explanation: Maximum marks get scored when he solves 1 easy and 4 hard problems. Example 2: Input: n = 50, x = 5, y = 10, a = 5 b = 3, p = 10, q = 20 Output: 5 0 Explanation : Maximum marks gets scored when he solves 5 easy problems or 1 easy and 2 hard problems or 3 easy and 1 hard problem. But he always try to solve the easiest problem therefore solves 5 easy problems. Your Task: You don't need to read or print anything. Your task is to complete the function maximumScore() which takes n, x, y, a, b, p, and q as input parameter and returns a list which contains the total number of easy problems and hard problems required to solve to get the maximum score. Expected Time Complexity: O(a * b) Expected Space Complexity: O(1) Constraints: 1 <= n <= 1000 1 <= x < y <= 100 1 <= a, b <= 100 1 <= p < q <= 100
taco
class Solution: def maximumScore(self, n, x, y, a, b, p, q): a = [n, x, y, a, b, p, q] h = 0 k = [] for i in range(a[3], -1, -1): for j in range(a[4], -1, -1): c = a[5] * i + a[6] * j if c <= a[0]: l = i * a[1] + j * a[2] if h <= l: k.append([i, j, l]) h = l l = 0 p = [] for i in k: if i[2] == l: p.append(i) l = i[2] elif i[2] > l: p = [i] l = i[2] l = max(p) l.pop() return l if __name__ == '__main__': T = int(input()) for i in range(T): n = int(input()) (x, y) = input().split() (a, b) = input().split() (p, q) = input().split() x = int(x) y = int(y) a = int(a) b = int(b) p = int(p) q = int(q) ob = Solution() ans = ob.maximumScore(n, x, y, a, b, p, q) for _ in ans: print(_, end=' ') print()
python
17
0.407911
45
17.386364
44
Nikit has to give a short contest of duration "n" minutes. The contest is divided into 2 sections-Easy and Hard. x and y marks will be awarded per problem for Easy and Hard respectively. Assume that he will take p minutes to solve an Easy problem and q minutes to solve a Hard problem successfully. There are a and b number of Easy and Hard problems respectively. Calculate how many problems of a particular section should he perform to get the maximum score in time. Note: Assume he will always try to solve the easiest problem. Example 1: Input: n = 180, x = 2, y = 5, a = 4 b = 6,p = 20, q = 40 Output: 1 4 Explanation: Maximum marks get scored when he solves 1 easy and 4 hard problems. Example 2: Input: n = 50, x = 5, y = 10, a = 5 b = 3, p = 10, q = 20 Output: 5 0 Explanation : Maximum marks gets scored when he solves 5 easy problems or 1 easy and 2 hard problems or 3 easy and 1 hard problem. But he always try to solve the easiest problem therefore solves 5 easy problems. Your Task: You don't need to read or print anything. Your task is to complete the function maximumScore() which takes n, x, y, a, b, p, and q as input parameter and returns a list which contains the total number of easy problems and hard problems required to solve to get the maximum score. Expected Time Complexity: O(a * b) Expected Space Complexity: O(1) Constraints: 1 <= n <= 1000 1 <= x < y <= 100 1 <= a, b <= 100 1 <= p < q <= 100
taco
import re def solve_for_x(equation): (left, right) = equation.split('=') answer = False TrialAndErrorRipMs = -1000 while answer == False: FinalLeft = re.sub('x', str(TrialAndErrorRipMs), left) FinalRight = re.sub('x', str(TrialAndErrorRipMs), right) if eval(FinalLeft) == eval(FinalRight): return TrialAndErrorRipMs TrialAndErrorRipMs += 1
python
11
0.710674
58
28.666667
12
# Solve For X You will be given an equation as a string and you will need to [solve for X](https://www.mathplacementreview.com/algebra/basic-algebra.php#solve-for-a-variable) and return x's value. For example: ```python solve_for_x('x - 5 = 20') # should return 25 solve_for_x('20 = 5 * x - 5') # should return 5 solve_for_x('5 * x = x + 8') # should return 2 solve_for_x('(5 - 3) * x = x + 2') # should return 2 ``` NOTES: * All numbers will be whole numbers * Don't forget about the [order of operations](https://www.mathplacementreview.com/algebra/basic-algebra.php#order-of-operations). * If the random tests don't pass the first time, just run them again.
taco