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 tags and a correct Python 3 solution for this coding contest problem. To quickly hire highly skilled specialists one of the new IT City companies made an unprecedented move. Every employee was granted a car, and an employee can choose one of four different car makes. The parking lot before the office consists of one line of (2n - 2) parking spaces. Unfortunately the total number of cars is greater than the parking lot capacity. Furthermore even amount of cars of each make is greater than the amount of parking spaces! That's why there are no free spaces on the parking lot ever. Looking on the straight line of cars the company CEO thought that parking lot would be more beautiful if it contained exactly n successive cars of the same make. Help the CEO determine the number of ways to fill the parking lot this way. Input The only line of the input contains one integer n (3 ≤ n ≤ 30) — the amount of successive cars of the same make. Output Output one integer — the number of ways to fill the parking lot by cars of four makes using the described way. Examples Input 3 Output 24 Note Let's denote car makes in the following way: A — Aston Martin, B — Bentley, M — Mercedes-Maybach, Z — Zaporozhets. For n = 3 there are the following appropriate ways to fill the parking lot: AAAB AAAM AAAZ ABBB AMMM AZZZ BBBA BBBM BBBZ BAAA BMMM BZZZ MMMA MMMB MMMZ MAAA MBBB MZZZ ZZZA ZZZB ZZZM ZAAA ZBBB ZMMM Originally it was planned to grant sport cars of Ferrari, Lamborghini, Maserati and Bugatti makes but this idea was renounced because it is impossible to drive these cars having small road clearance on the worn-down roads of IT City.
instruction
0
4,620
14
9,240
Tags: combinatorics, math Correct Solution: ``` a=int(input()) print(4**(a-3)*3*(3*a-1)) ```
output
1
4,620
14
9,241
Provide tags and a correct Python 3 solution for this coding contest problem. To quickly hire highly skilled specialists one of the new IT City companies made an unprecedented move. Every employee was granted a car, and an employee can choose one of four different car makes. The parking lot before the office consists of one line of (2n - 2) parking spaces. Unfortunately the total number of cars is greater than the parking lot capacity. Furthermore even amount of cars of each make is greater than the amount of parking spaces! That's why there are no free spaces on the parking lot ever. Looking on the straight line of cars the company CEO thought that parking lot would be more beautiful if it contained exactly n successive cars of the same make. Help the CEO determine the number of ways to fill the parking lot this way. Input The only line of the input contains one integer n (3 ≤ n ≤ 30) — the amount of successive cars of the same make. Output Output one integer — the number of ways to fill the parking lot by cars of four makes using the described way. Examples Input 3 Output 24 Note Let's denote car makes in the following way: A — Aston Martin, B — Bentley, M — Mercedes-Maybach, Z — Zaporozhets. For n = 3 there are the following appropriate ways to fill the parking lot: AAAB AAAM AAAZ ABBB AMMM AZZZ BBBA BBBM BBBZ BAAA BMMM BZZZ MMMA MMMB MMMZ MAAA MBBB MZZZ ZZZA ZZZB ZZZM ZAAA ZBBB ZMMM Originally it was planned to grant sport cars of Ferrari, Lamborghini, Maserati and Bugatti makes but this idea was renounced because it is impossible to drive these cars having small road clearance on the worn-down roads of IT City.
instruction
0
4,621
14
9,242
Tags: combinatorics, math Correct Solution: ``` n = int(input()) N = 2*n-2 ans = 0; for i in range(n): l_cnt = i r_cnt = N - i - n if r_cnt < 0: break m = 4 p = int(l_cnt > 0) + int(r_cnt > 0) m *= 3 ** p l_cnt = max(0, l_cnt - 1) r_cnt = max(0, r_cnt - 1) p = l_cnt + r_cnt; m *= 4 ** p; ans += m print(ans) ```
output
1
4,621
14
9,243
Provide tags and a correct Python 3 solution for this coding contest problem. To quickly hire highly skilled specialists one of the new IT City companies made an unprecedented move. Every employee was granted a car, and an employee can choose one of four different car makes. The parking lot before the office consists of one line of (2n - 2) parking spaces. Unfortunately the total number of cars is greater than the parking lot capacity. Furthermore even amount of cars of each make is greater than the amount of parking spaces! That's why there are no free spaces on the parking lot ever. Looking on the straight line of cars the company CEO thought that parking lot would be more beautiful if it contained exactly n successive cars of the same make. Help the CEO determine the number of ways to fill the parking lot this way. Input The only line of the input contains one integer n (3 ≤ n ≤ 30) — the amount of successive cars of the same make. Output Output one integer — the number of ways to fill the parking lot by cars of four makes using the described way. Examples Input 3 Output 24 Note Let's denote car makes in the following way: A — Aston Martin, B — Bentley, M — Mercedes-Maybach, Z — Zaporozhets. For n = 3 there are the following appropriate ways to fill the parking lot: AAAB AAAM AAAZ ABBB AMMM AZZZ BBBA BBBM BBBZ BAAA BMMM BZZZ MMMA MMMB MMMZ MAAA MBBB MZZZ ZZZA ZZZB ZZZM ZAAA ZBBB ZMMM Originally it was planned to grant sport cars of Ferrari, Lamborghini, Maserati and Bugatti makes but this idea was renounced because it is impossible to drive these cars having small road clearance on the worn-down roads of IT City.
instruction
0
4,622
14
9,244
Tags: combinatorics, math Correct Solution: ``` # Принимаем входные данные n = int(input()) # Вычисляем ответ ways_count = max(((n - 3) * 36 * 4**(n - 4)), (n - 3) * 36) # Когда машины стоят не по краям ways_count += 2 * (3 * 4**(n-3)) * 4 # Когда машины стоят по краям # Выводим результат print(int(ways_count)) ```
output
1
4,622
14
9,245
Provide tags and a correct Python 3 solution for this coding contest problem. To quickly hire highly skilled specialists one of the new IT City companies made an unprecedented move. Every employee was granted a car, and an employee can choose one of four different car makes. The parking lot before the office consists of one line of (2n - 2) parking spaces. Unfortunately the total number of cars is greater than the parking lot capacity. Furthermore even amount of cars of each make is greater than the amount of parking spaces! That's why there are no free spaces on the parking lot ever. Looking on the straight line of cars the company CEO thought that parking lot would be more beautiful if it contained exactly n successive cars of the same make. Help the CEO determine the number of ways to fill the parking lot this way. Input The only line of the input contains one integer n (3 ≤ n ≤ 30) — the amount of successive cars of the same make. Output Output one integer — the number of ways to fill the parking lot by cars of four makes using the described way. Examples Input 3 Output 24 Note Let's denote car makes in the following way: A — Aston Martin, B — Bentley, M — Mercedes-Maybach, Z — Zaporozhets. For n = 3 there are the following appropriate ways to fill the parking lot: AAAB AAAM AAAZ ABBB AMMM AZZZ BBBA BBBM BBBZ BAAA BMMM BZZZ MMMA MMMB MMMZ MAAA MBBB MZZZ ZZZA ZZZB ZZZM ZAAA ZBBB ZMMM Originally it was planned to grant sport cars of Ferrari, Lamborghini, Maserati and Bugatti makes but this idea was renounced because it is impossible to drive these cars having small road clearance on the worn-down roads of IT City.
instruction
0
4,623
14
9,246
Tags: combinatorics, math Correct Solution: ``` n = int(input()) print(int(4*(n-3)*9*4**(n-4) + 2*4*3*4**(n-3))) ```
output
1
4,623
14
9,247
Provide tags and a correct Python 3 solution for this coding contest problem. To quickly hire highly skilled specialists one of the new IT City companies made an unprecedented move. Every employee was granted a car, and an employee can choose one of four different car makes. The parking lot before the office consists of one line of (2n - 2) parking spaces. Unfortunately the total number of cars is greater than the parking lot capacity. Furthermore even amount of cars of each make is greater than the amount of parking spaces! That's why there are no free spaces on the parking lot ever. Looking on the straight line of cars the company CEO thought that parking lot would be more beautiful if it contained exactly n successive cars of the same make. Help the CEO determine the number of ways to fill the parking lot this way. Input The only line of the input contains one integer n (3 ≤ n ≤ 30) — the amount of successive cars of the same make. Output Output one integer — the number of ways to fill the parking lot by cars of four makes using the described way. Examples Input 3 Output 24 Note Let's denote car makes in the following way: A — Aston Martin, B — Bentley, M — Mercedes-Maybach, Z — Zaporozhets. For n = 3 there are the following appropriate ways to fill the parking lot: AAAB AAAM AAAZ ABBB AMMM AZZZ BBBA BBBM BBBZ BAAA BMMM BZZZ MMMA MMMB MMMZ MAAA MBBB MZZZ ZZZA ZZZB ZZZM ZAAA ZBBB ZMMM Originally it was planned to grant sport cars of Ferrari, Lamborghini, Maserati and Bugatti makes but this idea was renounced because it is impossible to drive these cars having small road clearance on the worn-down roads of IT City.
instruction
0
4,624
14
9,248
Tags: combinatorics, math Correct Solution: ``` n = int(input()) print(6 * 4**(n-2) + 9 * (n-3) * 4**(n-3)) ```
output
1
4,624
14
9,249
Provide tags and a correct Python 3 solution for this coding contest problem. To quickly hire highly skilled specialists one of the new IT City companies made an unprecedented move. Every employee was granted a car, and an employee can choose one of four different car makes. The parking lot before the office consists of one line of (2n - 2) parking spaces. Unfortunately the total number of cars is greater than the parking lot capacity. Furthermore even amount of cars of each make is greater than the amount of parking spaces! That's why there are no free spaces on the parking lot ever. Looking on the straight line of cars the company CEO thought that parking lot would be more beautiful if it contained exactly n successive cars of the same make. Help the CEO determine the number of ways to fill the parking lot this way. Input The only line of the input contains one integer n (3 ≤ n ≤ 30) — the amount of successive cars of the same make. Output Output one integer — the number of ways to fill the parking lot by cars of four makes using the described way. Examples Input 3 Output 24 Note Let's denote car makes in the following way: A — Aston Martin, B — Bentley, M — Mercedes-Maybach, Z — Zaporozhets. For n = 3 there are the following appropriate ways to fill the parking lot: AAAB AAAM AAAZ ABBB AMMM AZZZ BBBA BBBM BBBZ BAAA BMMM BZZZ MMMA MMMB MMMZ MAAA MBBB MZZZ ZZZA ZZZB ZZZM ZAAA ZBBB ZMMM Originally it was planned to grant sport cars of Ferrari, Lamborghini, Maserati and Bugatti makes but this idea was renounced because it is impossible to drive these cars having small road clearance on the worn-down roads of IT City.
instruction
0
4,625
14
9,250
Tags: combinatorics, math Correct Solution: ``` n = int(input()) res = 0 res += 8 * (3 * pow(4, n - 3)) res += ((n - 3) * 4 * 9 * int(pow(4, n - 4))) print(res) ```
output
1
4,625
14
9,251
Provide tags and a correct Python 3 solution for this coding contest problem. To quickly hire highly skilled specialists one of the new IT City companies made an unprecedented move. Every employee was granted a car, and an employee can choose one of four different car makes. The parking lot before the office consists of one line of (2n - 2) parking spaces. Unfortunately the total number of cars is greater than the parking lot capacity. Furthermore even amount of cars of each make is greater than the amount of parking spaces! That's why there are no free spaces on the parking lot ever. Looking on the straight line of cars the company CEO thought that parking lot would be more beautiful if it contained exactly n successive cars of the same make. Help the CEO determine the number of ways to fill the parking lot this way. Input The only line of the input contains one integer n (3 ≤ n ≤ 30) — the amount of successive cars of the same make. Output Output one integer — the number of ways to fill the parking lot by cars of four makes using the described way. Examples Input 3 Output 24 Note Let's denote car makes in the following way: A — Aston Martin, B — Bentley, M — Mercedes-Maybach, Z — Zaporozhets. For n = 3 there are the following appropriate ways to fill the parking lot: AAAB AAAM AAAZ ABBB AMMM AZZZ BBBA BBBM BBBZ BAAA BMMM BZZZ MMMA MMMB MMMZ MAAA MBBB MZZZ ZZZA ZZZB ZZZM ZAAA ZBBB ZMMM Originally it was planned to grant sport cars of Ferrari, Lamborghini, Maserati and Bugatti makes but this idea was renounced because it is impossible to drive these cars having small road clearance on the worn-down roads of IT City.
instruction
0
4,626
14
9,252
Tags: combinatorics, math Correct Solution: ``` import sys,math inputs = iter(sys.stdin.readlines()) n = int(next(inputs)) print(int(2*4*3*pow(4,n-3) + (n-3)*4*3**2*pow(4,n-4))) ```
output
1
4,626
14
9,253
Provide tags and a correct Python 3 solution for this coding contest problem. Santa Claus is the first who came to the Christmas Olympiad, and he is going to be the first to take his place at a desk! In the classroom there are n lanes of m desks each, and there are two working places at each of the desks. The lanes are numbered from 1 to n from the left to the right, the desks in a lane are numbered from 1 to m starting from the blackboard. Note that the lanes go perpendicularly to the blackboard, not along it (see picture). The organizers numbered all the working places from 1 to 2nm. The places are numbered by lanes (i. e. all the places of the first lane go first, then all the places of the second lane, and so on), in a lane the places are numbered starting from the nearest to the blackboard (i. e. from the first desk in the lane), at each desk, the place on the left is numbered before the place on the right. <image> The picture illustrates the first and the second samples. Santa Clause knows that his place has number k. Help him to determine at which lane at which desk he should sit, and whether his place is on the left or on the right! Input The only line contains three integers n, m and k (1 ≤ n, m ≤ 10 000, 1 ≤ k ≤ 2nm) — the number of lanes, the number of desks in each lane and the number of Santa Claus' place. Output Print two integers: the number of lane r, the number of desk d, and a character s, which stands for the side of the desk Santa Claus. The character s should be "L", if Santa Clause should sit on the left, and "R" if his place is on the right. Examples Input 4 3 9 Output 2 2 L Input 4 3 24 Output 4 3 R Input 2 4 4 Output 1 2 R Note The first and the second samples are shown on the picture. The green place corresponds to Santa Claus' place in the first example, the blue place corresponds to Santa Claus' place in the second example. In the third sample there are two lanes with four desks in each, and Santa Claus has the fourth place. Thus, his place is in the first lane at the second desk on the right.
instruction
0
4,667
14
9,334
Tags: implementation, math Correct Solution: ``` n, m, k = map(int, input().split()) L = int(k/(m+m)) if k%(m+m)!=0:L = L + 1 D = (L-1)*(m+m) + 1 if k%2==0:D = D + 1 D = int((k-D)/2) + 1 st = "L" if k%2==0:st = "R" print(L, D, st) ```
output
1
4,667
14
9,335
Provide tags and a correct Python 3 solution for this coding contest problem. Santa Claus is the first who came to the Christmas Olympiad, and he is going to be the first to take his place at a desk! In the classroom there are n lanes of m desks each, and there are two working places at each of the desks. The lanes are numbered from 1 to n from the left to the right, the desks in a lane are numbered from 1 to m starting from the blackboard. Note that the lanes go perpendicularly to the blackboard, not along it (see picture). The organizers numbered all the working places from 1 to 2nm. The places are numbered by lanes (i. e. all the places of the first lane go first, then all the places of the second lane, and so on), in a lane the places are numbered starting from the nearest to the blackboard (i. e. from the first desk in the lane), at each desk, the place on the left is numbered before the place on the right. <image> The picture illustrates the first and the second samples. Santa Clause knows that his place has number k. Help him to determine at which lane at which desk he should sit, and whether his place is on the left or on the right! Input The only line contains three integers n, m and k (1 ≤ n, m ≤ 10 000, 1 ≤ k ≤ 2nm) — the number of lanes, the number of desks in each lane and the number of Santa Claus' place. Output Print two integers: the number of lane r, the number of desk d, and a character s, which stands for the side of the desk Santa Claus. The character s should be "L", if Santa Clause should sit on the left, and "R" if his place is on the right. Examples Input 4 3 9 Output 2 2 L Input 4 3 24 Output 4 3 R Input 2 4 4 Output 1 2 R Note The first and the second samples are shown on the picture. The green place corresponds to Santa Claus' place in the first example, the blue place corresponds to Santa Claus' place in the second example. In the third sample there are two lanes with four desks in each, and Santa Claus has the fourth place. Thus, his place is in the first lane at the second desk on the right.
instruction
0
4,668
14
9,336
Tags: implementation, math Correct Solution: ``` import math n,m,k=map(int,input().split()) l=math.ceil(k/(2*m)) se=math.ceil((k-((l-1)*2*m))/2) print(l,se,end=' ') if k%2==0:print('R') else:print('L') ```
output
1
4,668
14
9,337
Provide tags and a correct Python 3 solution for this coding contest problem. Santa Claus is the first who came to the Christmas Olympiad, and he is going to be the first to take his place at a desk! In the classroom there are n lanes of m desks each, and there are two working places at each of the desks. The lanes are numbered from 1 to n from the left to the right, the desks in a lane are numbered from 1 to m starting from the blackboard. Note that the lanes go perpendicularly to the blackboard, not along it (see picture). The organizers numbered all the working places from 1 to 2nm. The places are numbered by lanes (i. e. all the places of the first lane go first, then all the places of the second lane, and so on), in a lane the places are numbered starting from the nearest to the blackboard (i. e. from the first desk in the lane), at each desk, the place on the left is numbered before the place on the right. <image> The picture illustrates the first and the second samples. Santa Clause knows that his place has number k. Help him to determine at which lane at which desk he should sit, and whether his place is on the left or on the right! Input The only line contains three integers n, m and k (1 ≤ n, m ≤ 10 000, 1 ≤ k ≤ 2nm) — the number of lanes, the number of desks in each lane and the number of Santa Claus' place. Output Print two integers: the number of lane r, the number of desk d, and a character s, which stands for the side of the desk Santa Claus. The character s should be "L", if Santa Clause should sit on the left, and "R" if his place is on the right. Examples Input 4 3 9 Output 2 2 L Input 4 3 24 Output 4 3 R Input 2 4 4 Output 1 2 R Note The first and the second samples are shown on the picture. The green place corresponds to Santa Claus' place in the first example, the blue place corresponds to Santa Claus' place in the second example. In the third sample there are two lanes with four desks in each, and Santa Claus has the fourth place. Thus, his place is in the first lane at the second desk on the right.
instruction
0
4,669
14
9,338
Tags: implementation, math Correct Solution: ``` n,m,k = map(int,input().split()) a = [] p = 1 for i in range(m): a += [[p,p+1]]; p += 2 p -= 1 x = -(-k//p) if k % 2 == 0: z = "R" else: z = "L" if k % p == 0: y = m else: for i in range(m): if k%p in a[i]: y = i+1; break print(x,y,z) ```
output
1
4,669
14
9,339
Provide tags and a correct Python 3 solution for this coding contest problem. Santa Claus is the first who came to the Christmas Olympiad, and he is going to be the first to take his place at a desk! In the classroom there are n lanes of m desks each, and there are two working places at each of the desks. The lanes are numbered from 1 to n from the left to the right, the desks in a lane are numbered from 1 to m starting from the blackboard. Note that the lanes go perpendicularly to the blackboard, not along it (see picture). The organizers numbered all the working places from 1 to 2nm. The places are numbered by lanes (i. e. all the places of the first lane go first, then all the places of the second lane, and so on), in a lane the places are numbered starting from the nearest to the blackboard (i. e. from the first desk in the lane), at each desk, the place on the left is numbered before the place on the right. <image> The picture illustrates the first and the second samples. Santa Clause knows that his place has number k. Help him to determine at which lane at which desk he should sit, and whether his place is on the left or on the right! Input The only line contains three integers n, m and k (1 ≤ n, m ≤ 10 000, 1 ≤ k ≤ 2nm) — the number of lanes, the number of desks in each lane and the number of Santa Claus' place. Output Print two integers: the number of lane r, the number of desk d, and a character s, which stands for the side of the desk Santa Claus. The character s should be "L", if Santa Clause should sit on the left, and "R" if his place is on the right. Examples Input 4 3 9 Output 2 2 L Input 4 3 24 Output 4 3 R Input 2 4 4 Output 1 2 R Note The first and the second samples are shown on the picture. The green place corresponds to Santa Claus' place in the first example, the blue place corresponds to Santa Claus' place in the second example. In the third sample there are two lanes with four desks in each, and Santa Claus has the fourth place. Thus, his place is in the first lane at the second desk on the right.
instruction
0
4,670
14
9,340
Tags: implementation, math Correct Solution: ``` # Santa Claus and a Place in a Class def santa(n, m, k): direction = 'R' if k & 1 == 1: direction = 'L' lane = 2 while True: if k <= (m * lane): prev = (m * (lane - 2)) + 1 desk = 1 while True: if k == prev or k == prev + 1: break desk += 1 prev += 2 break lane += 2 lane //= 2 print(lane, desk, direction) n, m, k = list(map(int, input().split())) santa(n, m, k) ```
output
1
4,670
14
9,341
Provide tags and a correct Python 3 solution for this coding contest problem. Santa Claus is the first who came to the Christmas Olympiad, and he is going to be the first to take his place at a desk! In the classroom there are n lanes of m desks each, and there are two working places at each of the desks. The lanes are numbered from 1 to n from the left to the right, the desks in a lane are numbered from 1 to m starting from the blackboard. Note that the lanes go perpendicularly to the blackboard, not along it (see picture). The organizers numbered all the working places from 1 to 2nm. The places are numbered by lanes (i. e. all the places of the first lane go first, then all the places of the second lane, and so on), in a lane the places are numbered starting from the nearest to the blackboard (i. e. from the first desk in the lane), at each desk, the place on the left is numbered before the place on the right. <image> The picture illustrates the first and the second samples. Santa Clause knows that his place has number k. Help him to determine at which lane at which desk he should sit, and whether his place is on the left or on the right! Input The only line contains three integers n, m and k (1 ≤ n, m ≤ 10 000, 1 ≤ k ≤ 2nm) — the number of lanes, the number of desks in each lane and the number of Santa Claus' place. Output Print two integers: the number of lane r, the number of desk d, and a character s, which stands for the side of the desk Santa Claus. The character s should be "L", if Santa Clause should sit on the left, and "R" if his place is on the right. Examples Input 4 3 9 Output 2 2 L Input 4 3 24 Output 4 3 R Input 2 4 4 Output 1 2 R Note The first and the second samples are shown on the picture. The green place corresponds to Santa Claus' place in the first example, the blue place corresponds to Santa Claus' place in the second example. In the third sample there are two lanes with four desks in each, and Santa Claus has the fourth place. Thus, his place is in the first lane at the second desk on the right.
instruction
0
4,671
14
9,342
Tags: implementation, math Correct Solution: ``` n,m,k=map(int,input().split()) a=k%2 x={1:"L",0:"R"} r=k//(2*m) if(k%(2*m)!=0): r+=1 d=(k-(m*(r-1)*2)) if(d%2!=0): d+=1 d=d//2 print(r,d,x[a]) ```
output
1
4,671
14
9,343
Provide tags and a correct Python 3 solution for this coding contest problem. Santa Claus is the first who came to the Christmas Olympiad, and he is going to be the first to take his place at a desk! In the classroom there are n lanes of m desks each, and there are two working places at each of the desks. The lanes are numbered from 1 to n from the left to the right, the desks in a lane are numbered from 1 to m starting from the blackboard. Note that the lanes go perpendicularly to the blackboard, not along it (see picture). The organizers numbered all the working places from 1 to 2nm. The places are numbered by lanes (i. e. all the places of the first lane go first, then all the places of the second lane, and so on), in a lane the places are numbered starting from the nearest to the blackboard (i. e. from the first desk in the lane), at each desk, the place on the left is numbered before the place on the right. <image> The picture illustrates the first and the second samples. Santa Clause knows that his place has number k. Help him to determine at which lane at which desk he should sit, and whether his place is on the left or on the right! Input The only line contains three integers n, m and k (1 ≤ n, m ≤ 10 000, 1 ≤ k ≤ 2nm) — the number of lanes, the number of desks in each lane and the number of Santa Claus' place. Output Print two integers: the number of lane r, the number of desk d, and a character s, which stands for the side of the desk Santa Claus. The character s should be "L", if Santa Clause should sit on the left, and "R" if his place is on the right. Examples Input 4 3 9 Output 2 2 L Input 4 3 24 Output 4 3 R Input 2 4 4 Output 1 2 R Note The first and the second samples are shown on the picture. The green place corresponds to Santa Claus' place in the first example, the blue place corresponds to Santa Claus' place in the second example. In the third sample there are two lanes with four desks in each, and Santa Claus has the fourth place. Thus, his place is in the first lane at the second desk on the right.
instruction
0
4,672
14
9,344
Tags: implementation, math Correct Solution: ``` n, m, k = map(int, input().split()) z = (k + 2 * m - 1) // (2 * m) q = k % (2 * m) if q == 0: q = 2 * m q = (q + 1) // 2 w = q % 2 if k % 2: w = 'L' else: w = 'R' print(z, q, w) ```
output
1
4,672
14
9,345
Provide tags and a correct Python 3 solution for this coding contest problem. Santa Claus is the first who came to the Christmas Olympiad, and he is going to be the first to take his place at a desk! In the classroom there are n lanes of m desks each, and there are two working places at each of the desks. The lanes are numbered from 1 to n from the left to the right, the desks in a lane are numbered from 1 to m starting from the blackboard. Note that the lanes go perpendicularly to the blackboard, not along it (see picture). The organizers numbered all the working places from 1 to 2nm. The places are numbered by lanes (i. e. all the places of the first lane go first, then all the places of the second lane, and so on), in a lane the places are numbered starting from the nearest to the blackboard (i. e. from the first desk in the lane), at each desk, the place on the left is numbered before the place on the right. <image> The picture illustrates the first and the second samples. Santa Clause knows that his place has number k. Help him to determine at which lane at which desk he should sit, and whether his place is on the left or on the right! Input The only line contains three integers n, m and k (1 ≤ n, m ≤ 10 000, 1 ≤ k ≤ 2nm) — the number of lanes, the number of desks in each lane and the number of Santa Claus' place. Output Print two integers: the number of lane r, the number of desk d, and a character s, which stands for the side of the desk Santa Claus. The character s should be "L", if Santa Clause should sit on the left, and "R" if his place is on the right. Examples Input 4 3 9 Output 2 2 L Input 4 3 24 Output 4 3 R Input 2 4 4 Output 1 2 R Note The first and the second samples are shown on the picture. The green place corresponds to Santa Claus' place in the first example, the blue place corresponds to Santa Claus' place in the second example. In the third sample there are two lanes with four desks in each, and Santa Claus has the fourth place. Thus, his place is in the first lane at the second desk on the right.
instruction
0
4,673
14
9,346
Tags: implementation, math Correct Solution: ``` from math import ceil def solve(n, m, k): t = ceil(k / 2) x = m if t % m == 0 else t % m y = ceil(t / m) p = 'L' if k % 2 == 1 else 'R' return f'{y} {x} {p}' def main(): n, m, k = list(map(int, input().split())) print(solve(n, m, k)) main() ```
output
1
4,673
14
9,347
Provide tags and a correct Python 3 solution for this coding contest problem. Santa Claus is the first who came to the Christmas Olympiad, and he is going to be the first to take his place at a desk! In the classroom there are n lanes of m desks each, and there are two working places at each of the desks. The lanes are numbered from 1 to n from the left to the right, the desks in a lane are numbered from 1 to m starting from the blackboard. Note that the lanes go perpendicularly to the blackboard, not along it (see picture). The organizers numbered all the working places from 1 to 2nm. The places are numbered by lanes (i. e. all the places of the first lane go first, then all the places of the second lane, and so on), in a lane the places are numbered starting from the nearest to the blackboard (i. e. from the first desk in the lane), at each desk, the place on the left is numbered before the place on the right. <image> The picture illustrates the first and the second samples. Santa Clause knows that his place has number k. Help him to determine at which lane at which desk he should sit, and whether his place is on the left or on the right! Input The only line contains three integers n, m and k (1 ≤ n, m ≤ 10 000, 1 ≤ k ≤ 2nm) — the number of lanes, the number of desks in each lane and the number of Santa Claus' place. Output Print two integers: the number of lane r, the number of desk d, and a character s, which stands for the side of the desk Santa Claus. The character s should be "L", if Santa Clause should sit on the left, and "R" if his place is on the right. Examples Input 4 3 9 Output 2 2 L Input 4 3 24 Output 4 3 R Input 2 4 4 Output 1 2 R Note The first and the second samples are shown on the picture. The green place corresponds to Santa Claus' place in the first example, the blue place corresponds to Santa Claus' place in the second example. In the third sample there are two lanes with four desks in each, and Santa Claus has the fourth place. Thus, his place is in the first lane at the second desk on the right.
instruction
0
4,674
14
9,348
Tags: implementation, math Correct Solution: ``` # A. Santa Claus and a Place in a Class n, m, k = map(int, input().split()) r = k // (2 * m) if k % (2 * m) != 0: r += 1 d = (k - (r - 1) * 2 * m) // 2 if (k - (r - 1) * 2 * m) % 2 != 0: d += 1 s = "L" if k % 2 != 0 else "R" print(r, d, s) ```
output
1
4,674
14
9,349
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. During the final part of fashion show all models come to the stage and stay in one row and fashion designer stays to right to model on the right. During the rehearsal, Izabella noticed, that row isn't nice, but she can't figure out how to fix it. Like many other creative people, Izabella has a specific sense of beauty. Evaluating beauty of row of models Izabella looks at heights of models. She thinks that row is nice if for each model distance to nearest model with less height (model or fashion designer) to the right of her doesn't exceed k (distance between adjacent people equals 1, the distance between people with exactly one man between them equals 2, etc). She wants to make row nice, but fashion designer has his own sense of beauty, so she can at most one time select two models from the row and swap their positions if the left model from this pair is higher than the right model from this pair. Fashion designer (man to the right of rightmost model) has less height than all models and can't be selected for exchange. You should tell if it's possible to make at most one exchange in such a way that row becomes nice for Izabella. Input In first line there are two integers n and k (1 ≤ n ≤ 5·105, 1 ≤ k ≤ n) — number of models and required distance. Second line contains n space-separated integers ai (1 ≤ ai ≤ 109) — height of each model. Pay attention that height of fashion designer is not given and can be less than 1. Output Print «YES» (without quotes) if it's possible to make row nice using at most one exchange, and «NO» (without quotes) otherwise. Examples Input 5 4 2 3 5 2 5 Output NO Input 5 2 3 6 2 2 1 Output YES Input 5 2 5 3 6 5 2 Output YES Submitted Solution: ``` temp, k = [int(i) for i in input().split()] models = [int(i) for i in input().split()] min = models[0] steps = 1 index = 0 index2 = 0 steps2 = 1 problems = 0 changed = False for i in range(temp): if models[i] < min: min = models[i] index = i steps = 1 else: steps += 1 if steps > k: problems += 1 min2 = min if not (temp - i - k > 0): newk= temp - i else: newk = k for j in range(newk): if models[i+j] < min2: if changed: changed = False break else: min2 = models[i+j] changed = True if not changed: break else: problems -= 1 min = models[i] steps = 1 if problems: print('NO') else: print('YES') ```
instruction
0
4,747
14
9,494
No
output
1
4,747
14
9,495
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. During the final part of fashion show all models come to the stage and stay in one row and fashion designer stays to right to model on the right. During the rehearsal, Izabella noticed, that row isn't nice, but she can't figure out how to fix it. Like many other creative people, Izabella has a specific sense of beauty. Evaluating beauty of row of models Izabella looks at heights of models. She thinks that row is nice if for each model distance to nearest model with less height (model or fashion designer) to the right of her doesn't exceed k (distance between adjacent people equals 1, the distance between people with exactly one man between them equals 2, etc). She wants to make row nice, but fashion designer has his own sense of beauty, so she can at most one time select two models from the row and swap their positions if the left model from this pair is higher than the right model from this pair. Fashion designer (man to the right of rightmost model) has less height than all models and can't be selected for exchange. You should tell if it's possible to make at most one exchange in such a way that row becomes nice for Izabella. Input In first line there are two integers n and k (1 ≤ n ≤ 5·105, 1 ≤ k ≤ n) — number of models and required distance. Second line contains n space-separated integers ai (1 ≤ ai ≤ 109) — height of each model. Pay attention that height of fashion designer is not given and can be less than 1. Output Print «YES» (without quotes) if it's possible to make row nice using at most one exchange, and «NO» (without quotes) otherwise. Examples Input 5 4 2 3 5 2 5 Output NO Input 5 2 3 6 2 2 1 Output YES Input 5 2 5 3 6 5 2 Output YES Submitted Solution: ``` n,k=list(map(int,input().split())) number=list(map(int,input().split())) count=0 flag=0 for i in range(n-1): j=i while(j<n and number[j]>=number[i]): j=j+1 if j<n and count==0 and j-i>k: temp=number[j] number[j]=number[i] number[i]=temp count=1 elif j==n and j-i>k: flag=1 print('NO') break elif j-i>k and count==1: flag=1 print('NO') break if flag==0: print('YES') ```
instruction
0
4,748
14
9,496
No
output
1
4,748
14
9,497
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. During the final part of fashion show all models come to the stage and stay in one row and fashion designer stays to right to model on the right. During the rehearsal, Izabella noticed, that row isn't nice, but she can't figure out how to fix it. Like many other creative people, Izabella has a specific sense of beauty. Evaluating beauty of row of models Izabella looks at heights of models. She thinks that row is nice if for each model distance to nearest model with less height (model or fashion designer) to the right of her doesn't exceed k (distance between adjacent people equals 1, the distance between people with exactly one man between them equals 2, etc). She wants to make row nice, but fashion designer has his own sense of beauty, so she can at most one time select two models from the row and swap their positions if the left model from this pair is higher than the right model from this pair. Fashion designer (man to the right of rightmost model) has less height than all models and can't be selected for exchange. You should tell if it's possible to make at most one exchange in such a way that row becomes nice for Izabella. Input In first line there are two integers n and k (1 ≤ n ≤ 5·105, 1 ≤ k ≤ n) — number of models and required distance. Second line contains n space-separated integers ai (1 ≤ ai ≤ 109) — height of each model. Pay attention that height of fashion designer is not given and can be less than 1. Output Print «YES» (without quotes) if it's possible to make row nice using at most one exchange, and «NO» (without quotes) otherwise. Examples Input 5 4 2 3 5 2 5 Output NO Input 5 2 3 6 2 2 1 Output YES Input 5 2 5 3 6 5 2 Output YES Submitted Solution: ``` n,k=list(map(int,input().split())) number=list(map(int,input().split())) count=0 flag=0 for i in range(n): j=i while(j<n and number[j]>=number[i]): j=j+1 if j<n-1 and count==0 and j-i>k: temp=number[j] number[j]=number[i] number[i]=temp count=1 elif j==n-1: flag=1 print('NO') break elif j-i>k and count==1: flag=1 print('NO') break if flag==0: print('YES') ```
instruction
0
4,749
14
9,498
No
output
1
4,749
14
9,499
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. During the final part of fashion show all models come to the stage and stay in one row and fashion designer stays to right to model on the right. During the rehearsal, Izabella noticed, that row isn't nice, but she can't figure out how to fix it. Like many other creative people, Izabella has a specific sense of beauty. Evaluating beauty of row of models Izabella looks at heights of models. She thinks that row is nice if for each model distance to nearest model with less height (model or fashion designer) to the right of her doesn't exceed k (distance between adjacent people equals 1, the distance between people with exactly one man between them equals 2, etc). She wants to make row nice, but fashion designer has his own sense of beauty, so she can at most one time select two models from the row and swap their positions if the left model from this pair is higher than the right model from this pair. Fashion designer (man to the right of rightmost model) has less height than all models and can't be selected for exchange. You should tell if it's possible to make at most one exchange in such a way that row becomes nice for Izabella. Input In first line there are two integers n and k (1 ≤ n ≤ 5·105, 1 ≤ k ≤ n) — number of models and required distance. Second line contains n space-separated integers ai (1 ≤ ai ≤ 109) — height of each model. Pay attention that height of fashion designer is not given and can be less than 1. Output Print «YES» (without quotes) if it's possible to make row nice using at most one exchange, and «NO» (without quotes) otherwise. Examples Input 5 4 2 3 5 2 5 Output NO Input 5 2 3 6 2 2 1 Output YES Input 5 2 5 3 6 5 2 Output YES Submitted Solution: ``` temp, k = [int(i) for i in input().split()] models = [int(i) for i in input().split()] min = models[0] steps = 0 index = 0 index2 = 0 steps2 = 0 problems = 0 changed = False for i in range(temp): if models[i] < min: min = models[i] index = i steps = 0 else: steps += 1 if steps > k: problems += 1 min2 = min if temp - i - k > 0: for j in range(k): if models[i+j] < min2: if changed: changed = False break else: min2 = models[i+j] changed = True if not changed: break else: problems -= 1 min = models[i] steps = 0 if problems: print('NO') else: print('YES') ```
instruction
0
4,750
14
9,500
No
output
1
4,750
14
9,501
Provide tags and a correct Python 3 solution for this coding contest problem. Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
instruction
0
5,211
14
10,422
Tags: constructive algorithms, divide and conquer, dp, math, trees Correct Solution: ``` from heapq import * import sys def ng(): print("NO") exit() n, k = map(int,input().split()) if (n,k)==(1,0): print("YES") print(0) exit() ans = [0] * n popcnt = lambda x: bin(x).count("1") if n & 1 == 0 or n < 2 * k + 3 or (popcnt(n + 1) > 1 and k == 0): ng() u = 0 if popcnt(n + 1 - 2 * (k - 1)) == 1: for v in range(1, 4): ans[v] = v // 2+1 k -= 1 if n - 4 < 2 * k + 3 or k==0 or n<11: ng() ans[4] = 1 u = 4 for _ in range(k - 1): ans[u + 1] = ans[u + 2] = u+1 u += 2 for v in range(1,n-u):ans[v+u]=(v-1)//2+u+1 print("YES") print(*ans) ```
output
1
5,211
14
10,423
Provide tags and a correct Python 3 solution for this coding contest problem. Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
instruction
0
5,212
14
10,424
Tags: constructive algorithms, divide and conquer, dp, math, trees Correct Solution: ``` from heapq import * import sys int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] def ng(): print("NO") exit() n, k = map(int,input().split()) if (n,k)==(1,0): print("YES") print(0) exit() ans = [0] * n popcnt = lambda x: bin(x).count("1") if n & 1 == 0 or n < 2 * k + 3 or (popcnt(n + 1) > 1 and k == 0): ng() u = 0 if popcnt(n + 1 - 2 * (k - 1)) == 1: for v in range(1, 4): ans[v] = v // 2+1 k -= 1 if n - 4 < 2 * k + 3 or k==0 or n<11: ng() ans[4] = 1 u = 4 for _ in range(k - 1): ans[u + 1] = ans[u + 2] = u+1 u += 2 for v in range(1,n-u):ans[v+u]=(v-1)//2+u+1 print("YES") print(*ans) ```
output
1
5,212
14
10,425
Provide tags and a correct Python 3 solution for this coding contest problem. Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
instruction
0
5,213
14
10,426
Tags: constructive algorithms, divide and conquer, dp, math, trees Correct Solution: ``` from heapq import * import sys int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] def ng(): print("NO") exit() n, k = MI() if (n,k)==(1,0): print("YES") print(0) exit() ans = [0] * n popcnt = lambda x: bin(x).count("1") if n & 1 == 0 or n < 2 * k + 3 or (popcnt(n + 1) > 1 and k == 0): ng() u = 0 if popcnt(n + 1 - 2 * (k - 1)) == 1: for v in range(1, 4): ans[v] = v // 2+1 k -= 1 if n - 4 < 2 * k + 3 or k==0 or n<11: ng() ans[4] = 1 u = 4 #print(n, k, u, ans) for _ in range(k - 1): ans[u + 1] = ans[u + 2] = u+1 u += 2 #print(n, k, u, ans) for v in range(1,n-u):ans[v+u]=(v-1)//2+u+1 print("YES") print(*ans) ```
output
1
5,213
14
10,427
Provide tags and a correct Python 3 solution for this coding contest problem. Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
instruction
0
5,214
14
10,428
Tags: constructive algorithms, divide and conquer, dp, math, trees Correct Solution: ``` def ng():print("NO");exit() n, k = map(int,input().split()) if (n,k)==(1,0):print("YES");print(0);exit() ans = [0] * n;u = 0;popcnt = lambda x: bin(x).count("1") if n & 1 == 0 or n < 2 * k + 3 or (popcnt(n + 1) > 1 and k == 0): ng() if popcnt(n + 1 - 2 * (k - 1)) == 1: for v in range(1, 4): ans[v] = v // 2+1 k -= 1 if n - 4 < 2 * k + 3 or k==0 or n<11: ng() ans[4] = 1;u = 4 for _ in range(k - 1):ans[u + 1] = ans[u + 2] = u+1;u += 2 for v in range(1,n-u):ans[v+u]=(v-1)//2+u+1 print("YES");print(*ans) ```
output
1
5,214
14
10,429
Provide tags and a correct Python 3 solution for this coding contest problem. Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
instruction
0
5,215
14
10,430
Tags: constructive algorithms, divide and conquer, dp, math, trees Correct Solution: ``` def ng(): print("NO") exit() n, k = map(int,input().split()) if (n,k)==(1,0): print("YES") print(0) exit() ans = [0] * n popcnt = lambda x: bin(x).count("1") if n & 1 == 0 or n < 2 * k + 3 or (popcnt(n + 1) > 1 and k == 0): ng() u = 0 if popcnt(n + 1 - 2 * (k - 1)) == 1: for v in range(1, 4): ans[v] = v // 2+1 k -= 1 if n - 4 < 2 * k + 3 or k==0 or n<11: ng() ans[4] = 1 u = 4 for _ in range(k - 1): ans[u + 1] = ans[u + 2] = u+1 u += 2 for v in range(1,n-u):ans[v+u]=(v-1)//2+u+1 print("YES") print(*ans) ```
output
1
5,215
14
10,431
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors. Submitted Solution: ``` from heapq import * import sys int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] def ng(): print("NO") exit() n, k = MI() if (n,k)==(1,0): print("YES") print(0) exit() ans = [0] * n popcnt = lambda x: bin(x).count("1") if n & 1 == 0 or n < 2 * k + 3 or (popcnt(n + 1) > 1 and k == 0): ng() u = 0 if popcnt(n + 1 - 2 * (k - 1)) == 1: for v in range(1, 4): ans[v] = v // 2+1 k -= 1 if n - 4 < 2 * k + 3: ng() ans[4] = 1 u = 4 #print(n, k, u, ans) for _ in range(k - 1): ans[u + 1] = ans[u + 2] = u+1 u += 2 #print(n, k, u, ans) for v in range(1,n-u):ans[v+u]=(v-1)//2+u+1 print("YES") print(*ans) ```
instruction
0
5,216
14
10,432
No
output
1
5,216
14
10,433
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors. Submitted Solution: ``` from heapq import * import sys int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] n,k=MI() if n&1==0 or n<2*k+3 or k<bin(n+1).count("1")-1: print("NO") exit() hp=[] two=0 bit=n+1 for i in range(n): if bit&1: if i==1:two+=1 else:heappush(hp,1<<i) bit>>=1 #print(two,hp) while two+len(hp)-1<k: s=heappop(hp) if s==4:two+=2 else: heappush(hp,s//2) heappush(hp,s//2) #print(two,hp) ans=[0]*n for u in range(0,two*2,2):ans[u+1]=ans[u+2]=u+1 #print(ans) u=2*two while hp: s=heappop(hp) if not hp: for v in range(2, s): ans[v + u-1] = v // 2 + u break for v in range(1,s):ans[v+u]=v//2+u+1 ans[u+s]=u+1 u+=s print("YES") print(*ans) ```
instruction
0
5,217
14
10,434
No
output
1
5,217
14
10,435
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors. Submitted Solution: ``` import math n, k = input().replace("\n", "").split(" ") n = int(n) k = int(k) if 2*k > n-3: print("NO") else: if((n-2*k)%4 == 1): print("NO") else: s = {} print("YES") for i in range(1, n-2*k+1): s[i] = math.floor(i/2) if k != 0: s[1] = n - 2*k + 1 s[n - 2*k + 2] = n - 2*k + 1 for i in range(1, k+1): if i == 1: s[n-2*k+i] = 0 else: s[n-2*k+i] = n- 2*k + i - 1 s[n-2*k+i+k] = n - 2*k + i for x in s: print(s[x], end = " ") ```
instruction
0
5,218
14
10,436
No
output
1
5,218
14
10,437
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from 1 to n, and s_i denotes the child of the person i (and s_i = 0 for exactly one person who does not have any children). We say that a is an ancestor of b if either a = b, or a has a child, who is an ancestor of b. That is a is an ancestor for a, s_a, s_{s_a}, etc. We say that person i is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got k people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with n people that have k imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n), the total number of people and the number of imbalanced people. Output If there are no constructions with n people and k imbalanced people, output NO. Otherwise output YES on the first line, and then n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ n), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Examples Input 3 0 Output YES 0 1 1 Input 5 1 Output YES 0 1 1 3 3 Input 3 2 Output NO Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: <image> Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors. Submitted Solution: ``` import sys sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] n,k=MI() if n&1==0 or n<2*k+3: print("NO") exit() ans=[0]*n def tree(u,size,imb): #print(u,size,imb) if size==1:return ans[u + 1] = u + 1 if imb: ans[u+2]=u+1 tree(u+2,size-2,imb-1) else: sl=size//4*2+1 ans[u+1+sl]=u+1 tree(u+1,sl,0) tree(u+1+sl,size-1-sl,0) tree(0,n,k) print("YES") print(*ans) ```
instruction
0
5,219
14
10,438
No
output
1
5,219
14
10,439
Provide tags and a correct Python 3 solution for this coding contest problem. One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
instruction
0
5,301
14
10,602
Tags: brute force, dfs and similar, implementation Correct Solution: ``` #!/usr/local/bin/python3 from __future__ import print_function import sys DEBUG = '-d' in sys.argv def debug(*args, **kwargs): if DEBUG: print(*args, file=sys.stderr, **kwargs) return None def main(): n = int(input()) cnt = [0] * (n + 1) edge = [] for i in range(0, n + 1): edge.append(set()) for i in range(0, 2 * n): s, t = map(int, input().split()) edge[s].add(t) edge[t].add(s) cnt[s] += 1 cnt[t] += 1 c4 = 0 for i in range(1, n + 1): if cnt[i] == 4: c4 += 1 if c4 != n: print(-1) else: for v2 in edge[1]: for v3 in edge[1]: if v2 in edge[v3]: mark = [True] * (n + 1) mark[1] = False mark[v2] = False res = [1, v2] i = v3 try: while True: res.append(i) mark[i] = False if len(res) == n: print(' '.join([str(x) for x in res])) sys.exit(0) for e in edge[i]: if e != i and mark[e] and res[-2] in edge[e]: i = e break if not mark[i]: raise StopIteration except StopIteration: pass print(-1) if __name__ == '__main__': main() ```
output
1
5,301
14
10,603
Provide tags and a correct Python 3 solution for this coding contest problem. One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs. For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2). Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs. Input The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order. Output If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number "-1" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction. Examples Input 5 1 2 2 3 3 4 4 5 5 1 1 3 2 4 3 5 4 1 5 2 Output 1 2 3 4 5 Input 6 5 6 4 3 5 3 2 4 6 1 3 1 6 2 2 5 1 4 3 6 1 2 4 5 Output 1 2 4 5 3 6
instruction
0
5,302
14
10,604
Tags: brute force, dfs and similar, implementation Correct Solution: ``` from sys import stdin all_in = stdin.readlines() n = int(all_in[0]) pairs = list(map(lambda x: tuple(map(int, x.split())), all_in[1:])) if n == 5: print(1, 2, 3, 4, 5) exit() neigs = {i: set() for i in range(1, n + 1)} for (a, b) in pairs: neigs[a].add(b) neigs[b].add(a) for el in neigs.values(): if len(el) != 4: print(-1) exit() ans = [1] used = {i: False for i in range(1, n + 1)} used[1] = True for i in range(n - 1): el_ = ans[-1] ne = neigs[el_] for el in ne: ne_ = neigs[el] and_ = ne & ne_ if len(and_) == 2: if i: if ans[-2] not in and_: continue if not used[el]: ans.append(el) used[el] = True break if len(ans) < n: print(-1) exit() print(' '.join(map(str, ans))) ```
output
1
5,302
14
10,605
Provide tags and a correct Python 3 solution for this coding contest problem. It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
instruction
0
5,391
14
10,782
Tags: binary search, implementation Correct Solution: ``` import sys import math a=int(input()) b=list(map(int,input().split())) c=int(input()) d=list(map(int,input().split())) e=[] f=0 for i in b: #print(f,i) e.append(f+i) f=i+f g=[0]*(e[-1]+1) h=0 for i in range(a): for j in range(h,e[i]+1): g[j]=i+1 h=e[i]+1 for k in d: print(g[k]) ```
output
1
5,391
14
10,783
Provide tags and a correct Python 3 solution for this coding contest problem. It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
instruction
0
5,392
14
10,784
Tags: binary search, implementation Correct Solution: ``` a=int(input()) b= list(map(int,input().split())) c=int(input()) d= list(map(int,input().split())) for k in range(1,a): b[k] += b[k-1] for j in d: l=0 r= a-1 while l<= r: if j <= b[0]: mid=0 break mid= (l+r)//2 if b[mid] >= j and b[mid-1] < j: break elif b[mid] > j: r=mid-1 else: l=mid+1 print(mid+1) ```
output
1
5,392
14
10,785
Provide tags and a correct Python 3 solution for this coding contest problem. It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
instruction
0
5,393
14
10,786
Tags: binary search, implementation Correct Solution: ``` def read(): input() sizes = list(map(int, input().split())) input() queries = list(map(int, input().split())) return sizes, queries def calc_indexes(sizes): start_indexes = [1] for i in range(len(sizes)): start_indexes.append(start_indexes[i] + sizes[i]) return start_indexes def bin_search(ar, query): begin = 0 end = len(ar) while (end - begin) > 1: middle = (end + begin) // 2 if query < ar[middle]: end = middle else: begin = middle return begin # for i in range(begin, end): # if ar[i] > query: # return i - 1 # return end - 1 sizes, queries = read() start_indexes = calc_indexes(sizes) for q in queries: print (bin_search(start_indexes, q) + 1) ```
output
1
5,393
14
10,787
Provide tags and a correct Python 3 solution for this coding contest problem. It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
instruction
0
5,394
14
10,788
Tags: binary search, implementation Correct Solution: ``` import bisect as bs n = int(input()) A = list(map(int, input().split())) m = int(input()) B = list(map(int, input().split())) S = [0] for a in A: S.append(S[-1] + a) for b in B: print(bs.bisect_left(S, b)) ```
output
1
5,394
14
10,789
Provide tags and a correct Python 3 solution for this coding contest problem. It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
instruction
0
5,395
14
10,790
Tags: binary search, implementation Correct Solution: ``` n=int(input()) a=list(map(int,input().split())) m=int(input()) q=list(map(int,input().split())) d=[0] for i in range(n): d.append(d[i]+a[i]) d.append(1000001) for i in range(m): start=0;end=len(d)-1 while start<end: k=(start+end)//2 if q[i]<d[k]: end=k-1 else: start=k+1 for t in range(max(0,end-4),n+1): if q[i]<=d[t]: print(t) break else: print(n) ```
output
1
5,395
14
10,791
Provide tags and a correct Python 3 solution for this coding contest problem. It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
instruction
0
5,396
14
10,792
Tags: binary search, implementation Correct Solution: ``` input() d=[] m=1 for i in map(int,input().split()): for j in range(i): d.append(m) m+=1 input() for i in map(int,input().split()): print(d[i-1]) ```
output
1
5,396
14
10,793
Provide tags and a correct Python 3 solution for this coding contest problem. It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
instruction
0
5,397
14
10,794
Tags: binary search, implementation Correct Solution: ``` n=int(input()) x=list(map(int,input().split())) t=int(input()) y=list(map(int,input().split())) ans=[] for i in range(len(x)): ans.extend([i+1 for j in range(x[i])]) for i in y: print(ans[i-1]) ```
output
1
5,397
14
10,795
Provide tags and a correct Python 3 solution for this coding contest problem. It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
instruction
0
5,398
14
10,796
Tags: binary search, implementation Correct Solution: ``` n=int(input()) s=list(map(int,input().split())) n1=int(input()) s1=list(map(int,input().split())) w=[] i=-1 d=n-1 r=0 while i!=d: i+=1 for j in range(s[i]): r+=1 w.append(r) ans=[] for i in range(n1): l,r=-1,n while (l+1<r): md=(l+r)//2 if s1[i]<=w[md]: r=md else: l=md ans.append(r+1) print(*ans,sep='\n') ```
output
1
5,398
14
10,797
Provide tags and a correct Python 3 solution for this coding contest problem. Mike is the president of country What-The-Fatherland. There are n bears living in this country besides Mike. All of them are standing in a line and they are numbered from 1 to n from left to right. i-th bear is exactly ai feet high. <image> A group of bears is a non-empty contiguous segment of the line. The size of a group is the number of bears in that group. The strength of a group is the minimum height of the bear in that group. Mike is a curious to know for each x such that 1 ≤ x ≤ n the maximum strength among all groups of size x. Input The first line of input contains integer n (1 ≤ n ≤ 2 × 105), the number of bears. The second line contains n integers separated by space, a1, a2, ..., an (1 ≤ ai ≤ 109), heights of bears. Output Print n integers in one line. For each x from 1 to n, print the maximum strength among all groups of size x. Examples Input 10 1 2 3 4 5 4 3 2 1 6 Output 6 4 4 3 3 2 2 1 1 1
instruction
0
5,428
14
10,856
Tags: binary search, data structures, dp, dsu Correct Solution: ``` from sys import stdin, stdout def input(): return stdin.readline().strip() def print(x, end='\n'): stdout.write(str(x) + end) n, lst = int(input()), list(map(int, input().split())) nse, pse, stk, ans = [n for i in range(n)], [-1 for i in range(n)], [], [0 for i in range(n)] for i in range(n): while stk and lst[stk[-1]] > lst[i]: nse[stk.pop()] = i stk.append(i) stk.clear() for i in range(n-1, -1, -1): while stk and lst[stk[-1]] > lst[i]: pse[stk.pop()] = i stk.append(i) for i in range(n): ans[nse[i] - pse[i] - 2] = max(lst[i], ans[nse[i] - pse[i] - 2]) for i in range(n-2, -1, -1): ans[i] = max(ans[i+1], ans[i]) print(' '.join(map(str, ans))) ```
output
1
5,428
14
10,857
Provide tags and a correct Python 3 solution for this coding contest problem. Mike is the president of country What-The-Fatherland. There are n bears living in this country besides Mike. All of them are standing in a line and they are numbered from 1 to n from left to right. i-th bear is exactly ai feet high. <image> A group of bears is a non-empty contiguous segment of the line. The size of a group is the number of bears in that group. The strength of a group is the minimum height of the bear in that group. Mike is a curious to know for each x such that 1 ≤ x ≤ n the maximum strength among all groups of size x. Input The first line of input contains integer n (1 ≤ n ≤ 2 × 105), the number of bears. The second line contains n integers separated by space, a1, a2, ..., an (1 ≤ ai ≤ 109), heights of bears. Output Print n integers in one line. For each x from 1 to n, print the maximum strength among all groups of size x. Examples Input 10 1 2 3 4 5 4 3 2 1 6 Output 6 4 4 3 3 2 2 1 1 1
instruction
0
5,429
14
10,858
Tags: binary search, data structures, dp, dsu Correct Solution: ``` from sys import stdin, stdout def input(): return stdin.readline().strip() def print(x, end='\n'): stdout.write(str(x) + end) n, lst = int(input()), list(map(int, input().split())) nse, pse, stk, ans = [n for i in range(n)], [-1 for i in range(n)], [], [0 for i in range(n+1)] for i in range(n): while stk and lst[stk[-1]] > lst[i]: nse[stk.pop()] = i stk.append(i) stk.clear() for i in range(n-1, -1, -1): while stk and lst[stk[-1]] > lst[i]: pse[stk.pop()] = i stk.append(i) for i in range(n): ans[nse[i] - pse[i] - 1] = max(lst[i], ans[nse[i] - pse[i] - 1]) mnow = ans[n] for i in range(n, -1, -1): mnow = max(mnow, ans[i]) ans[i] = mnow print(' '.join(map(str, ans[1:]))) ```
output
1
5,429
14
10,859
Provide tags and a correct Python 3 solution for this coding contest problem. Mike is the president of country What-The-Fatherland. There are n bears living in this country besides Mike. All of them are standing in a line and they are numbered from 1 to n from left to right. i-th bear is exactly ai feet high. <image> A group of bears is a non-empty contiguous segment of the line. The size of a group is the number of bears in that group. The strength of a group is the minimum height of the bear in that group. Mike is a curious to know for each x such that 1 ≤ x ≤ n the maximum strength among all groups of size x. Input The first line of input contains integer n (1 ≤ n ≤ 2 × 105), the number of bears. The second line contains n integers separated by space, a1, a2, ..., an (1 ≤ ai ≤ 109), heights of bears. Output Print n integers in one line. For each x from 1 to n, print the maximum strength among all groups of size x. Examples Input 10 1 2 3 4 5 4 3 2 1 6 Output 6 4 4 3 3 2 2 1 1 1
instruction
0
5,431
14
10,862
Tags: binary search, data structures, dp, dsu Correct Solution: ``` # import math,string,itertools,fractions,heapq,collections,re,array,bisect # from itertools import chain, dropwhile, permutations, combinations # from collections import defaultdict def VI(): return list(map(int,input().split())) def main_old(n,a): s = list(a) print(max(a),end=" ") for i in range(1,n): ns = list(s[:-1]) mx = 0 for j in range(len(ns)): ns[j] = min(s[j],s[j+1]) if ns[j] > mx: mx = ns[j] s = ns print(mx,end=" ") #print(max(s),end=" ") #print(min(a)) from sys import stdout def main(n,a): # correct, but too slow O(n^2); needs a better data structure. s = a buf = str(max(a)) for i in range(n-1,0,-1): mx = 0 for j in range(i): s[j] = min(s[j],s[j+1]) if s[j] > mx: mx = s[j] buf += " "+str(mx) stdout.write(buf+"\n") def run(n,a): stack = [] l, r = [0]*n, [0]*n for x in range(n): while len(stack)>0 and a[stack[-1]] >= a[x]: stack.pop() if len(stack)==0: l[x] = -1 else: l[x] = stack[-1] stack.append(x) stack = [] for x in range(n-1,-1,-1): while len(stack)>0 and a[stack[-1]]>=a[x]: stack.pop() if len(stack)==0: r[x]=n else: r[x] = stack[-1] stack.append(x) sl = [-1] * n for x in range(n): v = r[x]-l[x]-1 sl[v-1] = max(sl[v-1],a[x]) for x in range(n-2,-1,-1): sl[x] = max(sl[x], sl[x+1]) print(" ".join([str(x) for x in sl])) def main_input(info=0): n = int(input()) a = VI() #main(n,a) run(n,a) if __name__ == "__main__": main_input() ```
output
1
5,431
14
10,863
Provide tags and a correct Python 3 solution for this coding contest problem. Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≤ n ≤ 105) — the number of columns. The next n lines contain the pairs of integers li and ri (1 ≤ li, ri ≤ 500) — the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k — the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg — 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg — 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
instruction
0
5,498
14
10,996
Tags: math Correct Solution: ``` l = [] r = [] sum_r = 0 sum_l = 0 max_diff_r = 0 max_r = 0 max_diff_l = 0 max_l = 0 for i in range(0, int(input())): x, y = input().split(' ') l.append(int(x)) sum_l+=l[i] r.append(int(y)) sum_r+=r[i] if max_diff_r < r[i] - l[i]: max_r=i+1 max_diff_r=r[i] - l[i] if max_diff_l < l[i] - r[i]: max_l=i+1 max_diff_l=l[i] - r[i] d = max(abs(sum_r-sum_l), sum_l-sum_r+2*max_diff_r, sum_r-sum_l+2*max_diff_l) if d == abs(sum_r-sum_l): print(0) elif d ==sum_r-sum_l+2*max_diff_l: print(max_l) else:print(max_r) ```
output
1
5,498
14
10,997
Provide tags and a correct Python 3 solution for this coding contest problem. Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≤ n ≤ 105) — the number of columns. The next n lines contain the pairs of integers li and ri (1 ≤ li, ri ≤ 500) — the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k — the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg — 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg — 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
instruction
0
5,499
14
10,998
Tags: math Correct Solution: ``` n = int(input()) ls = [] rs = [] for i in range(n): l, r = [int(x) for x in input().split()] ls.append(l) rs.append(r) lss = sum(ls) rss = sum(rs) best = abs(lss - rss) ans = None for i in range(n): lss1 = lss - ls[i] + rs[i] rss1 = rss - rs[i] + ls[i] if abs(lss1 - rss1) > best: best = abs(lss1 - rss1) ans = i print(0 if ans is None else ans + 1) ```
output
1
5,499
14
10,999
Provide tags and a correct Python 3 solution for this coding contest problem. Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≤ n ≤ 105) — the number of columns. The next n lines contain the pairs of integers li and ri (1 ≤ li, ri ≤ 500) — the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k — the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg — 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg — 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
instruction
0
5,500
14
11,000
Tags: math Correct Solution: ``` import math,sys,bisect,heapq from collections import defaultdict,Counter,deque from itertools import groupby,accumulate #sys.setrecursionlimit(200000000) input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__ ilele = lambda: map(int,input().split()) alele = lambda: list(map(int, input().split())) def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] #MOD = 1000000000 + 7 def Y(c): print(["NO","YES"][c]) def y(c): print(["no","yes"][c]) def Yy(c): print(["No","Yes"][c]) A = [];B = [] for _ in range(int(input())): a,b = ilele() A.append(a) B.append(b) l = sum(A);r = sum(B) Ans = 0 maxi = abs(l-r) for i in range(len(A)): x = A[i];y = B[i] x1 = l -x;y1 = r -y x2 = x1 + y;y2 = y1 + x z = abs(x2-y2) if z > maxi: maxi = z Ans = i+1 print(Ans) ```
output
1
5,500
14
11,001
Provide tags and a correct Python 3 solution for this coding contest problem. Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≤ n ≤ 105) — the number of columns. The next n lines contain the pairs of integers li and ri (1 ≤ li, ri ≤ 500) — the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k — the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg — 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg — 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
instruction
0
5,501
14
11,002
Tags: math Correct Solution: ``` n = int(input()) t = [] for i in range(n): l, r = map(int, input().split()) t.append((l, r)) L = 0 R = 0 for i in t: L += i[0] R += i[1] m = abs(L - R) mi = 0 for i in range(n): l, r = t[i][0], t[i][1] if l == r: continue d = abs((L - l + r) - (R - r + l)) if d > m: m = d mi = i if m == abs(L - R): print(0) else: print(mi + 1) ```
output
1
5,501
14
11,003
Provide tags and a correct Python 3 solution for this coding contest problem. Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≤ n ≤ 105) — the number of columns. The next n lines contain the pairs of integers li and ri (1 ≤ li, ri ≤ 500) — the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k — the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg — 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg — 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
instruction
0
5,502
14
11,004
Tags: math Correct Solution: ``` n = int(input()) l, r = [0]*n, [0]*n for i in range(n): l[i], r[i] = map(int, input().split(" ")) L, R = sum(l), sum(r) index, beauty = -1, abs(L-R) for i in range(n): t = abs((L-l[i]+r[i]) - (R-r[i]+l[i])) if t > beauty: index, beauty = i, t print(index+1) ```
output
1
5,502
14
11,005
Provide tags and a correct Python 3 solution for this coding contest problem. Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≤ n ≤ 105) — the number of columns. The next n lines contain the pairs of integers li and ri (1 ≤ li, ri ≤ 500) — the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k — the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg — 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg — 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
instruction
0
5,503
14
11,006
Tags: math Correct Solution: ``` n = int(input()) left = [] right = [] R = 0 L = 0 for i in range(n): a, b = map(int, input().split()) left.append(a) L += a R += b right.append(b) base = abs(L - R) m = base cur = -1 for i in range(n): L1 = L - left[i] + right[i] R1 = R - right[i] + left[i] curm = abs(L1 - R1) if curm > m: m = curm cur = i print(cur + 1) ```
output
1
5,503
14
11,007
Provide tags and a correct Python 3 solution for this coding contest problem. Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≤ n ≤ 105) — the number of columns. The next n lines contain the pairs of integers li and ri (1 ≤ li, ri ≤ 500) — the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k — the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg — 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg — 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
instruction
0
5,504
14
11,008
Tags: math Correct Solution: ``` n = int(input()) sum = 0 a = [] b = [] for i in range(0, n): c = input() c = c.split() a.append(int(c[0])) b.append(int(c[1])) sum += a[i] - b[i] v = abs(sum) ans = -1 for i in range(0, n): vv = sum - 2*(a[i] - b[i]) vv = abs(vv) if(vv > v): v = vv ans = i print(ans + 1) ```
output
1
5,504
14
11,009
Provide tags and a correct Python 3 solution for this coding contest problem. Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≤ n ≤ 105) — the number of columns. The next n lines contain the pairs of integers li and ri (1 ≤ li, ri ≤ 500) — the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k — the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg — 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg — 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
instruction
0
5,505
14
11,010
Tags: math Correct Solution: ``` n = int(input()) l,r = [],[] for i in range(n): x = list(map(int,input().split())) l.append(x[0]) r.append(x[1]) L,R = sum(l),sum(r) ans = abs(L-R) ind = 0 for i in range(n): x = L-l[i]+r[i] y = R-r[i]+l[i] if ans<abs(x-y): ans = max(ans,abs(x-y)) ind = i+1 print(ind) ```
output
1
5,505
14
11,011
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≤ n ≤ 105) — the number of columns. The next n lines contain the pairs of integers li and ri (1 ≤ li, ri ≤ 500) — the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k — the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg — 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg — 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. Submitted Solution: ``` l = [0] for _ in range(int(input())): a, b = map(int, input().split()) l.append(a - b) s, a, b = sum(l), min(l), max(l) print(l.index(a) if a + b < s else l.index(b)) ```
instruction
0
5,506
14
11,012
Yes
output
1
5,506
14
11,013