message
stringlengths
2
65.1k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
0
108k
cluster
float64
14
14
__index_level_0__
int64
0
217k
Provide a correct Python 3 solution for this coding contest problem. 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. Examples Input 5 WEEWW Output 1 Input 12 WEWEWEEEWWWE Output 4 Input 8 WWWWWEEE Output 3
instruction
0
27,222
14
54,444
"Correct Solution: ``` n=int(input()) s=input() cnt=s[1:].count('E') mi=cnt for i in range(1,n): if s[i]=='E': cnt-=1 if s[i-1]=='W': cnt+=1 mi=min(mi,cnt) print(mi) ```
output
1
27,222
14
54,445
Provide a correct Python 3 solution for this coding contest problem. 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. Examples Input 5 WEEWW Output 1 Input 12 WEWEWEEEWWWE Output 4 Input 8 WWWWWEEE Output 3
instruction
0
27,223
14
54,446
"Correct Solution: ``` N = int(input()) S = input() r = 0 for w in S[1:]: if w == 'E': r += 1 m = r for i in range(1,len(S)): if S[i] == 'E': r -= 1 if S[i-1] == 'W': r += 1 if r < m: m = r print(m) ```
output
1
27,223
14
54,447
Provide a correct Python 3 solution for this coding contest problem. 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. Examples Input 5 WEEWW Output 1 Input 12 WEWEWEEEWWWE Output 4 Input 8 WWWWWEEE Output 3
instruction
0
27,224
14
54,448
"Correct Solution: ``` n = int(input()) s = input() ans = 10**9 l = 0 r = 0 L = [0] R = [0] for i in range(n - 1): l += s[i].count('W') r += s[-i - 1].count('E') L.append(l) R.append(r) for l, r in zip(L, R[::-1]): ans = min(ans, l + r) print(ans) ```
output
1
27,224
14
54,449
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. 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. Examples Input 5 WEEWW Output 1 Input 12 WEWEWEEEWWWE Output 4 Input 8 WWWWWEEE Output 3 Submitted Solution: ``` input() S = input() tmp = sum([1 for s in S[1:] if s == "E"]) ans = tmp for i in range(1, len(S)): if S[i-1] == "W": tmp += 1 if S[i] == "E": tmp -= 1 ans = min(ans, tmp) print(ans) ```
instruction
0
27,226
14
54,452
Yes
output
1
27,226
14
54,453
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. 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. Examples Input 5 WEEWW Output 1 Input 12 WEWEWEEEWWWE Output 4 Input 8 WWWWWEEE Output 3 Submitted Solution: ``` N = int(input()) S = list(input()) ans = 0 for i in range(1,N): if S[i] == 'E': ans += 1 tmp = ans for i in range(1,N): if S[i-1] == 'W': tmp += 1 if S[i] == 'E': tmp -= 1 if tmp < ans: ans = tmp #print(tmp) print(ans) ```
instruction
0
27,227
14
54,454
Yes
output
1
27,227
14
54,455
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. 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. Examples Input 5 WEEWW Output 1 Input 12 WEWEWEEEWWWE Output 4 Input 8 WWWWWEEE Output 3 Submitted Solution: ``` n=int(input()) s=input() tmp=0 ans=10**6 W=s.count('W') for i in range(n): if s[i]=='W': ans=min(ans,n-1-(W-1)+tmp) tmp+=1 else: ans=min(ans,n-1-W+tmp) tmp-=1 print(ans) ```
instruction
0
27,228
14
54,456
Yes
output
1
27,228
14
54,457
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. 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. Examples Input 5 WEEWW Output 1 Input 12 WEWEWEEEWWWE Output 4 Input 8 WWWWWEEE Output 3 Submitted Solution: ``` a = int(input()) ar = list(input()) wc = 0 ec = ar.count("E") br = [] for i in range(a): if ar[i] == "E": ec -= 1 br.append(ec + wc) if ar[i] == "W": wc += 1 print(min(br)) ```
instruction
0
27,229
14
54,458
Yes
output
1
27,229
14
54,459
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. 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. Examples Input 5 WEEWW Output 1 Input 12 WEWEWEEEWWWE Output 4 Input 8 WWWWWEEE Output 3 Submitted Solution: ``` N = int(input()) S = input() count_e = [0] * N count_w = [0] * N for i in range(N): if S[i] == "E": count_e[i] = 1 else: count_w[i] = 1 ans = float('inf') for i in range(N): ans = min(ans, sum(count_w[:i]) + sum(count_e[i:])) print(ans) ```
instruction
0
27,230
14
54,460
No
output
1
27,230
14
54,461
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. 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. Examples Input 5 WEEWW Output 1 Input 12 WEWEWEEEWWWE Output 4 Input 8 WWWWWEEE Output 3 Submitted Solution: ``` #include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int N; cin >> N; string S; cin >> S; vector<int> ruiseki_E(N, 0); vector<int> ruiseki_W(N, 0); for (int i = 1; i < N; ++i) { if (S[i - 1] == 'W') { ruiseki_E[i] = ruiseki_E[i - 1] + 1; } } for (int i = N - 2; i != -1; --i) { if (S[i - 1] == 'E') { ruiseki_E[i] = ruiseki_E[i + 1] + 1; } } int ans = ruiseki_E[0] + ruiseki_W[0]; for (int i = 1; i < N; ++i) { ans = min(ans, ruiseki_E[0] + ruiseki_W[0]); } cout << ans << endl; return 0; } ```
instruction
0
27,231
14
54,462
No
output
1
27,231
14
54,463
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. 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. Examples Input 5 WEEWW Output 1 Input 12 WEWEWEEEWWWE Output 4 Input 8 WWWWWEEE Output 3 Submitted Solution: ``` N = int(input()) S = list(input()) ans = N + 1 for i in range(1,N - 1): E = S[:i - 1].count('W') W = S[i + 1:].count('E') ans = min(ans, E + W) first = S[0:].count('E') end = S[:N - 1].count('W') ans = min(ans, first, end) print(ans) ```
instruction
0
27,232
14
54,464
No
output
1
27,232
14
54,465
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. 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. Examples Input 5 WEEWW Output 1 Input 12 WEWEWEEEWWWE Output 4 Input 8 WWWWWEEE Output 3 Submitted Solution: ``` #!/usr/bin/env python E = "E" # -> W = "W" # <- class Line: def __init__(self, people): self.people = people def get_change_count(self, leader): leader_i = leader - 1 count = 0 for i in range(len(self.people)): if i == leader_i: continue elif i < leader_i: if self.people[i] == W: count += 1 elif leader_i < i: if self.people[i] == E: count += 1 else: assert () return count def main(): people_count = int(input().rstrip()) people = input().rstrip() line = Line(people) min_leader_no = None min_change_count = float('inf') for i in range(1, people_count + 1): change_count = line.get_change_count(i) if change_count < min_change_count: min_leader_no = i min_change_count = change_count if min_leader_no <= 1: break print(min_change_count) main() ```
instruction
0
27,233
14
54,466
No
output
1
27,233
14
54,467
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has recently got a job as a cashier at a local store. His day at work is L minutes long. Vasya has already memorized n regular customers, the i-th of which comes after t_{i} minutes after the beginning of the day, and his service consumes l_{i} minutes. It is guaranteed that no customer will arrive while Vasya is servicing another customer. Vasya is a bit lazy, so he likes taking smoke breaks for a minutes each. Those breaks may go one after another, but Vasya must be present at work during all the time periods he must serve regular customers, otherwise one of them may alert his boss. What is the maximum number of breaks Vasya can take during the day? Input The first line contains three integers n, L and a (0 ≀ n ≀ 10^{5}, 1 ≀ L ≀ 10^{9}, 1 ≀ a ≀ L). The i-th of the next n lines contains two integers t_{i} and l_{i} (0 ≀ t_{i} ≀ L - 1, 1 ≀ l_{i} ≀ L). It is guaranteed that t_{i} + l_{i} ≀ t_{i + 1} and t_{n} + l_{n} ≀ L. Output Output one integer β€” the maximum number of breaks. Examples Input 2 11 3 0 1 1 1 Output 3 Input 0 5 2 Output 2 Input 1 3 2 1 2 Output 0 Note In the first sample Vasya can take 3 breaks starting after 2, 5 and 8 minutes after the beginning of the day. In the second sample Vasya can take 2 breaks starting after 0 and 2 minutes after the beginning of the day. In the third sample Vasya can't take any breaks.
instruction
0
27,413
14
54,826
Tags: implementation Correct Solution: ``` n, L, a = map(int, input().split()) s = 0 p = 0 for i in range(n): t, l = map(int, input().split()) s += (t - p) // a p = l + t s += (L - p) // a print(s) ```
output
1
27,413
14
54,827
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has recently got a job as a cashier at a local store. His day at work is L minutes long. Vasya has already memorized n regular customers, the i-th of which comes after t_{i} minutes after the beginning of the day, and his service consumes l_{i} minutes. It is guaranteed that no customer will arrive while Vasya is servicing another customer. Vasya is a bit lazy, so he likes taking smoke breaks for a minutes each. Those breaks may go one after another, but Vasya must be present at work during all the time periods he must serve regular customers, otherwise one of them may alert his boss. What is the maximum number of breaks Vasya can take during the day? Input The first line contains three integers n, L and a (0 ≀ n ≀ 10^{5}, 1 ≀ L ≀ 10^{9}, 1 ≀ a ≀ L). The i-th of the next n lines contains two integers t_{i} and l_{i} (0 ≀ t_{i} ≀ L - 1, 1 ≀ l_{i} ≀ L). It is guaranteed that t_{i} + l_{i} ≀ t_{i + 1} and t_{n} + l_{n} ≀ L. Output Output one integer β€” the maximum number of breaks. Examples Input 2 11 3 0 1 1 1 Output 3 Input 0 5 2 Output 2 Input 1 3 2 1 2 Output 0 Note In the first sample Vasya can take 3 breaks starting after 2, 5 and 8 minutes after the beginning of the day. In the second sample Vasya can take 2 breaks starting after 0 and 2 minutes after the beginning of the day. In the third sample Vasya can't take any breaks.
instruction
0
27,414
14
54,828
Tags: implementation Correct Solution: ``` n,L,div=list(map(int,input().split())) ans=0 x=[[0,0]] for o in range(n): a,b=list(map(int,input().split())) x.append([a,b+a]) x.append([L,L]) for i in range(1,len(x)): ans=ans+(x[i][0]-x[i-1][1])//div print(ans) ```
output
1
27,414
14
54,829
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has recently got a job as a cashier at a local store. His day at work is L minutes long. Vasya has already memorized n regular customers, the i-th of which comes after t_{i} minutes after the beginning of the day, and his service consumes l_{i} minutes. It is guaranteed that no customer will arrive while Vasya is servicing another customer. Vasya is a bit lazy, so he likes taking smoke breaks for a minutes each. Those breaks may go one after another, but Vasya must be present at work during all the time periods he must serve regular customers, otherwise one of them may alert his boss. What is the maximum number of breaks Vasya can take during the day? Input The first line contains three integers n, L and a (0 ≀ n ≀ 10^{5}, 1 ≀ L ≀ 10^{9}, 1 ≀ a ≀ L). The i-th of the next n lines contains two integers t_{i} and l_{i} (0 ≀ t_{i} ≀ L - 1, 1 ≀ l_{i} ≀ L). It is guaranteed that t_{i} + l_{i} ≀ t_{i + 1} and t_{n} + l_{n} ≀ L. Output Output one integer β€” the maximum number of breaks. Examples Input 2 11 3 0 1 1 1 Output 3 Input 0 5 2 Output 2 Input 1 3 2 1 2 Output 0 Note In the first sample Vasya can take 3 breaks starting after 2, 5 and 8 minutes after the beginning of the day. In the second sample Vasya can take 2 breaks starting after 0 and 2 minutes after the beginning of the day. In the third sample Vasya can't take any breaks.
instruction
0
27,415
14
54,830
Tags: implementation Correct Solution: ``` a,b,c=map(int,input().split()) i=0 x=0 z=0 while i<a: p,q=map(int,input().split()) z+=(p-x)//c x=p+q i+=1 z+=(b-x)//c print (z) ```
output
1
27,415
14
54,831
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has recently got a job as a cashier at a local store. His day at work is L minutes long. Vasya has already memorized n regular customers, the i-th of which comes after t_{i} minutes after the beginning of the day, and his service consumes l_{i} minutes. It is guaranteed that no customer will arrive while Vasya is servicing another customer. Vasya is a bit lazy, so he likes taking smoke breaks for a minutes each. Those breaks may go one after another, but Vasya must be present at work during all the time periods he must serve regular customers, otherwise one of them may alert his boss. What is the maximum number of breaks Vasya can take during the day? Input The first line contains three integers n, L and a (0 ≀ n ≀ 10^{5}, 1 ≀ L ≀ 10^{9}, 1 ≀ a ≀ L). The i-th of the next n lines contains two integers t_{i} and l_{i} (0 ≀ t_{i} ≀ L - 1, 1 ≀ l_{i} ≀ L). It is guaranteed that t_{i} + l_{i} ≀ t_{i + 1} and t_{n} + l_{n} ≀ L. Output Output one integer β€” the maximum number of breaks. Examples Input 2 11 3 0 1 1 1 Output 3 Input 0 5 2 Output 2 Input 1 3 2 1 2 Output 0 Note In the first sample Vasya can take 3 breaks starting after 2, 5 and 8 minutes after the beginning of the day. In the second sample Vasya can take 2 breaks starting after 0 and 2 minutes after the beginning of the day. In the third sample Vasya can't take any breaks.
instruction
0
27,416
14
54,832
Tags: implementation Correct Solution: ``` n,l,k=map(int,input().split()) c=d=0 for i in range(n): p,q=map(int,input().split()) c+=int(abs(p-d)/k) d=p+q if d<l: c+=int((l-d)/k) print(c) ```
output
1
27,416
14
54,833
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has recently got a job as a cashier at a local store. His day at work is L minutes long. Vasya has already memorized n regular customers, the i-th of which comes after t_{i} minutes after the beginning of the day, and his service consumes l_{i} minutes. It is guaranteed that no customer will arrive while Vasya is servicing another customer. Vasya is a bit lazy, so he likes taking smoke breaks for a minutes each. Those breaks may go one after another, but Vasya must be present at work during all the time periods he must serve regular customers, otherwise one of them may alert his boss. What is the maximum number of breaks Vasya can take during the day? Input The first line contains three integers n, L and a (0 ≀ n ≀ 10^{5}, 1 ≀ L ≀ 10^{9}, 1 ≀ a ≀ L). The i-th of the next n lines contains two integers t_{i} and l_{i} (0 ≀ t_{i} ≀ L - 1, 1 ≀ l_{i} ≀ L). It is guaranteed that t_{i} + l_{i} ≀ t_{i + 1} and t_{n} + l_{n} ≀ L. Output Output one integer β€” the maximum number of breaks. Examples Input 2 11 3 0 1 1 1 Output 3 Input 0 5 2 Output 2 Input 1 3 2 1 2 Output 0 Note In the first sample Vasya can take 3 breaks starting after 2, 5 and 8 minutes after the beginning of the day. In the second sample Vasya can take 2 breaks starting after 0 and 2 minutes after the beginning of the day. In the third sample Vasya can't take any breaks.
instruction
0
27,417
14
54,834
Tags: implementation Correct Solution: ``` n, l, a = list(map(int, input().split())) arrives = [0] * n takes = [0] * n for i in range(n): arrives[i], takes[i] = list(map(int, input().split())) cur = 0 ans = 0 for i in range(n): gap = arrives[i] - cur ans += gap // a cur = arrives[i] + takes[i] ans += (l - cur) // a print(ans) ```
output
1
27,417
14
54,835
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has recently got a job as a cashier at a local store. His day at work is L minutes long. Vasya has already memorized n regular customers, the i-th of which comes after t_{i} minutes after the beginning of the day, and his service consumes l_{i} minutes. It is guaranteed that no customer will arrive while Vasya is servicing another customer. Vasya is a bit lazy, so he likes taking smoke breaks for a minutes each. Those breaks may go one after another, but Vasya must be present at work during all the time periods he must serve regular customers, otherwise one of them may alert his boss. What is the maximum number of breaks Vasya can take during the day? Input The first line contains three integers n, L and a (0 ≀ n ≀ 10^{5}, 1 ≀ L ≀ 10^{9}, 1 ≀ a ≀ L). The i-th of the next n lines contains two integers t_{i} and l_{i} (0 ≀ t_{i} ≀ L - 1, 1 ≀ l_{i} ≀ L). It is guaranteed that t_{i} + l_{i} ≀ t_{i + 1} and t_{n} + l_{n} ≀ L. Output Output one integer β€” the maximum number of breaks. Examples Input 2 11 3 0 1 1 1 Output 3 Input 0 5 2 Output 2 Input 1 3 2 1 2 Output 0 Note In the first sample Vasya can take 3 breaks starting after 2, 5 and 8 minutes after the beginning of the day. In the second sample Vasya can take 2 breaks starting after 0 and 2 minutes after the beginning of the day. In the third sample Vasya can't take any breaks.
instruction
0
27,418
14
54,836
Tags: implementation Correct Solution: ``` n,L,a=map(int,input().split()) c=0 emp=[] for i in range(n): l,t=map(int,input().split()) if emp: c+=(l-(emp[-1][0]+emp[-1][1]))//a else: c+=l//a emp.append([l,t]) if emp: c+=(L-(emp[-1][0]+emp[-1][1]))//a else: c+=L//a print(c) ```
output
1
27,418
14
54,837
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has recently got a job as a cashier at a local store. His day at work is L minutes long. Vasya has already memorized n regular customers, the i-th of which comes after t_{i} minutes after the beginning of the day, and his service consumes l_{i} minutes. It is guaranteed that no customer will arrive while Vasya is servicing another customer. Vasya is a bit lazy, so he likes taking smoke breaks for a minutes each. Those breaks may go one after another, but Vasya must be present at work during all the time periods he must serve regular customers, otherwise one of them may alert his boss. What is the maximum number of breaks Vasya can take during the day? Input The first line contains three integers n, L and a (0 ≀ n ≀ 10^{5}, 1 ≀ L ≀ 10^{9}, 1 ≀ a ≀ L). The i-th of the next n lines contains two integers t_{i} and l_{i} (0 ≀ t_{i} ≀ L - 1, 1 ≀ l_{i} ≀ L). It is guaranteed that t_{i} + l_{i} ≀ t_{i + 1} and t_{n} + l_{n} ≀ L. Output Output one integer β€” the maximum number of breaks. Examples Input 2 11 3 0 1 1 1 Output 3 Input 0 5 2 Output 2 Input 1 3 2 1 2 Output 0 Note In the first sample Vasya can take 3 breaks starting after 2, 5 and 8 minutes after the beginning of the day. In the second sample Vasya can take 2 breaks starting after 0 and 2 minutes after the beginning of the day. In the third sample Vasya can't take any breaks.
instruction
0
27,419
14
54,838
Tags: implementation Correct Solution: ``` import sys lines = iter(sys.stdin) def nexts(): return next(lines) def nextint(): return int(nexts()) def snexts(): return next(lines).split(' ') def snextint(): return map(int, snexts()) n, l, a = snextint() cust = [] cust.append((0, 0)) for _ in range(n): t, tlen = snextint() cust.append((t, t + tlen)) cust.append((l, 0)) cust.sort(key=lambda tup: tup[0]) count = 0 # print(cust) for ind, cus in enumerate(cust[:-1]): diff = cust[ind + 1][0] - cus[1] count += diff // a # print('for cus %s, diff of %d' % (str(cus), diff)) print(count) ```
output
1
27,419
14
54,839
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya has recently got a job as a cashier at a local store. His day at work is L minutes long. Vasya has already memorized n regular customers, the i-th of which comes after t_{i} minutes after the beginning of the day, and his service consumes l_{i} minutes. It is guaranteed that no customer will arrive while Vasya is servicing another customer. Vasya is a bit lazy, so he likes taking smoke breaks for a minutes each. Those breaks may go one after another, but Vasya must be present at work during all the time periods he must serve regular customers, otherwise one of them may alert his boss. What is the maximum number of breaks Vasya can take during the day? Input The first line contains three integers n, L and a (0 ≀ n ≀ 10^{5}, 1 ≀ L ≀ 10^{9}, 1 ≀ a ≀ L). The i-th of the next n lines contains two integers t_{i} and l_{i} (0 ≀ t_{i} ≀ L - 1, 1 ≀ l_{i} ≀ L). It is guaranteed that t_{i} + l_{i} ≀ t_{i + 1} and t_{n} + l_{n} ≀ L. Output Output one integer β€” the maximum number of breaks. Examples Input 2 11 3 0 1 1 1 Output 3 Input 0 5 2 Output 2 Input 1 3 2 1 2 Output 0 Note In the first sample Vasya can take 3 breaks starting after 2, 5 and 8 minutes after the beginning of the day. In the second sample Vasya can take 2 breaks starting after 0 and 2 minutes after the beginning of the day. In the third sample Vasya can't take any breaks.
instruction
0
27,420
14
54,840
Tags: implementation Correct Solution: ``` n,l,m=map(int,input().split()) a=[] for i in range(n): x,y=map(int,input().split()) a.append([x,x+y]) s=0 b=0 for i in range(n): z=a[i][0]-b s+=z//m b=a[i][1] s+=(l-b)//m print(s) ```
output
1
27,420
14
54,841
Provide tags and a correct Python 3 solution for this coding contest problem. All cinema halls in Berland are rectangles with K rows of K seats each, and K is an odd number. Rows and seats are numbered from 1 to K. For safety reasons people, who come to the box office to buy tickets, are not allowed to choose seats themselves. Formerly the choice was made by a cashier, but now this is the responsibility of a special seating program. It was found out that the large majority of Berland's inhabitants go to the cinema in order to watch a movie, that's why they want to sit as close to the hall center as possible. Moreover, a company of M people, who come to watch a movie, want necessarily to occupy M successive seats in one row. Let's formulate the algorithm, according to which the program chooses seats and sells tickets. As the request for M seats comes, the program should determine the row number x and the segment [yl, yr] of the seats numbers in this row, where yr - yl + 1 = M. From all such possible variants as a final result the program should choose the one with the minimum function value of total seats remoteness from the center. Say, <image> β€” the row and the seat numbers of the most "central" seat. Then the function value of seats remoteness from the hall center is <image>. If the amount of minimum function values is more than one, the program should choose the one that is closer to the screen (i.e. the row number x is lower). If the variants are still multiple, it should choose the one with the minimum yl. If you did not get yet, your task is to simulate the work of this program. Input The first line contains two integers N and K (1 ≀ N ≀ 1000, 1 ≀ K ≀ 99) β€” the amount of requests and the hall size respectively. The second line contains N space-separated integers Mi from the range [1, K] β€” requests to the program. Output Output N lines. In the i-th line output Β«-1Β» (without quotes), if it is impossible to find Mi successive seats in one row, otherwise output three numbers x, yl, yr. Separate the numbers with a space. Examples Input 2 1 1 1 Output 1 1 1 -1 Input 4 3 1 2 3 1 Output 2 2 2 1 1 2 3 1 3 2 1 1
instruction
0
27,445
14
54,890
Tags: dp, implementation Correct Solution: ``` n,k=map(int,input().split()) group=list(map(int,input().split())) available=[[k,1] for i in range(k+1)] center=(k+1)//2 def calc(center,row,col,num): end_col=col+num-1 distance=abs(center-row)*num if col>=center: distance+=(col-center)*num+(num-1)*num//2 elif end_col<=center: distance+=(center-end_col)*num+(num-1)*num//2 else: distance+=(center-col)*(center-col+1)//2+(end_col-center)*(end_col-center+1)//2 return distance for m in group: close,best_row,best_col=10**9,-1,-1 for row in range(1,k+1): col=0 if available[row][0]<m and k-available[row][1]+1<m: continue if available[row][0]==k: col=center-m//2 elif center-available[row][0]<=available[row][1]-center: col=available[row][0]-m+1 else: col=available[row][1] distance=calc(center,row,col,m) if distance<close: close=distance best_row=row best_col=col if close==10**9: print(-1) else: print(best_row,best_col,best_col+m-1) available[best_row][0]=min(available[best_row][0],best_col-1) available[best_row][1]=max(available[best_row][1],best_col+m) ```
output
1
27,445
14
54,891
Provide tags and a correct Python 3 solution for this coding contest problem. All cinema halls in Berland are rectangles with K rows of K seats each, and K is an odd number. Rows and seats are numbered from 1 to K. For safety reasons people, who come to the box office to buy tickets, are not allowed to choose seats themselves. Formerly the choice was made by a cashier, but now this is the responsibility of a special seating program. It was found out that the large majority of Berland's inhabitants go to the cinema in order to watch a movie, that's why they want to sit as close to the hall center as possible. Moreover, a company of M people, who come to watch a movie, want necessarily to occupy M successive seats in one row. Let's formulate the algorithm, according to which the program chooses seats and sells tickets. As the request for M seats comes, the program should determine the row number x and the segment [yl, yr] of the seats numbers in this row, where yr - yl + 1 = M. From all such possible variants as a final result the program should choose the one with the minimum function value of total seats remoteness from the center. Say, <image> β€” the row and the seat numbers of the most "central" seat. Then the function value of seats remoteness from the hall center is <image>. If the amount of minimum function values is more than one, the program should choose the one that is closer to the screen (i.e. the row number x is lower). If the variants are still multiple, it should choose the one with the minimum yl. If you did not get yet, your task is to simulate the work of this program. Input The first line contains two integers N and K (1 ≀ N ≀ 1000, 1 ≀ K ≀ 99) β€” the amount of requests and the hall size respectively. The second line contains N space-separated integers Mi from the range [1, K] β€” requests to the program. Output Output N lines. In the i-th line output Β«-1Β» (without quotes), if it is impossible to find Mi successive seats in one row, otherwise output three numbers x, yl, yr. Separate the numbers with a space. Examples Input 2 1 1 1 Output 1 1 1 -1 Input 4 3 1 2 3 1 Output 2 2 2 1 1 2 3 1 3 2 1 1
instruction
0
27,446
14
54,892
Tags: dp, implementation Correct Solution: ``` #10B def solve(): # n requests, k rows and seats on each row n, k = map(int, input().split()) # succesive seats group = map(int, input().split()) # make map with available seats available = [[k, 1][:] for i in range(k + 1)] center = (k + 1) // 2 # for each request for m in group: closest, best_row, best_col = 10000, -1, -1 # search places in each row for row in range(1, k + 1): col = 0 # if didn't fit, skip row if available[row][0] < m and k - available[row][1] + 1 < m: continue # if all empty choose center column if available[row][0] == k: col = center - m // 2 # else if fit in row, take seats elif center - available[row][0] <= available[row][1] - center: col = available[row][0] - m + 1 else: col = available[row][1] # compute distance to the screen distance = calc_distance(center, row, col, m) if distance < closest: closest = distance best_row = row best_col = col # if didn't found places if closest == 10000: print(-1) else: print(best_row, best_col, best_col + m - 1) available[best_row][0] = min(available[best_row][0], best_col - 1) available[best_row][1] = max(available[best_row][1], best_col + m) def calc_distance(center, row, col, num): end_col = col + num - 1 distance = abs(center - row) * num if col >= center: distance += (col - center) * num + (num - 1) * num // 2 elif end_col <= center: distance += (center - end_col) * num + (num - 1) * num // 2 else: distance += ((center - col) * (center - col + 1) // 2 + (end_col - center) * (end_col - center + 1) // 2) return distance solve() ```
output
1
27,446
14
54,893
Provide tags and a correct Python 3 solution for this coding contest problem. All cinema halls in Berland are rectangles with K rows of K seats each, and K is an odd number. Rows and seats are numbered from 1 to K. For safety reasons people, who come to the box office to buy tickets, are not allowed to choose seats themselves. Formerly the choice was made by a cashier, but now this is the responsibility of a special seating program. It was found out that the large majority of Berland's inhabitants go to the cinema in order to watch a movie, that's why they want to sit as close to the hall center as possible. Moreover, a company of M people, who come to watch a movie, want necessarily to occupy M successive seats in one row. Let's formulate the algorithm, according to which the program chooses seats and sells tickets. As the request for M seats comes, the program should determine the row number x and the segment [yl, yr] of the seats numbers in this row, where yr - yl + 1 = M. From all such possible variants as a final result the program should choose the one with the minimum function value of total seats remoteness from the center. Say, <image> β€” the row and the seat numbers of the most "central" seat. Then the function value of seats remoteness from the hall center is <image>. If the amount of minimum function values is more than one, the program should choose the one that is closer to the screen (i.e. the row number x is lower). If the variants are still multiple, it should choose the one with the minimum yl. If you did not get yet, your task is to simulate the work of this program. Input The first line contains two integers N and K (1 ≀ N ≀ 1000, 1 ≀ K ≀ 99) β€” the amount of requests and the hall size respectively. The second line contains N space-separated integers Mi from the range [1, K] β€” requests to the program. Output Output N lines. In the i-th line output Β«-1Β» (without quotes), if it is impossible to find Mi successive seats in one row, otherwise output three numbers x, yl, yr. Separate the numbers with a space. Examples Input 2 1 1 1 Output 1 1 1 -1 Input 4 3 1 2 3 1 Output 2 2 2 1 1 2 3 1 3 2 1 1
instruction
0
27,447
14
54,894
Tags: dp, implementation Correct Solution: ``` def solve(): n, k = map(int, input().split()) group = map(int, input().split()) available = [[k, 1][:] for _ in range(k + 1)] center = (k + 1) // 2 for m in group: closest, best_row, best_col = 10000, -1, -1 for row in range(1, k + 1): col = 0 if available[row][0] < m and k - available[row][1] + 1 < m: continue if available[row][0] == k: col = center - m // 2 elif center - available[row][0] <= available[row][1] - center: col = available[row][0] - m + 1 else: col = available[row][1] distance = calc_distance(center, row, col, m) if distance < closest: closest = distance best_row = row best_col = col if closest == 10000: print(-1) else: print(best_row, best_col, best_col + m - 1) available[best_row][0] = min(available[best_row][0], best_col - 1) available[best_row][1] = max(available[best_row][1], best_col + m) def calc_distance(center, row, col, num): end_col = col + num - 1 distance = abs(center - row) * num if col >= center: distance += (col - center) * num + (num - 1) * num // 2 elif end_col <= center: distance += (center - end_col) * num + (num - 1) * num // 2 else: distance += ((center - col) * (center - col + 1) // 2 + (end_col - center) * (end_col - center + 1) // 2) return distance solve() ```
output
1
27,447
14
54,895
Provide tags and a correct Python 3 solution for this coding contest problem. All cinema halls in Berland are rectangles with K rows of K seats each, and K is an odd number. Rows and seats are numbered from 1 to K. For safety reasons people, who come to the box office to buy tickets, are not allowed to choose seats themselves. Formerly the choice was made by a cashier, but now this is the responsibility of a special seating program. It was found out that the large majority of Berland's inhabitants go to the cinema in order to watch a movie, that's why they want to sit as close to the hall center as possible. Moreover, a company of M people, who come to watch a movie, want necessarily to occupy M successive seats in one row. Let's formulate the algorithm, according to which the program chooses seats and sells tickets. As the request for M seats comes, the program should determine the row number x and the segment [yl, yr] of the seats numbers in this row, where yr - yl + 1 = M. From all such possible variants as a final result the program should choose the one with the minimum function value of total seats remoteness from the center. Say, <image> β€” the row and the seat numbers of the most "central" seat. Then the function value of seats remoteness from the hall center is <image>. If the amount of minimum function values is more than one, the program should choose the one that is closer to the screen (i.e. the row number x is lower). If the variants are still multiple, it should choose the one with the minimum yl. If you did not get yet, your task is to simulate the work of this program. Input The first line contains two integers N and K (1 ≀ N ≀ 1000, 1 ≀ K ≀ 99) β€” the amount of requests and the hall size respectively. The second line contains N space-separated integers Mi from the range [1, K] β€” requests to the program. Output Output N lines. In the i-th line output Β«-1Β» (without quotes), if it is impossible to find Mi successive seats in one row, otherwise output three numbers x, yl, yr. Separate the numbers with a space. Examples Input 2 1 1 1 Output 1 1 1 -1 Input 4 3 1 2 3 1 Output 2 2 2 1 1 2 3 1 3 2 1 1
instruction
0
27,448
14
54,896
Tags: dp, implementation Correct Solution: ``` n,k=list(map(int,input().split())) a=list(map(int,input().split())) b=[-1]*k for m in a: c=[-1] for x in range(1,k+1): j=x-1 if b[j]==-1: yl=k//2+1-m//2 yr=yl+m-1 d=abs(x-k//2-1)*m+(2*(k//2+1)-(yl+min(yr,k//2+1)))*max(0,min(yr,k//2+1)-yl+1)//2+((max(k//2+2,yl)+yr)-2*(k//2+1))*max(0,yr-max(k//2+2,yl)+1)//2 x1,yl1,yr1=x,yl,yr if c!=[-1]: x,yl,yr=c if c==[-1] or d<abs(x-k//2-1)*m+(2*(k//2+1)-(yl+min(yr,k//2+1)))*max(0,min(yr,k//2+1)-yl+1)//2+((max(k//2+2,yl)+yr)-2*(k//2+1))*max(0,yr-max(k//2+2,yl)+1)//2: c=[x1,yl1,yr1] else: if b[j][0]>m: yl=b[j][0]-m yr=yl+m-1 d=abs(x-k//2-1)*m+(2*(k//2+1)-(yl+min(yr,k//2+1)))*max(0,min(yr,k//2+1)-yl+1)//2+((max(k//2+2,yl)+yr)-2*(k//2+1))*max(0,yr-max(k//2+2,yl)+1)//2 x1,yl1,yr1=x,yl,yr if c!=[-1]: x,yl,yr=c if c==[-1] or d<abs(x-k//2-1)*m+(2*(k//2+1)-(yl+min(yr,k//2+1)))*max(0,min(yr,k//2+1)-yl+1)//2+((max(k//2+2,yl)+yr)-2*(k//2+1))*max(0,yr-max(k//2+2,yl)+1)//2: c=[x1,yl1,yr1] x=x1 if k-b[j][1]>=m: yl=b[j][1]+1 yr=yl+m-1 d=abs(x-k//2-1)*m+(2*(k//2+1)-(yl+min(yr,k//2+1)))*max(0,min(yr,k//2+1)-yl+1)//2+((max(k//2+2,yl)+yr)-2*(k//2+1))*max(0,yr-max(k//2+2,yl)+1)//2 x1,yl1,yr1=x,yl,yr if c!=[-1]: x,yl,yr=c if c==[-1] or d<abs(x-k//2-1)*m+(2*(k//2+1)-(yl+min(yr,k//2+1)))*max(0,min(yr,k//2+1)-yl+1)//2+((max(k//2+2,yl)+yr)-2*(k//2+1))*max(0,yr-max(k//2+2,yl)+1)//2: c=[x1,yl1,yr1] print(*c) if c!=[-1]: if b[c[0]-1]==-1: b[c[0]-1]=c[1:] else: b[c[0]-1][1]=max(b[c[0]-1][1],c[2]) b[c[0]-1][0]=min(b[c[0]-1][0],c[1]) ```
output
1
27,448
14
54,897
Provide tags and a correct Python 3 solution for this coding contest problem. All cinema halls in Berland are rectangles with K rows of K seats each, and K is an odd number. Rows and seats are numbered from 1 to K. For safety reasons people, who come to the box office to buy tickets, are not allowed to choose seats themselves. Formerly the choice was made by a cashier, but now this is the responsibility of a special seating program. It was found out that the large majority of Berland's inhabitants go to the cinema in order to watch a movie, that's why they want to sit as close to the hall center as possible. Moreover, a company of M people, who come to watch a movie, want necessarily to occupy M successive seats in one row. Let's formulate the algorithm, according to which the program chooses seats and sells tickets. As the request for M seats comes, the program should determine the row number x and the segment [yl, yr] of the seats numbers in this row, where yr - yl + 1 = M. From all such possible variants as a final result the program should choose the one with the minimum function value of total seats remoteness from the center. Say, <image> β€” the row and the seat numbers of the most "central" seat. Then the function value of seats remoteness from the hall center is <image>. If the amount of minimum function values is more than one, the program should choose the one that is closer to the screen (i.e. the row number x is lower). If the variants are still multiple, it should choose the one with the minimum yl. If you did not get yet, your task is to simulate the work of this program. Input The first line contains two integers N and K (1 ≀ N ≀ 1000, 1 ≀ K ≀ 99) β€” the amount of requests and the hall size respectively. The second line contains N space-separated integers Mi from the range [1, K] β€” requests to the program. Output Output N lines. In the i-th line output Β«-1Β» (without quotes), if it is impossible to find Mi successive seats in one row, otherwise output three numbers x, yl, yr. Separate the numbers with a space. Examples Input 2 1 1 1 Output 1 1 1 -1 Input 4 3 1 2 3 1 Output 2 2 2 1 1 2 3 1 3 2 1 1
instruction
0
27,449
14
54,898
Tags: dp, implementation Correct Solution: ``` import math from itertools import accumulate R = lambda: map(int, input().split()) n, k = R() xc = yc = (k + 1) // 2 row = list(accumulate(abs(j - yc) for j in range(k + 1))) dp = [[k + 1 - j for j in range(k + 1)] for _ in range(k + 1)] for l in R(): xt, yt, c = -1, -1, math.inf for i in range(1, k + 1): for j in range(1, k + 1 - l + 1): if dp[i][j] >= l: ct = row[j + l - 1] - row[j - 1] + abs(i - xc) * l if ct < c or (ct == c and i < xt) or (ct == c and i == xt and j < yt): xt, yt, c = i, j, ct if c < math.inf: for j in range(yt + l - 1, 0, -1): dp[xt][j] = min(dp[xt][j], max(0, yt - j)) print(xt, yt, yt + l - 1, sep=' ') else: print(-1) ```
output
1
27,449
14
54,899
Provide tags and a correct Python 3 solution for this coding contest problem. All cinema halls in Berland are rectangles with K rows of K seats each, and K is an odd number. Rows and seats are numbered from 1 to K. For safety reasons people, who come to the box office to buy tickets, are not allowed to choose seats themselves. Formerly the choice was made by a cashier, but now this is the responsibility of a special seating program. It was found out that the large majority of Berland's inhabitants go to the cinema in order to watch a movie, that's why they want to sit as close to the hall center as possible. Moreover, a company of M people, who come to watch a movie, want necessarily to occupy M successive seats in one row. Let's formulate the algorithm, according to which the program chooses seats and sells tickets. As the request for M seats comes, the program should determine the row number x and the segment [yl, yr] of the seats numbers in this row, where yr - yl + 1 = M. From all such possible variants as a final result the program should choose the one with the minimum function value of total seats remoteness from the center. Say, <image> β€” the row and the seat numbers of the most "central" seat. Then the function value of seats remoteness from the hall center is <image>. If the amount of minimum function values is more than one, the program should choose the one that is closer to the screen (i.e. the row number x is lower). If the variants are still multiple, it should choose the one with the minimum yl. If you did not get yet, your task is to simulate the work of this program. Input The first line contains two integers N and K (1 ≀ N ≀ 1000, 1 ≀ K ≀ 99) β€” the amount of requests and the hall size respectively. The second line contains N space-separated integers Mi from the range [1, K] β€” requests to the program. Output Output N lines. In the i-th line output Β«-1Β» (without quotes), if it is impossible to find Mi successive seats in one row, otherwise output three numbers x, yl, yr. Separate the numbers with a space. Examples Input 2 1 1 1 Output 1 1 1 -1 Input 4 3 1 2 3 1 Output 2 2 2 1 1 2 3 1 3 2 1 1
instruction
0
27,450
14
54,900
Tags: dp, implementation Correct Solution: ``` #10B def solve(): # n - requests, k - rows and seats on each row n, k = map(int, input().split()) #We group succesive seats: group = map(int, input().split()) #Now, we elaborate a map with available seats available = [[k, 1][:] for i in range(k + 1)] center = (k + 1) // 2 #For each request: for m in group: closest, best_row, best_col = 10000, -1, -1 #We look through seats in each row: for row in range(1, k + 1): col = 0 #If it doesn't fit the request, the program skips the row: if available[row][0] < m and k - available[row][1] + 1 < m: continue #If all of them are empty, we choose the central column: if available[row][0] == k: col = center - m // 2 #Else, if the available seats in row match the request, we mark down the seats: elif center - available[row][0] <= available[row][1] - center: col = available[row][0] - m + 1 else: col = available[row][1] #We compute the distance to the screen: distance = calc_distance(center, row, col, m) if distance < closest: closest = distance best_row = row best_col = col #If we didn't find available seats: if closest == 10000: print(-1) else: print(best_row, best_col, best_col + m - 1) available[best_row][0] = min(available[best_row][0], best_col - 1) available[best_row][1] = max(available[best_row][1], best_col + m) def calc_distance(center, row, col, num): end_col = col + num - 1 distance = abs(center - row) * num if col >= center: distance += (col - center) * num + (num - 1) * num // 2 elif end_col <= center: distance += (center - end_col) * num + (num - 1) * num // 2 else: distance += ((center - col) * (center - col + 1) // 2 + (end_col - center) * (end_col - center + 1) // 2) return distance solve() ```
output
1
27,450
14
54,901
Provide tags and a correct Python 3 solution for this coding contest problem. All cinema halls in Berland are rectangles with K rows of K seats each, and K is an odd number. Rows and seats are numbered from 1 to K. For safety reasons people, who come to the box office to buy tickets, are not allowed to choose seats themselves. Formerly the choice was made by a cashier, but now this is the responsibility of a special seating program. It was found out that the large majority of Berland's inhabitants go to the cinema in order to watch a movie, that's why they want to sit as close to the hall center as possible. Moreover, a company of M people, who come to watch a movie, want necessarily to occupy M successive seats in one row. Let's formulate the algorithm, according to which the program chooses seats and sells tickets. As the request for M seats comes, the program should determine the row number x and the segment [yl, yr] of the seats numbers in this row, where yr - yl + 1 = M. From all such possible variants as a final result the program should choose the one with the minimum function value of total seats remoteness from the center. Say, <image> β€” the row and the seat numbers of the most "central" seat. Then the function value of seats remoteness from the hall center is <image>. If the amount of minimum function values is more than one, the program should choose the one that is closer to the screen (i.e. the row number x is lower). If the variants are still multiple, it should choose the one with the minimum yl. If you did not get yet, your task is to simulate the work of this program. Input The first line contains two integers N and K (1 ≀ N ≀ 1000, 1 ≀ K ≀ 99) β€” the amount of requests and the hall size respectively. The second line contains N space-separated integers Mi from the range [1, K] β€” requests to the program. Output Output N lines. In the i-th line output Β«-1Β» (without quotes), if it is impossible to find Mi successive seats in one row, otherwise output three numbers x, yl, yr. Separate the numbers with a space. Examples Input 2 1 1 1 Output 1 1 1 -1 Input 4 3 1 2 3 1 Output 2 2 2 1 1 2 3 1 3 2 1 1
instruction
0
27,451
14
54,902
Tags: dp, implementation Correct Solution: ``` import math import sys from bisect import bisect_right, bisect_left, insort_right from collections import Counter, defaultdict from heapq import heappop, heappush from itertools import accumulate, permutations, combinations from sys import stdout R = lambda: map(int, input().split()) n, k = R() xc = yc = (k + 1) // 2 row = list(accumulate(abs(j - yc) for j in range(k + 1))) dp = [[k + 1 - j for j in range(k + 1)] for _ in range(k + 1)] for l in R(): xt, yt, c = -1, -1, math.inf for i in range(1, k + 1): for j in range(1, k + 1 - l + 1): if dp[i][j] >= l: ct = row[j + l - 1] - row[j - 1] + abs(i - xc) * l if ct < c or (ct == c and i < xt) or (ct == c and i == xt and j < yt): xt, yt, c = i, j, ct if c < math.inf: for j in range(yt + l - 1, 0, -1): dp[xt][j] = min(dp[xt][j], max(0, yt - j)) print(xt, yt, yt + l - 1, sep=' ') else: print(-1) ```
output
1
27,451
14
54,903
Provide tags and a correct Python 3 solution for this coding contest problem. All cinema halls in Berland are rectangles with K rows of K seats each, and K is an odd number. Rows and seats are numbered from 1 to K. For safety reasons people, who come to the box office to buy tickets, are not allowed to choose seats themselves. Formerly the choice was made by a cashier, but now this is the responsibility of a special seating program. It was found out that the large majority of Berland's inhabitants go to the cinema in order to watch a movie, that's why they want to sit as close to the hall center as possible. Moreover, a company of M people, who come to watch a movie, want necessarily to occupy M successive seats in one row. Let's formulate the algorithm, according to which the program chooses seats and sells tickets. As the request for M seats comes, the program should determine the row number x and the segment [yl, yr] of the seats numbers in this row, where yr - yl + 1 = M. From all such possible variants as a final result the program should choose the one with the minimum function value of total seats remoteness from the center. Say, <image> β€” the row and the seat numbers of the most "central" seat. Then the function value of seats remoteness from the hall center is <image>. If the amount of minimum function values is more than one, the program should choose the one that is closer to the screen (i.e. the row number x is lower). If the variants are still multiple, it should choose the one with the minimum yl. If you did not get yet, your task is to simulate the work of this program. Input The first line contains two integers N and K (1 ≀ N ≀ 1000, 1 ≀ K ≀ 99) β€” the amount of requests and the hall size respectively. The second line contains N space-separated integers Mi from the range [1, K] β€” requests to the program. Output Output N lines. In the i-th line output Β«-1Β» (without quotes), if it is impossible to find Mi successive seats in one row, otherwise output three numbers x, yl, yr. Separate the numbers with a space. Examples Input 2 1 1 1 Output 1 1 1 -1 Input 4 3 1 2 3 1 Output 2 2 2 1 1 2 3 1 3 2 1 1
instruction
0
27,452
14
54,904
Tags: dp, implementation Correct Solution: ``` import sys # import logging # logging.root.setLevel(level=logging.DEBUG) # logging.root.setLevel(level=logging.INFO) import re from math import ceil length,size = map(int,sys.stdin.readline().split()) peoples = map(int,sys.stdin.readline().split()) has_occupied = [[True]+[False]*size for _ in range(size+1)] has_occupied[0] = [True]*(size+1) center_x = ceil(size/2) center_y = ceil(size/2) # logging.debug(f"center_x={center_x},center_y={center_y}") distance = [[abs(x-center_x)+ abs(y-center_y) for y in range(size+1)] for x in range(size+1)] row_empty = [size]*(size+1) total_empty = size**2 # logging.debug(f"distance ={distance}") for people in peoples: if people > size or people > total_empty: print(-1) continue d = float('inf') ans = None for x in range(1,size+1): if row_empty[x] < people: continue for left in range(1,size+2-people): total = sum(distance[x][left:left+people]) if any(has_occupied[x][left:left+people]) == False and \ total < d: # logging.debug(f"({x},{left}):{total} < {d},{ans} changed") ans = (x,left,left+people-1) d = total if ans: print(*ans) has_occupied[ans[0]][ans[1]:ans[2]+1] = [True]*people total_empty -= people row_empty[ans[0]] -= people else: print(-1) ```
output
1
27,452
14
54,905
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. All cinema halls in Berland are rectangles with K rows of K seats each, and K is an odd number. Rows and seats are numbered from 1 to K. For safety reasons people, who come to the box office to buy tickets, are not allowed to choose seats themselves. Formerly the choice was made by a cashier, but now this is the responsibility of a special seating program. It was found out that the large majority of Berland's inhabitants go to the cinema in order to watch a movie, that's why they want to sit as close to the hall center as possible. Moreover, a company of M people, who come to watch a movie, want necessarily to occupy M successive seats in one row. Let's formulate the algorithm, according to which the program chooses seats and sells tickets. As the request for M seats comes, the program should determine the row number x and the segment [yl, yr] of the seats numbers in this row, where yr - yl + 1 = M. From all such possible variants as a final result the program should choose the one with the minimum function value of total seats remoteness from the center. Say, <image> β€” the row and the seat numbers of the most "central" seat. Then the function value of seats remoteness from the hall center is <image>. If the amount of minimum function values is more than one, the program should choose the one that is closer to the screen (i.e. the row number x is lower). If the variants are still multiple, it should choose the one with the minimum yl. If you did not get yet, your task is to simulate the work of this program. Input The first line contains two integers N and K (1 ≀ N ≀ 1000, 1 ≀ K ≀ 99) β€” the amount of requests and the hall size respectively. The second line contains N space-separated integers Mi from the range [1, K] β€” requests to the program. Output Output N lines. In the i-th line output Β«-1Β» (without quotes), if it is impossible to find Mi successive seats in one row, otherwise output three numbers x, yl, yr. Separate the numbers with a space. Examples Input 2 1 1 1 Output 1 1 1 -1 Input 4 3 1 2 3 1 Output 2 2 2 1 1 2 3 1 3 2 1 1 Submitted Solution: ``` def inp_spl(f): return map(f,input().split()) def inp(f): return map(f,input()) NMAX = 101 left = [None] * NMAX right = [None] * NMAX K = 1 mid = 1 N = 1 def initialize(): global left global right global mid for i in range(NMAX): left[i] = K right[i] = K mid = K // 2 + 1 def find_space(size, row, side, flush): #side - 0 for left, 1 for right; flush - 0 for check, 1 for flush global left global right if(side == 1): if(right[row] >= size): if(right[row] == K): if(flush == 1): print('{} {} {}'.format(row, mid - size // 2, mid + size // 2 - 1 + size % 2)) right[row] = (K - size) // 2 + (K - size) % 2 left[row] = right[row] - 1 + size % 2 else: return size * abs(row - mid) + (size // 2) * (size // 2 + 1) - ( size // 2 * ( (size % 2 + 1 ) % 2 ) ) else: if(flush == 1): print('{} {} {}'.format(row, K - right[row] + 1 , K - right[row] + size)) right[row] -= size else: return size * abs(row - mid) + (mid - right[row] - 1) * size + size * (size + 1) // 2 else: if(left[row] >= size): if(left[row] == K): if(flush == 1): print('{} {} {}'.format(row, mid - size // 2, mid + size // 2 - 1 + size % 2)) right[row] = (K - size) // 2 + (K - size) % 2 left[row] = right[row] - 1 + size % 2 else: return size * abs(row - mid) + (size // 2) * (size // 2 + 1) - ( size // 2 * ( (size % 2 + 1 ) % 2 ) ) else: if(flush == 1): print('{} {} {}'.format(row, left[row] - size + 1 , left[row])) left[row] -= size else: return size * abs(row - mid) + (mid - left[row] - 1) * size + size * (size + 1) // 2 if(flush == 0): return -1 def Main(): global N global K N, K = inp_spl(int) initialize() for x in inp_spl(int): ud = -987 lr = -987 minResult = 100000 for j in range(0, K // 2 + 1): sideResult = find_space(x, mid - j, 0, 0) if(sideResult != -1 and minResult >= sideResult): minResult = sideResult ud = -j lr = 0 sideResult = find_space(x, mid - j, 1, 0) if( sideResult != -1 and ( minResult > sideResult or (minResult == sideResult and ud != -j) )): minResult = sideResult ud = -j lr = 1 sideResult = find_space(x, mid + j, 0, 0) if(sideResult != -1 and minResult > sideResult): minResult = sideResult ud = j lr = 0 sideResult = find_space(x, mid + j, 1, 0) if(sideResult != -1 and minResult > sideResult): minResult = sideResult ud = j lr = 1 if(ud == -987): print (-1) else: find_space(x, mid + ud, lr, 1) Main() ```
instruction
0
27,453
14
54,906
Yes
output
1
27,453
14
54,907
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. All cinema halls in Berland are rectangles with K rows of K seats each, and K is an odd number. Rows and seats are numbered from 1 to K. For safety reasons people, who come to the box office to buy tickets, are not allowed to choose seats themselves. Formerly the choice was made by a cashier, but now this is the responsibility of a special seating program. It was found out that the large majority of Berland's inhabitants go to the cinema in order to watch a movie, that's why they want to sit as close to the hall center as possible. Moreover, a company of M people, who come to watch a movie, want necessarily to occupy M successive seats in one row. Let's formulate the algorithm, according to which the program chooses seats and sells tickets. As the request for M seats comes, the program should determine the row number x and the segment [yl, yr] of the seats numbers in this row, where yr - yl + 1 = M. From all such possible variants as a final result the program should choose the one with the minimum function value of total seats remoteness from the center. Say, <image> β€” the row and the seat numbers of the most "central" seat. Then the function value of seats remoteness from the hall center is <image>. If the amount of minimum function values is more than one, the program should choose the one that is closer to the screen (i.e. the row number x is lower). If the variants are still multiple, it should choose the one with the minimum yl. If you did not get yet, your task is to simulate the work of this program. Input The first line contains two integers N and K (1 ≀ N ≀ 1000, 1 ≀ K ≀ 99) β€” the amount of requests and the hall size respectively. The second line contains N space-separated integers Mi from the range [1, K] β€” requests to the program. Output Output N lines. In the i-th line output Β«-1Β» (without quotes), if it is impossible to find Mi successive seats in one row, otherwise output three numbers x, yl, yr. Separate the numbers with a space. Examples Input 2 1 1 1 Output 1 1 1 -1 Input 4 3 1 2 3 1 Output 2 2 2 1 1 2 3 1 3 2 1 1 Submitted Solution: ``` __author__ = 'Darren' def solve(): n, k = map(int, input().split()) group = map(int, input().split()) available = [[k, 1][:] for _ in range(k+1)] center = (k + 1) // 2 for m in group: closest, best_row, best_col = 10000, -1, -1 for row in range(1, k+1): col = 0 if available[row][0] < m and k - available[row][1] + 1 < m: continue if available[row][0] == k: col = center - m // 2 elif center - available[row][0] <= available[row][1] - center: col = available[row][0] - m + 1 else: col = available[row][1] distance = calc_distance(center, row, col, m) if distance < closest: closest = distance best_row = row best_col = col if closest == 10000: print(-1) else: print(best_row, best_col, best_col+m-1) available[best_row][0] = min(available[best_row][0], best_col-1) available[best_row][1] = max(available[best_row][1], best_col+m) def calc_distance(center, row, col, num): end_col = col + num - 1 distance = abs(center - row) * num if col >= center: distance += (col - center) * num + (num - 1) * num // 2 elif end_col <= center: distance += (center - end_col) * num + (num - 1) * num // 2 else: distance += ((center - col) * (center - col + 1) // 2 + (end_col - center) * (end_col - center + 1) // 2) return distance if __name__ == '__main__': solve() ```
instruction
0
27,454
14
54,908
Yes
output
1
27,454
14
54,909
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. All cinema halls in Berland are rectangles with K rows of K seats each, and K is an odd number. Rows and seats are numbered from 1 to K. For safety reasons people, who come to the box office to buy tickets, are not allowed to choose seats themselves. Formerly the choice was made by a cashier, but now this is the responsibility of a special seating program. It was found out that the large majority of Berland's inhabitants go to the cinema in order to watch a movie, that's why they want to sit as close to the hall center as possible. Moreover, a company of M people, who come to watch a movie, want necessarily to occupy M successive seats in one row. Let's formulate the algorithm, according to which the program chooses seats and sells tickets. As the request for M seats comes, the program should determine the row number x and the segment [yl, yr] of the seats numbers in this row, where yr - yl + 1 = M. From all such possible variants as a final result the program should choose the one with the minimum function value of total seats remoteness from the center. Say, <image> β€” the row and the seat numbers of the most "central" seat. Then the function value of seats remoteness from the hall center is <image>. If the amount of minimum function values is more than one, the program should choose the one that is closer to the screen (i.e. the row number x is lower). If the variants are still multiple, it should choose the one with the minimum yl. If you did not get yet, your task is to simulate the work of this program. Input The first line contains two integers N and K (1 ≀ N ≀ 1000, 1 ≀ K ≀ 99) β€” the amount of requests and the hall size respectively. The second line contains N space-separated integers Mi from the range [1, K] β€” requests to the program. Output Output N lines. In the i-th line output Β«-1Β» (without quotes), if it is impossible to find Mi successive seats in one row, otherwise output three numbers x, yl, yr. Separate the numbers with a space. Examples Input 2 1 1 1 Output 1 1 1 -1 Input 4 3 1 2 3 1 Output 2 2 2 1 1 2 3 1 3 2 1 1 Submitted Solution: ``` ''' Jana Goodman6 10B Cinema Cashier ''' import math #import time #import random SPACE = ' ' MX = 10 ** 16 class Seat: def __init__(self, x, y, rem): self.x = x self.y = y self.remoteness = rem self.empty = True class Row: def __init__(self, max_seat, x, xc): self.max_seat = max_seat self.x = x self.xc = xc self.yc = xc self.free_seats = max_seat self.seats = [Seat(x, y, self.remoteness(y)) for y in range(0, max_seat + 1)] def remoteness(self, y): return abs(self.x - self.xc) + abs(y - self.yc) def total_remoteness(self, y, m): return sum(seat.remoteness for seat in self.seats[y:y+m]) def block_empty(self, y, m): return all(seat.empty for seat in self.seats[y:y + m]) def set_occupied(self, y_left, m): for y in range(y_left, y_left + m): self.seats[y].empty = False self.free_seats -= m class Cinema: def __init__(self, size): self.max_seat = size self.x_center = math.ceil(self.max_seat / 2) self.y_center = self.x_center self.seats_left = size * size self.rows = [Row(size, x, self.x_center) for x in range(0, size + 1)] def get_min_remoteness(self, m): if self.seats_left < m: return '-1' min_remoteness = MX min_x, min_yl, min_yr = None, None, None for x in range(1, self.max_seat + 1): if self.rows[x].free_seats < m: continue for y in range(1, self.max_seat - m + 2): if self.rows[x].block_empty(y, m): rem = self.rows[x].total_remoteness(y, m) if min_remoteness > rem: min_remoteness = rem min_x, min_yl, min_yr = x, y, y + m - 1 if min_x is None: return '-1' self.rows[min_x].set_occupied(min_yl, m) self.seats_left -= m return f'{min_x} {min_yl} {min_yr}' if __name__ == '__main__': n, k = map(int, input().strip().split(SPACE)) requests = list(map(int, input().strip().split(SPACE))) # n, k = 1000, 99 # requests = [random.randint(1, k) for _ in range(0, n)] # print(requests) # start_time = time.time() cinema = Cinema(k) for request in requests: print(cinema.get_min_remoteness(request)) # print(f'Elapsed time: {time.time() - start_time}') ```
instruction
0
27,455
14
54,910
Yes
output
1
27,455
14
54,911
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. All cinema halls in Berland are rectangles with K rows of K seats each, and K is an odd number. Rows and seats are numbered from 1 to K. For safety reasons people, who come to the box office to buy tickets, are not allowed to choose seats themselves. Formerly the choice was made by a cashier, but now this is the responsibility of a special seating program. It was found out that the large majority of Berland's inhabitants go to the cinema in order to watch a movie, that's why they want to sit as close to the hall center as possible. Moreover, a company of M people, who come to watch a movie, want necessarily to occupy M successive seats in one row. Let's formulate the algorithm, according to which the program chooses seats and sells tickets. As the request for M seats comes, the program should determine the row number x and the segment [yl, yr] of the seats numbers in this row, where yr - yl + 1 = M. From all such possible variants as a final result the program should choose the one with the minimum function value of total seats remoteness from the center. Say, <image> β€” the row and the seat numbers of the most "central" seat. Then the function value of seats remoteness from the hall center is <image>. If the amount of minimum function values is more than one, the program should choose the one that is closer to the screen (i.e. the row number x is lower). If the variants are still multiple, it should choose the one with the minimum yl. If you did not get yet, your task is to simulate the work of this program. Input The first line contains two integers N and K (1 ≀ N ≀ 1000, 1 ≀ K ≀ 99) β€” the amount of requests and the hall size respectively. The second line contains N space-separated integers Mi from the range [1, K] β€” requests to the program. Output Output N lines. In the i-th line output Β«-1Β» (without quotes), if it is impossible to find Mi successive seats in one row, otherwise output three numbers x, yl, yr. Separate the numbers with a space. Examples Input 2 1 1 1 Output 1 1 1 -1 Input 4 3 1 2 3 1 Output 2 2 2 1 1 2 3 1 3 2 1 1 Submitted Solution: ``` n, k = map(int, input().split()) m = list(map(int, input().split())) hall = [[] for i in range(k)] midPos = k // 2 availableSeat = [k for i in range(k)] if k > 1: rows = [midPos - 1, midPos, midPos + 1] else: rows = [midPos] for mi in m: minValue = 10000 rowPos, colPos = -1, -1 for row in rows: if hall[row] == []: col = (k - mi) // 2 func = sum([abs(row - midPos) + abs(i - midPos) for i in range(col, col + mi)]) if func < minValue: rowPos, colPos, minValue = row, col, func else: if hall[row][0] >= mi: col = hall[row][0] - mi func = sum([abs(row - midPos) + abs(i - midPos) for i in range(col, col + mi)]) if func < minValue: rowPos, colPos, minValue = row, col, func if hall[row][1] < k - mi: col = hall[row][1] + 1 func = sum([abs(row - midPos) + abs(i - midPos) for i in range(col, col + mi)]) if func < minValue: rowPos, colPos, minValue = row, col, func if rowPos == -1: print(-1) else: print(rowPos + 1, colPos + 1, colPos + mi) if hall[rowPos] == []: hall[rowPos] += [colPos, colPos + mi - 1] elif hall[rowPos][0] > colPos: hall[rowPos][0] = colPos else: hall[rowPos][1] = colPos + mi - 1 if rowPos == rows[0] and rowPos > 0: rows.insert(0, rowPos - 1) elif rowPos == rows[-1] and rowPos < k - 1: rows.append(rowPos + 1) ```
instruction
0
27,456
14
54,912
Yes
output
1
27,456
14
54,913
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. All cinema halls in Berland are rectangles with K rows of K seats each, and K is an odd number. Rows and seats are numbered from 1 to K. For safety reasons people, who come to the box office to buy tickets, are not allowed to choose seats themselves. Formerly the choice was made by a cashier, but now this is the responsibility of a special seating program. It was found out that the large majority of Berland's inhabitants go to the cinema in order to watch a movie, that's why they want to sit as close to the hall center as possible. Moreover, a company of M people, who come to watch a movie, want necessarily to occupy M successive seats in one row. Let's formulate the algorithm, according to which the program chooses seats and sells tickets. As the request for M seats comes, the program should determine the row number x and the segment [yl, yr] of the seats numbers in this row, where yr - yl + 1 = M. From all such possible variants as a final result the program should choose the one with the minimum function value of total seats remoteness from the center. Say, <image> β€” the row and the seat numbers of the most "central" seat. Then the function value of seats remoteness from the hall center is <image>. If the amount of minimum function values is more than one, the program should choose the one that is closer to the screen (i.e. the row number x is lower). If the variants are still multiple, it should choose the one with the minimum yl. If you did not get yet, your task is to simulate the work of this program. Input The first line contains two integers N and K (1 ≀ N ≀ 1000, 1 ≀ K ≀ 99) β€” the amount of requests and the hall size respectively. The second line contains N space-separated integers Mi from the range [1, K] β€” requests to the program. Output Output N lines. In the i-th line output Β«-1Β» (without quotes), if it is impossible to find Mi successive seats in one row, otherwise output three numbers x, yl, yr. Separate the numbers with a space. Examples Input 2 1 1 1 Output 1 1 1 -1 Input 4 3 1 2 3 1 Output 2 2 2 1 1 2 3 1 3 2 1 1 Submitted Solution: ``` import math import sys from bisect import bisect_right, bisect_left, insort_right from collections import Counter, defaultdict from heapq import heappop, heappush from itertools import accumulate, permutations, combinations from sys import stdout R = lambda: map(int, input().split()) n, k = R() xc = yc = (k + 1) // 2 row = [0] + list(accumulate(abs(j - yc) for j in range(1, k + 1))) dp = [[k + 1 - j for j in range(k + 1)] for _ in range(k + 1)] for l in R(): xt, yt, c = -1, -1, math.inf for i in range(1, k + 1): for j in range(1, k + 1 - l + 1): if dp[i][j] >= l: ct = row[j + l - 1] - row[j - 1] + abs(i - xc) * l if ct < c or (ct == c and i < xt) or (ct == c and i == xt and j < yt): xt, yt, c = i, j, ct if c < math.inf: for j in range(yt + l - 1, 0, -1): dp[xt][j] = max(0, yt - j) print(xt, yt, yt + l - 1, sep=' ') else: print(-1) ```
instruction
0
27,457
14
54,914
No
output
1
27,457
14
54,915
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. All cinema halls in Berland are rectangles with K rows of K seats each, and K is an odd number. Rows and seats are numbered from 1 to K. For safety reasons people, who come to the box office to buy tickets, are not allowed to choose seats themselves. Formerly the choice was made by a cashier, but now this is the responsibility of a special seating program. It was found out that the large majority of Berland's inhabitants go to the cinema in order to watch a movie, that's why they want to sit as close to the hall center as possible. Moreover, a company of M people, who come to watch a movie, want necessarily to occupy M successive seats in one row. Let's formulate the algorithm, according to which the program chooses seats and sells tickets. As the request for M seats comes, the program should determine the row number x and the segment [yl, yr] of the seats numbers in this row, where yr - yl + 1 = M. From all such possible variants as a final result the program should choose the one with the minimum function value of total seats remoteness from the center. Say, <image> β€” the row and the seat numbers of the most "central" seat. Then the function value of seats remoteness from the hall center is <image>. If the amount of minimum function values is more than one, the program should choose the one that is closer to the screen (i.e. the row number x is lower). If the variants are still multiple, it should choose the one with the minimum yl. If you did not get yet, your task is to simulate the work of this program. Input The first line contains two integers N and K (1 ≀ N ≀ 1000, 1 ≀ K ≀ 99) β€” the amount of requests and the hall size respectively. The second line contains N space-separated integers Mi from the range [1, K] β€” requests to the program. Output Output N lines. In the i-th line output Β«-1Β» (without quotes), if it is impossible to find Mi successive seats in one row, otherwise output three numbers x, yl, yr. Separate the numbers with a space. Examples Input 2 1 1 1 Output 1 1 1 -1 Input 4 3 1 2 3 1 Output 2 2 2 1 1 2 3 1 3 2 1 1 Submitted Solution: ``` n,k=list(map(int,input().split())) a=list(map(int,input().split())) b=[-1]*k for m in a: c=[-1] for x in range(1,k+1): j=x-1 if b[j]==-1: yl=k//2+1-m//2 yr=yl+m-1 d=abs(x-k//2-1)*m+(yl+min(yr,k//2+1))//2*(min(yr,k//2+1)-yl+1)+((1+yr)//2)*max(0,yr-k//2-1) x1,yl1,yr1=x,yl,yr if c!=[-1]: x,yl,yr=c if c==[-1] or d<abs(x-k//2-1)*m+(yl+min(yr,k//2+1))//2*(min(yr,k//2+1)-yl+1)+((1+yr)//2)*max(0,yr-k//2-1): c=[x1,yl1,yr1] else: if b[j][0]>m: yl=b[j][0]-m yr=yl+m-1 d=abs(x-k//2-1)*m+(yl+min(yr,k//2+1))//2*(min(yr,k//2+1)-yl+1)+((1+yr)//2)*max(0,yr-k//2-1) x1,yl1,yr1=x,yl,yr if c!=[-1]: x,yl,yr=c if c==[-1] or d<abs(x-k//2-1)*m+(yl+min(yr,k//2+1))//2*(min(yr,k//2+1)-yl+1)+((1+yr)//2)*max(0,yr-k//2-1): c=[x1,yl1,yr1] if k-b[j][1]>=m: yl=b[j][1]+1 yr=yl+m-1 d=abs(x-k//2-1)*m+(yl+min(yr,k//2+1))//2*(min(yr,k//2+1)-yl+1)+((1+yr)//2)*max(0,yr-k//2-1) x1,yl1,yr1=x,yl,yr if c!=[-1]: x,yl,yr=c if c==[-1] or d<abs(x-k//2-1)*m+(yl+min(yr,k//2+1))//2*(min(yr,k//2+1)-yl+1)+((1+yr)//2)*max(0,yr-k//2-1): c=[x1,yl1,yr1] print(*c) if c!=[-1]: if b[c[0]-1]==-1: b[c[0]-1]=c[1:] else: b[c[0]-1][1]=max(b[c[0]-1][1],c[2]) b[c[0]-1][0]=min(b[c[0]-1][0],c[1]) ```
instruction
0
27,458
14
54,916
No
output
1
27,458
14
54,917
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. All cinema halls in Berland are rectangles with K rows of K seats each, and K is an odd number. Rows and seats are numbered from 1 to K. For safety reasons people, who come to the box office to buy tickets, are not allowed to choose seats themselves. Formerly the choice was made by a cashier, but now this is the responsibility of a special seating program. It was found out that the large majority of Berland's inhabitants go to the cinema in order to watch a movie, that's why they want to sit as close to the hall center as possible. Moreover, a company of M people, who come to watch a movie, want necessarily to occupy M successive seats in one row. Let's formulate the algorithm, according to which the program chooses seats and sells tickets. As the request for M seats comes, the program should determine the row number x and the segment [yl, yr] of the seats numbers in this row, where yr - yl + 1 = M. From all such possible variants as a final result the program should choose the one with the minimum function value of total seats remoteness from the center. Say, <image> β€” the row and the seat numbers of the most "central" seat. Then the function value of seats remoteness from the hall center is <image>. If the amount of minimum function values is more than one, the program should choose the one that is closer to the screen (i.e. the row number x is lower). If the variants are still multiple, it should choose the one with the minimum yl. If you did not get yet, your task is to simulate the work of this program. Input The first line contains two integers N and K (1 ≀ N ≀ 1000, 1 ≀ K ≀ 99) β€” the amount of requests and the hall size respectively. The second line contains N space-separated integers Mi from the range [1, K] β€” requests to the program. Output Output N lines. In the i-th line output Β«-1Β» (without quotes), if it is impossible to find Mi successive seats in one row, otherwise output three numbers x, yl, yr. Separate the numbers with a space. Examples Input 2 1 1 1 Output 1 1 1 -1 Input 4 3 1 2 3 1 Output 2 2 2 1 1 2 3 1 3 2 1 1 Submitted Solution: ``` n, k = map(int, input().split()) m = list(map(int, input().split())) hall = [[False for i in range(k)] for i in range(k)] midPos = k // 2 if k > 1: rows = [midPos - 1, midPos, midPos + 1] else: rows = [midPos] for mi in m: minValue = 10000 rowPos, colPos = -1, -1 for row in rows: for col in range(k - mi + 1): if True not in hall[row][col:col + mi] and sum([abs(row - midPos) + abs(i - midPos) for i in range(col, col + mi)]) < minValue: rowPos, colPos, minValue = row, col, sum([abs(row - midPos) + abs(i - midPos) for i in range(col, col + mi)]) if rowPos == -1: print(-1) else: print(rowPos + 1, colPos + 1, colPos + mi) if rowPos == rows[0] and rowPos > 0: rows.insert(rowPos - 1, 0) elif rowPos == rows[-1] and rowPos < k - 1: rows.append(rowPos + 1) for i in range(colPos, colPos + mi): hall[rowPos][i] = True for row in rows: if False not in hall[row]: rows.remove(row) ```
instruction
0
27,459
14
54,918
No
output
1
27,459
14
54,919
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. All cinema halls in Berland are rectangles with K rows of K seats each, and K is an odd number. Rows and seats are numbered from 1 to K. For safety reasons people, who come to the box office to buy tickets, are not allowed to choose seats themselves. Formerly the choice was made by a cashier, but now this is the responsibility of a special seating program. It was found out that the large majority of Berland's inhabitants go to the cinema in order to watch a movie, that's why they want to sit as close to the hall center as possible. Moreover, a company of M people, who come to watch a movie, want necessarily to occupy M successive seats in one row. Let's formulate the algorithm, according to which the program chooses seats and sells tickets. As the request for M seats comes, the program should determine the row number x and the segment [yl, yr] of the seats numbers in this row, where yr - yl + 1 = M. From all such possible variants as a final result the program should choose the one with the minimum function value of total seats remoteness from the center. Say, <image> β€” the row and the seat numbers of the most "central" seat. Then the function value of seats remoteness from the hall center is <image>. If the amount of minimum function values is more than one, the program should choose the one that is closer to the screen (i.e. the row number x is lower). If the variants are still multiple, it should choose the one with the minimum yl. If you did not get yet, your task is to simulate the work of this program. Input The first line contains two integers N and K (1 ≀ N ≀ 1000, 1 ≀ K ≀ 99) β€” the amount of requests and the hall size respectively. The second line contains N space-separated integers Mi from the range [1, K] β€” requests to the program. Output Output N lines. In the i-th line output Β«-1Β» (without quotes), if it is impossible to find Mi successive seats in one row, otherwise output three numbers x, yl, yr. Separate the numbers with a space. Examples Input 2 1 1 1 Output 1 1 1 -1 Input 4 3 1 2 3 1 Output 2 2 2 1 1 2 3 1 3 2 1 1 Submitted Solution: ``` def find_avaible(size): pos = [] for i in range(k): j = 0 c = 0 while j < k: # print('pos',pos) # print('j' , j) if th[i][j] == 0: c += 1 if c == 1: start = j if c == size: end = j pos.append([i+1, start+1, end+1]) j = start + 1 #print('j',j) c = 0 else: j+=1 else : j+=1 c = 0 #print ('pos', pos) # print('---- pos -----') # print('pos', pos) if len(pos) == 0: return -1 else: return pos # row, yl, yr, # [[1, 1, 2], [1, 2, 3], [2, 1, 2], [2, 2, 3], [3, 1, 2], [3, 2, 3]] # [[0, 1, 1], [1, 1, 0], [2, 0, 1], [3, 0, 0], [4, 1, 1], [5, 1, 0]] def find_dist(arr, idx): dist_y = 0 if arr[2] - arr[1] == 0: dist_y = abs(arr[1] - center) else: for i in range(arr[1], arr[2]+1): dist_y += abs(i - center) dist_x = abs(arr[0] - center) #dist_y = abs(arr[1] - center) #dist = [dist_x, dist_y, i] pair = [dist_x, dist_y, idx] # print(pair) return pair def closest(ok, size): dists = [] #print('-----closest sort -----') for i in range(len(ok)): d = find_dist(ok[i], i) dists.append(d) #print('dis',dists) if size == 1: dists.sort(key = lambda x: (x[0], x[1])) else: dists.sort(key = lambda x: (x[0], x[1])) #print('sor', dists) best = dists[0][2] #print(best) places = ok[best] #print(places) return places def buy(places): global th #print('range', places[1]-1) #print('range', places[2]-1) for i in range(places[1]-1, places[2]): th[places[0]-1][i] = 1 #print(th) n ,k = map(int, input().split()) m = list(map(int, input().split())) #print(m) r = c = k th = [[0 for x in range(r)] for y in range(c)] #print(th) center = (k+1) // 2 #print('center', center) res = [] for i in range(len(m)): p = find_avaible(m[i]) if p == -1: print(-1) else: c = closest(p , m[i]) print(*c, sep= ' ') buy(c) ```
instruction
0
27,460
14
54,920
No
output
1
27,460
14
54,921
Provide tags and a correct Python 3 solution for this coding contest problem. You and your n - 1 friends have found an array of integers a_1, a_2, ..., a_n. You have decided to share it in the following way: All n of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process. You are standing in the m-th position in the line. Before the process starts, you may choose up to k different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded. Suppose that you're doing your choices optimally. What is the greatest integer x such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to x? Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains three space-separated integers n, m and k (1 ≀ m ≀ n ≀ 3500, 0 ≀ k ≀ n - 1) β€” the number of elements in the array, your position in line and the number of people whose choices you can fix. The second line of each test case contains n positive integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^9) β€” elements of the array. It is guaranteed that the sum of n over all test cases does not exceed 3500. Output For each test case, print the largest integer x such that you can guarantee to obtain at least x. Example Input 4 6 4 2 2 9 2 3 8 5 4 4 1 2 13 60 4 4 1 3 1 2 2 1 2 2 0 1 2 Output 8 4 1 1 Note In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. * the first person will take the last element (5) because he or she was forced by you to take the last element. After this turn the remaining array will be [2, 9, 2, 3, 8]; * the second person will take the first element (2) because he or she was forced by you to take the first element. After this turn the remaining array will be [9, 2, 3, 8]; * if the third person will choose to take the first element (9), at your turn the remaining array will be [2, 3, 8] and you will take 8 (the last element); * if the third person will choose to take the last element (8), at your turn the remaining array will be [9, 2, 3] and you will take 9 (the first element). Thus, this strategy guarantees to end up with at least 8. We can prove that there is no strategy that guarantees to end up with at least 9. Hence, the answer is 8. In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with 4.
instruction
0
27,545
14
55,090
Tags: brute force, data structures, implementation Correct Solution: ``` a=int(input()) for i in range(a): n,m,k=map(int,input().split()) z=list(map(int,input().split())) if(m-1<=k): ans1=z[::-1] print(max(max(z[0:m]),max(ans1[0:m]))) else: alpha=[] for i in range(k+1): ans=z[i:len(z)-(k-i)] t=m-k-1 #elements chosen by others kapa=[] for i in range(t+1): kapa.append(max(ans[i],ans[-1*(t-i+1)])) alpha.append(min(kapa)) print(max(alpha)) ```
output
1
27,545
14
55,091
Provide tags and a correct Python 3 solution for this coding contest problem. You and your n - 1 friends have found an array of integers a_1, a_2, ..., a_n. You have decided to share it in the following way: All n of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process. You are standing in the m-th position in the line. Before the process starts, you may choose up to k different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded. Suppose that you're doing your choices optimally. What is the greatest integer x such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to x? Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains three space-separated integers n, m and k (1 ≀ m ≀ n ≀ 3500, 0 ≀ k ≀ n - 1) β€” the number of elements in the array, your position in line and the number of people whose choices you can fix. The second line of each test case contains n positive integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^9) β€” elements of the array. It is guaranteed that the sum of n over all test cases does not exceed 3500. Output For each test case, print the largest integer x such that you can guarantee to obtain at least x. Example Input 4 6 4 2 2 9 2 3 8 5 4 4 1 2 13 60 4 4 1 3 1 2 2 1 2 2 0 1 2 Output 8 4 1 1 Note In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. * the first person will take the last element (5) because he or she was forced by you to take the last element. After this turn the remaining array will be [2, 9, 2, 3, 8]; * the second person will take the first element (2) because he or she was forced by you to take the first element. After this turn the remaining array will be [9, 2, 3, 8]; * if the third person will choose to take the first element (9), at your turn the remaining array will be [2, 3, 8] and you will take 8 (the last element); * if the third person will choose to take the last element (8), at your turn the remaining array will be [9, 2, 3] and you will take 9 (the first element). Thus, this strategy guarantees to end up with at least 8. We can prove that there is no strategy that guarantees to end up with at least 9. Hence, the answer is 8. In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with 4.
instruction
0
27,546
14
55,092
Tags: brute force, data structures, implementation Correct Solution: ``` from sys import stdin,stdout from math import gcd,sqrt from collections import deque input=stdin.readline R=lambda:map(int,input().split()) I=lambda:int(input()) S=lambda:input().rstrip('\n') L=lambda:list(R()) P=lambda x:stdout.write(x) lcm=lambda x,y:(x*y)//gcd(x,y) hg=lambda x,y:((y+x-1)//x)*x pw=lambda x:0 if x==1 else 1+pw(x//2) chk=lambda x:chk(x//2) if not x%2 else True if x==1 else False sm=lambda x:(x**2+x)//2 N=10**9+7 def mn(p): return max(p[0],p[-1]) h=I() for _ in range(h): n,m,k=R() a=L() if k+1>=m:print(max(max(a[:m]),max(a[::-1][:m])));continue val=[] for i in range(m): val+=max(a[i],a[n-m+i]), res=m-(k+1) ans=0 for i in range(k+1): mn=N for j in range(res+1): mn=min(val[i+j],mn) ans=max(mn,ans) print(ans) ```
output
1
27,546
14
55,093
Provide tags and a correct Python 3 solution for this coding contest problem. You and your n - 1 friends have found an array of integers a_1, a_2, ..., a_n. You have decided to share it in the following way: All n of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process. You are standing in the m-th position in the line. Before the process starts, you may choose up to k different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded. Suppose that you're doing your choices optimally. What is the greatest integer x such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to x? Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains three space-separated integers n, m and k (1 ≀ m ≀ n ≀ 3500, 0 ≀ k ≀ n - 1) β€” the number of elements in the array, your position in line and the number of people whose choices you can fix. The second line of each test case contains n positive integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^9) β€” elements of the array. It is guaranteed that the sum of n over all test cases does not exceed 3500. Output For each test case, print the largest integer x such that you can guarantee to obtain at least x. Example Input 4 6 4 2 2 9 2 3 8 5 4 4 1 2 13 60 4 4 1 3 1 2 2 1 2 2 0 1 2 Output 8 4 1 1 Note In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. * the first person will take the last element (5) because he or she was forced by you to take the last element. After this turn the remaining array will be [2, 9, 2, 3, 8]; * the second person will take the first element (2) because he or she was forced by you to take the first element. After this turn the remaining array will be [9, 2, 3, 8]; * if the third person will choose to take the first element (9), at your turn the remaining array will be [2, 3, 8] and you will take 8 (the last element); * if the third person will choose to take the last element (8), at your turn the remaining array will be [9, 2, 3] and you will take 9 (the first element). Thus, this strategy guarantees to end up with at least 8. We can prove that there is no strategy that guarantees to end up with at least 9. Hence, the answer is 8. In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with 4.
instruction
0
27,547
14
55,094
Tags: brute force, data structures, implementation Correct Solution: ``` for _ in range(int(input())): n,m,k = map(int,input().split()) a = list(map(int,input().split())) mn = min(k,m-1) x,y = m-1,n-m ans = -1 for i in range(mn+1): val = float('inf') for j in range(m-mn): v = max(a[i+j],a[n-(m-i-j)]) val = min(val,v) ans = max(ans,val) print(ans) ```
output
1
27,547
14
55,095
Provide tags and a correct Python 3 solution for this coding contest problem. You and your n - 1 friends have found an array of integers a_1, a_2, ..., a_n. You have decided to share it in the following way: All n of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process. You are standing in the m-th position in the line. Before the process starts, you may choose up to k different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded. Suppose that you're doing your choices optimally. What is the greatest integer x such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to x? Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains three space-separated integers n, m and k (1 ≀ m ≀ n ≀ 3500, 0 ≀ k ≀ n - 1) β€” the number of elements in the array, your position in line and the number of people whose choices you can fix. The second line of each test case contains n positive integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^9) β€” elements of the array. It is guaranteed that the sum of n over all test cases does not exceed 3500. Output For each test case, print the largest integer x such that you can guarantee to obtain at least x. Example Input 4 6 4 2 2 9 2 3 8 5 4 4 1 2 13 60 4 4 1 3 1 2 2 1 2 2 0 1 2 Output 8 4 1 1 Note In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. * the first person will take the last element (5) because he or she was forced by you to take the last element. After this turn the remaining array will be [2, 9, 2, 3, 8]; * the second person will take the first element (2) because he or she was forced by you to take the first element. After this turn the remaining array will be [9, 2, 3, 8]; * if the third person will choose to take the first element (9), at your turn the remaining array will be [2, 3, 8] and you will take 8 (the last element); * if the third person will choose to take the last element (8), at your turn the remaining array will be [9, 2, 3] and you will take 9 (the first element). Thus, this strategy guarantees to end up with at least 8. We can prove that there is no strategy that guarantees to end up with at least 9. Hence, the answer is 8. In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with 4.
instruction
0
27,548
14
55,096
Tags: brute force, data structures, implementation Correct Solution: ``` def Input(): tem = input().split() ans = [] for it in tem: ans.append(int(it)) return ans T = Input()[0] for t in range(T): n,m,k = Input() a = Input() if k+1>=m: l, r = 0, n-m ans = -10000000000000 while r<n: ans = max(ans, a[l], a[r]) l+=1 r+=1 print(ans) else: b = [] l, r = 0, n-m while r<n: b.append(max(a[l],a[r])) l+=1 r+=1 # q = m-1-k l, r = 1, m-k que = [] max_que = [] for i in range(m-k): que.append([b[i],i]) while len(max_que)>0 and max_que[-1][0]>=b[i]: max_que.pop() max_que.append([b[i],i]) ans = max_que[0][0] while r<m: que.append([b[r],r]) while len(max_que)>0 and max_que[-1][0]>=b[r]: max_que.pop() max_que.append([b[r],r]) while que[0][1]>=max_que[0][1]: del max_que[0] del que[0] ans = max(ans, max_que[0][0]) l+=1 r+=1 print(ans) ```
output
1
27,548
14
55,097
Provide tags and a correct Python 3 solution for this coding contest problem. You and your n - 1 friends have found an array of integers a_1, a_2, ..., a_n. You have decided to share it in the following way: All n of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process. You are standing in the m-th position in the line. Before the process starts, you may choose up to k different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded. Suppose that you're doing your choices optimally. What is the greatest integer x such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to x? Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains three space-separated integers n, m and k (1 ≀ m ≀ n ≀ 3500, 0 ≀ k ≀ n - 1) β€” the number of elements in the array, your position in line and the number of people whose choices you can fix. The second line of each test case contains n positive integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^9) β€” elements of the array. It is guaranteed that the sum of n over all test cases does not exceed 3500. Output For each test case, print the largest integer x such that you can guarantee to obtain at least x. Example Input 4 6 4 2 2 9 2 3 8 5 4 4 1 2 13 60 4 4 1 3 1 2 2 1 2 2 0 1 2 Output 8 4 1 1 Note In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. * the first person will take the last element (5) because he or she was forced by you to take the last element. After this turn the remaining array will be [2, 9, 2, 3, 8]; * the second person will take the first element (2) because he or she was forced by you to take the first element. After this turn the remaining array will be [9, 2, 3, 8]; * if the third person will choose to take the first element (9), at your turn the remaining array will be [2, 3, 8] and you will take 8 (the last element); * if the third person will choose to take the last element (8), at your turn the remaining array will be [9, 2, 3] and you will take 9 (the first element). Thus, this strategy guarantees to end up with at least 8. We can prove that there is no strategy that guarantees to end up with at least 9. Hence, the answer is 8. In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with 4.
instruction
0
27,549
14
55,098
Tags: brute force, data structures, implementation Correct Solution: ``` for _ in range(int(input())) : n, m, k = map(int, input().split()) a = list(map(int, input().split())) mx = -1 if k >= m : for i in range(n) : if i < m or i > n - m - 1 : mx = max(mx, a[i]) print(mx) else : ans = 0 for t in range(k + 1) : val = 10000000000 for i in range(m - k) : val = min(val, max(a[t + i], a[n - 1 - k + t - (m - k - 1) + i])) ans = max(ans, val) print(ans) ```
output
1
27,549
14
55,099
Provide tags and a correct Python 3 solution for this coding contest problem. You and your n - 1 friends have found an array of integers a_1, a_2, ..., a_n. You have decided to share it in the following way: All n of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process. You are standing in the m-th position in the line. Before the process starts, you may choose up to k different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded. Suppose that you're doing your choices optimally. What is the greatest integer x such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to x? Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains three space-separated integers n, m and k (1 ≀ m ≀ n ≀ 3500, 0 ≀ k ≀ n - 1) β€” the number of elements in the array, your position in line and the number of people whose choices you can fix. The second line of each test case contains n positive integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^9) β€” elements of the array. It is guaranteed that the sum of n over all test cases does not exceed 3500. Output For each test case, print the largest integer x such that you can guarantee to obtain at least x. Example Input 4 6 4 2 2 9 2 3 8 5 4 4 1 2 13 60 4 4 1 3 1 2 2 1 2 2 0 1 2 Output 8 4 1 1 Note In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. * the first person will take the last element (5) because he or she was forced by you to take the last element. After this turn the remaining array will be [2, 9, 2, 3, 8]; * the second person will take the first element (2) because he or she was forced by you to take the first element. After this turn the remaining array will be [9, 2, 3, 8]; * if the third person will choose to take the first element (9), at your turn the remaining array will be [2, 3, 8] and you will take 8 (the last element); * if the third person will choose to take the last element (8), at your turn the remaining array will be [9, 2, 3] and you will take 9 (the first element). Thus, this strategy guarantees to end up with at least 8. We can prove that there is no strategy that guarantees to end up with at least 9. Hence, the answer is 8. In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with 4.
instruction
0
27,550
14
55,100
Tags: brute force, data structures, implementation Correct Solution: ``` T = int(input()) for cs in range(T): n, m, k = map(int,input().split()) a = list(map(int,input().split())) k = min(k,m-1) # i only need to persuade infront of me p = m-k mx = 0 for dummy in range(10): for i in range(k+1): mn = 1000000007 for j in range(p): mn = min(mn,max(a[i+j],a[n-k+i-p+j])) mx = max(mx,mn) print(mx) ```
output
1
27,550
14
55,101
Provide tags and a correct Python 3 solution for this coding contest problem. You and your n - 1 friends have found an array of integers a_1, a_2, ..., a_n. You have decided to share it in the following way: All n of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process. You are standing in the m-th position in the line. Before the process starts, you may choose up to k different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded. Suppose that you're doing your choices optimally. What is the greatest integer x such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to x? Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains three space-separated integers n, m and k (1 ≀ m ≀ n ≀ 3500, 0 ≀ k ≀ n - 1) β€” the number of elements in the array, your position in line and the number of people whose choices you can fix. The second line of each test case contains n positive integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^9) β€” elements of the array. It is guaranteed that the sum of n over all test cases does not exceed 3500. Output For each test case, print the largest integer x such that you can guarantee to obtain at least x. Example Input 4 6 4 2 2 9 2 3 8 5 4 4 1 2 13 60 4 4 1 3 1 2 2 1 2 2 0 1 2 Output 8 4 1 1 Note In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. * the first person will take the last element (5) because he or she was forced by you to take the last element. After this turn the remaining array will be [2, 9, 2, 3, 8]; * the second person will take the first element (2) because he or she was forced by you to take the first element. After this turn the remaining array will be [9, 2, 3, 8]; * if the third person will choose to take the first element (9), at your turn the remaining array will be [2, 3, 8] and you will take 8 (the last element); * if the third person will choose to take the last element (8), at your turn the remaining array will be [9, 2, 3] and you will take 9 (the first element). Thus, this strategy guarantees to end up with at least 8. We can prove that there is no strategy that guarantees to end up with at least 9. Hence, the answer is 8. In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with 4.
instruction
0
27,551
14
55,102
Tags: brute force, data structures, implementation Correct Solution: ``` def chek(x,li,l): # print(li,x,l) mn=max(li[0],li[l-1]) for i in range(x+1): mx=max(li[i],li[l+i-1]) mn=min(mn,mx) # print(mn) return mn t=int(input()) for _ in range(t): n,m,k=map(int,input().split()) ar=list(map(int,input().split())) b=0 x=m-1-k l=n-k y=n-(m-1) if x<=0: print(max(max(ar[:m]),max(ar[n-m:]))) continue for j in range(k+1): a=chek(x,ar[j:j+l],y) b=max(a,b) print(b) ```
output
1
27,551
14
55,103
Provide tags and a correct Python 3 solution for this coding contest problem. You and your n - 1 friends have found an array of integers a_1, a_2, ..., a_n. You have decided to share it in the following way: All n of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process. You are standing in the m-th position in the line. Before the process starts, you may choose up to k different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded. Suppose that you're doing your choices optimally. What is the greatest integer x such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to x? Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains three space-separated integers n, m and k (1 ≀ m ≀ n ≀ 3500, 0 ≀ k ≀ n - 1) β€” the number of elements in the array, your position in line and the number of people whose choices you can fix. The second line of each test case contains n positive integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^9) β€” elements of the array. It is guaranteed that the sum of n over all test cases does not exceed 3500. Output For each test case, print the largest integer x such that you can guarantee to obtain at least x. Example Input 4 6 4 2 2 9 2 3 8 5 4 4 1 2 13 60 4 4 1 3 1 2 2 1 2 2 0 1 2 Output 8 4 1 1 Note In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. * the first person will take the last element (5) because he or she was forced by you to take the last element. After this turn the remaining array will be [2, 9, 2, 3, 8]; * the second person will take the first element (2) because he or she was forced by you to take the first element. After this turn the remaining array will be [9, 2, 3, 8]; * if the third person will choose to take the first element (9), at your turn the remaining array will be [2, 3, 8] and you will take 8 (the last element); * if the third person will choose to take the last element (8), at your turn the remaining array will be [9, 2, 3] and you will take 9 (the first element). Thus, this strategy guarantees to end up with at least 8. We can prove that there is no strategy that guarantees to end up with at least 9. Hence, the answer is 8. In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with 4.
instruction
0
27,552
14
55,104
Tags: brute force, data structures, implementation Correct Solution: ``` #------------------------------warmup---------------------------- import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #-------------------game starts now----------------------------------------------------- for i1 in range(int(input())): n,m,k=map(int,input().split()) l=list(map(int,input().split())) x=min(k,m-1) y=m-1-x ans=99999999999999999999 ansf=0 for j in range(x+1): ans=99999999999999999999 for i in range(y+1): ans=min(max(l[i+j],l[n-m+i+j]),ans) ansf=max(ansf,ans) print(ansf) ```
output
1
27,552
14
55,105
Provide tags and a correct Python 3 solution for this coding contest problem. In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai β€” how many people who are taller than him/her and stand in queue in front of him. After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order. When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai. Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi β€” the heights of people in the queue, so that the numbers ai are correct. Input The first input line contains integer n β€” the number of people in the queue (1 ≀ n ≀ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 ≀ ai ≀ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different. Output If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique. Examples Input 4 a 0 b 2 c 0 d 0 Output a 150 c 170 d 180 b 160 Input 4 vasya 0 petya 1 manya 3 dunay 3 Output -1
instruction
0
27,617
14
55,234
Tags: constructive algorithms, greedy, sortings Correct Solution: ``` n = int(input()) arr = [] for i in range(n): a,b = list(map(str,input().split())) b = int(b) arr.append([b,a]) arr.sort() ans = [] for i in arr: if i[0]>len(ans): print(-1) exit() ans.insert(i[0],(i[1],n)) n-=1 for i in ans: print(i[0],i[1]) ```
output
1
27,617
14
55,235
Provide tags and a correct Python 3 solution for this coding contest problem. In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai β€” how many people who are taller than him/her and stand in queue in front of him. After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order. When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai. Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi β€” the heights of people in the queue, so that the numbers ai are correct. Input The first input line contains integer n β€” the number of people in the queue (1 ≀ n ≀ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 ≀ ai ≀ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different. Output If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique. Examples Input 4 a 0 b 2 c 0 d 0 Output a 150 c 170 d 180 b 160 Input 4 vasya 0 petya 1 manya 3 dunay 3 Output -1
instruction
0
27,618
14
55,236
Tags: constructive algorithms, greedy, sortings Correct Solution: ``` #!/usr/bin/python3 n = int(input()) a = [] for i in range(n): s, c = input().split() a.append((s, int(c))) a.sort(key=lambda x: x[1]) ans = [] for i in range(n): if len(ans) < a[i][1]: print(-1) exit(0) ans.insert(a[i][1], (a[i][0], n - i)) for s, a in ans: print(s, a) ```
output
1
27,618
14
55,237
Provide tags and a correct Python 3 solution for this coding contest problem. In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai β€” how many people who are taller than him/her and stand in queue in front of him. After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order. When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai. Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi β€” the heights of people in the queue, so that the numbers ai are correct. Input The first input line contains integer n β€” the number of people in the queue (1 ≀ n ≀ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 ≀ ai ≀ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different. Output If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique. Examples Input 4 a 0 b 2 c 0 d 0 Output a 150 c 170 d 180 b 160 Input 4 vasya 0 petya 1 manya 3 dunay 3 Output -1
instruction
0
27,619
14
55,238
Tags: constructive algorithms, greedy, sortings Correct Solution: ``` n = int(input()) a = sorted([list(map(lambda x: x if x[0] >= 'a' and x[0] <= 'z' else int(x), input().split()))[::-1] for i in range(n)]) fl = 1 ans = [] for i in range(len(a)): v, name = a[i] if i == 0: if v > 0: fl = 0 break else: ans.append((name, n,)) else: if (v > i): fl = 0 break ans.insert(v, (name, n - i,)) if not fl: print(-1) else: for i in ans: print(*i) ```
output
1
27,619
14
55,239
Provide tags and a correct Python 3 solution for this coding contest problem. In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai β€” how many people who are taller than him/her and stand in queue in front of him. After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order. When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai. Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi β€” the heights of people in the queue, so that the numbers ai are correct. Input The first input line contains integer n β€” the number of people in the queue (1 ≀ n ≀ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 ≀ ai ≀ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different. Output If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique. Examples Input 4 a 0 b 2 c 0 d 0 Output a 150 c 170 d 180 b 160 Input 4 vasya 0 petya 1 manya 3 dunay 3 Output -1
instruction
0
27,620
14
55,240
Tags: constructive algorithms, greedy, sortings Correct Solution: ``` n = int(input()) a = [] for _ in range(n): x = input().split() a.append((x[0], int(x[1]))) a.sort(key=lambda x: x[1]) ans = [] for x in a: if x[1] > len(ans): print(-1) exit() ans.insert(x[1], (x[0], n)) n -= 1 for x in ans: print(x[0], x[1]) ```
output
1
27,620
14
55,241
Provide tags and a correct Python 3 solution for this coding contest problem. In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai β€” how many people who are taller than him/her and stand in queue in front of him. After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order. When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai. Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi β€” the heights of people in the queue, so that the numbers ai are correct. Input The first input line contains integer n β€” the number of people in the queue (1 ≀ n ≀ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 ≀ ai ≀ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different. Output If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique. Examples Input 4 a 0 b 2 c 0 d 0 Output a 150 c 170 d 180 b 160 Input 4 vasya 0 petya 1 manya 3 dunay 3 Output -1
instruction
0
27,621
14
55,242
Tags: constructive algorithms, greedy, sortings Correct Solution: ``` n = int(input()) arr = [input().split() for _ in range(n)] arr = [[int(arr[i][1]), arr[i][0]] for i in range(n)] arr.sort() res = [] for i in range(n): res.append(i - arr[i][0]) if res[i] < 0: print(-1) exit() for j in range(i): if res[j] >= res[i]: res[j] += 1 for i in range(n): print(arr[i][1], res[i]+1) ```
output
1
27,621
14
55,243
Provide tags and a correct Python 3 solution for this coding contest problem. In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai β€” how many people who are taller than him/her and stand in queue in front of him. After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order. When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai. Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi β€” the heights of people in the queue, so that the numbers ai are correct. Input The first input line contains integer n β€” the number of people in the queue (1 ≀ n ≀ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 ≀ ai ≀ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different. Output If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique. Examples Input 4 a 0 b 2 c 0 d 0 Output a 150 c 170 d 180 b 160 Input 4 vasya 0 petya 1 manya 3 dunay 3 Output -1
instruction
0
27,622
14
55,244
Tags: constructive algorithms, greedy, sortings Correct Solution: ``` n=int(input()) arr=[] for i in range(n): a,b=input().split() arr.append([a,int(b)]) arr.sort(key=lambda x:x[1]) ans=[] #print(arr) for x in arr: if x[1]>len(ans): print(-1) exit() ans.insert(x[1],(x[0],n)) n-=1 for x in ans:print(x[0],x[1]) ```
output
1
27,622
14
55,245
Provide tags and a correct Python 3 solution for this coding contest problem. In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai β€” how many people who are taller than him/her and stand in queue in front of him. After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order. When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai. Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi β€” the heights of people in the queue, so that the numbers ai are correct. Input The first input line contains integer n β€” the number of people in the queue (1 ≀ n ≀ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 ≀ ai ≀ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different. Output If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique. Examples Input 4 a 0 b 2 c 0 d 0 Output a 150 c 170 d 180 b 160 Input 4 vasya 0 petya 1 manya 3 dunay 3 Output -1
instruction
0
27,623
14
55,246
Tags: constructive algorithms, greedy, sortings Correct Solution: ``` import sys n = int(input()) person = [] for i in range(n): person.append(input().split()) person.sort(key = lambda x: int(x[1])) high = 10 ** 9 low = 1 cntHigh = 0 for i in range(n): dif = int(person[i][1]) - cntHigh for j in range(i - 1, -1, -1): if dif == 0: break if person[j][2] < high: person[j][2] = high high -= 1 cntHigh += 1 dif -= 1 if dif > 0: print('-1') sys.exit(0) person[i] = [person[i][0], person[i][1], low] low += 1 for p in person: print(p[0], p[2]) ```
output
1
27,623
14
55,247