message stringlengths 2 20.1k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 1.95k 109k | cluster float64 17 17 | __index_level_0__ int64 3.91k 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
The All-Berland National Olympiad in Informatics has just ended! Now Vladimir wants to upload the contest from the Olympiad as a gym to a popular Codehorses website.
Unfortunately, the archive with Olympiad's data is a mess. For example, the files with tests are named arbitrary without any logic.
Vladimir wants to rename the files with tests so that their names are distinct integers starting from 1 without any gaps, namely, "1", "2", ..., "n', where n is the total number of tests.
Some of the files contain tests from statements (examples), while others contain regular tests. It is possible that there are no examples, and it is possible that all tests are examples. Vladimir wants to rename the files so that the examples are the first several tests, all all the next files contain regular tests only.
The only operation Vladimir can perform is the "move" command. Vladimir wants to write a script file, each of the lines in which is "move file_1 file_2", that means that the file "file_1" is to be renamed to "file_2". If there is a file "file_2" at the moment of this line being run, then this file is to be rewritten. After the line "move file_1 file_2" the file "file_1" doesn't exist, but there is a file "file_2" with content equal to the content of "file_1" before the "move" command.
Help Vladimir to write the script file with the minimum possible number of lines so that after this script is run:
* all examples are the first several tests having filenames "1", "2", ..., "e", where e is the total number of examples;
* all other files contain regular tests with filenames "e + 1", "e + 2", ..., "n", where n is the total number of all tests.
Input
The first line contains single integer n (1 ≤ n ≤ 105) — the number of files with tests.
n lines follow, each describing a file with test. Each line has a form of "name_i type_i", where "name_i" is the filename, and "type_i" equals "1", if the i-th file contains an example test, and "0" if it contains a regular test. Filenames of each file are strings of digits and small English letters with length from 1 to 6 characters. The filenames are guaranteed to be distinct.
Output
In the first line print the minimum number of lines in Vladimir's script file.
After that print the script file, each line should be "move file_1 file_2", where "file_1" is an existing at the moment of this line being run filename, and "file_2" — is a string of digits and small English letters with length from 1 to 6.
Examples
Input
5
01 0
2 1
2extra 0
3 1
99 0
Output
4
move 3 1
move 01 5
move 2extra 4
move 99 3
Input
2
1 0
2 1
Output
3
move 1 3
move 2 1
move 3 2
Input
5
1 0
11 1
111 0
1111 1
11111 0
Output
5
move 1 5
move 11 1
move 1111 2
move 111 4
move 11111 3 | instruction | 0 | 68,633 | 17 | 137,266 |
Tags: greedy, implementation
Correct Solution:
```
import random
def genTemp():
sl = ""
firstTime = True
while firstTime or sl in pre or sl in post:
sl = ""
firstTime = False
for i in range(6):
sl += chr(random.randint(ord("a"), ord("z")))
return sl
n = int(input())
e = 0
pre = set()
post = set()
for i in range(n):
name, tp = input().split()
if tp == "1":
e += 1
pre.add(name)
else:
post.add(name)
temp = genTemp()
preAns = {str(x) for x in range(1, e + 1)}
postAns = {str(x) for x in range(e + 1, n + 1)}
preMissing = preAns - pre
postMissing = postAns - post
preToChange = pre - preAns
postToChange = post - postAns
preFree = preMissing - postToChange
postFree = postMissing - preToChange
preWrong = preToChange & postMissing
postWrong = postToChange & preMissing
ans = []
while preToChange or postToChange:
if not postFree and not preFree:
if preToChange:
x = preToChange.pop()
preWrong.discard(x)
ans.append(("move", x, temp))
preToChange.add(temp)
#postMissing.discard(x)
if x in postAns:
postFree.add(x)
else:
x = postToChange.pop()
ans.append(("move", x, temp))
postWrong.discard(x)
postToChange.add(temp)
#preMissing.discard(x)
if x in postAns:
preFree.add(x)
elif preFree:
if preWrong:
x = preWrong.pop()
preToChange.discard(x)
else:
x = preToChange.pop()
y = preFree.pop()
ans.append(("move", x, y))
preMissing.discard(y)
if x in postAns:
postFree.add(x)
else:
if postWrong:
x = postWrong.pop()
postToChange.discard(x)
else:
x = postToChange.pop()
y = postFree.pop()
ans.append(("move", x, y))
postMissing.discard(y)
if x in preAns:
preFree.add(x)
print(len(ans))
for tup in ans:
print(*tup)
``` | output | 1 | 68,633 | 17 | 137,267 |
Provide a correct Python 3 solution for this coding contest problem.
Hierarchical Democracy
The presidential election in Republic of Democratia is carried out through multiple stages as follows.
1. There are exactly two presidential candidates.
2. At the first stage, eligible voters go to the polls of his/her electoral district. The winner of the district is the candidate who takes a majority of the votes. Voters cast their ballots only at this first stage.
3. A district of the k-th stage (k > 1) consists of multiple districts of the (k − 1)-th stage. In contrast, a district of the (k − 1)-th stage is a sub-district of one and only one district of the k-th stage. The winner of a district of the k-th stage is the candidate who wins in a majority of its sub-districts of the (k − 1)-th stage.
4. The final stage has just one nation-wide district. The winner of the final stage is chosen as the president.
You can assume the following about the presidential election of this country.
* Every eligible voter casts a vote.
* The number of the eligible voters of each electoral district of the first stage is odd.
* The number of the sub-districts of the (k − 1)-th stage that constitute a district of the k-th stage (k > 1) is also odd.
This means that each district of every stage has its winner (there is no tie).
Your mission is to write a program that finds a way to win the presidential election with the minimum number of votes. Suppose, for instance, that the district of the final stage has three sub-districts of the first stage and that the numbers of the eligible voters of the sub-districts are 123, 4567, and 89, respectively. The minimum number of votes required to be the winner is 107, that is, 62 from the first district and 45 from the third. In this case, even if the other candidate were given all the 4567 votes in the second district, s/he would inevitably be the loser. Although you might consider this election system unfair, you should accept it as a reality.
Input
The entire input looks like:
> the number of datasets (=n)
> 1st dataset
> 2nd dataset
> …
> n-th dataset
>
The number of datasets, n, is no more than 100.
The number of the eligible voters of each district and the part-whole relations among districts are denoted as follows.
* An electoral district of the first stage is denoted as [c], where c is the number of the eligible voters of the district.
* A district of the k-th stage (k > 1) is denoted as [d1d2…dm], where d1, d2, …, dm denote its sub-districts of the (k − 1)-th stage in this notation.
For instance, an electoral district of the first stage that has 123 eligible voters is denoted as [123]. A district of the second stage consisting of three sub-districts of the first stage that have 123, 4567, and 89 eligible voters, respectively, is denoted as [[123][4567][89]].
Each dataset is a line that contains the character string denoting the district of the final stage in the aforementioned notation. You can assume the following.
* The character string in each dataset does not include any characters except digits ('0', '1', …, '9') and square brackets ('[', ']'), and its length is between 11 and 10000, inclusive.
* The number of the eligible voters of each electoral district of the first stage is between 3 and 9999, inclusive.
The number of stages is a nation-wide constant. So, for instance, [[[9][9][9]][9][9]] never appears in the input. [[[[9]]]] may not appear either since each district of the second or later stage must have multiple sub-districts of the previous stage.
Output
For each dataset, print the minimum number of votes required to be the winner of the presidential election in a line. No output line may include any characters except the digits with which the number is written.
Sample Input
6
[[123][4567][89]]
[[5][3][7][3][9]]
[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]
[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]
[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]]
[[8231][3721][203][3271][8843]]
Output for the Sample Input
107
7
175
95
21
3599
Example
Input
6
[[123][4567][89]]
[[5][3][7][3][9]]
[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]
[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]
[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]]
[[8231][3721][203][3271][8843]]
Output
107
7
175
95
21
3599 | instruction | 0 | 68,869 | 17 | 137,738 |
"Correct Solution:
```
import re
n = int(input())
for _ in range(n):
dataset = input()
stage = 1
while (re.match('\[[0-9]+\]', dataset) is None):
iterator = re.finditer('\[([0-9]+\]\[)+[0-9]+\]', dataset)
for match in iterator:
areas = list(map(int, match.group()[1:-1].split('][')))
minimum_areas = sorted(areas)[:len(areas)//2 + 1]
if stage == 1:
minimum_voters = [m // 2 + 1 for m in minimum_areas]
minimum_vote = sum(minimum_voters)
else:
minimum_vote = sum(minimum_areas)
dataset = dataset.replace(match.group(), '*' + str(minimum_vote) + '*', 1)
dataset = dataset.replace('*', '')
# print(dataset)
stage += 1
print(dataset[1:-1])
``` | output | 1 | 68,869 | 17 | 137,739 |
Provide a correct Python 3 solution for this coding contest problem.
Hierarchical Democracy
The presidential election in Republic of Democratia is carried out through multiple stages as follows.
1. There are exactly two presidential candidates.
2. At the first stage, eligible voters go to the polls of his/her electoral district. The winner of the district is the candidate who takes a majority of the votes. Voters cast their ballots only at this first stage.
3. A district of the k-th stage (k > 1) consists of multiple districts of the (k − 1)-th stage. In contrast, a district of the (k − 1)-th stage is a sub-district of one and only one district of the k-th stage. The winner of a district of the k-th stage is the candidate who wins in a majority of its sub-districts of the (k − 1)-th stage.
4. The final stage has just one nation-wide district. The winner of the final stage is chosen as the president.
You can assume the following about the presidential election of this country.
* Every eligible voter casts a vote.
* The number of the eligible voters of each electoral district of the first stage is odd.
* The number of the sub-districts of the (k − 1)-th stage that constitute a district of the k-th stage (k > 1) is also odd.
This means that each district of every stage has its winner (there is no tie).
Your mission is to write a program that finds a way to win the presidential election with the minimum number of votes. Suppose, for instance, that the district of the final stage has three sub-districts of the first stage and that the numbers of the eligible voters of the sub-districts are 123, 4567, and 89, respectively. The minimum number of votes required to be the winner is 107, that is, 62 from the first district and 45 from the third. In this case, even if the other candidate were given all the 4567 votes in the second district, s/he would inevitably be the loser. Although you might consider this election system unfair, you should accept it as a reality.
Input
The entire input looks like:
> the number of datasets (=n)
> 1st dataset
> 2nd dataset
> …
> n-th dataset
>
The number of datasets, n, is no more than 100.
The number of the eligible voters of each district and the part-whole relations among districts are denoted as follows.
* An electoral district of the first stage is denoted as [c], where c is the number of the eligible voters of the district.
* A district of the k-th stage (k > 1) is denoted as [d1d2…dm], where d1, d2, …, dm denote its sub-districts of the (k − 1)-th stage in this notation.
For instance, an electoral district of the first stage that has 123 eligible voters is denoted as [123]. A district of the second stage consisting of three sub-districts of the first stage that have 123, 4567, and 89 eligible voters, respectively, is denoted as [[123][4567][89]].
Each dataset is a line that contains the character string denoting the district of the final stage in the aforementioned notation. You can assume the following.
* The character string in each dataset does not include any characters except digits ('0', '1', …, '9') and square brackets ('[', ']'), and its length is between 11 and 10000, inclusive.
* The number of the eligible voters of each electoral district of the first stage is between 3 and 9999, inclusive.
The number of stages is a nation-wide constant. So, for instance, [[[9][9][9]][9][9]] never appears in the input. [[[[9]]]] may not appear either since each district of the second or later stage must have multiple sub-districts of the previous stage.
Output
For each dataset, print the minimum number of votes required to be the winner of the presidential election in a line. No output line may include any characters except the digits with which the number is written.
Sample Input
6
[[123][4567][89]]
[[5][3][7][3][9]]
[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]
[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]
[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]]
[[8231][3721][203][3271][8843]]
Output for the Sample Input
107
7
175
95
21
3599
Example
Input
6
[[123][4567][89]]
[[5][3][7][3][9]]
[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]
[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]
[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]]
[[8231][3721][203][3271][8843]]
Output
107
7
175
95
21
3599 | instruction | 0 | 68,870 | 17 | 137,740 |
"Correct Solution:
```
def cost(s):
lens = len(s)
if lens == 1:
return int(s[0]) // 2 + 1
costs = [cost(x) for x in s]
costs.sort()
return sum(costs[:lens // 2 + 1])
n = int(input())
for _ in range(n):
s = input()
s = s.replace("][", "],[")
s = eval(s)
print(cost(s))
``` | output | 1 | 68,870 | 17 | 137,741 |
Provide a correct Python 3 solution for this coding contest problem.
Hierarchical Democracy
The presidential election in Republic of Democratia is carried out through multiple stages as follows.
1. There are exactly two presidential candidates.
2. At the first stage, eligible voters go to the polls of his/her electoral district. The winner of the district is the candidate who takes a majority of the votes. Voters cast their ballots only at this first stage.
3. A district of the k-th stage (k > 1) consists of multiple districts of the (k − 1)-th stage. In contrast, a district of the (k − 1)-th stage is a sub-district of one and only one district of the k-th stage. The winner of a district of the k-th stage is the candidate who wins in a majority of its sub-districts of the (k − 1)-th stage.
4. The final stage has just one nation-wide district. The winner of the final stage is chosen as the president.
You can assume the following about the presidential election of this country.
* Every eligible voter casts a vote.
* The number of the eligible voters of each electoral district of the first stage is odd.
* The number of the sub-districts of the (k − 1)-th stage that constitute a district of the k-th stage (k > 1) is also odd.
This means that each district of every stage has its winner (there is no tie).
Your mission is to write a program that finds a way to win the presidential election with the minimum number of votes. Suppose, for instance, that the district of the final stage has three sub-districts of the first stage and that the numbers of the eligible voters of the sub-districts are 123, 4567, and 89, respectively. The minimum number of votes required to be the winner is 107, that is, 62 from the first district and 45 from the third. In this case, even if the other candidate were given all the 4567 votes in the second district, s/he would inevitably be the loser. Although you might consider this election system unfair, you should accept it as a reality.
Input
The entire input looks like:
> the number of datasets (=n)
> 1st dataset
> 2nd dataset
> …
> n-th dataset
>
The number of datasets, n, is no more than 100.
The number of the eligible voters of each district and the part-whole relations among districts are denoted as follows.
* An electoral district of the first stage is denoted as [c], where c is the number of the eligible voters of the district.
* A district of the k-th stage (k > 1) is denoted as [d1d2…dm], where d1, d2, …, dm denote its sub-districts of the (k − 1)-th stage in this notation.
For instance, an electoral district of the first stage that has 123 eligible voters is denoted as [123]. A district of the second stage consisting of three sub-districts of the first stage that have 123, 4567, and 89 eligible voters, respectively, is denoted as [[123][4567][89]].
Each dataset is a line that contains the character string denoting the district of the final stage in the aforementioned notation. You can assume the following.
* The character string in each dataset does not include any characters except digits ('0', '1', …, '9') and square brackets ('[', ']'), and its length is between 11 and 10000, inclusive.
* The number of the eligible voters of each electoral district of the first stage is between 3 and 9999, inclusive.
The number of stages is a nation-wide constant. So, for instance, [[[9][9][9]][9][9]] never appears in the input. [[[[9]]]] may not appear either since each district of the second or later stage must have multiple sub-districts of the previous stage.
Output
For each dataset, print the minimum number of votes required to be the winner of the presidential election in a line. No output line may include any characters except the digits with which the number is written.
Sample Input
6
[[123][4567][89]]
[[5][3][7][3][9]]
[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]
[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]
[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]]
[[8231][3721][203][3271][8843]]
Output for the Sample Input
107
7
175
95
21
3599
Example
Input
6
[[123][4567][89]]
[[5][3][7][3][9]]
[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]
[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]
[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]]
[[8231][3721][203][3271][8843]]
Output
107
7
175
95
21
3599 | instruction | 0 | 68,871 | 17 | 137,742 |
"Correct Solution:
```
# -*- coding: utf-8 -*-
from sys import setrecursionlimit
setrecursionlimit(100000)
def cal(L):
if type(L[0][0]) == list:
L = [[cal(l)*2] for l in L]
return sum(sorted([-(-l[0]//2) for l in L])[:-(-len(L)//2)])
for _ in range(int(input())):
print(cal(eval(input().replace("][", "],["))))
``` | output | 1 | 68,871 | 17 | 137,743 |
Provide a correct Python 3 solution for this coding contest problem.
Hierarchical Democracy
The presidential election in Republic of Democratia is carried out through multiple stages as follows.
1. There are exactly two presidential candidates.
2. At the first stage, eligible voters go to the polls of his/her electoral district. The winner of the district is the candidate who takes a majority of the votes. Voters cast their ballots only at this first stage.
3. A district of the k-th stage (k > 1) consists of multiple districts of the (k − 1)-th stage. In contrast, a district of the (k − 1)-th stage is a sub-district of one and only one district of the k-th stage. The winner of a district of the k-th stage is the candidate who wins in a majority of its sub-districts of the (k − 1)-th stage.
4. The final stage has just one nation-wide district. The winner of the final stage is chosen as the president.
You can assume the following about the presidential election of this country.
* Every eligible voter casts a vote.
* The number of the eligible voters of each electoral district of the first stage is odd.
* The number of the sub-districts of the (k − 1)-th stage that constitute a district of the k-th stage (k > 1) is also odd.
This means that each district of every stage has its winner (there is no tie).
Your mission is to write a program that finds a way to win the presidential election with the minimum number of votes. Suppose, for instance, that the district of the final stage has three sub-districts of the first stage and that the numbers of the eligible voters of the sub-districts are 123, 4567, and 89, respectively. The minimum number of votes required to be the winner is 107, that is, 62 from the first district and 45 from the third. In this case, even if the other candidate were given all the 4567 votes in the second district, s/he would inevitably be the loser. Although you might consider this election system unfair, you should accept it as a reality.
Input
The entire input looks like:
> the number of datasets (=n)
> 1st dataset
> 2nd dataset
> …
> n-th dataset
>
The number of datasets, n, is no more than 100.
The number of the eligible voters of each district and the part-whole relations among districts are denoted as follows.
* An electoral district of the first stage is denoted as [c], where c is the number of the eligible voters of the district.
* A district of the k-th stage (k > 1) is denoted as [d1d2…dm], where d1, d2, …, dm denote its sub-districts of the (k − 1)-th stage in this notation.
For instance, an electoral district of the first stage that has 123 eligible voters is denoted as [123]. A district of the second stage consisting of three sub-districts of the first stage that have 123, 4567, and 89 eligible voters, respectively, is denoted as [[123][4567][89]].
Each dataset is a line that contains the character string denoting the district of the final stage in the aforementioned notation. You can assume the following.
* The character string in each dataset does not include any characters except digits ('0', '1', …, '9') and square brackets ('[', ']'), and its length is between 11 and 10000, inclusive.
* The number of the eligible voters of each electoral district of the first stage is between 3 and 9999, inclusive.
The number of stages is a nation-wide constant. So, for instance, [[[9][9][9]][9][9]] never appears in the input. [[[[9]]]] may not appear either since each district of the second or later stage must have multiple sub-districts of the previous stage.
Output
For each dataset, print the minimum number of votes required to be the winner of the presidential election in a line. No output line may include any characters except the digits with which the number is written.
Sample Input
6
[[123][4567][89]]
[[5][3][7][3][9]]
[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]
[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]
[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]]
[[8231][3721][203][3271][8843]]
Output for the Sample Input
107
7
175
95
21
3599
Example
Input
6
[[123][4567][89]]
[[5][3][7][3][9]]
[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]
[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]
[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]]
[[8231][3721][203][3271][8843]]
Output
107
7
175
95
21
3599 | instruction | 0 | 68,872 | 17 | 137,744 |
"Correct Solution:
```
N=int(input())
for i in range(N):
s__=input()
s_=""
s=[]
#step1
for i in range(len(s__)):
if s__[i].isdigit():
s_+=s__[i]
else:
s_+=" "+s__[i]+" "
s_=s_.split(" ")
for i in range(len(s_)):
if s_[i]!='':
s.append(s_[i])
#step2
M=[[] for i in range(10)]
M_=[0]*10
D=[]
count=0
for i in range(len(s)):
if s[i]=='[':
count +=1
elif s[i]==']':
M[count].append(M_[count])
M_[count]=0
M_[count-1]+=1
count-=1
else:
D.append(int(s[i]))
for i in range(1,len(M)):
if len(M[i])==0:
M=M[1:i]
break
for i in range(len(M[-1])):
M[-1][i]=1
for i in range(len(M)):
M[i]=[0]+M[i]
for i in range(len(M)):
for j in range(1,len(M[i])):
M[i][j]=M[i][j]+M[i][j-1]
#step3
ans=[]
for i in range(len(M)):
ans.append([0]*(len(M[i])-1))
for j in range(len(ans[-1])):
ans[-1][j]=(D[j]//2)+1
for i in range(len(M)-2,-1,-1):
for j in range(len(ans[i])):
partial_sorted=sorted(ans[i+1][M[i][j]:M[i][j+1]])
ans[i][j]=sum(partial_sorted[:((len(partial_sorted)//2)+1)])
print(ans[0][0])
``` | output | 1 | 68,872 | 17 | 137,745 |
Provide a correct Python 3 solution for this coding contest problem.
Hierarchical Democracy
The presidential election in Republic of Democratia is carried out through multiple stages as follows.
1. There are exactly two presidential candidates.
2. At the first stage, eligible voters go to the polls of his/her electoral district. The winner of the district is the candidate who takes a majority of the votes. Voters cast their ballots only at this first stage.
3. A district of the k-th stage (k > 1) consists of multiple districts of the (k − 1)-th stage. In contrast, a district of the (k − 1)-th stage is a sub-district of one and only one district of the k-th stage. The winner of a district of the k-th stage is the candidate who wins in a majority of its sub-districts of the (k − 1)-th stage.
4. The final stage has just one nation-wide district. The winner of the final stage is chosen as the president.
You can assume the following about the presidential election of this country.
* Every eligible voter casts a vote.
* The number of the eligible voters of each electoral district of the first stage is odd.
* The number of the sub-districts of the (k − 1)-th stage that constitute a district of the k-th stage (k > 1) is also odd.
This means that each district of every stage has its winner (there is no tie).
Your mission is to write a program that finds a way to win the presidential election with the minimum number of votes. Suppose, for instance, that the district of the final stage has three sub-districts of the first stage and that the numbers of the eligible voters of the sub-districts are 123, 4567, and 89, respectively. The minimum number of votes required to be the winner is 107, that is, 62 from the first district and 45 from the third. In this case, even if the other candidate were given all the 4567 votes in the second district, s/he would inevitably be the loser. Although you might consider this election system unfair, you should accept it as a reality.
Input
The entire input looks like:
> the number of datasets (=n)
> 1st dataset
> 2nd dataset
> …
> n-th dataset
>
The number of datasets, n, is no more than 100.
The number of the eligible voters of each district and the part-whole relations among districts are denoted as follows.
* An electoral district of the first stage is denoted as [c], where c is the number of the eligible voters of the district.
* A district of the k-th stage (k > 1) is denoted as [d1d2…dm], where d1, d2, …, dm denote its sub-districts of the (k − 1)-th stage in this notation.
For instance, an electoral district of the first stage that has 123 eligible voters is denoted as [123]. A district of the second stage consisting of three sub-districts of the first stage that have 123, 4567, and 89 eligible voters, respectively, is denoted as [[123][4567][89]].
Each dataset is a line that contains the character string denoting the district of the final stage in the aforementioned notation. You can assume the following.
* The character string in each dataset does not include any characters except digits ('0', '1', …, '9') and square brackets ('[', ']'), and its length is between 11 and 10000, inclusive.
* The number of the eligible voters of each electoral district of the first stage is between 3 and 9999, inclusive.
The number of stages is a nation-wide constant. So, for instance, [[[9][9][9]][9][9]] never appears in the input. [[[[9]]]] may not appear either since each district of the second or later stage must have multiple sub-districts of the previous stage.
Output
For each dataset, print the minimum number of votes required to be the winner of the presidential election in a line. No output line may include any characters except the digits with which the number is written.
Sample Input
6
[[123][4567][89]]
[[5][3][7][3][9]]
[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]
[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]
[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]]
[[8231][3721][203][3271][8843]]
Output for the Sample Input
107
7
175
95
21
3599
Example
Input
6
[[123][4567][89]]
[[5][3][7][3][9]]
[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]
[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]
[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]]
[[8231][3721][203][3271][8843]]
Output
107
7
175
95
21
3599 | instruction | 0 | 68,873 | 17 | 137,746 |
"Correct Solution:
```
N=int(input())
for i in range(N):
S=input()
stack=[]
num=[0]
st=""
N=len(S)
for j in range(N):
i=S[j]
#print(num)
#print(stack)
#print('"'+i+'"',st)
if i=="[":
stack.append(0)
num.append(0)
elif i=="]":
n=num.pop()
num[-1]+=1
if st!="":
stack[-1]+=((int(st)+1)//2)
else:
tmp=[]
for k in range(n):
tmp.append(stack[-1])
stack.pop()
tmp.sort()
stack[-1]+=sum(tmp[:(len(tmp)+1)//2])
st=""
else:
st=st+str(i)
#print(num)
#print(stack)
print(stack[0])
``` | output | 1 | 68,873 | 17 | 137,747 |
Provide a correct Python 3 solution for this coding contest problem.
Hierarchical Democracy
The presidential election in Republic of Democratia is carried out through multiple stages as follows.
1. There are exactly two presidential candidates.
2. At the first stage, eligible voters go to the polls of his/her electoral district. The winner of the district is the candidate who takes a majority of the votes. Voters cast their ballots only at this first stage.
3. A district of the k-th stage (k > 1) consists of multiple districts of the (k − 1)-th stage. In contrast, a district of the (k − 1)-th stage is a sub-district of one and only one district of the k-th stage. The winner of a district of the k-th stage is the candidate who wins in a majority of its sub-districts of the (k − 1)-th stage.
4. The final stage has just one nation-wide district. The winner of the final stage is chosen as the president.
You can assume the following about the presidential election of this country.
* Every eligible voter casts a vote.
* The number of the eligible voters of each electoral district of the first stage is odd.
* The number of the sub-districts of the (k − 1)-th stage that constitute a district of the k-th stage (k > 1) is also odd.
This means that each district of every stage has its winner (there is no tie).
Your mission is to write a program that finds a way to win the presidential election with the minimum number of votes. Suppose, for instance, that the district of the final stage has three sub-districts of the first stage and that the numbers of the eligible voters of the sub-districts are 123, 4567, and 89, respectively. The minimum number of votes required to be the winner is 107, that is, 62 from the first district and 45 from the third. In this case, even if the other candidate were given all the 4567 votes in the second district, s/he would inevitably be the loser. Although you might consider this election system unfair, you should accept it as a reality.
Input
The entire input looks like:
> the number of datasets (=n)
> 1st dataset
> 2nd dataset
> …
> n-th dataset
>
The number of datasets, n, is no more than 100.
The number of the eligible voters of each district and the part-whole relations among districts are denoted as follows.
* An electoral district of the first stage is denoted as [c], where c is the number of the eligible voters of the district.
* A district of the k-th stage (k > 1) is denoted as [d1d2…dm], where d1, d2, …, dm denote its sub-districts of the (k − 1)-th stage in this notation.
For instance, an electoral district of the first stage that has 123 eligible voters is denoted as [123]. A district of the second stage consisting of three sub-districts of the first stage that have 123, 4567, and 89 eligible voters, respectively, is denoted as [[123][4567][89]].
Each dataset is a line that contains the character string denoting the district of the final stage in the aforementioned notation. You can assume the following.
* The character string in each dataset does not include any characters except digits ('0', '1', …, '9') and square brackets ('[', ']'), and its length is between 11 and 10000, inclusive.
* The number of the eligible voters of each electoral district of the first stage is between 3 and 9999, inclusive.
The number of stages is a nation-wide constant. So, for instance, [[[9][9][9]][9][9]] never appears in the input. [[[[9]]]] may not appear either since each district of the second or later stage must have multiple sub-districts of the previous stage.
Output
For each dataset, print the minimum number of votes required to be the winner of the presidential election in a line. No output line may include any characters except the digits with which the number is written.
Sample Input
6
[[123][4567][89]]
[[5][3][7][3][9]]
[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]
[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]
[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]]
[[8231][3721][203][3271][8843]]
Output for the Sample Input
107
7
175
95
21
3599
Example
Input
6
[[123][4567][89]]
[[5][3][7][3][9]]
[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]
[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]
[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]]
[[8231][3721][203][3271][8843]]
Output
107
7
175
95
21
3599 | instruction | 0 | 68,874 | 17 | 137,748 |
"Correct Solution:
```
def AC():
def solve(s):
if s.count('[')==1:
return int(s[1:-1])
elif '][' in s and ']][[' not in s:
tmp = sorted(map(int,s[2:-2].split('][')))
ans=0
for i in range(len(tmp)//2+1):
ans+=tmp[i]//2+1
return ans
else:
count=0
for i in range(len(s)):
if s[i]=='[':
count+=1
else:
count-=1
break
tmp=s[1:-1].split(']'*count+'['*count)
d=[]
for i in range(len(tmp)):
if i==0:
tmp[i]=tmp[i]+']'*count
elif i==len(tmp)-1:
tmp[i]='['*count+tmp[i]
else:
tmp[i]='['*count+tmp[i]+']'*count
d.append(solve(tmp[i]))
tmp = sorted(d)
ans=0
for i in range(len(tmp)//2+1):
ans+=tmp[i]
return ans
if __name__ == '__main__':
n=int(input())
for i in range(n):
print(solve(input()))
AC()
``` | output | 1 | 68,874 | 17 | 137,749 |
Provide a correct Python 3 solution for this coding contest problem.
Hierarchical Democracy
The presidential election in Republic of Democratia is carried out through multiple stages as follows.
1. There are exactly two presidential candidates.
2. At the first stage, eligible voters go to the polls of his/her electoral district. The winner of the district is the candidate who takes a majority of the votes. Voters cast their ballots only at this first stage.
3. A district of the k-th stage (k > 1) consists of multiple districts of the (k − 1)-th stage. In contrast, a district of the (k − 1)-th stage is a sub-district of one and only one district of the k-th stage. The winner of a district of the k-th stage is the candidate who wins in a majority of its sub-districts of the (k − 1)-th stage.
4. The final stage has just one nation-wide district. The winner of the final stage is chosen as the president.
You can assume the following about the presidential election of this country.
* Every eligible voter casts a vote.
* The number of the eligible voters of each electoral district of the first stage is odd.
* The number of the sub-districts of the (k − 1)-th stage that constitute a district of the k-th stage (k > 1) is also odd.
This means that each district of every stage has its winner (there is no tie).
Your mission is to write a program that finds a way to win the presidential election with the minimum number of votes. Suppose, for instance, that the district of the final stage has three sub-districts of the first stage and that the numbers of the eligible voters of the sub-districts are 123, 4567, and 89, respectively. The minimum number of votes required to be the winner is 107, that is, 62 from the first district and 45 from the third. In this case, even if the other candidate were given all the 4567 votes in the second district, s/he would inevitably be the loser. Although you might consider this election system unfair, you should accept it as a reality.
Input
The entire input looks like:
> the number of datasets (=n)
> 1st dataset
> 2nd dataset
> …
> n-th dataset
>
The number of datasets, n, is no more than 100.
The number of the eligible voters of each district and the part-whole relations among districts are denoted as follows.
* An electoral district of the first stage is denoted as [c], where c is the number of the eligible voters of the district.
* A district of the k-th stage (k > 1) is denoted as [d1d2…dm], where d1, d2, …, dm denote its sub-districts of the (k − 1)-th stage in this notation.
For instance, an electoral district of the first stage that has 123 eligible voters is denoted as [123]. A district of the second stage consisting of three sub-districts of the first stage that have 123, 4567, and 89 eligible voters, respectively, is denoted as [[123][4567][89]].
Each dataset is a line that contains the character string denoting the district of the final stage in the aforementioned notation. You can assume the following.
* The character string in each dataset does not include any characters except digits ('0', '1', …, '9') and square brackets ('[', ']'), and its length is between 11 and 10000, inclusive.
* The number of the eligible voters of each electoral district of the first stage is between 3 and 9999, inclusive.
The number of stages is a nation-wide constant. So, for instance, [[[9][9][9]][9][9]] never appears in the input. [[[[9]]]] may not appear either since each district of the second or later stage must have multiple sub-districts of the previous stage.
Output
For each dataset, print the minimum number of votes required to be the winner of the presidential election in a line. No output line may include any characters except the digits with which the number is written.
Sample Input
6
[[123][4567][89]]
[[5][3][7][3][9]]
[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]
[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]
[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]]
[[8231][3721][203][3271][8843]]
Output for the Sample Input
107
7
175
95
21
3599
Example
Input
6
[[123][4567][89]]
[[5][3][7][3][9]]
[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]
[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]
[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]]
[[8231][3721][203][3271][8843]]
Output
107
7
175
95
21
3599 | instruction | 0 | 68,875 | 17 | 137,750 |
"Correct Solution:
```
#2005_c
"""
n = int(input())
k = list("mcxi")
for i in range(n):
d = {"m":0,"c":0,"x":0,"i":0}
a,b = input().split()
a = list(a)
b = list(b)
a.insert(0,1)
b.insert(0,1)
for j in range(1,len(a)):
if a[j] in k:
if a[j-1] in k:
d[a[j]] += 1
else:
d[a[j]] += int(a[j-1])
for j in range(1,len(b))[::-1]:
if b[j] in k:
if b[j-1] in k:
d[b[j]] += 1
else:
d[b[j]] += int(b[j-1])
if d[b[j]] >= 10:
l = b[j]
while d[l] >= 10:
d[l] -= 10
l = k[k.index(l)-1]
d[l] += 1
for j in k:
if d[j]:
if d[j] == 1:
print(j,end = "")
else:
print(str(d[j])+j,end = "")
print()
"""
#2017_c
"""
while 1:
h, w = map(int, input().split())
if h == w == 0:
break
s = [list(map(int, input().split())) for i in range(h)]
ans = 0
for u in range(h):
for d in range(u+2,h):
for l in range(w):
for r in range(l+2,w):
m = float("inf")
for i in range(u,d+1):
m = min(m,s[i][l],s[i][r])
for i in range(l,r+1):
m = min(m,s[u][i],s[d][i])
f = 1
su = 0
for i in range(u+1,d):
for j in range(l+1,r):
su += (m-s[i][j])
if s[i][j] >= m:
f = 0
break
if not f:
break
if f:
ans = max(ans,su)
print(ans)
"""
#2016_c
"""
while 1:
m,n = map(int, input().split())
if m == n == 0:
break
d = {}
ma = 7368791
for i in range(m,ma+1):
d[i] = 1
z = m
for i in range(n):
for j in range(z,ma+1):
if d[j]:
z = j
break
j = 1
while z*j <= ma:
d[z*j] = 0
j += 1
for j in range(z,ma+1):
if d[j]:
print(j)
break
"""
#2018_c
"""
def factorize(n):
if n < 4:
return [1,n]
i = 2
l = [1]
while i**2 <= n:
if n%i == 0:
l.append(i)
if n//i != i:
l.append(n//i)
i += 1
l.append(n)
l.sort()
return l
while 1:
b = int(input())
if b == 0:
break
f = factorize(2*b)
for n in f[::-1]:
a = 1-n+(2*b)//n
if a >= 1 and a%2 == 0:
print(a//2,n)
break
"""
#2010_c
"""
import sys
dp = [100]*1000000
dp_2 = [100]*1000000
dp[0] = 0
dp_2[0] = 0
for i in range(1,181):
s = i*(i+1)*(i+2)//6
for j in range(s,1000000):
if dp[j-s]+1 < dp[j]:
dp[j] = dp[j-s]+1
if s%2:
for j in range(s,1000000):
if dp_2[j-s]+1 < dp_2[j]:
dp_2[j] = dp_2[j-s]+1
while 1:
m = int(sys.stdin.readline())
if m == 0:
break
print(dp[m],dp_2[m])
"""
#2015_c
"""
from collections import deque
while 1:
n = int(input())
if n == 0:
break
s = [input() for i in range(n)]
d = [s[i].count(".") for i in range(n)]
m = max(d)
c = [s[i][-1] for i in range(n)]
q = deque()
for i in range(1,m+1)[::-1]:
j = 0
while j < n:
for k in range(j,n):
if d[k] == i:break
else:
break
j = k
op = c[j-1]
while j < n and d[j] == i:
q.append(j)
j += 1
j = k
if op == "+":
k = 0
while q:
x = q.pop()
k += int(c[x])
c.pop(x)
d.pop(x)
n -= 1
else:
k = 1
while q:
x = q.pop()
k *= int(c[x])
c.pop(x)
d.pop(x)
n -= 1
c[j-1] = k
print(c[0])
"""
#2013_c
from collections import defaultdict
def parse_expr(s,i):
i += 1
if s[i] == "[":
q = []
while s[i] != "]":
e,i = parse_expr(s,i)
q.append(e)
return (calc(q),i+1)
else:
n,i = parse_num(s,i)
return (calc([n]),i+1)
def parse_num(s,i):
m = int(s[i])
i += 1
while f_num[s[i]]:
m *= 10
m += int(s[i])
i += 1
return (m,i)
def calc(q):
if len(q) == 1:
return (q[0]+1)//2
q.sort()
return sum(q[:len(q)//2+1])
f_num = defaultdict(lambda : 0)
for i in range(10):
f_num[str(i)] = 1
n = int(input())
for i in range(n):
s = input()
print(parse_expr(s,0)[0])
``` | output | 1 | 68,875 | 17 | 137,751 |
Provide a correct Python 3 solution for this coding contest problem.
Hierarchical Democracy
The presidential election in Republic of Democratia is carried out through multiple stages as follows.
1. There are exactly two presidential candidates.
2. At the first stage, eligible voters go to the polls of his/her electoral district. The winner of the district is the candidate who takes a majority of the votes. Voters cast their ballots only at this first stage.
3. A district of the k-th stage (k > 1) consists of multiple districts of the (k − 1)-th stage. In contrast, a district of the (k − 1)-th stage is a sub-district of one and only one district of the k-th stage. The winner of a district of the k-th stage is the candidate who wins in a majority of its sub-districts of the (k − 1)-th stage.
4. The final stage has just one nation-wide district. The winner of the final stage is chosen as the president.
You can assume the following about the presidential election of this country.
* Every eligible voter casts a vote.
* The number of the eligible voters of each electoral district of the first stage is odd.
* The number of the sub-districts of the (k − 1)-th stage that constitute a district of the k-th stage (k > 1) is also odd.
This means that each district of every stage has its winner (there is no tie).
Your mission is to write a program that finds a way to win the presidential election with the minimum number of votes. Suppose, for instance, that the district of the final stage has three sub-districts of the first stage and that the numbers of the eligible voters of the sub-districts are 123, 4567, and 89, respectively. The minimum number of votes required to be the winner is 107, that is, 62 from the first district and 45 from the third. In this case, even if the other candidate were given all the 4567 votes in the second district, s/he would inevitably be the loser. Although you might consider this election system unfair, you should accept it as a reality.
Input
The entire input looks like:
> the number of datasets (=n)
> 1st dataset
> 2nd dataset
> …
> n-th dataset
>
The number of datasets, n, is no more than 100.
The number of the eligible voters of each district and the part-whole relations among districts are denoted as follows.
* An electoral district of the first stage is denoted as [c], where c is the number of the eligible voters of the district.
* A district of the k-th stage (k > 1) is denoted as [d1d2…dm], where d1, d2, …, dm denote its sub-districts of the (k − 1)-th stage in this notation.
For instance, an electoral district of the first stage that has 123 eligible voters is denoted as [123]. A district of the second stage consisting of three sub-districts of the first stage that have 123, 4567, and 89 eligible voters, respectively, is denoted as [[123][4567][89]].
Each dataset is a line that contains the character string denoting the district of the final stage in the aforementioned notation. You can assume the following.
* The character string in each dataset does not include any characters except digits ('0', '1', …, '9') and square brackets ('[', ']'), and its length is between 11 and 10000, inclusive.
* The number of the eligible voters of each electoral district of the first stage is between 3 and 9999, inclusive.
The number of stages is a nation-wide constant. So, for instance, [[[9][9][9]][9][9]] never appears in the input. [[[[9]]]] may not appear either since each district of the second or later stage must have multiple sub-districts of the previous stage.
Output
For each dataset, print the minimum number of votes required to be the winner of the presidential election in a line. No output line may include any characters except the digits with which the number is written.
Sample Input
6
[[123][4567][89]]
[[5][3][7][3][9]]
[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]
[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]
[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]]
[[8231][3721][203][3271][8843]]
Output for the Sample Input
107
7
175
95
21
3599
Example
Input
6
[[123][4567][89]]
[[5][3][7][3][9]]
[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]
[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]
[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]]
[[8231][3721][203][3271][8843]]
Output
107
7
175
95
21
3599 | instruction | 0 | 68,876 | 17 | 137,752 |
"Correct Solution:
```
import re
n = int(input())
for _ in range(n):
dataset = input()
stage = 1
while (re.match('\[[0-9]+\]', dataset) is None):
iterator = re.finditer('\[([0-9]+\]\[)+[0-9]+\]', dataset)
minimum_votes = []
for i, match in enumerate(iterator):
areas = list(map(int, match.group()[1:-1].split('][')))
minimum_areas = sorted(areas)[:len(areas)//2 + 1]
if stage == 1:
minimum_voters = [m // 2 + 1 for m in minimum_areas]
minimum_vote = sum(minimum_voters)
else:
minimum_vote = sum(minimum_areas)
minimum_votes.append(str(minimum_vote))
dataset = dataset.replace(match.group(), str(-i-1), 1)
for i, minimum_vote in enumerate(minimum_votes):
dataset = dataset.replace(str(-i-1), minimum_vote, 1)
stage += 1
print(dataset[1:-1])
``` | output | 1 | 68,876 | 17 | 137,753 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hierarchical Democracy
The presidential election in Republic of Democratia is carried out through multiple stages as follows.
1. There are exactly two presidential candidates.
2. At the first stage, eligible voters go to the polls of his/her electoral district. The winner of the district is the candidate who takes a majority of the votes. Voters cast their ballots only at this first stage.
3. A district of the k-th stage (k > 1) consists of multiple districts of the (k − 1)-th stage. In contrast, a district of the (k − 1)-th stage is a sub-district of one and only one district of the k-th stage. The winner of a district of the k-th stage is the candidate who wins in a majority of its sub-districts of the (k − 1)-th stage.
4. The final stage has just one nation-wide district. The winner of the final stage is chosen as the president.
You can assume the following about the presidential election of this country.
* Every eligible voter casts a vote.
* The number of the eligible voters of each electoral district of the first stage is odd.
* The number of the sub-districts of the (k − 1)-th stage that constitute a district of the k-th stage (k > 1) is also odd.
This means that each district of every stage has its winner (there is no tie).
Your mission is to write a program that finds a way to win the presidential election with the minimum number of votes. Suppose, for instance, that the district of the final stage has three sub-districts of the first stage and that the numbers of the eligible voters of the sub-districts are 123, 4567, and 89, respectively. The minimum number of votes required to be the winner is 107, that is, 62 from the first district and 45 from the third. In this case, even if the other candidate were given all the 4567 votes in the second district, s/he would inevitably be the loser. Although you might consider this election system unfair, you should accept it as a reality.
Input
The entire input looks like:
> the number of datasets (=n)
> 1st dataset
> 2nd dataset
> …
> n-th dataset
>
The number of datasets, n, is no more than 100.
The number of the eligible voters of each district and the part-whole relations among districts are denoted as follows.
* An electoral district of the first stage is denoted as [c], where c is the number of the eligible voters of the district.
* A district of the k-th stage (k > 1) is denoted as [d1d2…dm], where d1, d2, …, dm denote its sub-districts of the (k − 1)-th stage in this notation.
For instance, an electoral district of the first stage that has 123 eligible voters is denoted as [123]. A district of the second stage consisting of three sub-districts of the first stage that have 123, 4567, and 89 eligible voters, respectively, is denoted as [[123][4567][89]].
Each dataset is a line that contains the character string denoting the district of the final stage in the aforementioned notation. You can assume the following.
* The character string in each dataset does not include any characters except digits ('0', '1', …, '9') and square brackets ('[', ']'), and its length is between 11 and 10000, inclusive.
* The number of the eligible voters of each electoral district of the first stage is between 3 and 9999, inclusive.
The number of stages is a nation-wide constant. So, for instance, [[[9][9][9]][9][9]] never appears in the input. [[[[9]]]] may not appear either since each district of the second or later stage must have multiple sub-districts of the previous stage.
Output
For each dataset, print the minimum number of votes required to be the winner of the presidential election in a line. No output line may include any characters except the digits with which the number is written.
Sample Input
6
[[123][4567][89]]
[[5][3][7][3][9]]
[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]
[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]
[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]]
[[8231][3721][203][3271][8843]]
Output for the Sample Input
107
7
175
95
21
3599
Example
Input
6
[[123][4567][89]]
[[5][3][7][3][9]]
[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]
[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]
[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]]
[[8231][3721][203][3271][8843]]
Output
107
7
175
95
21
3599
Submitted Solution:
```
def rec(s, l, now=0):
if s[now].isdigit():
res = ''
while now < l and s[now] != ']':
res += s[now]
now += 1
return int(res) // 2 + 1, now + 1
else:
g = []
while now < l and s[now] == '[':
res, now = rec(s, l, now + 1)
g.append(res)
g.sort()
return sum(g[:len(g) // 2 + 1]), now + 1
for _ in range(int(input())):
s = input()
l = len(s)
print(rec(s, l)[0])
``` | instruction | 0 | 68,877 | 17 | 137,754 |
Yes | output | 1 | 68,877 | 17 | 137,755 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hierarchical Democracy
The presidential election in Republic of Democratia is carried out through multiple stages as follows.
1. There are exactly two presidential candidates.
2. At the first stage, eligible voters go to the polls of his/her electoral district. The winner of the district is the candidate who takes a majority of the votes. Voters cast their ballots only at this first stage.
3. A district of the k-th stage (k > 1) consists of multiple districts of the (k − 1)-th stage. In contrast, a district of the (k − 1)-th stage is a sub-district of one and only one district of the k-th stage. The winner of a district of the k-th stage is the candidate who wins in a majority of its sub-districts of the (k − 1)-th stage.
4. The final stage has just one nation-wide district. The winner of the final stage is chosen as the president.
You can assume the following about the presidential election of this country.
* Every eligible voter casts a vote.
* The number of the eligible voters of each electoral district of the first stage is odd.
* The number of the sub-districts of the (k − 1)-th stage that constitute a district of the k-th stage (k > 1) is also odd.
This means that each district of every stage has its winner (there is no tie).
Your mission is to write a program that finds a way to win the presidential election with the minimum number of votes. Suppose, for instance, that the district of the final stage has three sub-districts of the first stage and that the numbers of the eligible voters of the sub-districts are 123, 4567, and 89, respectively. The minimum number of votes required to be the winner is 107, that is, 62 from the first district and 45 from the third. In this case, even if the other candidate were given all the 4567 votes in the second district, s/he would inevitably be the loser. Although you might consider this election system unfair, you should accept it as a reality.
Input
The entire input looks like:
> the number of datasets (=n)
> 1st dataset
> 2nd dataset
> …
> n-th dataset
>
The number of datasets, n, is no more than 100.
The number of the eligible voters of each district and the part-whole relations among districts are denoted as follows.
* An electoral district of the first stage is denoted as [c], where c is the number of the eligible voters of the district.
* A district of the k-th stage (k > 1) is denoted as [d1d2…dm], where d1, d2, …, dm denote its sub-districts of the (k − 1)-th stage in this notation.
For instance, an electoral district of the first stage that has 123 eligible voters is denoted as [123]. A district of the second stage consisting of three sub-districts of the first stage that have 123, 4567, and 89 eligible voters, respectively, is denoted as [[123][4567][89]].
Each dataset is a line that contains the character string denoting the district of the final stage in the aforementioned notation. You can assume the following.
* The character string in each dataset does not include any characters except digits ('0', '1', …, '9') and square brackets ('[', ']'), and its length is between 11 and 10000, inclusive.
* The number of the eligible voters of each electoral district of the first stage is between 3 and 9999, inclusive.
The number of stages is a nation-wide constant. So, for instance, [[[9][9][9]][9][9]] never appears in the input. [[[[9]]]] may not appear either since each district of the second or later stage must have multiple sub-districts of the previous stage.
Output
For each dataset, print the minimum number of votes required to be the winner of the presidential election in a line. No output line may include any characters except the digits with which the number is written.
Sample Input
6
[[123][4567][89]]
[[5][3][7][3][9]]
[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]
[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]
[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]]
[[8231][3721][203][3271][8843]]
Output for the Sample Input
107
7
175
95
21
3599
Example
Input
6
[[123][4567][89]]
[[5][3][7][3][9]]
[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]
[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]
[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]]
[[8231][3721][203][3271][8843]]
Output
107
7
175
95
21
3599
Submitted Solution:
```
n = int(input())
for i in range(n):
t = input()
nums = [[] for i in range(100)]
num = ""
nest = 0
maxnest = 0
for e in t:
if e=="[":
nest += 1
maxnest = max(maxnest, nest)
elif e!="[" and e!="]":
num += e
elif e=="]" and len(num):
nums[nest].append(int(num)//2+1)
num = ""
nest -= 1
else:
nums[nest+1].sort()
t = 0
for i in range(len(nums[nest+1])//2+1):
t += nums[nest+1][i]
nums[nest+1] = []
nums[nest].append(t)
nest -= 1
print(nums[1][0])
``` | instruction | 0 | 68,878 | 17 | 137,756 |
Yes | output | 1 | 68,878 | 17 | 137,757 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hierarchical Democracy
The presidential election in Republic of Democratia is carried out through multiple stages as follows.
1. There are exactly two presidential candidates.
2. At the first stage, eligible voters go to the polls of his/her electoral district. The winner of the district is the candidate who takes a majority of the votes. Voters cast their ballots only at this first stage.
3. A district of the k-th stage (k > 1) consists of multiple districts of the (k − 1)-th stage. In contrast, a district of the (k − 1)-th stage is a sub-district of one and only one district of the k-th stage. The winner of a district of the k-th stage is the candidate who wins in a majority of its sub-districts of the (k − 1)-th stage.
4. The final stage has just one nation-wide district. The winner of the final stage is chosen as the president.
You can assume the following about the presidential election of this country.
* Every eligible voter casts a vote.
* The number of the eligible voters of each electoral district of the first stage is odd.
* The number of the sub-districts of the (k − 1)-th stage that constitute a district of the k-th stage (k > 1) is also odd.
This means that each district of every stage has its winner (there is no tie).
Your mission is to write a program that finds a way to win the presidential election with the minimum number of votes. Suppose, for instance, that the district of the final stage has three sub-districts of the first stage and that the numbers of the eligible voters of the sub-districts are 123, 4567, and 89, respectively. The minimum number of votes required to be the winner is 107, that is, 62 from the first district and 45 from the third. In this case, even if the other candidate were given all the 4567 votes in the second district, s/he would inevitably be the loser. Although you might consider this election system unfair, you should accept it as a reality.
Input
The entire input looks like:
> the number of datasets (=n)
> 1st dataset
> 2nd dataset
> …
> n-th dataset
>
The number of datasets, n, is no more than 100.
The number of the eligible voters of each district and the part-whole relations among districts are denoted as follows.
* An electoral district of the first stage is denoted as [c], where c is the number of the eligible voters of the district.
* A district of the k-th stage (k > 1) is denoted as [d1d2…dm], where d1, d2, …, dm denote its sub-districts of the (k − 1)-th stage in this notation.
For instance, an electoral district of the first stage that has 123 eligible voters is denoted as [123]. A district of the second stage consisting of three sub-districts of the first stage that have 123, 4567, and 89 eligible voters, respectively, is denoted as [[123][4567][89]].
Each dataset is a line that contains the character string denoting the district of the final stage in the aforementioned notation. You can assume the following.
* The character string in each dataset does not include any characters except digits ('0', '1', …, '9') and square brackets ('[', ']'), and its length is between 11 and 10000, inclusive.
* The number of the eligible voters of each electoral district of the first stage is between 3 and 9999, inclusive.
The number of stages is a nation-wide constant. So, for instance, [[[9][9][9]][9][9]] never appears in the input. [[[[9]]]] may not appear either since each district of the second or later stage must have multiple sub-districts of the previous stage.
Output
For each dataset, print the minimum number of votes required to be the winner of the presidential election in a line. No output line may include any characters except the digits with which the number is written.
Sample Input
6
[[123][4567][89]]
[[5][3][7][3][9]]
[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]
[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]
[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]]
[[8231][3721][203][3271][8843]]
Output for the Sample Input
107
7
175
95
21
3599
Example
Input
6
[[123][4567][89]]
[[5][3][7][3][9]]
[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]
[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]
[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]]
[[8231][3721][203][3271][8843]]
Output
107
7
175
95
21
3599
Submitted Solution:
```
import sys
sys.setrecursionlimit(10**5)
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LI_(): return [-1*int(x) for x in input().split()]
def II(): return int(input())
def IF(): return float(input())
def LM(func,n): return [[func(x) for x in input().split()]for i in range(n)]
mod = 1000000007
inf = float('INF')
def solve(L):
L = L[1:-1]
if L[0] != '[':
return int(L)//2+1
ret = []
s=0
c = 0
for i in range(len(L)):
if L[i] == '[':
c += 1
elif L[i] == ']':
c -= 1
if c == 0:
ret.append(solve(L[s:i + 1]))
s = i + 1
ret.sort()
return sum(ret[0:len(ret)//2+1])
N = II()
for i in range(N):
L = input()
print(solve(L))
``` | instruction | 0 | 68,879 | 17 | 137,758 |
Yes | output | 1 | 68,879 | 17 | 137,759 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hierarchical Democracy
The presidential election in Republic of Democratia is carried out through multiple stages as follows.
1. There are exactly two presidential candidates.
2. At the first stage, eligible voters go to the polls of his/her electoral district. The winner of the district is the candidate who takes a majority of the votes. Voters cast their ballots only at this first stage.
3. A district of the k-th stage (k > 1) consists of multiple districts of the (k − 1)-th stage. In contrast, a district of the (k − 1)-th stage is a sub-district of one and only one district of the k-th stage. The winner of a district of the k-th stage is the candidate who wins in a majority of its sub-districts of the (k − 1)-th stage.
4. The final stage has just one nation-wide district. The winner of the final stage is chosen as the president.
You can assume the following about the presidential election of this country.
* Every eligible voter casts a vote.
* The number of the eligible voters of each electoral district of the first stage is odd.
* The number of the sub-districts of the (k − 1)-th stage that constitute a district of the k-th stage (k > 1) is also odd.
This means that each district of every stage has its winner (there is no tie).
Your mission is to write a program that finds a way to win the presidential election with the minimum number of votes. Suppose, for instance, that the district of the final stage has three sub-districts of the first stage and that the numbers of the eligible voters of the sub-districts are 123, 4567, and 89, respectively. The minimum number of votes required to be the winner is 107, that is, 62 from the first district and 45 from the third. In this case, even if the other candidate were given all the 4567 votes in the second district, s/he would inevitably be the loser. Although you might consider this election system unfair, you should accept it as a reality.
Input
The entire input looks like:
> the number of datasets (=n)
> 1st dataset
> 2nd dataset
> …
> n-th dataset
>
The number of datasets, n, is no more than 100.
The number of the eligible voters of each district and the part-whole relations among districts are denoted as follows.
* An electoral district of the first stage is denoted as [c], where c is the number of the eligible voters of the district.
* A district of the k-th stage (k > 1) is denoted as [d1d2…dm], where d1, d2, …, dm denote its sub-districts of the (k − 1)-th stage in this notation.
For instance, an electoral district of the first stage that has 123 eligible voters is denoted as [123]. A district of the second stage consisting of three sub-districts of the first stage that have 123, 4567, and 89 eligible voters, respectively, is denoted as [[123][4567][89]].
Each dataset is a line that contains the character string denoting the district of the final stage in the aforementioned notation. You can assume the following.
* The character string in each dataset does not include any characters except digits ('0', '1', …, '9') and square brackets ('[', ']'), and its length is between 11 and 10000, inclusive.
* The number of the eligible voters of each electoral district of the first stage is between 3 and 9999, inclusive.
The number of stages is a nation-wide constant. So, for instance, [[[9][9][9]][9][9]] never appears in the input. [[[[9]]]] may not appear either since each district of the second or later stage must have multiple sub-districts of the previous stage.
Output
For each dataset, print the minimum number of votes required to be the winner of the presidential election in a line. No output line may include any characters except the digits with which the number is written.
Sample Input
6
[[123][4567][89]]
[[5][3][7][3][9]]
[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]
[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]
[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]]
[[8231][3721][203][3271][8843]]
Output for the Sample Input
107
7
175
95
21
3599
Example
Input
6
[[123][4567][89]]
[[5][3][7][3][9]]
[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]
[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]
[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]]
[[8231][3721][203][3271][8843]]
Output
107
7
175
95
21
3599
Submitted Solution:
```
def rec(s):
ans = int(1e8)
c_list = []
st = []
n = len(s)
l = 0
r = len(s)-1
if s[0]!='[':
return 1+int(s)//2
for i in range(n):
_s = s[i]
if _s=='[':
if len(st)==0:
l = i+1
st.append(_s)
elif _s==']':
st = st[:-1]
if len(st)==0:
r = i
c_list.append(rec(s[l:r]))
c_list.sort()
return sum( c_list[:1+len(c_list)//2] )
N = int(input())
for _ in range(N):
s = input()
print(rec(s))
``` | instruction | 0 | 68,880 | 17 | 137,760 |
Yes | output | 1 | 68,880 | 17 | 137,761 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hierarchical Democracy
The presidential election in Republic of Democratia is carried out through multiple stages as follows.
1. There are exactly two presidential candidates.
2. At the first stage, eligible voters go to the polls of his/her electoral district. The winner of the district is the candidate who takes a majority of the votes. Voters cast their ballots only at this first stage.
3. A district of the k-th stage (k > 1) consists of multiple districts of the (k − 1)-th stage. In contrast, a district of the (k − 1)-th stage is a sub-district of one and only one district of the k-th stage. The winner of a district of the k-th stage is the candidate who wins in a majority of its sub-districts of the (k − 1)-th stage.
4. The final stage has just one nation-wide district. The winner of the final stage is chosen as the president.
You can assume the following about the presidential election of this country.
* Every eligible voter casts a vote.
* The number of the eligible voters of each electoral district of the first stage is odd.
* The number of the sub-districts of the (k − 1)-th stage that constitute a district of the k-th stage (k > 1) is also odd.
This means that each district of every stage has its winner (there is no tie).
Your mission is to write a program that finds a way to win the presidential election with the minimum number of votes. Suppose, for instance, that the district of the final stage has three sub-districts of the first stage and that the numbers of the eligible voters of the sub-districts are 123, 4567, and 89, respectively. The minimum number of votes required to be the winner is 107, that is, 62 from the first district and 45 from the third. In this case, even if the other candidate were given all the 4567 votes in the second district, s/he would inevitably be the loser. Although you might consider this election system unfair, you should accept it as a reality.
Input
The entire input looks like:
> the number of datasets (=n)
> 1st dataset
> 2nd dataset
> …
> n-th dataset
>
The number of datasets, n, is no more than 100.
The number of the eligible voters of each district and the part-whole relations among districts are denoted as follows.
* An electoral district of the first stage is denoted as [c], where c is the number of the eligible voters of the district.
* A district of the k-th stage (k > 1) is denoted as [d1d2…dm], where d1, d2, …, dm denote its sub-districts of the (k − 1)-th stage in this notation.
For instance, an electoral district of the first stage that has 123 eligible voters is denoted as [123]. A district of the second stage consisting of three sub-districts of the first stage that have 123, 4567, and 89 eligible voters, respectively, is denoted as [[123][4567][89]].
Each dataset is a line that contains the character string denoting the district of the final stage in the aforementioned notation. You can assume the following.
* The character string in each dataset does not include any characters except digits ('0', '1', …, '9') and square brackets ('[', ']'), and its length is between 11 and 10000, inclusive.
* The number of the eligible voters of each electoral district of the first stage is between 3 and 9999, inclusive.
The number of stages is a nation-wide constant. So, for instance, [[[9][9][9]][9][9]] never appears in the input. [[[[9]]]] may not appear either since each district of the second or later stage must have multiple sub-districts of the previous stage.
Output
For each dataset, print the minimum number of votes required to be the winner of the presidential election in a line. No output line may include any characters except the digits with which the number is written.
Sample Input
6
[[123][4567][89]]
[[5][3][7][3][9]]
[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]
[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]
[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]]
[[8231][3721][203][3271][8843]]
Output for the Sample Input
107
7
175
95
21
3599
Example
Input
6
[[123][4567][89]]
[[5][3][7][3][9]]
[[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]]
[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]]
[[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]]
[[8231][3721][203][3271][8843]]
Output
107
7
175
95
21
3599
Submitted Solution:
```
def calc_majority(l):
l.sort()
result = 0
for i in range(len(l) // 2 + 1):
result += l[i]
return result
#過半数になるような値を求めてそれを返す
def calc_fisrt_majority(l):
people_num = list(map(int, l))
people_num.sort()
n = 0
for i in range(len(people_num) // 2 + 1):
n += int(people_num[i]) // 2 + 1
return n
#括弧と括弧の間の数値を返す
def get_num(l, lp, rp):
#print(lp, end="")
#print(" ", end="")
#print(rp)
s = ''
for i in range(lp+1, rp):
s += l[i]
return s
#左かっこに対応する右かっこのインデックスを返す
def getRp(char_list, n):
count = 1
i = 0
while count:
c = char_list[i]
i += 1
if c == '[':
count += 1
elif c == ']':
count -= 1
else:
continue
return i+n
#かっこの位置全てのリストを返す
def brackets(s):
lp = []
rp = []
n = 0
bucket_nest = 0
l = list(s)
for i in l:
if i == '[':
bucket_nest += 1
if i != '[':
break
for i in l:
n += 1
if i == '[':
lp.append(n-1)
rp.append(getRp(l[n:], n-1))
for i in range(1, len(lp)):
return lp, rp, bucket_nest
def main():
N = int(input())
lp = []
rp = []
tmp_result = []
result = 0
people_num = []
nest = 0
n = 0
while N != 0:
s = input()
l = list(s)
N -= 1
lp, rp, nest = brackets(s)
n = 0
for i in lp:
if(l[i+1] != '['): #次が[でないなら必ず数字が来る
people_num.append(get_num(l, lp[n], rp[n]))
if(l[rp[n]+1] == ']'): #ネストの終わり
tmp_result.append(calc_fisrt_majority(people_num))
people_num.clear()
n += 1
print(tmp_result)
if nest == 2:
result = tmp_result[0]
while nest != 2:
result = calc_majority(tmp_result)
nest -= 1
print(result)
if __name__ == '__main__':
main()
#[[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]]
#[[57][95][5][69][31]][[99][59][65][73][31]]]
#[[43][9][43][48][79]]=9+43+43=95
``` | instruction | 0 | 68,881 | 17 | 137,762 |
No | output | 1 | 68,881 | 17 | 137,763 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Reminder: the [median](https://en.wikipedia.org/wiki/Median) of the array [a_1, a_2, ..., a_{2k+1}] of odd number of elements is defined as follows: let [b_1, b_2, ..., b_{2k+1}] be the elements of the array in the sorted order. Then median of this array is equal to b_{k+1}.
There are 2n students, the i-th student has skill level a_i. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the 2 classes such that each class has odd number of students (not divisible by 2). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10^4). The description of the test cases follows.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5) — the number of students halved.
The second line of each test case contains 2n integers a_1, a_2, ..., a_{2 n} (1 ≤ a_i ≤ 10^9) — skill levels of students.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
Example
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
Note
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be |1 - 1| = 0.
In the second test, one of the possible partitions is to make the first class of students with skill levels [6, 4, 2], so that the skill level of the first class will be 4, and second with [5, 1, 3], so that the skill level of the second class will be 3. Absolute difference will be |4 - 3| = 1.
Note that you can't assign like [2, 3], [6, 5, 4, 1] or [], [6, 5, 4, 1, 2, 3] because classes have even number of students.
[2], [1, 3, 4] is also not possible because students with skills 5 and 6 aren't assigned to a class.
In the third test you can assign the students in the following way: [3, 4, 13, 13, 20], [2, 5, 8, 16, 17] or [3, 8, 17], [2, 4, 5, 13, 13, 16, 20]. Both divisions give minimal possible absolute difference. | instruction | 0 | 69,055 | 17 | 138,110 |
Tags: greedy, implementation, sortings
Correct Solution:
```
import math
def inp_n():
return int(input())
def inp_list():
return list(map(int, input().split()))
def inp_mul_num():
return map(int, input().split())
def is_odd(n):
if n & 1:
return True
return False
for _ in range(inp_n()):
n = inp_n()
l = inp_list()
l.sort()
print(l[n] - l[n-1])
``` | output | 1 | 69,055 | 17 | 138,111 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Reminder: the [median](https://en.wikipedia.org/wiki/Median) of the array [a_1, a_2, ..., a_{2k+1}] of odd number of elements is defined as follows: let [b_1, b_2, ..., b_{2k+1}] be the elements of the array in the sorted order. Then median of this array is equal to b_{k+1}.
There are 2n students, the i-th student has skill level a_i. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the 2 classes such that each class has odd number of students (not divisible by 2). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10^4). The description of the test cases follows.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5) — the number of students halved.
The second line of each test case contains 2n integers a_1, a_2, ..., a_{2 n} (1 ≤ a_i ≤ 10^9) — skill levels of students.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
Example
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
Note
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be |1 - 1| = 0.
In the second test, one of the possible partitions is to make the first class of students with skill levels [6, 4, 2], so that the skill level of the first class will be 4, and second with [5, 1, 3], so that the skill level of the second class will be 3. Absolute difference will be |4 - 3| = 1.
Note that you can't assign like [2, 3], [6, 5, 4, 1] or [], [6, 5, 4, 1, 2, 3] because classes have even number of students.
[2], [1, 3, 4] is also not possible because students with skills 5 and 6 aren't assigned to a class.
In the third test you can assign the students in the following way: [3, 4, 13, 13, 20], [2, 5, 8, 16, 17] or [3, 8, 17], [2, 4, 5, 13, 13, 16, 20]. Both divisions give minimal possible absolute difference. | instruction | 0 | 69,056 | 17 | 138,112 |
Tags: greedy, implementation, sortings
Correct Solution:
```
t = int(input())
while int(t)>0:
n = int(input())
q = input()
a = q.split()
for i in range(0, n*2):
a[i] = int(a[i])
a.sort()
s = a[n]-a[n-1]
print(s)
t -= 1
``` | output | 1 | 69,056 | 17 | 138,113 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Reminder: the [median](https://en.wikipedia.org/wiki/Median) of the array [a_1, a_2, ..., a_{2k+1}] of odd number of elements is defined as follows: let [b_1, b_2, ..., b_{2k+1}] be the elements of the array in the sorted order. Then median of this array is equal to b_{k+1}.
There are 2n students, the i-th student has skill level a_i. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the 2 classes such that each class has odd number of students (not divisible by 2). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10^4). The description of the test cases follows.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5) — the number of students halved.
The second line of each test case contains 2n integers a_1, a_2, ..., a_{2 n} (1 ≤ a_i ≤ 10^9) — skill levels of students.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
Example
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
Note
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be |1 - 1| = 0.
In the second test, one of the possible partitions is to make the first class of students with skill levels [6, 4, 2], so that the skill level of the first class will be 4, and second with [5, 1, 3], so that the skill level of the second class will be 3. Absolute difference will be |4 - 3| = 1.
Note that you can't assign like [2, 3], [6, 5, 4, 1] or [], [6, 5, 4, 1, 2, 3] because classes have even number of students.
[2], [1, 3, 4] is also not possible because students with skills 5 and 6 aren't assigned to a class.
In the third test you can assign the students in the following way: [3, 4, 13, 13, 20], [2, 5, 8, 16, 17] or [3, 8, 17], [2, 4, 5, 13, 13, 16, 20]. Both divisions give minimal possible absolute difference. | instruction | 0 | 69,057 | 17 | 138,114 |
Tags: greedy, implementation, sortings
Correct Solution:
```
for i in range(int(input())):
n = int(input())
nums = sorted([int(a) for a in input().split()])
print(nums[n]-nums[n-1])
``` | output | 1 | 69,057 | 17 | 138,115 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Reminder: the [median](https://en.wikipedia.org/wiki/Median) of the array [a_1, a_2, ..., a_{2k+1}] of odd number of elements is defined as follows: let [b_1, b_2, ..., b_{2k+1}] be the elements of the array in the sorted order. Then median of this array is equal to b_{k+1}.
There are 2n students, the i-th student has skill level a_i. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the 2 classes such that each class has odd number of students (not divisible by 2). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10^4). The description of the test cases follows.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5) — the number of students halved.
The second line of each test case contains 2n integers a_1, a_2, ..., a_{2 n} (1 ≤ a_i ≤ 10^9) — skill levels of students.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
Example
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
Note
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be |1 - 1| = 0.
In the second test, one of the possible partitions is to make the first class of students with skill levels [6, 4, 2], so that the skill level of the first class will be 4, and second with [5, 1, 3], so that the skill level of the second class will be 3. Absolute difference will be |4 - 3| = 1.
Note that you can't assign like [2, 3], [6, 5, 4, 1] or [], [6, 5, 4, 1, 2, 3] because classes have even number of students.
[2], [1, 3, 4] is also not possible because students with skills 5 and 6 aren't assigned to a class.
In the third test you can assign the students in the following way: [3, 4, 13, 13, 20], [2, 5, 8, 16, 17] or [3, 8, 17], [2, 4, 5, 13, 13, 16, 20]. Both divisions give minimal possible absolute difference. | instruction | 0 | 69,058 | 17 | 138,116 |
Tags: greedy, implementation, sortings
Correct Solution:
```
"""
Author : thekushalghosh
Team : CodeDiggers
"""
import sys,math
input = sys.stdin.readline
############ ---- USER DEFINED INPUT FUNCTIONS ---- ############
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input()
return(s[:len(s) - 1])
def invr():
return(map(int,input().split()))
################################################################
############ ---- THE ACTUAL CODE STARTS BELOW ---- ############
t = 1
t = inp()
for tt in range(t):
n = inp()
a = inlt()
a.sort()
print(a[n] - a[n - 1])
``` | output | 1 | 69,058 | 17 | 138,117 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Reminder: the [median](https://en.wikipedia.org/wiki/Median) of the array [a_1, a_2, ..., a_{2k+1}] of odd number of elements is defined as follows: let [b_1, b_2, ..., b_{2k+1}] be the elements of the array in the sorted order. Then median of this array is equal to b_{k+1}.
There are 2n students, the i-th student has skill level a_i. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the 2 classes such that each class has odd number of students (not divisible by 2). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10^4). The description of the test cases follows.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5) — the number of students halved.
The second line of each test case contains 2n integers a_1, a_2, ..., a_{2 n} (1 ≤ a_i ≤ 10^9) — skill levels of students.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
Example
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
Note
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be |1 - 1| = 0.
In the second test, one of the possible partitions is to make the first class of students with skill levels [6, 4, 2], so that the skill level of the first class will be 4, and second with [5, 1, 3], so that the skill level of the second class will be 3. Absolute difference will be |4 - 3| = 1.
Note that you can't assign like [2, 3], [6, 5, 4, 1] or [], [6, 5, 4, 1, 2, 3] because classes have even number of students.
[2], [1, 3, 4] is also not possible because students with skills 5 and 6 aren't assigned to a class.
In the third test you can assign the students in the following way: [3, 4, 13, 13, 20], [2, 5, 8, 16, 17] or [3, 8, 17], [2, 4, 5, 13, 13, 16, 20]. Both divisions give minimal possible absolute difference. | instruction | 0 | 69,059 | 17 | 138,118 |
Tags: greedy, implementation, sortings
Correct Solution:
```
import sys
t=int(input())
for i in range(t):
n=int(input())
arr=list(eval(input().replace(' ',',')))
if type(arr) is int:
arr=[arr]
arr.sort()
print(arr[n]-arr[n-1])
sys.stdout.flush()
``` | output | 1 | 69,059 | 17 | 138,119 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Reminder: the [median](https://en.wikipedia.org/wiki/Median) of the array [a_1, a_2, ..., a_{2k+1}] of odd number of elements is defined as follows: let [b_1, b_2, ..., b_{2k+1}] be the elements of the array in the sorted order. Then median of this array is equal to b_{k+1}.
There are 2n students, the i-th student has skill level a_i. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the 2 classes such that each class has odd number of students (not divisible by 2). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10^4). The description of the test cases follows.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5) — the number of students halved.
The second line of each test case contains 2n integers a_1, a_2, ..., a_{2 n} (1 ≤ a_i ≤ 10^9) — skill levels of students.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
Example
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
Note
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be |1 - 1| = 0.
In the second test, one of the possible partitions is to make the first class of students with skill levels [6, 4, 2], so that the skill level of the first class will be 4, and second with [5, 1, 3], so that the skill level of the second class will be 3. Absolute difference will be |4 - 3| = 1.
Note that you can't assign like [2, 3], [6, 5, 4, 1] or [], [6, 5, 4, 1, 2, 3] because classes have even number of students.
[2], [1, 3, 4] is also not possible because students with skills 5 and 6 aren't assigned to a class.
In the third test you can assign the students in the following way: [3, 4, 13, 13, 20], [2, 5, 8, 16, 17] or [3, 8, 17], [2, 4, 5, 13, 13, 16, 20]. Both divisions give minimal possible absolute difference. | instruction | 0 | 69,060 | 17 | 138,120 |
Tags: greedy, implementation, sortings
Correct Solution:
```
import sys
T = int(sys.stdin.readline().strip())
for t in range (0, T):
n = int(sys.stdin.readline().strip())
a = list(map(int, sys.stdin.readline().strip().split()))
a.sort()
print(a[n]-a[n-1])
``` | output | 1 | 69,060 | 17 | 138,121 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Reminder: the [median](https://en.wikipedia.org/wiki/Median) of the array [a_1, a_2, ..., a_{2k+1}] of odd number of elements is defined as follows: let [b_1, b_2, ..., b_{2k+1}] be the elements of the array in the sorted order. Then median of this array is equal to b_{k+1}.
There are 2n students, the i-th student has skill level a_i. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the 2 classes such that each class has odd number of students (not divisible by 2). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10^4). The description of the test cases follows.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5) — the number of students halved.
The second line of each test case contains 2n integers a_1, a_2, ..., a_{2 n} (1 ≤ a_i ≤ 10^9) — skill levels of students.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
Example
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
Note
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be |1 - 1| = 0.
In the second test, one of the possible partitions is to make the first class of students with skill levels [6, 4, 2], so that the skill level of the first class will be 4, and second with [5, 1, 3], so that the skill level of the second class will be 3. Absolute difference will be |4 - 3| = 1.
Note that you can't assign like [2, 3], [6, 5, 4, 1] or [], [6, 5, 4, 1, 2, 3] because classes have even number of students.
[2], [1, 3, 4] is also not possible because students with skills 5 and 6 aren't assigned to a class.
In the third test you can assign the students in the following way: [3, 4, 13, 13, 20], [2, 5, 8, 16, 17] or [3, 8, 17], [2, 4, 5, 13, 13, 16, 20]. Both divisions give minimal possible absolute difference. | instruction | 0 | 69,061 | 17 | 138,122 |
Tags: greedy, implementation, sortings
Correct Solution:
```
from sys import stdin
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin
def ii(): return int(stdin.readline())
def fi(): return float(stdin.readline())
def mi(): return map(int, stdin.readline().split())
def fmi(): return map(float, stdin.readline().split())
def li(): return list(mi())
def lsi():
x=list(stdin.readline())
x.pop()
return x
def si(): return stdin.readline()
def sieve(x):
a=[True]*(x+1)
sq=floor(sqrt(x))
for i in range(3, sq+1, 2):
if a[i]:
for j in range(i*i, x+1, i):
a[j]=False
if x>1:
p=[2]
else:
p=[]
for i in range(3, x+1, 2):
if a[i]:
p.append(i)
return p
def prime_factor(x):
p=sieve(ceil(sqrt(x)))+[100000000]
a=[]
i=0
while p[i]*p[i]<=x:
if not x%p[i]:
a.append(p[i])
x//=p[i]
else:
i+=1
if x>1:
a.append(x)
return a
res=['YES', 'NO']
#vowel={'a', 'e', 'i', 'o', 'u', 'y', 'A', 'E', 'I', 'O', 'U', 'Y'}
#pow=[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648, 4294967296, 8589934592, 17179869184, 34359738368, 68719476736, 137438953472, 274877906944, 549755813888, 1099511627776, 2199023255552, 4398046511104, 8796093022208, 17592186044416, 35184372088832, 70368744177664, 140737488355328, 281474976710656, 562949953421312, 1125899906842624, 2251799813685248, 4503599627370496, 9007199254740992, 18014398509481984, 36028797018963968, 72057594037927936, 144115188075855872, 288230376151711744, 576460752303423488, 1152921504606846976, 2305843009213693952, 4611686018427387904, 9223372036854775808]
############# CODE STARTS HERE #############
for _ in range(ii()):
n=ii()
a=li()
a.sort()
print(a[n]-a[n-1])
``` | output | 1 | 69,061 | 17 | 138,123 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Reminder: the [median](https://en.wikipedia.org/wiki/Median) of the array [a_1, a_2, ..., a_{2k+1}] of odd number of elements is defined as follows: let [b_1, b_2, ..., b_{2k+1}] be the elements of the array in the sorted order. Then median of this array is equal to b_{k+1}.
There are 2n students, the i-th student has skill level a_i. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the 2 classes such that each class has odd number of students (not divisible by 2). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10^4). The description of the test cases follows.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5) — the number of students halved.
The second line of each test case contains 2n integers a_1, a_2, ..., a_{2 n} (1 ≤ a_i ≤ 10^9) — skill levels of students.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
Example
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
Note
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be |1 - 1| = 0.
In the second test, one of the possible partitions is to make the first class of students with skill levels [6, 4, 2], so that the skill level of the first class will be 4, and second with [5, 1, 3], so that the skill level of the second class will be 3. Absolute difference will be |4 - 3| = 1.
Note that you can't assign like [2, 3], [6, 5, 4, 1] or [], [6, 5, 4, 1, 2, 3] because classes have even number of students.
[2], [1, 3, 4] is also not possible because students with skills 5 and 6 aren't assigned to a class.
In the third test you can assign the students in the following way: [3, 4, 13, 13, 20], [2, 5, 8, 16, 17] or [3, 8, 17], [2, 4, 5, 13, 13, 16, 20]. Both divisions give minimal possible absolute difference. | instruction | 0 | 69,062 | 17 | 138,124 |
Tags: greedy, implementation, sortings
Correct Solution:
```
# Problem: B
# Date: February 11 2020
# Author: OBJECT.705X
t=int(input())
while t:
n=int(input())
a=[int(x) for x in input().split()]
a=sorted(a)
print(a[n]-a[n-1])
t-=1
``` | output | 1 | 69,062 | 17 | 138,125 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Reminder: the [median](https://en.wikipedia.org/wiki/Median) of the array [a_1, a_2, ..., a_{2k+1}] of odd number of elements is defined as follows: let [b_1, b_2, ..., b_{2k+1}] be the elements of the array in the sorted order. Then median of this array is equal to b_{k+1}.
There are 2n students, the i-th student has skill level a_i. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the 2 classes such that each class has odd number of students (not divisible by 2). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10^4). The description of the test cases follows.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5) — the number of students halved.
The second line of each test case contains 2n integers a_1, a_2, ..., a_{2 n} (1 ≤ a_i ≤ 10^9) — skill levels of students.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
Example
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
Note
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be |1 - 1| = 0.
In the second test, one of the possible partitions is to make the first class of students with skill levels [6, 4, 2], so that the skill level of the first class will be 4, and second with [5, 1, 3], so that the skill level of the second class will be 3. Absolute difference will be |4 - 3| = 1.
Note that you can't assign like [2, 3], [6, 5, 4, 1] or [], [6, 5, 4, 1, 2, 3] because classes have even number of students.
[2], [1, 3, 4] is also not possible because students with skills 5 and 6 aren't assigned to a class.
In the third test you can assign the students in the following way: [3, 4, 13, 13, 20], [2, 5, 8, 16, 17] or [3, 8, 17], [2, 4, 5, 13, 13, 16, 20]. Both divisions give minimal possible absolute difference.
Submitted Solution:
```
def customer(l,n):
l.sort()
return abs(l[n]-l[n-1])
t=int(input())
for _ in range(t):
n=int(input())
queries=list(map(int,input().split()))
print(customer(queries,n))
``` | instruction | 0 | 69,063 | 17 | 138,126 |
Yes | output | 1 | 69,063 | 17 | 138,127 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Reminder: the [median](https://en.wikipedia.org/wiki/Median) of the array [a_1, a_2, ..., a_{2k+1}] of odd number of elements is defined as follows: let [b_1, b_2, ..., b_{2k+1}] be the elements of the array in the sorted order. Then median of this array is equal to b_{k+1}.
There are 2n students, the i-th student has skill level a_i. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the 2 classes such that each class has odd number of students (not divisible by 2). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10^4). The description of the test cases follows.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5) — the number of students halved.
The second line of each test case contains 2n integers a_1, a_2, ..., a_{2 n} (1 ≤ a_i ≤ 10^9) — skill levels of students.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
Example
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
Note
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be |1 - 1| = 0.
In the second test, one of the possible partitions is to make the first class of students with skill levels [6, 4, 2], so that the skill level of the first class will be 4, and second with [5, 1, 3], so that the skill level of the second class will be 3. Absolute difference will be |4 - 3| = 1.
Note that you can't assign like [2, 3], [6, 5, 4, 1] or [], [6, 5, 4, 1, 2, 3] because classes have even number of students.
[2], [1, 3, 4] is also not possible because students with skills 5 and 6 aren't assigned to a class.
In the third test you can assign the students in the following way: [3, 4, 13, 13, 20], [2, 5, 8, 16, 17] or [3, 8, 17], [2, 4, 5, 13, 13, 16, 20]. Both divisions give minimal possible absolute difference.
Submitted Solution:
```
for i in range(int(input())):
n=int(input())
lis=[x for x in map(int,input().split())]
lis=sorted(lis)
print(lis[n]-lis[n-1])
``` | instruction | 0 | 69,064 | 17 | 138,128 |
Yes | output | 1 | 69,064 | 17 | 138,129 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Reminder: the [median](https://en.wikipedia.org/wiki/Median) of the array [a_1, a_2, ..., a_{2k+1}] of odd number of elements is defined as follows: let [b_1, b_2, ..., b_{2k+1}] be the elements of the array in the sorted order. Then median of this array is equal to b_{k+1}.
There are 2n students, the i-th student has skill level a_i. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the 2 classes such that each class has odd number of students (not divisible by 2). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10^4). The description of the test cases follows.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5) — the number of students halved.
The second line of each test case contains 2n integers a_1, a_2, ..., a_{2 n} (1 ≤ a_i ≤ 10^9) — skill levels of students.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
Example
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
Note
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be |1 - 1| = 0.
In the second test, one of the possible partitions is to make the first class of students with skill levels [6, 4, 2], so that the skill level of the first class will be 4, and second with [5, 1, 3], so that the skill level of the second class will be 3. Absolute difference will be |4 - 3| = 1.
Note that you can't assign like [2, 3], [6, 5, 4, 1] or [], [6, 5, 4, 1, 2, 3] because classes have even number of students.
[2], [1, 3, 4] is also not possible because students with skills 5 and 6 aren't assigned to a class.
In the third test you can assign the students in the following way: [3, 4, 13, 13, 20], [2, 5, 8, 16, 17] or [3, 8, 17], [2, 4, 5, 13, 13, 16, 20]. Both divisions give minimal possible absolute difference.
Submitted Solution:
```
import sys
import math
#to read string
get_string = lambda: sys.stdin.readline().strip()
#to read list of integers
get_int_list = lambda: list( map(int,sys.stdin.readline().strip().split()) )
#to read non spaced string and elements are integers to list of int
get_intList_from_str = lambda: list(map(int,list(sys.stdin.readline().strip())))
#to read non spaced string and elements are character to list of character
get_charList_from_str = lambda: list(sys.stdin.readline().strip())
#get word sepetared list of character
get_char_list = lambda: sys.stdin.readline().strip().split()
#to read integers
get_int = lambda: int(sys.stdin.readline())
#to print faster
pt = lambda x: sys.stdout.write(str(x))
#--------------------------------WhiteHat010--------------------------------#
for i in range(get_int()):
n = get_int()
lst = sorted(get_int_list())
i = n #middle element of sorted lst
j = n+1 #middle element of sorted lst
print(lst[j-1] - lst[i-1])
``` | instruction | 0 | 69,065 | 17 | 138,130 |
Yes | output | 1 | 69,065 | 17 | 138,131 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Reminder: the [median](https://en.wikipedia.org/wiki/Median) of the array [a_1, a_2, ..., a_{2k+1}] of odd number of elements is defined as follows: let [b_1, b_2, ..., b_{2k+1}] be the elements of the array in the sorted order. Then median of this array is equal to b_{k+1}.
There are 2n students, the i-th student has skill level a_i. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the 2 classes such that each class has odd number of students (not divisible by 2). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10^4). The description of the test cases follows.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5) — the number of students halved.
The second line of each test case contains 2n integers a_1, a_2, ..., a_{2 n} (1 ≤ a_i ≤ 10^9) — skill levels of students.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
Example
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
Note
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be |1 - 1| = 0.
In the second test, one of the possible partitions is to make the first class of students with skill levels [6, 4, 2], so that the skill level of the first class will be 4, and second with [5, 1, 3], so that the skill level of the second class will be 3. Absolute difference will be |4 - 3| = 1.
Note that you can't assign like [2, 3], [6, 5, 4, 1] or [], [6, 5, 4, 1, 2, 3] because classes have even number of students.
[2], [1, 3, 4] is also not possible because students with skills 5 and 6 aren't assigned to a class.
In the third test you can assign the students in the following way: [3, 4, 13, 13, 20], [2, 5, 8, 16, 17] or [3, 8, 17], [2, 4, 5, 13, 13, 16, 20]. Both divisions give minimal possible absolute difference.
Submitted Solution:
```
cases = int(input())
for i in range(cases):
line1 = int(input())
line2 = input().split(' ')
arr = []
for a in line2:
arr.append(int(a))
if (len(arr)== 2):
print(abs(arr[1]-arr[0]))
continue
arr.sort()
midleft = int(len(arr)/2)-1
midright = int(len(arr)/2)
med1 = arr[midleft]
med2 = arr[midright]
print (abs(med1-med2))
``` | instruction | 0 | 69,066 | 17 | 138,132 |
Yes | output | 1 | 69,066 | 17 | 138,133 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Reminder: the [median](https://en.wikipedia.org/wiki/Median) of the array [a_1, a_2, ..., a_{2k+1}] of odd number of elements is defined as follows: let [b_1, b_2, ..., b_{2k+1}] be the elements of the array in the sorted order. Then median of this array is equal to b_{k+1}.
There are 2n students, the i-th student has skill level a_i. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the 2 classes such that each class has odd number of students (not divisible by 2). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10^4). The description of the test cases follows.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5) — the number of students halved.
The second line of each test case contains 2n integers a_1, a_2, ..., a_{2 n} (1 ≤ a_i ≤ 10^9) — skill levels of students.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
Example
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
Note
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be |1 - 1| = 0.
In the second test, one of the possible partitions is to make the first class of students with skill levels [6, 4, 2], so that the skill level of the first class will be 4, and second with [5, 1, 3], so that the skill level of the second class will be 3. Absolute difference will be |4 - 3| = 1.
Note that you can't assign like [2, 3], [6, 5, 4, 1] or [], [6, 5, 4, 1, 2, 3] because classes have even number of students.
[2], [1, 3, 4] is also not possible because students with skills 5 and 6 aren't assigned to a class.
In the third test you can assign the students in the following way: [3, 4, 13, 13, 20], [2, 5, 8, 16, 17] or [3, 8, 17], [2, 4, 5, 13, 13, 16, 20]. Both divisions give minimal possible absolute difference.
Submitted Solution:
```
for _ in " "*int(input()):z=int(input());s=sorted(map(int,input().split()));print(abs(s[::2][z//2]-s[1::2][z//2]))
``` | instruction | 0 | 69,067 | 17 | 138,134 |
No | output | 1 | 69,067 | 17 | 138,135 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Reminder: the [median](https://en.wikipedia.org/wiki/Median) of the array [a_1, a_2, ..., a_{2k+1}] of odd number of elements is defined as follows: let [b_1, b_2, ..., b_{2k+1}] be the elements of the array in the sorted order. Then median of this array is equal to b_{k+1}.
There are 2n students, the i-th student has skill level a_i. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the 2 classes such that each class has odd number of students (not divisible by 2). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10^4). The description of the test cases follows.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5) — the number of students halved.
The second line of each test case contains 2n integers a_1, a_2, ..., a_{2 n} (1 ≤ a_i ≤ 10^9) — skill levels of students.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
Example
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
Note
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be |1 - 1| = 0.
In the second test, one of the possible partitions is to make the first class of students with skill levels [6, 4, 2], so that the skill level of the first class will be 4, and second with [5, 1, 3], so that the skill level of the second class will be 3. Absolute difference will be |4 - 3| = 1.
Note that you can't assign like [2, 3], [6, 5, 4, 1] or [], [6, 5, 4, 1, 2, 3] because classes have even number of students.
[2], [1, 3, 4] is also not possible because students with skills 5 and 6 aren't assigned to a class.
In the third test you can assign the students in the following way: [3, 4, 13, 13, 20], [2, 5, 8, 16, 17] or [3, 8, 17], [2, 4, 5, 13, 13, 16, 20]. Both divisions give minimal possible absolute difference.
Submitted Solution:
```
t = int(input())
while t!=0:
n = int(input())
list1 = list(map(int,input().split()))
list1.sort()
i = (n+1)//2-1
r = i*2
s = 1+i*2
print((list1[s]-list1[r]))
t-=1
``` | instruction | 0 | 69,068 | 17 | 138,136 |
No | output | 1 | 69,068 | 17 | 138,137 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Reminder: the [median](https://en.wikipedia.org/wiki/Median) of the array [a_1, a_2, ..., a_{2k+1}] of odd number of elements is defined as follows: let [b_1, b_2, ..., b_{2k+1}] be the elements of the array in the sorted order. Then median of this array is equal to b_{k+1}.
There are 2n students, the i-th student has skill level a_i. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the 2 classes such that each class has odd number of students (not divisible by 2). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10^4). The description of the test cases follows.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5) — the number of students halved.
The second line of each test case contains 2n integers a_1, a_2, ..., a_{2 n} (1 ≤ a_i ≤ 10^9) — skill levels of students.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
Example
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
Note
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be |1 - 1| = 0.
In the second test, one of the possible partitions is to make the first class of students with skill levels [6, 4, 2], so that the skill level of the first class will be 4, and second with [5, 1, 3], so that the skill level of the second class will be 3. Absolute difference will be |4 - 3| = 1.
Note that you can't assign like [2, 3], [6, 5, 4, 1] or [], [6, 5, 4, 1, 2, 3] because classes have even number of students.
[2], [1, 3, 4] is also not possible because students with skills 5 and 6 aren't assigned to a class.
In the third test you can assign the students in the following way: [3, 4, 13, 13, 20], [2, 5, 8, 16, 17] or [3, 8, 17], [2, 4, 5, 13, 13, 16, 20]. Both divisions give minimal possible absolute difference.
Submitted Solution:
```
import math
for i in range(int(input())):
k=int(input())
a=list(map(int,input().split()))
a.sort()
b=len(a)//2
c,d=[],[]
for i in range(0,len(a)-1,2):
c.append(a[i])
d.append(a[i+1])
k=len(c)//2
m=len(d)//2
print(abs(c[k]-d[m]))
``` | instruction | 0 | 69,069 | 17 | 138,138 |
No | output | 1 | 69,069 | 17 | 138,139 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Reminder: the [median](https://en.wikipedia.org/wiki/Median) of the array [a_1, a_2, ..., a_{2k+1}] of odd number of elements is defined as follows: let [b_1, b_2, ..., b_{2k+1}] be the elements of the array in the sorted order. Then median of this array is equal to b_{k+1}.
There are 2n students, the i-th student has skill level a_i. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the 2 classes such that each class has odd number of students (not divisible by 2). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10^4). The description of the test cases follows.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5) — the number of students halved.
The second line of each test case contains 2n integers a_1, a_2, ..., a_{2 n} (1 ≤ a_i ≤ 10^9) — skill levels of students.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
Example
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
Note
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be |1 - 1| = 0.
In the second test, one of the possible partitions is to make the first class of students with skill levels [6, 4, 2], so that the skill level of the first class will be 4, and second with [5, 1, 3], so that the skill level of the second class will be 3. Absolute difference will be |4 - 3| = 1.
Note that you can't assign like [2, 3], [6, 5, 4, 1] or [], [6, 5, 4, 1, 2, 3] because classes have even number of students.
[2], [1, 3, 4] is also not possible because students with skills 5 and 6 aren't assigned to a class.
In the third test you can assign the students in the following way: [3, 4, 13, 13, 20], [2, 5, 8, 16, 17] or [3, 8, 17], [2, 4, 5, 13, 13, 16, 20]. Both divisions give minimal possible absolute difference.
Submitted Solution:
```
def main():
_ = int(input())
for i in range(_):
n = int(input())
a = list(map(int, input().split()))
print(abs(a[n-1] - a[n]))
if __name__ == "__main__":
main()
``` | instruction | 0 | 69,070 | 17 | 138,140 |
No | output | 1 | 69,070 | 17 | 138,141 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Among other things, Bob is keen on photography. Especially he likes to take pictures of sportsmen. That was the reason why he placed himself in position x0 of a long straight racetrack and got ready to take pictures. But the problem was that not all the runners passed him. The total amount of sportsmen, training at that racetrack, equals n. And each of them regularly runs distances within a particular segment of the racetrack, which is the same for each sportsman. For example, the first sportsman runs from position a1 to position b1, the second — from a2 to b2
What is the minimum distance that Bob should move to have a chance to take pictures of each sportsman? Bob can take a picture of a sportsman, if he stands within the segment that this sportsman covers on the racetrack.
Input
The first line of the input file contains integers n and x0 (1 ≤ n ≤ 100; 0 ≤ x0 ≤ 1000). The following n lines contain pairs of integers ai, bi (0 ≤ ai, bi ≤ 1000; ai ≠ bi).
Output
Output the required minimum distance in the same units as the positions on the racetrack. If there is no such a position, output -1.
Examples
Input
3 3
0 7
14 2
4 6
Output
1 | instruction | 0 | 69,171 | 17 | 138,342 |
Tags: implementation
Correct Solution:
```
n,m = map(int,input().split())
pos,x = m,[]
for _ in range(n):
a,b = map(int,input().split())
x.append((min(a,b),max(a,b)))
x.sort(); l,h = x[0][0],x[0][1]
for j in x:
if j[0] > pos:
if j[0] <= h:
pos = j[0]
h = j[1] if j[1] < h else h
else: print(-1); exit()
elif j[1] < pos:
if j[1] >= l:
pos = j[1]
l = j[0] if j[0] > l else l
else: print(-1); exit()
print(abs(m-pos))
``` | output | 1 | 69,171 | 17 | 138,343 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Among other things, Bob is keen on photography. Especially he likes to take pictures of sportsmen. That was the reason why he placed himself in position x0 of a long straight racetrack and got ready to take pictures. But the problem was that not all the runners passed him. The total amount of sportsmen, training at that racetrack, equals n. And each of them regularly runs distances within a particular segment of the racetrack, which is the same for each sportsman. For example, the first sportsman runs from position a1 to position b1, the second — from a2 to b2
What is the minimum distance that Bob should move to have a chance to take pictures of each sportsman? Bob can take a picture of a sportsman, if he stands within the segment that this sportsman covers on the racetrack.
Input
The first line of the input file contains integers n and x0 (1 ≤ n ≤ 100; 0 ≤ x0 ≤ 1000). The following n lines contain pairs of integers ai, bi (0 ≤ ai, bi ≤ 1000; ai ≠ bi).
Output
Output the required minimum distance in the same units as the positions on the racetrack. If there is no such a position, output -1.
Examples
Input
3 3
0 7
14 2
4 6
Output
1 | instruction | 0 | 69,172 | 17 | 138,344 |
Tags: implementation
Correct Solution:
```
import sys
def input():
return sys.stdin.readline().strip()
def iinput():
return int(input())
def tinput():
return input().split()
def rinput():
return map(int, tinput())
def rlinput():
return list(rinput())
n,x0 = rinput()
left = []
right = []
for _ in range(n):
l,r = rinput()
left.append(min(l,r))
right.append(max(l,r))
left.sort(reverse = True)
right.sort()
m = left[0]
M = right[0]
if m > M:
print(-1)
elif x0 < m:
print(m-x0)
elif x0 >= m and x0 <= M:
print(0)
else:
print(x0-M)
``` | output | 1 | 69,172 | 17 | 138,345 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Among other things, Bob is keen on photography. Especially he likes to take pictures of sportsmen. That was the reason why he placed himself in position x0 of a long straight racetrack and got ready to take pictures. But the problem was that not all the runners passed him. The total amount of sportsmen, training at that racetrack, equals n. And each of them regularly runs distances within a particular segment of the racetrack, which is the same for each sportsman. For example, the first sportsman runs from position a1 to position b1, the second — from a2 to b2
What is the minimum distance that Bob should move to have a chance to take pictures of each sportsman? Bob can take a picture of a sportsman, if he stands within the segment that this sportsman covers on the racetrack.
Input
The first line of the input file contains integers n and x0 (1 ≤ n ≤ 100; 0 ≤ x0 ≤ 1000). The following n lines contain pairs of integers ai, bi (0 ≤ ai, bi ≤ 1000; ai ≠ bi).
Output
Output the required minimum distance in the same units as the positions on the racetrack. If there is no such a position, output -1.
Examples
Input
3 3
0 7
14 2
4 6
Output
1 | instruction | 0 | 69,173 | 17 | 138,346 |
Tags: implementation
Correct Solution:
```
n,k=map(int, input().split())
minn=-1; maxx=1001
for _ in range(n):
x,y=sorted(map(int, input().split()))
if x>minn: minn=x
if y<maxx: maxx=y
if minn<=k<=maxx: print(0)
elif maxx<minn: print(-1)
else: print(min(abs(minn-k), abs(k-maxx)))
``` | output | 1 | 69,173 | 17 | 138,347 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Among other things, Bob is keen on photography. Especially he likes to take pictures of sportsmen. That was the reason why he placed himself in position x0 of a long straight racetrack and got ready to take pictures. But the problem was that not all the runners passed him. The total amount of sportsmen, training at that racetrack, equals n. And each of them regularly runs distances within a particular segment of the racetrack, which is the same for each sportsman. For example, the first sportsman runs from position a1 to position b1, the second — from a2 to b2
What is the minimum distance that Bob should move to have a chance to take pictures of each sportsman? Bob can take a picture of a sportsman, if he stands within the segment that this sportsman covers on the racetrack.
Input
The first line of the input file contains integers n and x0 (1 ≤ n ≤ 100; 0 ≤ x0 ≤ 1000). The following n lines contain pairs of integers ai, bi (0 ≤ ai, bi ≤ 1000; ai ≠ bi).
Output
Output the required minimum distance in the same units as the positions on the racetrack. If there is no such a position, output -1.
Examples
Input
3 3
0 7
14 2
4 6
Output
1 | instruction | 0 | 69,175 | 17 | 138,350 |
Tags: implementation
Correct Solution:
```
# Getting Segment Count and initial Location.
count,loc = tuple(map(int,input().split(' ')))
# Method to determine if intersection is possible.
def empty_intersection(tOne,tTwo):
return True
# Method to Perform intersection of two segements.
def and_segments(t0,t1):
and_tuple = max(t0[0],t1[0]),min(t0[1],t1[1])
return and_tuple
# Alogrithm.
p_range = -1,1001
for i in range(count):
# Getting new tuple.
t = tuple(map(int,input().split(' ')))
t = min(t[0],t[1]),max(t[0],t[1])
# Anding Segments.
p_range = and_segments(p_range,t)
# Case no intersection.
if p_range[0]>p_range[1]:
min_dist = -1
# Case loc in range.
elif min(p_range)<=loc<=max(p_range):
min_dist = 0
# Case loc outside range.
else: min_dist = min(abs(p_range[0]-loc),abs(p_range[1]-loc))
# Verdict.
print(min_dist)
``` | output | 1 | 69,175 | 17 | 138,351 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Among other things, Bob is keen on photography. Especially he likes to take pictures of sportsmen. That was the reason why he placed himself in position x0 of a long straight racetrack and got ready to take pictures. But the problem was that not all the runners passed him. The total amount of sportsmen, training at that racetrack, equals n. And each of them regularly runs distances within a particular segment of the racetrack, which is the same for each sportsman. For example, the first sportsman runs from position a1 to position b1, the second — from a2 to b2
What is the minimum distance that Bob should move to have a chance to take pictures of each sportsman? Bob can take a picture of a sportsman, if he stands within the segment that this sportsman covers on the racetrack.
Input
The first line of the input file contains integers n and x0 (1 ≤ n ≤ 100; 0 ≤ x0 ≤ 1000). The following n lines contain pairs of integers ai, bi (0 ≤ ai, bi ≤ 1000; ai ≠ bi).
Output
Output the required minimum distance in the same units as the positions on the racetrack. If there is no such a position, output -1.
Examples
Input
3 3
0 7
14 2
4 6
Output
1 | instruction | 0 | 69,176 | 17 | 138,352 |
Tags: implementation
Correct Solution:
```
"""
Template written to be used by Python Programmers.
Use at your own risk!!!!
Owned by adi0311(rating - 5 star at CodeChef and Specialist at Codeforces).
"""
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nlargest, nsmallest, _heapify_max, _heapreplace_max
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque, Counter as c
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from fractions import Fraction
# sys.setrecursionlimit(2*pow(10, 6))
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
mod = pow(10, 9) + 7
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(var): sys.stdout.write(str(var))
def outln(var): sys.stdout.write(str(var)+"\n")
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]
n, xo = sp()
mat = []
for i in range(n):
mat.append(l())
answer = mod
for i in range(1001):
r = True
for j in mat:
if min(j[0], j[1]) <= i <= max(j[0], j[1]):
continue
r = False
break
if r:
answer = min(answer, abs(xo-i))
if answer == mod:
outln(-1)
exit()
outln(answer)
``` | output | 1 | 69,176 | 17 | 138,353 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Among other things, Bob is keen on photography. Especially he likes to take pictures of sportsmen. That was the reason why he placed himself in position x0 of a long straight racetrack and got ready to take pictures. But the problem was that not all the runners passed him. The total amount of sportsmen, training at that racetrack, equals n. And each of them regularly runs distances within a particular segment of the racetrack, which is the same for each sportsman. For example, the first sportsman runs from position a1 to position b1, the second — from a2 to b2
What is the minimum distance that Bob should move to have a chance to take pictures of each sportsman? Bob can take a picture of a sportsman, if he stands within the segment that this sportsman covers on the racetrack.
Input
The first line of the input file contains integers n and x0 (1 ≤ n ≤ 100; 0 ≤ x0 ≤ 1000). The following n lines contain pairs of integers ai, bi (0 ≤ ai, bi ≤ 1000; ai ≠ bi).
Output
Output the required minimum distance in the same units as the positions on the racetrack. If there is no such a position, output -1.
Examples
Input
3 3
0 7
14 2
4 6
Output
1 | instruction | 0 | 69,177 | 17 | 138,354 |
Tags: implementation
Correct Solution:
```
n,x=[int(x) for x in input().split()]
a,b=[int(x) for x in input().split()]
l,r=min(a,b),max(a,b)
# print(l,r)
f=0
for i in range(n-1):
# print(l,r)
a,b=[int(x) for x in input().split()]
l2,r2=min(a,b),max(a,b)
if r2<l or r<l2:
f=1
break
r=min(r,r2)
l=max(l,l2)
# print(l,r)
if f==1:
print(-1)
elif x<l:
print(l-x)
elif x>=l and x<=r:
print(0)
else:
print(x-r)
``` | output | 1 | 69,177 | 17 | 138,355 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Among other things, Bob is keen on photography. Especially he likes to take pictures of sportsmen. That was the reason why he placed himself in position x0 of a long straight racetrack and got ready to take pictures. But the problem was that not all the runners passed him. The total amount of sportsmen, training at that racetrack, equals n. And each of them regularly runs distances within a particular segment of the racetrack, which is the same for each sportsman. For example, the first sportsman runs from position a1 to position b1, the second — from a2 to b2
What is the minimum distance that Bob should move to have a chance to take pictures of each sportsman? Bob can take a picture of a sportsman, if he stands within the segment that this sportsman covers on the racetrack.
Input
The first line of the input file contains integers n and x0 (1 ≤ n ≤ 100; 0 ≤ x0 ≤ 1000). The following n lines contain pairs of integers ai, bi (0 ≤ ai, bi ≤ 1000; ai ≠ bi).
Output
Output the required minimum distance in the same units as the positions on the racetrack. If there is no such a position, output -1.
Examples
Input
3 3
0 7
14 2
4 6
Output
1 | instruction | 0 | 69,178 | 17 | 138,356 |
Tags: implementation
Correct Solution:
```
n,x=map(int,input().split())
li=[]
pos=[x]
flag=0
for i in range(n):
a=list(map(int,input().split()))
l,r=min(a[0],a[1]),max(a[0],a[1])
if l<=x and r>=x:
li.append([l,r])
elif x<l:
x=l
pos.append(x)
for i in li:
l,r=i
if x<l or r<x:
flag=1
break
li.append([l,r])
elif x>r:
x=r
pos.append(x)
for i in li:
l,r=i
if x<l or r<x:
flag=1
break
li.append([l,r])
if flag==1:
break
if flag==1:
print(-1)
else:
print(abs(pos[0]-pos[-1]))
``` | output | 1 | 69,178 | 17 | 138,357 |
Provide a correct Python 3 solution for this coding contest problem.
A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42 | instruction | 0 | 69,692 | 17 | 139,384 |
"Correct Solution:
```
n = int(input())
ma = 20
mv = 0
while n > 0:
a,v = map(int,input().split())
if mv < v:
ma,mv = a,v
elif mv == v and ma >= a:
ma,mv = a,v
n -= 1
print(ma,mv)
``` | output | 1 | 69,692 | 17 | 139,385 |
Provide a correct Python 3 solution for this coding contest problem.
A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42 | instruction | 0 | 69,693 | 17 | 139,386 |
"Correct Solution:
```
# AOJ 0095 Surf Smelt Fishing Contest
# Python3 2018.6.21 bal4u
n = int(input())
tbl = [0]*(n+1)
vmax = 0
for i in range(n):
a, v = list(map(int, input().split()))
tbl[a] += v
if tbl[a] > vmax: vmax = tbl[a]
for i in range(1, n+1):
if tbl[i] == vmax:
print(i, vmax)
break
``` | output | 1 | 69,693 | 17 | 139,387 |
Provide a correct Python 3 solution for this coding contest problem.
A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42 | instruction | 0 | 69,694 | 17 | 139,388 |
"Correct Solution:
```
from operator import itemgetter
n = int(input())
rs = [list(map(int,input().split())) for i in range(n)]
rs = sorted(rs,key=itemgetter(0))
print(' '.join(map(str, sorted(rs, key=itemgetter(1),reverse=True)[0])))
``` | output | 1 | 69,694 | 17 | 139,389 |
Provide a correct Python 3 solution for this coding contest problem.
A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42 | instruction | 0 | 69,695 | 17 | 139,390 |
"Correct Solution:
```
d={}
for _ in[0]*int(input()):
a,v=map(int,input().split())
d.setdefault(v,[])
d[v]+=[a]
m=max(d)
print(min(d[m]),m)
``` | output | 1 | 69,695 | 17 | 139,391 |
Provide a correct Python 3 solution for this coding contest problem.
A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42 | instruction | 0 | 69,696 | 17 | 139,392 |
"Correct Solution:
```
N = int(input())
maxA = -1
maxV = -1
for l in range(N):
a,v = [int(i) for i in input().split()]
if v > maxV:
maxV = v
maxA = a
elif v == maxV:
maxA = min(a, maxA)
print(maxA, maxV)
``` | output | 1 | 69,696 | 17 | 139,393 |
Provide a correct Python 3 solution for this coding contest problem.
A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42 | instruction | 0 | 69,697 | 17 | 139,394 |
"Correct Solution:
```
a={}
for i in range(int(input())):
b,c=map(int,input().split())
a[b]=c
d=max(a.items(),key=lambda x:x[1])
print(*d)
``` | output | 1 | 69,697 | 17 | 139,395 |
Provide a correct Python 3 solution for this coding contest problem.
A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42 | instruction | 0 | 69,698 | 17 | 139,396 |
"Correct Solution:
```
print(*sorted([list(map(int, input().split())) for _ in [0]*int(input())], key=lambda x:(-x[1],x[0]))[0])
``` | output | 1 | 69,698 | 17 | 139,397 |
Provide a correct Python 3 solution for this coding contest problem.
A smelt fishing tournament was held at Lake Hibara. The winner is the one who wins the most smelt.
Create a program that reads the list of participant numbers and the number of fish caught and outputs the number of winners and the number of fish caught. If there are multiple winners, output the one with the lowest participant number.
input
The input is given in the following format.
n
a1 v1
a2 v2
::
an vn
n (1 ≤ n ≤ 20) represents the number of participants and ai represents the participant number. Participant numbers are different integers between 1 and n. vi (0 ≤ vi ≤ 100) is the number of animals acquired by the participant ai.
output
Output the winner's participant number and the number of fish caught on one line separated by blanks.
Example
Input
6
1 14
2 25
3 42
4 11
5 40
6 37
Output
3 42 | instruction | 0 | 69,699 | 17 | 139,398 |
"Correct Solution:
```
# -*- coding: utf-8 -*-
import sys
import os
import math
N = int(input())
A = [None] * N
for i in range(N):
id, num = map(int, input().split())
id -= 1
A[id] = num
max_value = max(A)
max_index = A.index(max_value)
print(max_index + 1, max_value)
``` | output | 1 | 69,699 | 17 | 139,399 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a game called "Unique Bid Auction". You can read more about it here: https://en.wikipedia.org/wiki/Unique_bid_auction (though you don't have to do it to solve this problem).
Let's simplify this game a bit. Formally, there are n participants, the i-th participant chose the number a_i. The winner of the game is such a participant that the number he chose is unique (i. e. nobody else chose this number except him) and is minimal (i. e. among all unique values of a the minimum one is the winning one).
Your task is to find the index of the participant who won the game (or -1 if there is no winner). Indexing is 1-based, i. e. the participants are numbered from 1 to n.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 ≤ t ≤ 2 ⋅ 10^4) — the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of participants. The second line of the test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n), where a_i is the i-th participant chosen number.
It is guaranteed that the sum of n does not exceed 2 ⋅ 10^5 (∑ n ≤ 2 ⋅ 10^5).
Output
For each test case, print the answer — the index of the participant who won the game (or -1 if there is no winner). Note that the answer is always unique.
Example
Input
6
2
1 1
3
2 1 3
4
2 2 2 3
1
1
5
2 3 2 4 2
6
1 1 5 5 4 4
Output
-1
2
4
1
2
-1 | instruction | 0 | 70,060 | 17 | 140,120 |
Tags: implementation
Correct Solution:
```
# cook your dish here
for i in range(int(input())):
n=int(input())
l=list(map(int,input().split()))
l1=[0]*(n+1)
for j in range(n):
l1[l[j]]+=1
ans=0
for j in range(1,n+1):
if l1[j]==1:
ans=1
num=l.index(j)
break
if ans==0:
print(-1)
else:
print(num+1)
``` | output | 1 | 70,060 | 17 | 140,121 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a game called "Unique Bid Auction". You can read more about it here: https://en.wikipedia.org/wiki/Unique_bid_auction (though you don't have to do it to solve this problem).
Let's simplify this game a bit. Formally, there are n participants, the i-th participant chose the number a_i. The winner of the game is such a participant that the number he chose is unique (i. e. nobody else chose this number except him) and is minimal (i. e. among all unique values of a the minimum one is the winning one).
Your task is to find the index of the participant who won the game (or -1 if there is no winner). Indexing is 1-based, i. e. the participants are numbered from 1 to n.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 ≤ t ≤ 2 ⋅ 10^4) — the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of participants. The second line of the test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n), where a_i is the i-th participant chosen number.
It is guaranteed that the sum of n does not exceed 2 ⋅ 10^5 (∑ n ≤ 2 ⋅ 10^5).
Output
For each test case, print the answer — the index of the participant who won the game (or -1 if there is no winner). Note that the answer is always unique.
Example
Input
6
2
1 1
3
2 1 3
4
2 2 2 3
1
1
5
2 3 2 4 2
6
1 1 5 5 4 4
Output
-1
2
4
1
2
-1 | instruction | 0 | 70,061 | 17 | 140,122 |
Tags: implementation
Correct Solution:
```
for _ in range(int(input())):
n = int(input())
a = list(map(int,input().split()))
d = {i:0 for i in a}
for i in a:
if i not in d:
d[i] = 1
d[i] += 1
x = int(1e9)
for i,j in zip(d.keys(),d.values()):
if j == 1:
if i < x:
x = i
if x == int(1e9):
print(-1)
else:
print((a.index(x))+1)
``` | output | 1 | 70,061 | 17 | 140,123 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a game called "Unique Bid Auction". You can read more about it here: https://en.wikipedia.org/wiki/Unique_bid_auction (though you don't have to do it to solve this problem).
Let's simplify this game a bit. Formally, there are n participants, the i-th participant chose the number a_i. The winner of the game is such a participant that the number he chose is unique (i. e. nobody else chose this number except him) and is minimal (i. e. among all unique values of a the minimum one is the winning one).
Your task is to find the index of the participant who won the game (or -1 if there is no winner). Indexing is 1-based, i. e. the participants are numbered from 1 to n.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 ≤ t ≤ 2 ⋅ 10^4) — the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of participants. The second line of the test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n), where a_i is the i-th participant chosen number.
It is guaranteed that the sum of n does not exceed 2 ⋅ 10^5 (∑ n ≤ 2 ⋅ 10^5).
Output
For each test case, print the answer — the index of the participant who won the game (or -1 if there is no winner). Note that the answer is always unique.
Example
Input
6
2
1 1
3
2 1 3
4
2 2 2 3
1
1
5
2 3 2 4 2
6
1 1 5 5 4 4
Output
-1
2
4
1
2
-1 | instruction | 0 | 70,062 | 17 | 140,124 |
Tags: implementation
Correct Solution:
```
def main():
n = int(input())
counter = dict()
numbers = map(int, input().split())
for i, elem in enumerate(numbers):
if elem not in counter:
counter[elem] = i+1
else:
counter[elem] = None
result = sorted([(x, i) for (x, i) in counter.items() if i is not None], key=lambda x: x[0])
if result:
print(result[0][1])
else:
print(-1)
if __name__ == '__main__':
for _ in range(int(input())):
main()
``` | output | 1 | 70,062 | 17 | 140,125 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a game called "Unique Bid Auction". You can read more about it here: https://en.wikipedia.org/wiki/Unique_bid_auction (though you don't have to do it to solve this problem).
Let's simplify this game a bit. Formally, there are n participants, the i-th participant chose the number a_i. The winner of the game is such a participant that the number he chose is unique (i. e. nobody else chose this number except him) and is minimal (i. e. among all unique values of a the minimum one is the winning one).
Your task is to find the index of the participant who won the game (or -1 if there is no winner). Indexing is 1-based, i. e. the participants are numbered from 1 to n.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 ≤ t ≤ 2 ⋅ 10^4) — the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of participants. The second line of the test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n), where a_i is the i-th participant chosen number.
It is guaranteed that the sum of n does not exceed 2 ⋅ 10^5 (∑ n ≤ 2 ⋅ 10^5).
Output
For each test case, print the answer — the index of the participant who won the game (or -1 if there is no winner). Note that the answer is always unique.
Example
Input
6
2
1 1
3
2 1 3
4
2 2 2 3
1
1
5
2 3 2 4 2
6
1 1 5 5 4 4
Output
-1
2
4
1
2
-1 | instruction | 0 | 70,063 | 17 | 140,126 |
Tags: implementation
Correct Solution:
```
#from math import *
from bisect import *
from collections import *
from random import *
from decimal import *
from random import *
import sys
input=sys.stdin.readline
def inp():
return int(input())
def st():
return input().rstrip('\n')
def lis():
return list(map(int,input().split()))
def ma():
return map(int,input().split())
t=inp()
while(t):
t-=1
n=inp()
a=lis()
f=float('inf')
v=Counter(a)
for i in v.keys():
if(v[i]==1):
f=min(f,i)
if(f==float('inf')):
f=-1
else:
f=a.index(f)+1
print(f)
``` | output | 1 | 70,063 | 17 | 140,127 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a game called "Unique Bid Auction". You can read more about it here: https://en.wikipedia.org/wiki/Unique_bid_auction (though you don't have to do it to solve this problem).
Let's simplify this game a bit. Formally, there are n participants, the i-th participant chose the number a_i. The winner of the game is such a participant that the number he chose is unique (i. e. nobody else chose this number except him) and is minimal (i. e. among all unique values of a the minimum one is the winning one).
Your task is to find the index of the participant who won the game (or -1 if there is no winner). Indexing is 1-based, i. e. the participants are numbered from 1 to n.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 ≤ t ≤ 2 ⋅ 10^4) — the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of participants. The second line of the test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n), where a_i is the i-th participant chosen number.
It is guaranteed that the sum of n does not exceed 2 ⋅ 10^5 (∑ n ≤ 2 ⋅ 10^5).
Output
For each test case, print the answer — the index of the participant who won the game (or -1 if there is no winner). Note that the answer is always unique.
Example
Input
6
2
1 1
3
2 1 3
4
2 2 2 3
1
1
5
2 3 2 4 2
6
1 1 5 5 4 4
Output
-1
2
4
1
2
-1 | instruction | 0 | 70,064 | 17 | 140,128 |
Tags: implementation
Correct Solution:
```
from collections import Counter
def main():
for _ in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
d=dict(Counter(a))
l=[]
for i,j in enumerate(a):
if(d[j]==1):
l.append([j,i])
try:
print(min(l)[1]+1)
except :
print(-1)
main()
``` | output | 1 | 70,064 | 17 | 140,129 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.