inputs
stringlengths 50
14k
| targets
stringlengths 4
655k
|
---|---|
You are given a string $s$ of even length $n$. String $s$ is binary, in other words, consists only of 0's and 1's.
String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones ($n$ is even).
In one operation you can reverse any substring of $s$. A substring of a string is a contiguous subsequence of that string.
What is the minimum number of operations you need to make string $s$ alternating? A string is alternating if $s_i \neq s_{i + 1}$ for all $i$. There are two types of alternating strings in general: 01010101... or 10101010...
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^5$; $n$ is even)Β β the length of string $s$.
The second line of each test case contains a binary string $s$ of length $n$ ($s_i \in$ {0, 1}). String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the minimum number of operations to make $s$ alternating.
-----Example-----
Input
3
2
10
4
0110
8
11101000
Output
0
1
2
-----Note-----
In the first test case, string 10 is already alternating.
In the second test case, we can, for example, reverse the last two elements of $s$ and get: 0110 $\rightarrow$ 0101.
In the third test case, we can, for example, make the following two operations: 11101000 $\rightarrow$ 10101100; 10101100 $\rightarrow$ 10101010.
|
for _ in range(int(input())):
n = int(input())
s = input()
blocks = [[s[0], 1]]
for i in range(1, n):
if s[i] == blocks[-1][0]:
blocks[-1][1] += 1
else:
blocks += [[s[i], 1]]
one = 0
zero = 0
for i in range(len(blocks)):
if blocks[i][0] == '0':
zero += blocks[i][1] - 1
else:
one += blocks[i][1] - 1
print(max(one, zero))
|
You are given a string $s$ of even length $n$. String $s$ is binary, in other words, consists only of 0's and 1's.
String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones ($n$ is even).
In one operation you can reverse any substring of $s$. A substring of a string is a contiguous subsequence of that string.
What is the minimum number of operations you need to make string $s$ alternating? A string is alternating if $s_i \neq s_{i + 1}$ for all $i$. There are two types of alternating strings in general: 01010101... or 10101010...
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^5$; $n$ is even)Β β the length of string $s$.
The second line of each test case contains a binary string $s$ of length $n$ ($s_i \in$ {0, 1}). String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the minimum number of operations to make $s$ alternating.
-----Example-----
Input
3
2
10
4
0110
8
11101000
Output
0
1
2
-----Note-----
In the first test case, string 10 is already alternating.
In the second test case, we can, for example, reverse the last two elements of $s$ and get: 0110 $\rightarrow$ 0101.
In the third test case, we can, for example, make the following two operations: 11101000 $\rightarrow$ 10101100; 10101100 $\rightarrow$ 10101010.
|
for _ in range(int(input())):
n = int(input())
s = input()
l = list(s)
c0 = 0
c1 = 0
for i in range(n-1):
if(l[i] == l[i+1]):
if(l[i] == '0'):
c0 += 1
else:
c1 += 1
print(max(c0, c1))
|
You are given a string $s$ of even length $n$. String $s$ is binary, in other words, consists only of 0's and 1's.
String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones ($n$ is even).
In one operation you can reverse any substring of $s$. A substring of a string is a contiguous subsequence of that string.
What is the minimum number of operations you need to make string $s$ alternating? A string is alternating if $s_i \neq s_{i + 1}$ for all $i$. There are two types of alternating strings in general: 01010101... or 10101010...
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^5$; $n$ is even)Β β the length of string $s$.
The second line of each test case contains a binary string $s$ of length $n$ ($s_i \in$ {0, 1}). String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the minimum number of operations to make $s$ alternating.
-----Example-----
Input
3
2
10
4
0110
8
11101000
Output
0
1
2
-----Note-----
In the first test case, string 10 is already alternating.
In the second test case, we can, for example, reverse the last two elements of $s$ and get: 0110 $\rightarrow$ 0101.
In the third test case, we can, for example, make the following two operations: 11101000 $\rightarrow$ 10101100; 10101100 $\rightarrow$ 10101010.
|
t = int(input())
for _ in range(t):
n = int(input())
s = input()
S = 0
for j in range(1,len(s)):
if s[j-1]=='1' and s[j]=='1':
S+=1
if s[0]=='1' and s[-1]=='1' and len(s)>2:
S+=1
print(S)
|
You are given a string $s$ of even length $n$. String $s$ is binary, in other words, consists only of 0's and 1's.
String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones ($n$ is even).
In one operation you can reverse any substring of $s$. A substring of a string is a contiguous subsequence of that string.
What is the minimum number of operations you need to make string $s$ alternating? A string is alternating if $s_i \neq s_{i + 1}$ for all $i$. There are two types of alternating strings in general: 01010101... or 10101010...
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^5$; $n$ is even)Β β the length of string $s$.
The second line of each test case contains a binary string $s$ of length $n$ ($s_i \in$ {0, 1}). String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the minimum number of operations to make $s$ alternating.
-----Example-----
Input
3
2
10
4
0110
8
11101000
Output
0
1
2
-----Note-----
In the first test case, string 10 is already alternating.
In the second test case, we can, for example, reverse the last two elements of $s$ and get: 0110 $\rightarrow$ 0101.
In the third test case, we can, for example, make the following two operations: 11101000 $\rightarrow$ 10101100; 10101100 $\rightarrow$ 10101010.
|
import sys
input=sys.stdin.readline
T=int(input())
for _ in range(T):
n=int(input())
s=input()
ans1=0
ans2=0
i=0
while(i<n):
c=1
while (s[i]==s[i-1]):
c=c+1
i=i+1
if (s[i-1]=='1'):
ans2=ans2+c-1
else:
ans1=ans1+c-1
i=i+1
print(max(ans1,ans2))
|
You are given a string $s$ of even length $n$. String $s$ is binary, in other words, consists only of 0's and 1's.
String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones ($n$ is even).
In one operation you can reverse any substring of $s$. A substring of a string is a contiguous subsequence of that string.
What is the minimum number of operations you need to make string $s$ alternating? A string is alternating if $s_i \neq s_{i + 1}$ for all $i$. There are two types of alternating strings in general: 01010101... or 10101010...
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^5$; $n$ is even)Β β the length of string $s$.
The second line of each test case contains a binary string $s$ of length $n$ ($s_i \in$ {0, 1}). String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the minimum number of operations to make $s$ alternating.
-----Example-----
Input
3
2
10
4
0110
8
11101000
Output
0
1
2
-----Note-----
In the first test case, string 10 is already alternating.
In the second test case, we can, for example, reverse the last two elements of $s$ and get: 0110 $\rightarrow$ 0101.
In the third test case, we can, for example, make the following two operations: 11101000 $\rightarrow$ 10101100; 10101100 $\rightarrow$ 10101010.
|
t=int(input())
for i in range(t):
n=int(input())
s=input().strip()
o=0
z=0
for j in range(1,n):
if s[j]==s[j-1]:
if s[j]=='1':
o=o+1
else:
z=z+1
print(max(z,o))
|
You are given a string $s$ of even length $n$. String $s$ is binary, in other words, consists only of 0's and 1's.
String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones ($n$ is even).
In one operation you can reverse any substring of $s$. A substring of a string is a contiguous subsequence of that string.
What is the minimum number of operations you need to make string $s$ alternating? A string is alternating if $s_i \neq s_{i + 1}$ for all $i$. There are two types of alternating strings in general: 01010101... or 10101010...
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^5$; $n$ is even)Β β the length of string $s$.
The second line of each test case contains a binary string $s$ of length $n$ ($s_i \in$ {0, 1}). String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the minimum number of operations to make $s$ alternating.
-----Example-----
Input
3
2
10
4
0110
8
11101000
Output
0
1
2
-----Note-----
In the first test case, string 10 is already alternating.
In the second test case, we can, for example, reverse the last two elements of $s$ and get: 0110 $\rightarrow$ 0101.
In the third test case, we can, for example, make the following two operations: 11101000 $\rightarrow$ 10101100; 10101100 $\rightarrow$ 10101010.
|
import sys
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c for j in range(b)] for i in range(a)]
def list3d(a, b, c, d): return [[[d for k in range(c)] for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e for l in range(d)] for k in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return list(map(int, input().split()))
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10**19
MOD = 10**9 + 7
EPS = 10**-10
def RLE(data):
from itertools import groupby
return [(x, len(list(grp))) for x, grp in groupby(data)]
def check(S, T):
A = [0] * N
for i in range(N):
if S[i] != T[i]:
A[i] = 1
rle = RLE(A)
cnt = 0
for x, _ in rle:
if x:
cnt += 1
return cnt
for _ in range(INT()):
N = INT()
S = input()
T1 = '01' * (N//2)
T2 = '10' * (N//2)
ans = min(check(S, T1), check(S, T2))
print(ans)
|
You are given a string $s$ of even length $n$. String $s$ is binary, in other words, consists only of 0's and 1's.
String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones ($n$ is even).
In one operation you can reverse any substring of $s$. A substring of a string is a contiguous subsequence of that string.
What is the minimum number of operations you need to make string $s$ alternating? A string is alternating if $s_i \neq s_{i + 1}$ for all $i$. There are two types of alternating strings in general: 01010101... or 10101010...
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^5$; $n$ is even)Β β the length of string $s$.
The second line of each test case contains a binary string $s$ of length $n$ ($s_i \in$ {0, 1}). String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the minimum number of operations to make $s$ alternating.
-----Example-----
Input
3
2
10
4
0110
8
11101000
Output
0
1
2
-----Note-----
In the first test case, string 10 is already alternating.
In the second test case, we can, for example, reverse the last two elements of $s$ and get: 0110 $\rightarrow$ 0101.
In the third test case, we can, for example, make the following two operations: 11101000 $\rightarrow$ 10101100; 10101100 $\rightarrow$ 10101010.
|
import sys
input = sys.stdin.readline
t=int(input())
for tests in range(t):
n=int(input())
S=input().strip()
A=0
for i in range(1,n):
if S[i]==S[i-1]:
A+=1
print((A+1)//2)
|
You are given a string $s$ of even length $n$. String $s$ is binary, in other words, consists only of 0's and 1's.
String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones ($n$ is even).
In one operation you can reverse any substring of $s$. A substring of a string is a contiguous subsequence of that string.
What is the minimum number of operations you need to make string $s$ alternating? A string is alternating if $s_i \neq s_{i + 1}$ for all $i$. There are two types of alternating strings in general: 01010101... or 10101010...
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^5$; $n$ is even)Β β the length of string $s$.
The second line of each test case contains a binary string $s$ of length $n$ ($s_i \in$ {0, 1}). String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the minimum number of operations to make $s$ alternating.
-----Example-----
Input
3
2
10
4
0110
8
11101000
Output
0
1
2
-----Note-----
In the first test case, string 10 is already alternating.
In the second test case, we can, for example, reverse the last two elements of $s$ and get: 0110 $\rightarrow$ 0101.
In the third test case, we can, for example, make the following two operations: 11101000 $\rightarrow$ 10101100; 10101100 $\rightarrow$ 10101010.
|
for irjfr in range(int(input())):
input()
s = input()
res = int(s[0] == s[-1] == '1')
for i in range(len(s) - 1):
res += int(s[i] == s[i + 1] == '1')
print(res)
|
You are given a string $s$ of even length $n$. String $s$ is binary, in other words, consists only of 0's and 1's.
String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones ($n$ is even).
In one operation you can reverse any substring of $s$. A substring of a string is a contiguous subsequence of that string.
What is the minimum number of operations you need to make string $s$ alternating? A string is alternating if $s_i \neq s_{i + 1}$ for all $i$. There are two types of alternating strings in general: 01010101... or 10101010...
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^5$; $n$ is even)Β β the length of string $s$.
The second line of each test case contains a binary string $s$ of length $n$ ($s_i \in$ {0, 1}). String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the minimum number of operations to make $s$ alternating.
-----Example-----
Input
3
2
10
4
0110
8
11101000
Output
0
1
2
-----Note-----
In the first test case, string 10 is already alternating.
In the second test case, we can, for example, reverse the last two elements of $s$ and get: 0110 $\rightarrow$ 0101.
In the third test case, we can, for example, make the following two operations: 11101000 $\rightarrow$ 10101100; 10101100 $\rightarrow$ 10101010.
|
for _ in range(int(input())):
n = int(input())
s = input()
ans1 = 0
ans2 = 0
for i in range(n - 1):
if s[i] == s[i + 1]:
if s[i] == '0':
ans1 += 1
else:
ans2 += 1
print(max(ans1, ans2))
|
You are given a string $s$ of even length $n$. String $s$ is binary, in other words, consists only of 0's and 1's.
String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones ($n$ is even).
In one operation you can reverse any substring of $s$. A substring of a string is a contiguous subsequence of that string.
What is the minimum number of operations you need to make string $s$ alternating? A string is alternating if $s_i \neq s_{i + 1}$ for all $i$. There are two types of alternating strings in general: 01010101... or 10101010...
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^5$; $n$ is even)Β β the length of string $s$.
The second line of each test case contains a binary string $s$ of length $n$ ($s_i \in$ {0, 1}). String $s$ has exactly $\frac{n}{2}$ zeroes and $\frac{n}{2}$ ones.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $10^5$.
-----Output-----
For each test case, print the minimum number of operations to make $s$ alternating.
-----Example-----
Input
3
2
10
4
0110
8
11101000
Output
0
1
2
-----Note-----
In the first test case, string 10 is already alternating.
In the second test case, we can, for example, reverse the last two elements of $s$ and get: 0110 $\rightarrow$ 0101.
In the third test case, we can, for example, make the following two operations: 11101000 $\rightarrow$ 10101100; 10101100 $\rightarrow$ 10101010.
|
import math
for _ in range(int(input())):
n = int(input())
s = input()
r = 0
for i in range(1,n):
if s[i-1] != s[i]:
continue
else:
r+=1
print(math.ceil(r/2))
|
Skier rides on a snowy field. Its movements can be described by a string of characters 'S', 'N', 'W', 'E' (which correspond to $1$ meter movement in the south, north, west or east direction respectively).
It is known that if he moves along a previously unvisited segment of a path (i.e. this segment of the path is visited the first time), then the time of such movement is $5$ seconds. If he rolls along previously visited segment of a path (i.e., this segment of the path has been covered by his path before), then it takes $1$ second.
Find the skier's time to roll all the path.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
Each set is given by one nonempty string of the characters 'S', 'N', 'W', 'E'. The length of the string does not exceed $10^5$ characters.
The sum of the lengths of $t$ given lines over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the desired path time in seconds.
-----Example-----
Input
5
NNN
NS
WWEN
WWEE
NWNWS
Output
15
6
16
12
25
|
DIR = {"N": (0, 1), "S": (0, -1), "W": (-1, 0), "E": (1, 0)}
for t in range(int(input())):
path = input()
tracks = set()
x, y = 0, 0
time = 0
for char in path:
x1 = x + DIR[char][0]
y1 = y + DIR[char][1]
if (x, y, x1, y1) in tracks or (x1, y1, x, y) in tracks:
time += 1
else:
time += 5
tracks.add((x, y, x1, y1))
x, y = x1, y1
print(time)
|
Skier rides on a snowy field. Its movements can be described by a string of characters 'S', 'N', 'W', 'E' (which correspond to $1$ meter movement in the south, north, west or east direction respectively).
It is known that if he moves along a previously unvisited segment of a path (i.e. this segment of the path is visited the first time), then the time of such movement is $5$ seconds. If he rolls along previously visited segment of a path (i.e., this segment of the path has been covered by his path before), then it takes $1$ second.
Find the skier's time to roll all the path.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
Each set is given by one nonempty string of the characters 'S', 'N', 'W', 'E'. The length of the string does not exceed $10^5$ characters.
The sum of the lengths of $t$ given lines over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the desired path time in seconds.
-----Example-----
Input
5
NNN
NS
WWEN
WWEE
NWNWS
Output
15
6
16
12
25
|
import sys
INF = 10**20
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))
from math import gcd
from math import ceil
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br
t, = I()
while t:
t -= 1
s = input()
x, y = 0, 0
d = {'N': [0, 1], 'S': [0, -1], 'E': [1, 0], 'W': [-1, 0]}
ans = 0
v = dd(int)
for i in s:
a, b = x + d[i][0], y + d[i][1]
if (x, y, a, b) in v:
ans += 1
else:
ans += 5
v[(x, y, a, b)] = v[(a, b, x, y)] = 1
x, y = a, b
print(ans)
|
Skier rides on a snowy field. Its movements can be described by a string of characters 'S', 'N', 'W', 'E' (which correspond to $1$ meter movement in the south, north, west or east direction respectively).
It is known that if he moves along a previously unvisited segment of a path (i.e. this segment of the path is visited the first time), then the time of such movement is $5$ seconds. If he rolls along previously visited segment of a path (i.e., this segment of the path has been covered by his path before), then it takes $1$ second.
Find the skier's time to roll all the path.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
Each set is given by one nonempty string of the characters 'S', 'N', 'W', 'E'. The length of the string does not exceed $10^5$ characters.
The sum of the lengths of $t$ given lines over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the desired path time in seconds.
-----Example-----
Input
5
NNN
NS
WWEN
WWEE
NWNWS
Output
15
6
16
12
25
|
def read_int():
return int(input())
def read_ints():
return list(map(int, input().split(' ')))
t = read_int()
for case_num in range(t):
path = input()
pos = (0, 0)
ans = 0
use = set()
d = {'N': (0, 1), 'S': (0, -1), 'W': (-1, 0), 'E': (1, 0)}
for c in path:
ci, cj = pos
di, dj = d[c]
ni, nj = ci + di, cj + dj
pos = (ni, nj)
if ((ci, cj), (ni, nj)) in use:
ans += 1
else:
ans += 5
use.add(((ci, cj), (ni, nj)))
use.add(((ni, nj), (ci, cj)))
print(ans)
|
Skier rides on a snowy field. Its movements can be described by a string of characters 'S', 'N', 'W', 'E' (which correspond to $1$ meter movement in the south, north, west or east direction respectively).
It is known that if he moves along a previously unvisited segment of a path (i.e. this segment of the path is visited the first time), then the time of such movement is $5$ seconds. If he rolls along previously visited segment of a path (i.e., this segment of the path has been covered by his path before), then it takes $1$ second.
Find the skier's time to roll all the path.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
Each set is given by one nonempty string of the characters 'S', 'N', 'W', 'E'. The length of the string does not exceed $10^5$ characters.
The sum of the lengths of $t$ given lines over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the desired path time in seconds.
-----Example-----
Input
5
NNN
NS
WWEN
WWEE
NWNWS
Output
15
6
16
12
25
|
t=int(input())
for _ in range(t):
s=input()
aa={}
i=0
j=0
ans=0
for k in s:
if(k=="N"):
try:
x=aa[((i,j),(i,j-1))]
ans+=1
except:
ans+=5
aa[((i,j),(i,j-1))]=1
j-=1
elif(k=="E"):
try:
x=aa[((i+1,j),(i,j))]
ans+=1
except:
ans+=5
aa[((i+1,j),(i,j))]=1
i+=1
elif(k=="W"):
try:
x=aa[((i,j),(i-1,j))]
ans+=1
except:
ans+=5
aa[((i,j),(i-1,j))]=1
i-=1
else:
try:
x=aa[((i,j+1),(i,j))]
ans+=1
except:
ans+=5
aa[((i,j+1),(i,j))]=1
j+=1
print(ans)
|
Skier rides on a snowy field. Its movements can be described by a string of characters 'S', 'N', 'W', 'E' (which correspond to $1$ meter movement in the south, north, west or east direction respectively).
It is known that if he moves along a previously unvisited segment of a path (i.e. this segment of the path is visited the first time), then the time of such movement is $5$ seconds. If he rolls along previously visited segment of a path (i.e., this segment of the path has been covered by his path before), then it takes $1$ second.
Find the skier's time to roll all the path.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
Each set is given by one nonempty string of the characters 'S', 'N', 'W', 'E'. The length of the string does not exceed $10^5$ characters.
The sum of the lengths of $t$ given lines over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the desired path time in seconds.
-----Example-----
Input
5
NNN
NS
WWEN
WWEE
NWNWS
Output
15
6
16
12
25
|
dir = {
'N': (0, 1),
'E': (1, 0),
'W': (-1, 0),
'S': (0, -1),
}
for tc in range(int(input())):
cur, ans, vis = (0, 0), 0, set()
for c in input():
nxt = (cur[0] + dir[c][0], cur[1] + dir[c][1])
if (cur, nxt) in vis:
ans += 1
else:
ans += 5
vis.add((cur, nxt))
vis.add((nxt, cur))
cur = nxt
print(ans)
|
Skier rides on a snowy field. Its movements can be described by a string of characters 'S', 'N', 'W', 'E' (which correspond to $1$ meter movement in the south, north, west or east direction respectively).
It is known that if he moves along a previously unvisited segment of a path (i.e. this segment of the path is visited the first time), then the time of such movement is $5$ seconds. If he rolls along previously visited segment of a path (i.e., this segment of the path has been covered by his path before), then it takes $1$ second.
Find the skier's time to roll all the path.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
Each set is given by one nonempty string of the characters 'S', 'N', 'W', 'E'. The length of the string does not exceed $10^5$ characters.
The sum of the lengths of $t$ given lines over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the desired path time in seconds.
-----Example-----
Input
5
NNN
NS
WWEN
WWEE
NWNWS
Output
15
6
16
12
25
|
t=int(input())
for tests in range(t):
S=input().strip()
ANS=0
Already=set()
X=0
Y=0
for s in S:
if s=="N":
if (X,Y,X,Y+1) in Already:
ANS+=1
else:
ANS+=5
Already.add((X,Y,X,Y+1))
Already.add((X,Y+1,X,Y))
Y+=1
elif s=="S":
if (X,Y,X,Y-1) in Already:
ANS+=1
else:
ANS+=5
Already.add((X,Y,X,Y-1))
Already.add((X,Y-1,X,Y))
Y-=1
elif s=="W":
if (X,Y,X-1,Y) in Already:
ANS+=1
else:
ANS+=5
Already.add((X,Y,X-1,Y))
Already.add((X-1,Y,X,Y))
X-=1
else:
if (X,Y,X+1,Y) in Already:
ANS+=1
else:
ANS+=5
Already.add((X,Y,X+1,Y))
Already.add((X+1,Y,X,Y))
X+=1
print(ANS)
|
Skier rides on a snowy field. Its movements can be described by a string of characters 'S', 'N', 'W', 'E' (which correspond to $1$ meter movement in the south, north, west or east direction respectively).
It is known that if he moves along a previously unvisited segment of a path (i.e. this segment of the path is visited the first time), then the time of such movement is $5$ seconds. If he rolls along previously visited segment of a path (i.e., this segment of the path has been covered by his path before), then it takes $1$ second.
Find the skier's time to roll all the path.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
Each set is given by one nonempty string of the characters 'S', 'N', 'W', 'E'. The length of the string does not exceed $10^5$ characters.
The sum of the lengths of $t$ given lines over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the desired path time in seconds.
-----Example-----
Input
5
NNN
NS
WWEN
WWEE
NWNWS
Output
15
6
16
12
25
|
for _ in range(int(input())):
s = input()
se = set()
total = 0
curr = [0, 0]
for e in s:
seg = ()
if e == "E":
seg = (curr[0], curr[1], 0)
curr[0] += 1
elif e == "N":
seg = (curr[0], curr[1], 1)
curr[1] += 1
elif e == "W":
seg = (curr[0]-1, curr[1], 0)
curr[0] -= 1
elif e == "S":
seg = (curr[0], curr[1]-1, 1)
curr[1] -= 1
if seg in se:
total += 1
else:
total += 5
se.add(seg)
print(total)
|
Skier rides on a snowy field. Its movements can be described by a string of characters 'S', 'N', 'W', 'E' (which correspond to $1$ meter movement in the south, north, west or east direction respectively).
It is known that if he moves along a previously unvisited segment of a path (i.e. this segment of the path is visited the first time), then the time of such movement is $5$ seconds. If he rolls along previously visited segment of a path (i.e., this segment of the path has been covered by his path before), then it takes $1$ second.
Find the skier's time to roll all the path.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
Each set is given by one nonempty string of the characters 'S', 'N', 'W', 'E'. The length of the string does not exceed $10^5$ characters.
The sum of the lengths of $t$ given lines over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the desired path time in seconds.
-----Example-----
Input
5
NNN
NS
WWEN
WWEE
NWNWS
Output
15
6
16
12
25
|
def list_int(): return list(map(int, input().split()))
def int_in(): return int(input())
def map_in(): return list(map(int, input().split()))
def list_in(): return input().split()
t=int_in()
for _ in range(t):
v=set()
s=input()
x=0
y=0
c=0
for i in s:
#print(v, x, y, i)
if i=='N':
if (x,y,x+1,y) in v:
c+=1
elif (x+1,y, x,y) in v:
c+=1
else:
c+=5
v.add((x,y,x+1,y))
x+=1
elif i=='S':
if (x,y,x-1,y) in v:
c+=1
elif (x-1,y, x,y) in v:
c+=1
else:
c+=5
v.add((x,y,x-1,y))
x-=1
elif i=='W':
if (x,y,x,y+1) in v:
c+=1
elif (x, y+1, x,y) in v:
c+=1
else:
c+=5
v.add((x,y,x,y+1))
y+=1
else:
if (x,y,x,y-1) in v:
c+=1
elif (x, y-1, x,y) in v:
c+=1
else:
c+=5
v.add((x,y,x,y-1))
y-=1
print(c)
|
Skier rides on a snowy field. Its movements can be described by a string of characters 'S', 'N', 'W', 'E' (which correspond to $1$ meter movement in the south, north, west or east direction respectively).
It is known that if he moves along a previously unvisited segment of a path (i.e. this segment of the path is visited the first time), then the time of such movement is $5$ seconds. If he rolls along previously visited segment of a path (i.e., this segment of the path has been covered by his path before), then it takes $1$ second.
Find the skier's time to roll all the path.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
Each set is given by one nonempty string of the characters 'S', 'N', 'W', 'E'. The length of the string does not exceed $10^5$ characters.
The sum of the lengths of $t$ given lines over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the desired path time in seconds.
-----Example-----
Input
5
NNN
NS
WWEN
WWEE
NWNWS
Output
15
6
16
12
25
|
import sys
import heapq as hq
readline = sys.stdin.readline
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: list(map(int, readline().split()))
nl = lambda: list(map(int, readline().split()))
# eps = 10**-7
def solve():
s = ns()
d = dict()
cnt = 0
g = [(0, 1), (1, 0), (-1, 0), (0, -1)]
cur = (0, 0)
d[cur] = ''
for x in s:
for i in range(4):
if x == 'NEWS'[i]:
nx = (cur[0] + g[i][0], cur[1] + g[i][1])
if nx in d and x in d[cur]:
cnt += 1
else:
cnt += 5
if nx not in d:
d[nx] = ''
d[nx] += 'NEWS'[3-i]
d[cur] += x
cur = nx
break
print(cnt)
# solve()
T = ni()
for _ in range(T):
solve()
|
Skier rides on a snowy field. Its movements can be described by a string of characters 'S', 'N', 'W', 'E' (which correspond to $1$ meter movement in the south, north, west or east direction respectively).
It is known that if he moves along a previously unvisited segment of a path (i.e. this segment of the path is visited the first time), then the time of such movement is $5$ seconds. If he rolls along previously visited segment of a path (i.e., this segment of the path has been covered by his path before), then it takes $1$ second.
Find the skier's time to roll all the path.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
Each set is given by one nonempty string of the characters 'S', 'N', 'W', 'E'. The length of the string does not exceed $10^5$ characters.
The sum of the lengths of $t$ given lines over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the desired path time in seconds.
-----Example-----
Input
5
NNN
NS
WWEN
WWEE
NWNWS
Output
15
6
16
12
25
|
for __ in range(int(input())):
s=input()
x=0
y=0
ans=0
d={}
for i in range(len(s)):
if(s[i]=='N'):
if(d.get((x,y,x,y+1))==None):
ans+=5
d[(x,y,x,y+1)]=1
d[(x,y+1,x,y)]=1
else:
ans+=1
y=y+1
elif(s[i]=='S'):
if(d.get((x,y,x,y-1))==None):
ans+=5
d[(x,y,x,y-1)]=1
d[(x,y-1,x,y)]=1
else:
ans+=1
y=y-1
elif(s[i]=='W'):
if(d.get((x,y,x-1,y))==None):
ans+=5
d[(x,y,x-1,y)]=1
d[(x-1,y,x,y)]=1
else:
ans+=1
x=x-1
else:
if(d.get((x,y,x+1,y))==None):
ans+=5
d[(x,y,x+1,y)]=1
d[(x+1,y,x,y)]=1
else:
ans+=1
x=x+1
print(ans)
|
Skier rides on a snowy field. Its movements can be described by a string of characters 'S', 'N', 'W', 'E' (which correspond to $1$ meter movement in the south, north, west or east direction respectively).
It is known that if he moves along a previously unvisited segment of a path (i.e. this segment of the path is visited the first time), then the time of such movement is $5$ seconds. If he rolls along previously visited segment of a path (i.e., this segment of the path has been covered by his path before), then it takes $1$ second.
Find the skier's time to roll all the path.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
Each set is given by one nonempty string of the characters 'S', 'N', 'W', 'E'. The length of the string does not exceed $10^5$ characters.
The sum of the lengths of $t$ given lines over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the desired path time in seconds.
-----Example-----
Input
5
NNN
NS
WWEN
WWEE
NWNWS
Output
15
6
16
12
25
|
# alpha = "abcdefghijklmnopqrstuvwxyz"
# prime = 1000000007#998244353
# INF = 10000
# from sys import stdout
# from heapq import heappush, heappop
# from collections import defaultdict
# from collections import deque
# import bisect
# from math import sqrt
# from math import gcd
# from math import log2
# with open('input.in','r') as Reader:
# with open('output.out','w') as out:
# n = int(Reader.readline())
# print(len(arr))
# print(arr[:10])
t = int(input())
for test in range(t):
# n = int(input())
# n, m = list(map(int, input().split()))
# n2, m2 = list(map(int, input().split()))
s = input()
v = set()
start = 0
ans = 0
cur = [0, 0, 0, 0]
for i in s:
if i=="N":
cur[2] += 1
elif i=="S":
cur[2] -= 1
elif i == "E":
cur[3] += 1
else:
cur[3] -= 1
key1 = str(cur)
key2 = str([cur[2],cur[3], cur[0], cur[1]])
if key1 in v:
ans += 1
else:
ans += 5
v.add(key1)
v.add(key2)
cur[0] = cur[2]
cur[1] = cur[3]
print(ans)
|
Skier rides on a snowy field. Its movements can be described by a string of characters 'S', 'N', 'W', 'E' (which correspond to $1$ meter movement in the south, north, west or east direction respectively).
It is known that if he moves along a previously unvisited segment of a path (i.e. this segment of the path is visited the first time), then the time of such movement is $5$ seconds. If he rolls along previously visited segment of a path (i.e., this segment of the path has been covered by his path before), then it takes $1$ second.
Find the skier's time to roll all the path.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
Each set is given by one nonempty string of the characters 'S', 'N', 'W', 'E'. The length of the string does not exceed $10^5$ characters.
The sum of the lengths of $t$ given lines over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the desired path time in seconds.
-----Example-----
Input
5
NNN
NS
WWEN
WWEE
NWNWS
Output
15
6
16
12
25
|
def new_pos(pos, step):
if step == "N":
pos = pos[0], pos[1] + 1
elif step == "S":
pos = pos[0], pos[1] -1
elif step == "W":
pos = pos[0] + 1, pos[1]
else:
pos = pos[0] -1, pos[1]
return pos
t = int(input())
for _ in range(t):
ans = 0
s = input()
used_hor = set()
used_ver = set()
pos = (0, 0)
n = len(s)
for i in range(n):
next_st = new_pos(pos, s[i])
way = (min(pos[0], next_st[0]), min(pos[1], next_st[1]))
if s[i] == "N" or s[i] == "S":
if way in used_ver:
ans += 1
else:
ans += 5
used_ver.add(way)
else:
if way in used_hor:
ans += 1
else:
ans += 5
used_hor.add(way)
pos = next_st
# print("used_hor", used_hor)
# print("used_ver", used_ver)
print(ans)
|
Skier rides on a snowy field. Its movements can be described by a string of characters 'S', 'N', 'W', 'E' (which correspond to $1$ meter movement in the south, north, west or east direction respectively).
It is known that if he moves along a previously unvisited segment of a path (i.e. this segment of the path is visited the first time), then the time of such movement is $5$ seconds. If he rolls along previously visited segment of a path (i.e., this segment of the path has been covered by his path before), then it takes $1$ second.
Find the skier's time to roll all the path.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
Each set is given by one nonempty string of the characters 'S', 'N', 'W', 'E'. The length of the string does not exceed $10^5$ characters.
The sum of the lengths of $t$ given lines over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the desired path time in seconds.
-----Example-----
Input
5
NNN
NS
WWEN
WWEE
NWNWS
Output
15
6
16
12
25
|
import sys
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return list(map(int, input().split()))
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
for _ in range(INT()):
S = input()
se = set()
h = w = 0
ans = 0
for s in S:
prev = (h, w)
if s == 'S':
h += 1
elif s == 'N':
h -= 1
elif s == 'W':
w -= 1
else:
w += 1
cur = (h, w)
key = (min(prev, cur), max(prev, cur))
if key in se:
ans += 1
else:
ans += 5
se.add(key)
print(ans)
|
Skier rides on a snowy field. Its movements can be described by a string of characters 'S', 'N', 'W', 'E' (which correspond to $1$ meter movement in the south, north, west or east direction respectively).
It is known that if he moves along a previously unvisited segment of a path (i.e. this segment of the path is visited the first time), then the time of such movement is $5$ seconds. If he rolls along previously visited segment of a path (i.e., this segment of the path has been covered by his path before), then it takes $1$ second.
Find the skier's time to roll all the path.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
Each set is given by one nonempty string of the characters 'S', 'N', 'W', 'E'. The length of the string does not exceed $10^5$ characters.
The sum of the lengths of $t$ given lines over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the desired path time in seconds.
-----Example-----
Input
5
NNN
NS
WWEN
WWEE
NWNWS
Output
15
6
16
12
25
|
t = int(input())
d = {'E': (1, 0), 'W':(-1, 0), 'N':(0, 1), 'S':(0, -1)}
for _ in range(t):
s = input()
time = 0
met = set()
x = y = 0
for c in s:
dx, dy = d[c]
xx = x + dx
yy = y + dy
if (x, y, xx, yy) in met or (xx, yy, x, y) in met:
time += 1
else:
time += 5
met.add((x, y, xx, yy))
x = xx
y = yy
print(time)
|
Skier rides on a snowy field. Its movements can be described by a string of characters 'S', 'N', 'W', 'E' (which correspond to $1$ meter movement in the south, north, west or east direction respectively).
It is known that if he moves along a previously unvisited segment of a path (i.e. this segment of the path is visited the first time), then the time of such movement is $5$ seconds. If he rolls along previously visited segment of a path (i.e., this segment of the path has been covered by his path before), then it takes $1$ second.
Find the skier's time to roll all the path.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
Each set is given by one nonempty string of the characters 'S', 'N', 'W', 'E'. The length of the string does not exceed $10^5$ characters.
The sum of the lengths of $t$ given lines over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the desired path time in seconds.
-----Example-----
Input
5
NNN
NS
WWEN
WWEE
NWNWS
Output
15
6
16
12
25
|
import sys,bisect,string,math,time,functools,random
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
def Golf():*a,=map(int,open(0))
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def GI(V,E,ls=None,Directed=False,index=1):
org_inp=[];g=[[] for i in range(V)]
FromStdin=True if ls==None else False
for i in range(E):
if FromStdin:
inp=LI()
org_inp.append(inp)
else:
inp=ls[i]
if len(inp)==2:
a,b=inp;c=1
else:
a,b,c=inp
if index==1:a-=1;b-=1
aa=(a,c);bb=(b,c);g[a].append(bb)
if not Directed:g[b].append(aa)
return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1):
#h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0}) # sample usage
mp=[boundary]*(w+2);found={}
for i in range(h):
s=input()
for char in search:
if char in s:
found[char]=((i+1)*(w+2)+s.index(char)+1)
mp_def[char]=mp_def[replacement_of_found]
mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
mp+=[boundary]*(w+2)
return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def bit_combination(k,n=2):
rt=[]
for tb in range(n**k):
s=[tb//(n**bt)%n for bt in range(k)];rt+=[s]
return rt
def show(*inp,end='\n'):
if show_flg:print(*inp,end=end)
YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()
class Tree:
def __init__(self,inp_size=None,init=True):
self.LCA_init_stat=False
self.ETtable=[]
if init:
self.stdin(inp_size)
return
def stdin(self,inp_size=None,index=1):
if inp_size==None:
self.size=int(input())
else:
self.size=inp_size
self.edges,_=GI(self.size,self.size-1,index=index)
return
def listin(self,ls,index=0):
self.size=len(ls)+1
self.edges,_=GI(self.size,self.size-1,ls,index=index)
return
def __str__(self):
return str(self.edges)
def dfs(self,x,func=lambda prv,nx,dist:prv+dist,root_v=0):
q=deque()
q.append(x)
v=[-1]*self.size
v[x]=root_v
while q:
c=q.pop()
for nb,d in self.edges[c]:
if v[nb]==-1:
q.append(nb)
v[nb]=func(v[c],nb,d)
return v
def EulerTour(self,x):
q=deque()
q.append(x)
self.depth=[None]*self.size
self.depth[x]=0
self.ETtable=[]
self.ETdepth=[]
self.ETin=[-1]*self.size
self.ETout=[-1]*self.size
cnt=0
while q:
c=q.pop()
if c<0:
ce=~c
else:
ce=c
for nb,d in self.edges[ce]:
if self.depth[nb]==None:
q.append(~ce)
q.append(nb)
self.depth[nb]=self.depth[ce]+1
self.ETtable.append(ce)
self.ETdepth.append(self.depth[ce])
if self.ETin[ce]==-1:
self.ETin[ce]=cnt
else:
self.ETout[ce]=cnt
cnt+=1
return
def LCA_init(self,root):
self.EulerTour(root)
self.st=SparseTable(self.ETdepth,init_func=min,init_idl=inf)
self.LCA_init_stat=True
return
def LCA(self,root,x,y):
if self.LCA_init_stat==False:
self.LCA_init(root)
xin,xout=self.ETin[x],self.ETout[x]
yin,yout=self.ETin[y],self.ETout[y]
a=min(xin,yin)
b=max(xout,yout,xin,yin)
id_of_min_dep_in_et=self.st.query_id(a,b+1)
return self.ETtable[id_of_min_dep_in_et]
class SparseTable: # O(N log N) for init, O(1) for query(l,r)
def __init__(self,ls,init_func=min,init_idl=float('inf')):
self.func=init_func
self.idl=init_idl
self.size=len(ls)
self.N0=self.size.bit_length()
self.table=[ls[:]]
self.index=[list(range(self.size))]
self.lg=[0]*(self.size+1)
for i in range(2,self.size+1):
self.lg[i]=self.lg[i>>1]+1
for i in range(self.N0):
tmp=[self.func(self.table[i][j],self.table[i][min(j+(1<<i),self.size-1)]) for j in range(self.size)]
tmp_id=[self.index[i][j] if self.table[i][j]==self.func(self.table[i][j],self.table[i][min(j+(1<<i),self.size-1)]) else self.index[i][min(j+(1<<i),self.size-1)] for j in range(self.size)]
self.table+=[tmp]
self.index+=[tmp_id]
# return func of [l,r)
def query(self,l,r):
#N=(r-l).bit_length()-1
N=self.lg[r-l]
return self.func(self.table[N][l],self.table[N][r-(1<<N)])
# return index of which val[i] = func of v among [l,r)
def query_id(self,l,r):
#N=(r-l).bit_length()-1
N=self.lg[r-l]
a,b=self.index[N][l],self.index[N][r-(1<<N)]
if self.table[0][a]==self.func(self.table[N][l],self.table[N][r-(1<<N)]):
b=a
return b
def __str__(self):
return str(self.table[0])
def print(self):
for i in self.table:
print(*i)
show_flg=False
show_flg=True
ans=0
D='EWNS'
m=[(1,0),(-1,0),(0,1),(0,-1)]
dc=dict(zip(D,m))
T=I()
for _ in range(T):
ans=0
s=input()
N=len(s)*2+5
x,y=(N,N)
p=x*N+y
f=dict()
for i in s:
dx,dy=dc[i]
nx=x+dx
ny=y+dy
X=min(x,nx)
Y=min(y,ny)
p=X*N+Y
p*=1 if dx==0 else -1
if p in f:
ans+=1
else:
ans+=5
f[p]=1
x,y=nx,ny
#show(x-N,y-N,p,ans,f,N)
print(ans)
|
Skier rides on a snowy field. Its movements can be described by a string of characters 'S', 'N', 'W', 'E' (which correspond to $1$ meter movement in the south, north, west or east direction respectively).
It is known that if he moves along a previously unvisited segment of a path (i.e. this segment of the path is visited the first time), then the time of such movement is $5$ seconds. If he rolls along previously visited segment of a path (i.e., this segment of the path has been covered by his path before), then it takes $1$ second.
Find the skier's time to roll all the path.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
Each set is given by one nonempty string of the characters 'S', 'N', 'W', 'E'. The length of the string does not exceed $10^5$ characters.
The sum of the lengths of $t$ given lines over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the desired path time in seconds.
-----Example-----
Input
5
NNN
NS
WWEN
WWEE
NWNWS
Output
15
6
16
12
25
|
t=int(input())
def an(x):
if x=='S':
return 'N'
if x=='N':
return 'S'
if x=='W':
return 'E'
if x=='E':
return 'W'
def mov(x,y):
if y=='S':
return (x[0]+1,x[1])
if y=='N':
return (x[0]-1,x[1])
if y=='W':
return (x[0],x[1]+1)
if y=='E':
return (x[0],x[1]-1)
while t>0:
t-=1
li={}
s=input()
at=(0,0)
ans=0
for i in s:
nx=mov(at,i)
if li.get((at,i),False):
ans+=1
else:
ans+=5
li[(at,i)]=True
li[(nx,an(i))]=True
at=nx
print(ans)
|
Skier rides on a snowy field. Its movements can be described by a string of characters 'S', 'N', 'W', 'E' (which correspond to $1$ meter movement in the south, north, west or east direction respectively).
It is known that if he moves along a previously unvisited segment of a path (i.e. this segment of the path is visited the first time), then the time of such movement is $5$ seconds. If he rolls along previously visited segment of a path (i.e., this segment of the path has been covered by his path before), then it takes $1$ second.
Find the skier's time to roll all the path.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
Each set is given by one nonempty string of the characters 'S', 'N', 'W', 'E'. The length of the string does not exceed $10^5$ characters.
The sum of the lengths of $t$ given lines over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the desired path time in seconds.
-----Example-----
Input
5
NNN
NS
WWEN
WWEE
NWNWS
Output
15
6
16
12
25
|
rilist = lambda :[int(i) for i in input().split()]
rlist = lambda :[i for i in input().split()]
rint = lambda: int(input())
rfloat = lambda: float(input())
def pmat(mat):
for i in range(len(mat)):
a = ' '.join(map(str, mat[i]))
print(a)
print()
d = {'N':(1,0),'S':(-1,0),'E':(0,1),'W':(0,-1)}
def solve(t):
path = input()
curr = (0, 0)
tmp = {}
res = 0
for p in path:
a,b = d[p]
next = (curr[0]+a,curr[1]+b)
key = sorted((curr, next), key=lambda x:x[0])
key = sorted(key, key=lambda x:x[1])
key = tuple(key)
curr = next
res += tmp.get( key ,5)
tmp[key]=1
print(res)
test = int(input())
for tc in range(test):
solve(tc+1)
|
Skier rides on a snowy field. Its movements can be described by a string of characters 'S', 'N', 'W', 'E' (which correspond to $1$ meter movement in the south, north, west or east direction respectively).
It is known that if he moves along a previously unvisited segment of a path (i.e. this segment of the path is visited the first time), then the time of such movement is $5$ seconds. If he rolls along previously visited segment of a path (i.e., this segment of the path has been covered by his path before), then it takes $1$ second.
Find the skier's time to roll all the path.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
Each set is given by one nonempty string of the characters 'S', 'N', 'W', 'E'. The length of the string does not exceed $10^5$ characters.
The sum of the lengths of $t$ given lines over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the desired path time in seconds.
-----Example-----
Input
5
NNN
NS
WWEN
WWEE
NWNWS
Output
15
6
16
12
25
|
t = int(input())
for _ in range(t):
crd = set()
path = input()
x,y = 0,0
sum = 0
for c in path:
if c=='N':
if (x,y-1,x,y) in crd:
sum += 1
elif (x,y,x,y-1) in crd:
sum += 1
else:
crd.add((x,y-1, x, y))
sum += 5
x,y=x,y-1
elif c == 'S':
if (x,y+1,x,y) in crd:
sum += 1
elif (x,y,x,y+1) in crd:
sum += 1
else:
crd.add((x,y+1, x, y))
sum += 5
x,y=x,y+1
elif c=='W':
if (x+1,y,x,y) in crd:
sum += 1
elif (x,y,x+1,y) in crd:
sum += 1
else:
crd.add((x+1,y, x, y))
sum += 5
x,y=x+1,y
elif c=='E':
if (x-1,y,x,y) in crd:
sum += 1
elif (x,y,x-1,y) in crd:
sum += 1
else:
crd.add((x-1,y, x, y))
sum += 5
x,y = x-1,y
print(sum)
|
Skier rides on a snowy field. Its movements can be described by a string of characters 'S', 'N', 'W', 'E' (which correspond to $1$ meter movement in the south, north, west or east direction respectively).
It is known that if he moves along a previously unvisited segment of a path (i.e. this segment of the path is visited the first time), then the time of such movement is $5$ seconds. If he rolls along previously visited segment of a path (i.e., this segment of the path has been covered by his path before), then it takes $1$ second.
Find the skier's time to roll all the path.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
Each set is given by one nonempty string of the characters 'S', 'N', 'W', 'E'. The length of the string does not exceed $10^5$ characters.
The sum of the lengths of $t$ given lines over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the desired path time in seconds.
-----Example-----
Input
5
NNN
NS
WWEN
WWEE
NWNWS
Output
15
6
16
12
25
|
from sys import stdin, exit
input = stdin.readline
def i(): return input()
def ii(): return int(input())
def iis(): return list(map(int, input().split()))
def liis(): return list(map(int, input().split()))
def print_array(a): print(" ".join(map(str, a)))
t = ii()
for _ in range(t):
time = 0
x, y = 0, 0
visited = set()
s = input()
for i in s:
old_x = x
old_y = y
if i == 'N': y += 1
elif i == 'S': y -= 1
elif i == 'E': x += 1
elif i == 'W': x -= 1
else: continue
if (old_x, old_y, x, y) in visited:
time += 1
else:
time += 5
visited.add((x, y, old_x, old_y))
visited.add((old_x, old_y, x, y))
print(time)
|
Skier rides on a snowy field. Its movements can be described by a string of characters 'S', 'N', 'W', 'E' (which correspond to $1$ meter movement in the south, north, west or east direction respectively).
It is known that if he moves along a previously unvisited segment of a path (i.e. this segment of the path is visited the first time), then the time of such movement is $5$ seconds. If he rolls along previously visited segment of a path (i.e., this segment of the path has been covered by his path before), then it takes $1$ second.
Find the skier's time to roll all the path.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
Each set is given by one nonempty string of the characters 'S', 'N', 'W', 'E'. The length of the string does not exceed $10^5$ characters.
The sum of the lengths of $t$ given lines over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the desired path time in seconds.
-----Example-----
Input
5
NNN
NS
WWEN
WWEE
NWNWS
Output
15
6
16
12
25
|
t = int(input())
for _ in range(t):
s = input()
st = set()
x, y = 0, 0
ans = 0
for c in s:
if c == 'S':
if (x, y + 1) in st:
ans += 1
else:
ans += 5
st.add((x, y + 1))
y += 2
elif c == 'N':
if (x, y - 1) in st:
ans += 1
else:
ans += 5
st.add((x, y - 1))
y -= 2
elif c == 'W':
if (x + 1, y) in st:
ans += 1
else:
ans += 5
st.add((x + 1, y))
x += 2
else:
if (x - 1, y) in st:
ans += 1
else:
ans += 5
st.add((x - 1, y))
x -= 2
print(ans)
|
Skier rides on a snowy field. Its movements can be described by a string of characters 'S', 'N', 'W', 'E' (which correspond to $1$ meter movement in the south, north, west or east direction respectively).
It is known that if he moves along a previously unvisited segment of a path (i.e. this segment of the path is visited the first time), then the time of such movement is $5$ seconds. If he rolls along previously visited segment of a path (i.e., this segment of the path has been covered by his path before), then it takes $1$ second.
Find the skier's time to roll all the path.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
Each set is given by one nonempty string of the characters 'S', 'N', 'W', 'E'. The length of the string does not exceed $10^5$ characters.
The sum of the lengths of $t$ given lines over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the desired path time in seconds.
-----Example-----
Input
5
NNN
NS
WWEN
WWEE
NWNWS
Output
15
6
16
12
25
|
from math import *
for zz in range(int(input())):
used = set()
ans = 0
pos = [0, 0]
a = 0
for i in range(35000):
a += 1
a = ans - 1
for x in input():
ppos = pos[:]
ppos = tuple(ppos)
if x == 'N':
pos[0] += 1
elif x == 'S':
pos[0] -= 1
elif x == 'W':
pos[1] -= 1
else:
pos[1] += 1
if ((ppos, tuple(pos)) in used) or ((tuple(pos), ppos) in used):
ans += 1
else:
used.add((ppos, tuple(pos)))
ans += 5
print(ans)
#aaa
|
Skier rides on a snowy field. Its movements can be described by a string of characters 'S', 'N', 'W', 'E' (which correspond to $1$ meter movement in the south, north, west or east direction respectively).
It is known that if he moves along a previously unvisited segment of a path (i.e. this segment of the path is visited the first time), then the time of such movement is $5$ seconds. If he rolls along previously visited segment of a path (i.e., this segment of the path has been covered by his path before), then it takes $1$ second.
Find the skier's time to roll all the path.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases in the input. Then $t$ test cases follow.
Each set is given by one nonempty string of the characters 'S', 'N', 'W', 'E'. The length of the string does not exceed $10^5$ characters.
The sum of the lengths of $t$ given lines over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the desired path time in seconds.
-----Example-----
Input
5
NNN
NS
WWEN
WWEE
NWNWS
Output
15
6
16
12
25
|
import time
import sys
readline = sys.stdin.readline
t = time.time()
d = {'N': -10**9, 'S': 10**9, 'E': 1, 'W': -1}
for _ in range(int(input())):
s = readline().rstrip()
pos = 0
visited = set()
dist = 0
for c in s:
dest = pos + d[c]
dist += 1 if (pos, dest) in visited or (dest, pos) in visited else 5
visited.update(((pos, dest), (dest, pos)))
pos = dest
print(dist)
while time.time() - t < 0.9:
pass
|
Lately, Mr. Chanek frequently plays the game Arena of Greed. As the name implies, the game's goal is to find the greediest of them all, who will then be crowned king of Compfestnesia.
The game is played by two people taking turns, where Mr. Chanek takes the first turn. Initially, there is a treasure chest containing $N$ gold coins. The game ends if there are no more gold coins in the chest. In each turn, the players can make one of the following moves: Take one gold coin from the chest. Take half of the gold coins on the chest. This move is only available if the number of coins in the chest is even.
Both players will try to maximize the number of coins they have. Mr. Chanek asks your help to find the maximum number of coins he can get at the end of the game if both he and the opponent plays optimally.
-----Input-----
The first line contains a single integer $T$ $(1 \le T \le 10^5)$ denotes the number of test cases.
The next $T$ lines each contain a single integer $N$ $(1 \le N \le 10^{18})$.
-----Output-----
$T$ lines, each line is the answer requested by Mr. Chanek.
-----Example-----
Input
2
5
6
Output
2
4
-----Note-----
For the first case, the game is as follows: Mr. Chanek takes one coin. The opponent takes two coins. Mr. Chanek takes one coin. The opponent takes one coin.
For the second case, the game is as follows: Mr. Chanek takes three coins. The opponent takes one coin. Mr. Chanek takes one coin. The opponent takes one coin.
|
from sys import stdin, stdout
from collections import defaultdict
input = stdin.readline
for _ in range(int(input())):
n = int(input())
chanek = 0
flag = 1
while n>0:
if n%4==0 and n!=4:
if flag:
chanek += 1
n-=1
flag = 0
else:
n-=1
flag = 1
elif n%2:
if flag:
chanek += 1
n-=1
flag = 0
else:
n-=1
flag = 1
else:
if flag:
chanek += n//2
n//=2
flag = 0
else:
n//=2
flag = 1
print(chanek)
|
Lately, Mr. Chanek frequently plays the game Arena of Greed. As the name implies, the game's goal is to find the greediest of them all, who will then be crowned king of Compfestnesia.
The game is played by two people taking turns, where Mr. Chanek takes the first turn. Initially, there is a treasure chest containing $N$ gold coins. The game ends if there are no more gold coins in the chest. In each turn, the players can make one of the following moves: Take one gold coin from the chest. Take half of the gold coins on the chest. This move is only available if the number of coins in the chest is even.
Both players will try to maximize the number of coins they have. Mr. Chanek asks your help to find the maximum number of coins he can get at the end of the game if both he and the opponent plays optimally.
-----Input-----
The first line contains a single integer $T$ $(1 \le T \le 10^5)$ denotes the number of test cases.
The next $T$ lines each contain a single integer $N$ $(1 \le N \le 10^{18})$.
-----Output-----
$T$ lines, each line is the answer requested by Mr. Chanek.
-----Example-----
Input
2
5
6
Output
2
4
-----Note-----
For the first case, the game is as follows: Mr. Chanek takes one coin. The opponent takes two coins. Mr. Chanek takes one coin. The opponent takes one coin.
For the second case, the game is as follows: Mr. Chanek takes three coins. The opponent takes one coin. Mr. Chanek takes one coin. The opponent takes one coin.
|
from sys import stdin
input = stdin.readline
def max_pos_coins(n):
a = 0
while n != 0:
if n == 4:
a += 3
n = 0
continue
if n % 4 == 0:
n -= 2
a += 1
else:
a += n // 2
n = n // 2 - 1
return a
for _ in range(int(input())):
n = int(input())
print(max_pos_coins(n) if n % 2 == 0 else n - max_pos_coins(n - 1))
|
Lately, Mr. Chanek frequently plays the game Arena of Greed. As the name implies, the game's goal is to find the greediest of them all, who will then be crowned king of Compfestnesia.
The game is played by two people taking turns, where Mr. Chanek takes the first turn. Initially, there is a treasure chest containing $N$ gold coins. The game ends if there are no more gold coins in the chest. In each turn, the players can make one of the following moves: Take one gold coin from the chest. Take half of the gold coins on the chest. This move is only available if the number of coins in the chest is even.
Both players will try to maximize the number of coins they have. Mr. Chanek asks your help to find the maximum number of coins he can get at the end of the game if both he and the opponent plays optimally.
-----Input-----
The first line contains a single integer $T$ $(1 \le T \le 10^5)$ denotes the number of test cases.
The next $T$ lines each contain a single integer $N$ $(1 \le N \le 10^{18})$.
-----Output-----
$T$ lines, each line is the answer requested by Mr. Chanek.
-----Example-----
Input
2
5
6
Output
2
4
-----Note-----
For the first case, the game is as follows: Mr. Chanek takes one coin. The opponent takes two coins. Mr. Chanek takes one coin. The opponent takes one coin.
For the second case, the game is as follows: Mr. Chanek takes three coins. The opponent takes one coin. Mr. Chanek takes one coin. The opponent takes one coin.
|
import itertools
def f(x):
scores = [0, 0]
for i in itertools.cycle([0, 1]):
if x & 1:
scores[i] += 1
x -= 1
elif x == 0:
return scores[0]
elif x == 4 or x & 0b10:
x >>= 1
scores[i] += x
else:
x -= 1
scores[i] += 1
N = int(input())
results = []
import sys
for n in map(f, map(int, sys.stdin.read().split())):
results.append(n)
print('\n'.join(map(str, results)))
|
Numbers $1, 2, 3, \dots n$ (each integer from $1$ to $n$ once) are written on a board. In one operation you can erase any two numbers $a$ and $b$ from the board and write one integer $\frac{a + b}{2}$ rounded up instead.
You should perform the given operation $n - 1$ times and make the resulting number that will be left on the board as small as possible.
For example, if $n = 4$, the following course of action is optimal: choose $a = 4$ and $b = 2$, so the new number is $3$, and the whiteboard contains $[1, 3, 3]$; choose $a = 3$ and $b = 3$, so the new number is $3$, and the whiteboard contains $[1, 3]$; choose $a = 1$ and $b = 3$, so the new number is $2$, and the whiteboard contains $[2]$.
It's easy to see that after $n - 1$ operations, there will be left only one number. Your goal is to minimize it.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The only line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$)Β β the number of integers written on the board initially.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, in the first line, print the minimum possible number left on the board after $n - 1$ operations. Each of the next $n - 1$ lines should contain two integersΒ β numbers $a$ and $b$ chosen and erased in each operation.
-----Example-----
Input
1
4
Output
2
2 4
3 3
3 1
|
t=int(input())
for i in range(t):
n=int(input())
print(2)
print(n-1,n)
for i in range(n-2,0,-1):
print(i,i+2)
|
Numbers $1, 2, 3, \dots n$ (each integer from $1$ to $n$ once) are written on a board. In one operation you can erase any two numbers $a$ and $b$ from the board and write one integer $\frac{a + b}{2}$ rounded up instead.
You should perform the given operation $n - 1$ times and make the resulting number that will be left on the board as small as possible.
For example, if $n = 4$, the following course of action is optimal: choose $a = 4$ and $b = 2$, so the new number is $3$, and the whiteboard contains $[1, 3, 3]$; choose $a = 3$ and $b = 3$, so the new number is $3$, and the whiteboard contains $[1, 3]$; choose $a = 1$ and $b = 3$, so the new number is $2$, and the whiteboard contains $[2]$.
It's easy to see that after $n - 1$ operations, there will be left only one number. Your goal is to minimize it.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The only line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$)Β β the number of integers written on the board initially.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, in the first line, print the minimum possible number left on the board after $n - 1$ operations. Each of the next $n - 1$ lines should contain two integersΒ β numbers $a$ and $b$ chosen and erased in each operation.
-----Example-----
Input
1
4
Output
2
2 4
3 3
3 1
|
for _ in range (int(input())):
n=int(input())
hold=n
res=[]
for i in range (n-1,0,-1):
res.append((hold,i))
hold=(hold+i+1)//2
print(hold)
for i in res:
print(*i)
|
Numbers $1, 2, 3, \dots n$ (each integer from $1$ to $n$ once) are written on a board. In one operation you can erase any two numbers $a$ and $b$ from the board and write one integer $\frac{a + b}{2}$ rounded up instead.
You should perform the given operation $n - 1$ times and make the resulting number that will be left on the board as small as possible.
For example, if $n = 4$, the following course of action is optimal: choose $a = 4$ and $b = 2$, so the new number is $3$, and the whiteboard contains $[1, 3, 3]$; choose $a = 3$ and $b = 3$, so the new number is $3$, and the whiteboard contains $[1, 3]$; choose $a = 1$ and $b = 3$, so the new number is $2$, and the whiteboard contains $[2]$.
It's easy to see that after $n - 1$ operations, there will be left only one number. Your goal is to minimize it.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The only line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$)Β β the number of integers written on the board initially.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, in the first line, print the minimum possible number left on the board after $n - 1$ operations. Each of the next $n - 1$ lines should contain two integersΒ β numbers $a$ and $b$ chosen and erased in each operation.
-----Example-----
Input
1
4
Output
2
2 4
3 3
3 1
|
t = int(input())
for _ in range(t):
n = int(input())
print(2)
print(n-1, n)
for i in range(n-2):
print(n-2-i, n-i)
|
Numbers $1, 2, 3, \dots n$ (each integer from $1$ to $n$ once) are written on a board. In one operation you can erase any two numbers $a$ and $b$ from the board and write one integer $\frac{a + b}{2}$ rounded up instead.
You should perform the given operation $n - 1$ times and make the resulting number that will be left on the board as small as possible.
For example, if $n = 4$, the following course of action is optimal: choose $a = 4$ and $b = 2$, so the new number is $3$, and the whiteboard contains $[1, 3, 3]$; choose $a = 3$ and $b = 3$, so the new number is $3$, and the whiteboard contains $[1, 3]$; choose $a = 1$ and $b = 3$, so the new number is $2$, and the whiteboard contains $[2]$.
It's easy to see that after $n - 1$ operations, there will be left only one number. Your goal is to minimize it.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The only line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$)Β β the number of integers written on the board initially.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, in the first line, print the minimum possible number left on the board after $n - 1$ operations. Each of the next $n - 1$ lines should contain two integersΒ β numbers $a$ and $b$ chosen and erased in each operation.
-----Example-----
Input
1
4
Output
2
2 4
3 3
3 1
|
import sys
import random
# import numpy as np
import math
import copy
from heapq import heappush, heappop, heapify
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
# sys.setrecursionlimit(1000000)
# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]
INF = float("inf")
MOD = 10 ** 9 + 7
divide = lambda x: pow(x, MOD-2, MOD)
def judge(at, ax, ay, bt, bx, by):
if abs(at - bt) >= abs(ax - bx) + abs(ay - by):
return True
else:
return False
def solve():
n = getN()
if n == 2:
print(2)
print(1, 2)
return
print(2)
print(n-2, n)
print(n-1, n-1)
for i in range(n-3):
print(n-1-i, n-3-i)
return
def main():
n = getN()
for _ in range(n):
solve()
return
def __starting_point():
main()
# solve()
__starting_point()
|
Numbers $1, 2, 3, \dots n$ (each integer from $1$ to $n$ once) are written on a board. In one operation you can erase any two numbers $a$ and $b$ from the board and write one integer $\frac{a + b}{2}$ rounded up instead.
You should perform the given operation $n - 1$ times and make the resulting number that will be left on the board as small as possible.
For example, if $n = 4$, the following course of action is optimal: choose $a = 4$ and $b = 2$, so the new number is $3$, and the whiteboard contains $[1, 3, 3]$; choose $a = 3$ and $b = 3$, so the new number is $3$, and the whiteboard contains $[1, 3]$; choose $a = 1$ and $b = 3$, so the new number is $2$, and the whiteboard contains $[2]$.
It's easy to see that after $n - 1$ operations, there will be left only one number. Your goal is to minimize it.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The only line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$)Β β the number of integers written on the board initially.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, in the first line, print the minimum possible number left on the board after $n - 1$ operations. Each of the next $n - 1$ lines should contain two integersΒ β numbers $a$ and $b$ chosen and erased in each operation.
-----Example-----
Input
1
4
Output
2
2 4
3 3
3 1
|
t=int(input())
for you in range(t):
n=int(input())
print(2)
l=[i+1 for i in range(n)]
for i in range(n-1):
print(l[-1],l[-2])
z=(l[-1]+l[-2]+1)//2
l.pop(-1)
l.pop(-1)
l.append(z)
|
Numbers $1, 2, 3, \dots n$ (each integer from $1$ to $n$ once) are written on a board. In one operation you can erase any two numbers $a$ and $b$ from the board and write one integer $\frac{a + b}{2}$ rounded up instead.
You should perform the given operation $n - 1$ times and make the resulting number that will be left on the board as small as possible.
For example, if $n = 4$, the following course of action is optimal: choose $a = 4$ and $b = 2$, so the new number is $3$, and the whiteboard contains $[1, 3, 3]$; choose $a = 3$ and $b = 3$, so the new number is $3$, and the whiteboard contains $[1, 3]$; choose $a = 1$ and $b = 3$, so the new number is $2$, and the whiteboard contains $[2]$.
It's easy to see that after $n - 1$ operations, there will be left only one number. Your goal is to minimize it.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The only line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$)Β β the number of integers written on the board initially.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, in the first line, print the minimum possible number left on the board after $n - 1$ operations. Each of the next $n - 1$ lines should contain two integersΒ β numbers $a$ and $b$ chosen and erased in each operation.
-----Example-----
Input
1
4
Output
2
2 4
3 3
3 1
|
for _ in range(int(input())):
n = int(input())
k = n
print(2)
for i in range(n-1,0,-1):
print(i,k)
if (k+i)%2!= 0:
k = (k+i)//2 + 1
else:
k = (k+i)//2
|
Numbers $1, 2, 3, \dots n$ (each integer from $1$ to $n$ once) are written on a board. In one operation you can erase any two numbers $a$ and $b$ from the board and write one integer $\frac{a + b}{2}$ rounded up instead.
You should perform the given operation $n - 1$ times and make the resulting number that will be left on the board as small as possible.
For example, if $n = 4$, the following course of action is optimal: choose $a = 4$ and $b = 2$, so the new number is $3$, and the whiteboard contains $[1, 3, 3]$; choose $a = 3$ and $b = 3$, so the new number is $3$, and the whiteboard contains $[1, 3]$; choose $a = 1$ and $b = 3$, so the new number is $2$, and the whiteboard contains $[2]$.
It's easy to see that after $n - 1$ operations, there will be left only one number. Your goal is to minimize it.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The only line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$)Β β the number of integers written on the board initially.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, in the first line, print the minimum possible number left on the board after $n - 1$ operations. Each of the next $n - 1$ lines should contain two integersΒ β numbers $a$ and $b$ chosen and erased in each operation.
-----Example-----
Input
1
4
Output
2
2 4
3 3
3 1
|
import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
n = int(input())
print(2)
if n == 2:
print(1,2)
else:
print(n,n-2)
print(n-1,n-1)
for j in range(n-3):
print(n-1-j,n-1-j-2)
|
Numbers $1, 2, 3, \dots n$ (each integer from $1$ to $n$ once) are written on a board. In one operation you can erase any two numbers $a$ and $b$ from the board and write one integer $\frac{a + b}{2}$ rounded up instead.
You should perform the given operation $n - 1$ times and make the resulting number that will be left on the board as small as possible.
For example, if $n = 4$, the following course of action is optimal: choose $a = 4$ and $b = 2$, so the new number is $3$, and the whiteboard contains $[1, 3, 3]$; choose $a = 3$ and $b = 3$, so the new number is $3$, and the whiteboard contains $[1, 3]$; choose $a = 1$ and $b = 3$, so the new number is $2$, and the whiteboard contains $[2]$.
It's easy to see that after $n - 1$ operations, there will be left only one number. Your goal is to minimize it.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The only line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$)Β β the number of integers written on the board initially.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, in the first line, print the minimum possible number left on the board after $n - 1$ operations. Each of the next $n - 1$ lines should contain two integersΒ β numbers $a$ and $b$ chosen and erased in each operation.
-----Example-----
Input
1
4
Output
2
2 4
3 3
3 1
|
#dt = {} for i in x: dt[i] = dt.get(i,0)+1
import sys;input = sys.stdin.readline
#import io,os; input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline #for pypy
inp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()]
for _ in range(inp()):
n = inp()
prev = n
print(2)
for i in range(n-1,0,-1):
print(i,prev)
prev = (i+prev-1)//2 +1
|
Numbers $1, 2, 3, \dots n$ (each integer from $1$ to $n$ once) are written on a board. In one operation you can erase any two numbers $a$ and $b$ from the board and write one integer $\frac{a + b}{2}$ rounded up instead.
You should perform the given operation $n - 1$ times and make the resulting number that will be left on the board as small as possible.
For example, if $n = 4$, the following course of action is optimal: choose $a = 4$ and $b = 2$, so the new number is $3$, and the whiteboard contains $[1, 3, 3]$; choose $a = 3$ and $b = 3$, so the new number is $3$, and the whiteboard contains $[1, 3]$; choose $a = 1$ and $b = 3$, so the new number is $2$, and the whiteboard contains $[2]$.
It's easy to see that after $n - 1$ operations, there will be left only one number. Your goal is to minimize it.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The only line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$)Β β the number of integers written on the board initially.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, in the first line, print the minimum possible number left on the board after $n - 1$ operations. Each of the next $n - 1$ lines should contain two integersΒ β numbers $a$ and $b$ chosen and erased in each operation.
-----Example-----
Input
1
4
Output
2
2 4
3 3
3 1
|
T = int(input())
for _ in range(T):
N = int(input())
A = list(range(1,N+1))
print(2)
while len(A) > 1:
a = A.pop()
b = A.pop()
c = (a+b+1)//2
print(a,b)
A.append(c)
|
Numbers $1, 2, 3, \dots n$ (each integer from $1$ to $n$ once) are written on a board. In one operation you can erase any two numbers $a$ and $b$ from the board and write one integer $\frac{a + b}{2}$ rounded up instead.
You should perform the given operation $n - 1$ times and make the resulting number that will be left on the board as small as possible.
For example, if $n = 4$, the following course of action is optimal: choose $a = 4$ and $b = 2$, so the new number is $3$, and the whiteboard contains $[1, 3, 3]$; choose $a = 3$ and $b = 3$, so the new number is $3$, and the whiteboard contains $[1, 3]$; choose $a = 1$ and $b = 3$, so the new number is $2$, and the whiteboard contains $[2]$.
It's easy to see that after $n - 1$ operations, there will be left only one number. Your goal is to minimize it.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The only line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$)Β β the number of integers written on the board initially.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, in the first line, print the minimum possible number left on the board after $n - 1$ operations. Each of the next $n - 1$ lines should contain two integersΒ β numbers $a$ and $b$ chosen and erased in each operation.
-----Example-----
Input
1
4
Output
2
2 4
3 3
3 1
|
import sys, math
import io, os
#data = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter
#from itertools import permutations,combinations
def data(): return sys.stdin.readline().strip()
def mdata(): return list(map(int, data().split()))
def outl(var) : sys.stdout.write('\n'.join(map(str, var))+'\n')
def out(var) : sys.stdout.write(str(var)+'\n')
#from decimal import Decimal
#from fractions import Fraction
#sys.setrecursionlimit(100000)
INF = float('inf')
mod=10**9+7
for t in range(int(data())):
n=int(data())
out(2)
ans=[]
k=n
for i in range(n-1,0,-1):
ans.append(str(k)+' '+str(i))
k=(k+i+1)//2
outl(ans)
|
Numbers $1, 2, 3, \dots n$ (each integer from $1$ to $n$ once) are written on a board. In one operation you can erase any two numbers $a$ and $b$ from the board and write one integer $\frac{a + b}{2}$ rounded up instead.
You should perform the given operation $n - 1$ times and make the resulting number that will be left on the board as small as possible.
For example, if $n = 4$, the following course of action is optimal: choose $a = 4$ and $b = 2$, so the new number is $3$, and the whiteboard contains $[1, 3, 3]$; choose $a = 3$ and $b = 3$, so the new number is $3$, and the whiteboard contains $[1, 3]$; choose $a = 1$ and $b = 3$, so the new number is $2$, and the whiteboard contains $[2]$.
It's easy to see that after $n - 1$ operations, there will be left only one number. Your goal is to minimize it.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The only line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$)Β β the number of integers written on the board initially.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, in the first line, print the minimum possible number left on the board after $n - 1$ operations. Each of the next $n - 1$ lines should contain two integersΒ β numbers $a$ and $b$ chosen and erased in each operation.
-----Example-----
Input
1
4
Output
2
2 4
3 3
3 1
|
from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush,heappop,heapify
import math
from collections import *
from functools import reduce,cmp_to_key
import sys
input = sys.stdin.readline
from itertools import accumulate
from functools import lru_cache
M = mod = 10 ** 9 + 7
def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def inv_mod(n):return pow(n, mod - 2, mod)
def li():return [int(i) for i in input().rstrip('\n').split()]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n')]
def li3():return [int(i) for i in input().rstrip('\n')]
for _ in range(val()):
n = val()
print(2)
l = list(range(1, n + 1))
for i in range(n - 1):
a, b = l.pop(), l.pop()
print(a, b)
l.append((a + b + 1) // 2)
|
Numbers $1, 2, 3, \dots n$ (each integer from $1$ to $n$ once) are written on a board. In one operation you can erase any two numbers $a$ and $b$ from the board and write one integer $\frac{a + b}{2}$ rounded up instead.
You should perform the given operation $n - 1$ times and make the resulting number that will be left on the board as small as possible.
For example, if $n = 4$, the following course of action is optimal: choose $a = 4$ and $b = 2$, so the new number is $3$, and the whiteboard contains $[1, 3, 3]$; choose $a = 3$ and $b = 3$, so the new number is $3$, and the whiteboard contains $[1, 3]$; choose $a = 1$ and $b = 3$, so the new number is $2$, and the whiteboard contains $[2]$.
It's easy to see that after $n - 1$ operations, there will be left only one number. Your goal is to minimize it.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The only line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$)Β β the number of integers written on the board initially.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, in the first line, print the minimum possible number left on the board after $n - 1$ operations. Each of the next $n - 1$ lines should contain two integersΒ β numbers $a$ and $b$ chosen and erased in each operation.
-----Example-----
Input
1
4
Output
2
2 4
3 3
3 1
|
from math import ceil
n = int(input())
for _ in range(n):
k = int(input())
arr = list(range(1, k+1))
o = []
for i in range(k-1):
a = arr.pop()
b = arr.pop()
o.append((a, b))
arr.append(ceil((a+b)/2))
print(arr[0])
for i in range(len(o)):
print(o[i][0], o[i][1])
|
Numbers $1, 2, 3, \dots n$ (each integer from $1$ to $n$ once) are written on a board. In one operation you can erase any two numbers $a$ and $b$ from the board and write one integer $\frac{a + b}{2}$ rounded up instead.
You should perform the given operation $n - 1$ times and make the resulting number that will be left on the board as small as possible.
For example, if $n = 4$, the following course of action is optimal: choose $a = 4$ and $b = 2$, so the new number is $3$, and the whiteboard contains $[1, 3, 3]$; choose $a = 3$ and $b = 3$, so the new number is $3$, and the whiteboard contains $[1, 3]$; choose $a = 1$ and $b = 3$, so the new number is $2$, and the whiteboard contains $[2]$.
It's easy to see that after $n - 1$ operations, there will be left only one number. Your goal is to minimize it.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The only line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$)Β β the number of integers written on the board initially.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, in the first line, print the minimum possible number left on the board after $n - 1$ operations. Each of the next $n - 1$ lines should contain two integersΒ β numbers $a$ and $b$ chosen and erased in each operation.
-----Example-----
Input
1
4
Output
2
2 4
3 3
3 1
|
import sys
input = sys.stdin.readline
def main():
t = int(input())
for _ in range(t):
N = int(input())
x = []
for i in range(1, N + 1):
x.append(i)
print(2)
while len(x) >= 2:
a = x.pop()
b = x.pop()
c = -(-(a + b) // 2)
print(a, b)
x.append(c)
def __starting_point():
main()
__starting_point()
|
Numbers $1, 2, 3, \dots n$ (each integer from $1$ to $n$ once) are written on a board. In one operation you can erase any two numbers $a$ and $b$ from the board and write one integer $\frac{a + b}{2}$ rounded up instead.
You should perform the given operation $n - 1$ times and make the resulting number that will be left on the board as small as possible.
For example, if $n = 4$, the following course of action is optimal: choose $a = 4$ and $b = 2$, so the new number is $3$, and the whiteboard contains $[1, 3, 3]$; choose $a = 3$ and $b = 3$, so the new number is $3$, and the whiteboard contains $[1, 3]$; choose $a = 1$ and $b = 3$, so the new number is $2$, and the whiteboard contains $[2]$.
It's easy to see that after $n - 1$ operations, there will be left only one number. Your goal is to minimize it.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The only line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$)Β β the number of integers written on the board initially.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, in the first line, print the minimum possible number left on the board after $n - 1$ operations. Each of the next $n - 1$ lines should contain two integersΒ β numbers $a$ and $b$ chosen and erased in each operation.
-----Example-----
Input
1
4
Output
2
2 4
3 3
3 1
|
t = int(input())
ns = []
for _ in range(t):
n = int(input())
ns.append(n)
for n in ns:
print(2)
print(n-1, n)
if n > 2:
for x in range(n, 2, -1):
print(x-2, x)
|
Numbers $1, 2, 3, \dots n$ (each integer from $1$ to $n$ once) are written on a board. In one operation you can erase any two numbers $a$ and $b$ from the board and write one integer $\frac{a + b}{2}$ rounded up instead.
You should perform the given operation $n - 1$ times and make the resulting number that will be left on the board as small as possible.
For example, if $n = 4$, the following course of action is optimal: choose $a = 4$ and $b = 2$, so the new number is $3$, and the whiteboard contains $[1, 3, 3]$; choose $a = 3$ and $b = 3$, so the new number is $3$, and the whiteboard contains $[1, 3]$; choose $a = 1$ and $b = 3$, so the new number is $2$, and the whiteboard contains $[2]$.
It's easy to see that after $n - 1$ operations, there will be left only one number. Your goal is to minimize it.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The only line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$)Β β the number of integers written on the board initially.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, in the first line, print the minimum possible number left on the board after $n - 1$ operations. Each of the next $n - 1$ lines should contain two integersΒ β numbers $a$ and $b$ chosen and erased in each operation.
-----Example-----
Input
1
4
Output
2
2 4
3 3
3 1
|
t = int(input())
for _ in range(t):
n = int(input())
print(2)
print('{0} {1}'.format(n-1, n))
for k in range(n, 2, -1):
print('{0} {1}'.format(k-2, k))
|
Numbers $1, 2, 3, \dots n$ (each integer from $1$ to $n$ once) are written on a board. In one operation you can erase any two numbers $a$ and $b$ from the board and write one integer $\frac{a + b}{2}$ rounded up instead.
You should perform the given operation $n - 1$ times and make the resulting number that will be left on the board as small as possible.
For example, if $n = 4$, the following course of action is optimal: choose $a = 4$ and $b = 2$, so the new number is $3$, and the whiteboard contains $[1, 3, 3]$; choose $a = 3$ and $b = 3$, so the new number is $3$, and the whiteboard contains $[1, 3]$; choose $a = 1$ and $b = 3$, so the new number is $2$, and the whiteboard contains $[2]$.
It's easy to see that after $n - 1$ operations, there will be left only one number. Your goal is to minimize it.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The only line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$)Β β the number of integers written on the board initially.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, in the first line, print the minimum possible number left on the board after $n - 1$ operations. Each of the next $n - 1$ lines should contain two integersΒ β numbers $a$ and $b$ chosen and erased in each operation.
-----Example-----
Input
1
4
Output
2
2 4
3 3
3 1
|
def solve():
n = int(input())
if n == 2:
print(2)
print(1, 2)
return 0
lst = list(range(1, n + 1))
ans = []
ans.append([n-2,n])
ans.append([n-1,n-1])
lst.pop()
lst.pop()
lst.pop()
lst.append(n-1)
while len(lst) > 1:
a = lst[-1]
b = lst[-2]
c = (a + b + 1) // 2
ans.append([a,b])
lst.pop()
lst.pop()
lst.append(c)
print(lst[0])
for i in ans:
print(*i)
for i in range(int(input())):
solve()
|
Numbers $1, 2, 3, \dots n$ (each integer from $1$ to $n$ once) are written on a board. In one operation you can erase any two numbers $a$ and $b$ from the board and write one integer $\frac{a + b}{2}$ rounded up instead.
You should perform the given operation $n - 1$ times and make the resulting number that will be left on the board as small as possible.
For example, if $n = 4$, the following course of action is optimal: choose $a = 4$ and $b = 2$, so the new number is $3$, and the whiteboard contains $[1, 3, 3]$; choose $a = 3$ and $b = 3$, so the new number is $3$, and the whiteboard contains $[1, 3]$; choose $a = 1$ and $b = 3$, so the new number is $2$, and the whiteboard contains $[2]$.
It's easy to see that after $n - 1$ operations, there will be left only one number. Your goal is to minimize it.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The only line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$)Β β the number of integers written on the board initially.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, in the first line, print the minimum possible number left on the board after $n - 1$ operations. Each of the next $n - 1$ lines should contain two integersΒ β numbers $a$ and $b$ chosen and erased in each operation.
-----Example-----
Input
1
4
Output
2
2 4
3 3
3 1
|
import sys
input = sys.stdin.readline
t=int(input())
for tests in range(t):
n=int(input())
print(2)
x=n
for i in range(n-1,0,-1):
print(x,i)
x=(x+i+1)//2
|
Numbers $1, 2, 3, \dots n$ (each integer from $1$ to $n$ once) are written on a board. In one operation you can erase any two numbers $a$ and $b$ from the board and write one integer $\frac{a + b}{2}$ rounded up instead.
You should perform the given operation $n - 1$ times and make the resulting number that will be left on the board as small as possible.
For example, if $n = 4$, the following course of action is optimal: choose $a = 4$ and $b = 2$, so the new number is $3$, and the whiteboard contains $[1, 3, 3]$; choose $a = 3$ and $b = 3$, so the new number is $3$, and the whiteboard contains $[1, 3]$; choose $a = 1$ and $b = 3$, so the new number is $2$, and the whiteboard contains $[2]$.
It's easy to see that after $n - 1$ operations, there will be left only one number. Your goal is to minimize it.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The only line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$)Β β the number of integers written on the board initially.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, in the first line, print the minimum possible number left on the board after $n - 1$ operations. Each of the next $n - 1$ lines should contain two integersΒ β numbers $a$ and $b$ chosen and erased in each operation.
-----Example-----
Input
1
4
Output
2
2 4
3 3
3 1
|
"""T=int(input())
for _ in range(0,T):
n=int(input())
a,b=map(int,input().split())
s=input()
s=[int(x) for x in input().split()]
for i in range(0,len(s)):
a,b=map(int,input().split())"""
T=int(input())
for _ in range(0,T):
n=int(input())
print(2)
num=n
for i in range(n-1,0,-1):
print(i,num)
if((num+i)%2==0):
num=(num+i)//2
|
Numbers $1, 2, 3, \dots n$ (each integer from $1$ to $n$ once) are written on a board. In one operation you can erase any two numbers $a$ and $b$ from the board and write one integer $\frac{a + b}{2}$ rounded up instead.
You should perform the given operation $n - 1$ times and make the resulting number that will be left on the board as small as possible.
For example, if $n = 4$, the following course of action is optimal: choose $a = 4$ and $b = 2$, so the new number is $3$, and the whiteboard contains $[1, 3, 3]$; choose $a = 3$ and $b = 3$, so the new number is $3$, and the whiteboard contains $[1, 3]$; choose $a = 1$ and $b = 3$, so the new number is $2$, and the whiteboard contains $[2]$.
It's easy to see that after $n - 1$ operations, there will be left only one number. Your goal is to minimize it.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The only line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$)Β β the number of integers written on the board initially.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, in the first line, print the minimum possible number left on the board after $n - 1$ operations. Each of the next $n - 1$ lines should contain two integersΒ β numbers $a$ and $b$ chosen and erased in each operation.
-----Example-----
Input
1
4
Output
2
2 4
3 3
3 1
|
t = int(input())
for _ in range(t):
n = int(input())
A = list(range(1, n+1))
ans = []
t = -1
for i in range(n-1):
ans.append((A[-2], A[-1]))
x = A.pop()
if (x+A[-1])%2 == 0:
A[-1] = (x+A[-1])//2
else:
A[-1] = (x+A[-1]+1)//2
print(A[0])
for i in range(len(ans)):
print(*ans[i])
|
Numbers $1, 2, 3, \dots n$ (each integer from $1$ to $n$ once) are written on a board. In one operation you can erase any two numbers $a$ and $b$ from the board and write one integer $\frac{a + b}{2}$ rounded up instead.
You should perform the given operation $n - 1$ times and make the resulting number that will be left on the board as small as possible.
For example, if $n = 4$, the following course of action is optimal: choose $a = 4$ and $b = 2$, so the new number is $3$, and the whiteboard contains $[1, 3, 3]$; choose $a = 3$ and $b = 3$, so the new number is $3$, and the whiteboard contains $[1, 3]$; choose $a = 1$ and $b = 3$, so the new number is $2$, and the whiteboard contains $[2]$.
It's easy to see that after $n - 1$ operations, there will be left only one number. Your goal is to minimize it.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The only line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$)Β β the number of integers written on the board initially.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, in the first line, print the minimum possible number left on the board after $n - 1$ operations. Each of the next $n - 1$ lines should contain two integersΒ β numbers $a$ and $b$ chosen and erased in each operation.
-----Example-----
Input
1
4
Output
2
2 4
3 3
3 1
|
for _ in range(int(input())):
n = int(input())
print(2, flush=False)
print(f'{n} {n-1}', flush=False)
if n > 2:
print('\n'.join(f'{x} {x-2}' for x in range(n, 2, -1)))
|
Numbers $1, 2, 3, \dots n$ (each integer from $1$ to $n$ once) are written on a board. In one operation you can erase any two numbers $a$ and $b$ from the board and write one integer $\frac{a + b}{2}$ rounded up instead.
You should perform the given operation $n - 1$ times and make the resulting number that will be left on the board as small as possible.
For example, if $n = 4$, the following course of action is optimal: choose $a = 4$ and $b = 2$, so the new number is $3$, and the whiteboard contains $[1, 3, 3]$; choose $a = 3$ and $b = 3$, so the new number is $3$, and the whiteboard contains $[1, 3]$; choose $a = 1$ and $b = 3$, so the new number is $2$, and the whiteboard contains $[2]$.
It's easy to see that after $n - 1$ operations, there will be left only one number. Your goal is to minimize it.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$)Β β the number of test cases.
The only line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$)Β β the number of integers written on the board initially.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, in the first line, print the minimum possible number left on the board after $n - 1$ operations. Each of the next $n - 1$ lines should contain two integersΒ β numbers $a$ and $b$ chosen and erased in each operation.
-----Example-----
Input
1
4
Output
2
2 4
3 3
3 1
|
# 3x + 5y + 7z
t = int(input())
while t:
t -= 1
n = int(input())
print(2)
arr = [n-1,n]
print(*arr)
for i in range(2,n):
arr = [n-i,n-i+2]
print(*arr)
|
You have a large electronic screen which can display up to $998244353$ decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of $7$ segments which can be turned on and off to compose different digits. The following picture describes how you can display all $10$ decimal digits:
[Image]
As you can see, different digits may require different number of segments to be turned on. For example, if you want to display $1$, you have to turn on $2$ segments of the screen, and if you want to display $8$, all $7$ segments of some place to display a digit should be turned on.
You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than $n$ segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than $n$ segments.
Your program should be able to process $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases in the input.
Then the test cases follow, each of them is represented by a separate line containing one integer $n$ ($2 \le n \le 10^5$) β the maximum number of segments that can be turned on in the corresponding testcase.
It is guaranteed that the sum of $n$ over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the greatest integer that can be displayed by turning on no more than $n$ segments of the screen. Note that the answer may not fit in the standard $32$-bit or $64$-bit integral data type.
-----Example-----
Input
2
3
4
Output
7
11
|
for _ in range(int(input())):
n = int(input())
if(n%2):
print("7"+"1"*((n-3)//2))
else:
print("1"*(n//2))
|
You have a large electronic screen which can display up to $998244353$ decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of $7$ segments which can be turned on and off to compose different digits. The following picture describes how you can display all $10$ decimal digits:
[Image]
As you can see, different digits may require different number of segments to be turned on. For example, if you want to display $1$, you have to turn on $2$ segments of the screen, and if you want to display $8$, all $7$ segments of some place to display a digit should be turned on.
You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than $n$ segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than $n$ segments.
Your program should be able to process $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases in the input.
Then the test cases follow, each of them is represented by a separate line containing one integer $n$ ($2 \le n \le 10^5$) β the maximum number of segments that can be turned on in the corresponding testcase.
It is guaranteed that the sum of $n$ over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the greatest integer that can be displayed by turning on no more than $n$ segments of the screen. Note that the answer may not fit in the standard $32$-bit or $64$-bit integral data type.
-----Example-----
Input
2
3
4
Output
7
11
|
t=int(input())
for i in ' '*t:
n=int(input())
if n%2==1:print('7'+'1'*((n-3)//2))
else:print('1'*(n//2))
|
You have a large electronic screen which can display up to $998244353$ decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of $7$ segments which can be turned on and off to compose different digits. The following picture describes how you can display all $10$ decimal digits:
[Image]
As you can see, different digits may require different number of segments to be turned on. For example, if you want to display $1$, you have to turn on $2$ segments of the screen, and if you want to display $8$, all $7$ segments of some place to display a digit should be turned on.
You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than $n$ segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than $n$ segments.
Your program should be able to process $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases in the input.
Then the test cases follow, each of them is represented by a separate line containing one integer $n$ ($2 \le n \le 10^5$) β the maximum number of segments that can be turned on in the corresponding testcase.
It is guaranteed that the sum of $n$ over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the greatest integer that can be displayed by turning on no more than $n$ segments of the screen. Note that the answer may not fit in the standard $32$-bit or $64$-bit integral data type.
-----Example-----
Input
2
3
4
Output
7
11
|
for _ in range(int(input())):
n = int(input())
if n % 2:
print('7', end = "")
n -= 3
while n:
print('1', end = "")
n -= 2
print()
|
You have a large electronic screen which can display up to $998244353$ decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of $7$ segments which can be turned on and off to compose different digits. The following picture describes how you can display all $10$ decimal digits:
[Image]
As you can see, different digits may require different number of segments to be turned on. For example, if you want to display $1$, you have to turn on $2$ segments of the screen, and if you want to display $8$, all $7$ segments of some place to display a digit should be turned on.
You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than $n$ segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than $n$ segments.
Your program should be able to process $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases in the input.
Then the test cases follow, each of them is represented by a separate line containing one integer $n$ ($2 \le n \le 10^5$) β the maximum number of segments that can be turned on in the corresponding testcase.
It is guaranteed that the sum of $n$ over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the greatest integer that can be displayed by turning on no more than $n$ segments of the screen. Note that the answer may not fit in the standard $32$-bit or $64$-bit integral data type.
-----Example-----
Input
2
3
4
Output
7
11
|
T = int(input())
for kase in range(T):
n = int(input())
if n&1:
print('7'+(n-3)//2*'1')
else:
print(n//2*'1')
|
You have a large electronic screen which can display up to $998244353$ decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of $7$ segments which can be turned on and off to compose different digits. The following picture describes how you can display all $10$ decimal digits:
[Image]
As you can see, different digits may require different number of segments to be turned on. For example, if you want to display $1$, you have to turn on $2$ segments of the screen, and if you want to display $8$, all $7$ segments of some place to display a digit should be turned on.
You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than $n$ segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than $n$ segments.
Your program should be able to process $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases in the input.
Then the test cases follow, each of them is represented by a separate line containing one integer $n$ ($2 \le n \le 10^5$) β the maximum number of segments that can be turned on in the corresponding testcase.
It is guaranteed that the sum of $n$ over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the greatest integer that can be displayed by turning on no more than $n$ segments of the screen. Note that the answer may not fit in the standard $32$-bit or $64$-bit integral data type.
-----Example-----
Input
2
3
4
Output
7
11
|
for _ in range(int(input())):
n=int(input())
if n%2==1:
print("7"+"1"*(n//2-1))
else:
print("1"*(n//2))
|
You have a large electronic screen which can display up to $998244353$ decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of $7$ segments which can be turned on and off to compose different digits. The following picture describes how you can display all $10$ decimal digits:
[Image]
As you can see, different digits may require different number of segments to be turned on. For example, if you want to display $1$, you have to turn on $2$ segments of the screen, and if you want to display $8$, all $7$ segments of some place to display a digit should be turned on.
You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than $n$ segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than $n$ segments.
Your program should be able to process $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases in the input.
Then the test cases follow, each of them is represented by a separate line containing one integer $n$ ($2 \le n \le 10^5$) β the maximum number of segments that can be turned on in the corresponding testcase.
It is guaranteed that the sum of $n$ over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the greatest integer that can be displayed by turning on no more than $n$ segments of the screen. Note that the answer may not fit in the standard $32$-bit or $64$-bit integral data type.
-----Example-----
Input
2
3
4
Output
7
11
|
import sys
input = sys.stdin.readline
def getInt(): return int(input())
def getVars(): return list(map(int, input().split()))
def getList(): return list(map(int, input().split()))
def getStr(): return input().strip()
## -------------------------------
t = getInt()
for _ in range(t):
n = getInt()
if n%2 == 1:
print('7' + '1' * (n//2 - 1))
else:
print('1'*(n//2))
|
You have a large electronic screen which can display up to $998244353$ decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of $7$ segments which can be turned on and off to compose different digits. The following picture describes how you can display all $10$ decimal digits:
[Image]
As you can see, different digits may require different number of segments to be turned on. For example, if you want to display $1$, you have to turn on $2$ segments of the screen, and if you want to display $8$, all $7$ segments of some place to display a digit should be turned on.
You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than $n$ segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than $n$ segments.
Your program should be able to process $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases in the input.
Then the test cases follow, each of them is represented by a separate line containing one integer $n$ ($2 \le n \le 10^5$) β the maximum number of segments that can be turned on in the corresponding testcase.
It is guaranteed that the sum of $n$ over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the greatest integer that can be displayed by turning on no more than $n$ segments of the screen. Note that the answer may not fit in the standard $32$-bit or $64$-bit integral data type.
-----Example-----
Input
2
3
4
Output
7
11
|
for _ in range(int(input())):
n = int(input())
if n % 2 == 0:
print ('1' * (n // 2))
else:
print ('7' + '1' * ((n - 3) // 2))
|
You have a large electronic screen which can display up to $998244353$ decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of $7$ segments which can be turned on and off to compose different digits. The following picture describes how you can display all $10$ decimal digits:
[Image]
As you can see, different digits may require different number of segments to be turned on. For example, if you want to display $1$, you have to turn on $2$ segments of the screen, and if you want to display $8$, all $7$ segments of some place to display a digit should be turned on.
You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than $n$ segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than $n$ segments.
Your program should be able to process $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases in the input.
Then the test cases follow, each of them is represented by a separate line containing one integer $n$ ($2 \le n \le 10^5$) β the maximum number of segments that can be turned on in the corresponding testcase.
It is guaranteed that the sum of $n$ over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the greatest integer that can be displayed by turning on no more than $n$ segments of the screen. Note that the answer may not fit in the standard $32$-bit or $64$-bit integral data type.
-----Example-----
Input
2
3
4
Output
7
11
|
t = int(input())
for i in range(t):
n = int(input())
print("7" * (n % 2) + "1" * (n // 2 - (n % 2)))
|
You have a large electronic screen which can display up to $998244353$ decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of $7$ segments which can be turned on and off to compose different digits. The following picture describes how you can display all $10$ decimal digits:
[Image]
As you can see, different digits may require different number of segments to be turned on. For example, if you want to display $1$, you have to turn on $2$ segments of the screen, and if you want to display $8$, all $7$ segments of some place to display a digit should be turned on.
You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than $n$ segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than $n$ segments.
Your program should be able to process $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases in the input.
Then the test cases follow, each of them is represented by a separate line containing one integer $n$ ($2 \le n \le 10^5$) β the maximum number of segments that can be turned on in the corresponding testcase.
It is guaranteed that the sum of $n$ over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the greatest integer that can be displayed by turning on no more than $n$ segments of the screen. Note that the answer may not fit in the standard $32$-bit or $64$-bit integral data type.
-----Example-----
Input
2
3
4
Output
7
11
|
t = int(input())
for i in range(t):
n = int(input())
arr = ''
if (n % 2 == 1):
arr = '7'
for j in range((n - 3) // 2):
arr += '1'
else:
for j in range(n // 2):
arr += '1'
print(arr)
|
You have a large electronic screen which can display up to $998244353$ decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of $7$ segments which can be turned on and off to compose different digits. The following picture describes how you can display all $10$ decimal digits:
[Image]
As you can see, different digits may require different number of segments to be turned on. For example, if you want to display $1$, you have to turn on $2$ segments of the screen, and if you want to display $8$, all $7$ segments of some place to display a digit should be turned on.
You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than $n$ segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than $n$ segments.
Your program should be able to process $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases in the input.
Then the test cases follow, each of them is represented by a separate line containing one integer $n$ ($2 \le n \le 10^5$) β the maximum number of segments that can be turned on in the corresponding testcase.
It is guaranteed that the sum of $n$ over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the greatest integer that can be displayed by turning on no more than $n$ segments of the screen. Note that the answer may not fit in the standard $32$-bit or $64$-bit integral data type.
-----Example-----
Input
2
3
4
Output
7
11
|
import sys
import math
import itertools
import functools
import collections
import operator
def ii(): return int(input())
def mi(): return list(map(int, input().split()))
def li(): return list(map(int, input().split()))
def lcm(a, b): return abs(a * b) // math.gcd(a, b)
def revn(n): return str(n)[::-1]
def dd(): return collections.defaultdict(int)
def ddl(): return collections.defaultdict(list)
def sieve(n):
if n < 2: return list()
prime = [True for _ in range(n + 1)]
p = 3
while p * p <= n:
if prime[p]:
for i in range(p * 2, n + 1, p):
prime[i] = False
p += 2
r = [2]
for p in range(3, n + 1, 2):
if prime[p]:
r.append(p)
return r
def divs(n, start=2):
r = []
for i in range(start, int(math.sqrt(n) + 1)):
if (n % i == 0):
if (n / i == i):
r.append(i)
else:
r.extend([i, n // i])
return r
def divn(n, primes):
divs_number = 1
for i in primes:
if n == 1:
return divs_number
t = 1
while n % i == 0:
t += 1
n //= i
divs_number *= t
def prime(n):
if n == 2: return True
if n % 2 == 0 or n <= 1: return False
sqr = int(math.sqrt(n)) + 1
for d in range(3, sqr, 2):
if n % d == 0: return False
return True
def convn(number, base):
newnumber = 0
while number > 0:
newnumber += number % base
number //= base
return newnumber
def cdiv(n, k): return n // k + (n % k != 0)
t = ii()
for _ in range(t):
n = ii()
if n % 2:
print('7' + '1' * ((n - 3) // 2))
else:
print('1' * (n // 2))
|
You have a large electronic screen which can display up to $998244353$ decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of $7$ segments which can be turned on and off to compose different digits. The following picture describes how you can display all $10$ decimal digits:
[Image]
As you can see, different digits may require different number of segments to be turned on. For example, if you want to display $1$, you have to turn on $2$ segments of the screen, and if you want to display $8$, all $7$ segments of some place to display a digit should be turned on.
You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than $n$ segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than $n$ segments.
Your program should be able to process $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases in the input.
Then the test cases follow, each of them is represented by a separate line containing one integer $n$ ($2 \le n \le 10^5$) β the maximum number of segments that can be turned on in the corresponding testcase.
It is guaranteed that the sum of $n$ over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the greatest integer that can be displayed by turning on no more than $n$ segments of the screen. Note that the answer may not fit in the standard $32$-bit or $64$-bit integral data type.
-----Example-----
Input
2
3
4
Output
7
11
|
t = int(input())
for i in range(t):
n = int(input())
s = ''
if n % 2:
s = '7'
n -= 3
else:
s = '1'
n -= 2
s += '1' * (n // 2)
print(s)
|
You have a large electronic screen which can display up to $998244353$ decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of $7$ segments which can be turned on and off to compose different digits. The following picture describes how you can display all $10$ decimal digits:
[Image]
As you can see, different digits may require different number of segments to be turned on. For example, if you want to display $1$, you have to turn on $2$ segments of the screen, and if you want to display $8$, all $7$ segments of some place to display a digit should be turned on.
You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than $n$ segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than $n$ segments.
Your program should be able to process $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases in the input.
Then the test cases follow, each of them is represented by a separate line containing one integer $n$ ($2 \le n \le 10^5$) β the maximum number of segments that can be turned on in the corresponding testcase.
It is guaranteed that the sum of $n$ over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the greatest integer that can be displayed by turning on no more than $n$ segments of the screen. Note that the answer may not fit in the standard $32$-bit or $64$-bit integral data type.
-----Example-----
Input
2
3
4
Output
7
11
|
t=int(input())
for _ in range(t):
n=int(input())
if n%2==0:
print("1"*(n//2))
else:
print("7"+"1"*((n-3)//2))
|
You have a large electronic screen which can display up to $998244353$ decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of $7$ segments which can be turned on and off to compose different digits. The following picture describes how you can display all $10$ decimal digits:
[Image]
As you can see, different digits may require different number of segments to be turned on. For example, if you want to display $1$, you have to turn on $2$ segments of the screen, and if you want to display $8$, all $7$ segments of some place to display a digit should be turned on.
You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than $n$ segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than $n$ segments.
Your program should be able to process $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases in the input.
Then the test cases follow, each of them is represented by a separate line containing one integer $n$ ($2 \le n \le 10^5$) β the maximum number of segments that can be turned on in the corresponding testcase.
It is guaranteed that the sum of $n$ over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the greatest integer that can be displayed by turning on no more than $n$ segments of the screen. Note that the answer may not fit in the standard $32$-bit or $64$-bit integral data type.
-----Example-----
Input
2
3
4
Output
7
11
|
t = int(input())
for _ in range(t):
n = int(input())
if (n%2==0):
print('1'*(n//2))
else:
print('7'+'1'*((n-3)//2))
|
You have a large electronic screen which can display up to $998244353$ decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of $7$ segments which can be turned on and off to compose different digits. The following picture describes how you can display all $10$ decimal digits:
[Image]
As you can see, different digits may require different number of segments to be turned on. For example, if you want to display $1$, you have to turn on $2$ segments of the screen, and if you want to display $8$, all $7$ segments of some place to display a digit should be turned on.
You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than $n$ segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than $n$ segments.
Your program should be able to process $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases in the input.
Then the test cases follow, each of them is represented by a separate line containing one integer $n$ ($2 \le n \le 10^5$) β the maximum number of segments that can be turned on in the corresponding testcase.
It is guaranteed that the sum of $n$ over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the greatest integer that can be displayed by turning on no more than $n$ segments of the screen. Note that the answer may not fit in the standard $32$-bit or $64$-bit integral data type.
-----Example-----
Input
2
3
4
Output
7
11
|
for _ in range(int(input())):
n=int(input())
if n%2==0:
print('1'*(n//2))
else:
print('7'+('1'*((n//2)-1)))
|
You have a large electronic screen which can display up to $998244353$ decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of $7$ segments which can be turned on and off to compose different digits. The following picture describes how you can display all $10$ decimal digits:
[Image]
As you can see, different digits may require different number of segments to be turned on. For example, if you want to display $1$, you have to turn on $2$ segments of the screen, and if you want to display $8$, all $7$ segments of some place to display a digit should be turned on.
You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than $n$ segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than $n$ segments.
Your program should be able to process $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases in the input.
Then the test cases follow, each of them is represented by a separate line containing one integer $n$ ($2 \le n \le 10^5$) β the maximum number of segments that can be turned on in the corresponding testcase.
It is guaranteed that the sum of $n$ over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the greatest integer that can be displayed by turning on no more than $n$ segments of the screen. Note that the answer may not fit in the standard $32$-bit or $64$-bit integral data type.
-----Example-----
Input
2
3
4
Output
7
11
|
t = int(input())
while t:
t -= 1
n = int(input())
if n % 2 == 0:
print('1' * (n//2))
else:
print('7' + '1' * ((n - 3)//2))
|
You have a large electronic screen which can display up to $998244353$ decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of $7$ segments which can be turned on and off to compose different digits. The following picture describes how you can display all $10$ decimal digits:
[Image]
As you can see, different digits may require different number of segments to be turned on. For example, if you want to display $1$, you have to turn on $2$ segments of the screen, and if you want to display $8$, all $7$ segments of some place to display a digit should be turned on.
You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than $n$ segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than $n$ segments.
Your program should be able to process $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases in the input.
Then the test cases follow, each of them is represented by a separate line containing one integer $n$ ($2 \le n \le 10^5$) β the maximum number of segments that can be turned on in the corresponding testcase.
It is guaranteed that the sum of $n$ over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the greatest integer that can be displayed by turning on no more than $n$ segments of the screen. Note that the answer may not fit in the standard $32$-bit or $64$-bit integral data type.
-----Example-----
Input
2
3
4
Output
7
11
|
t = int(input())
for _ in range(t):
n = int(input())
if n % 2 == 0:
print('1'*(n//2))
else:
print('7'+'1'*(n//2-1))
|
You have a large electronic screen which can display up to $998244353$ decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of $7$ segments which can be turned on and off to compose different digits. The following picture describes how you can display all $10$ decimal digits:
[Image]
As you can see, different digits may require different number of segments to be turned on. For example, if you want to display $1$, you have to turn on $2$ segments of the screen, and if you want to display $8$, all $7$ segments of some place to display a digit should be turned on.
You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than $n$ segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than $n$ segments.
Your program should be able to process $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases in the input.
Then the test cases follow, each of them is represented by a separate line containing one integer $n$ ($2 \le n \le 10^5$) β the maximum number of segments that can be turned on in the corresponding testcase.
It is guaranteed that the sum of $n$ over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the greatest integer that can be displayed by turning on no more than $n$ segments of the screen. Note that the answer may not fit in the standard $32$-bit or $64$-bit integral data type.
-----Example-----
Input
2
3
4
Output
7
11
|
t = int(input())
while t:
n = int(input())
o = "1" * (n // 2)
if n % 2: o = "7" + o[1:]
print(o)
t -= 1
|
You have a large electronic screen which can display up to $998244353$ decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of $7$ segments which can be turned on and off to compose different digits. The following picture describes how you can display all $10$ decimal digits:
[Image]
As you can see, different digits may require different number of segments to be turned on. For example, if you want to display $1$, you have to turn on $2$ segments of the screen, and if you want to display $8$, all $7$ segments of some place to display a digit should be turned on.
You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than $n$ segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than $n$ segments.
Your program should be able to process $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases in the input.
Then the test cases follow, each of them is represented by a separate line containing one integer $n$ ($2 \le n \le 10^5$) β the maximum number of segments that can be turned on in the corresponding testcase.
It is guaranteed that the sum of $n$ over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the greatest integer that can be displayed by turning on no more than $n$ segments of the screen. Note that the answer may not fit in the standard $32$-bit or $64$-bit integral data type.
-----Example-----
Input
2
3
4
Output
7
11
|
n=int(input())
for i in range(n):
d=int(input())
if d%2==1:
e=(d-3)//2
s='7'+'1'*e
else:
e=d//2
s='1'*e
print(s)
|
You have a large electronic screen which can display up to $998244353$ decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of $7$ segments which can be turned on and off to compose different digits. The following picture describes how you can display all $10$ decimal digits:
[Image]
As you can see, different digits may require different number of segments to be turned on. For example, if you want to display $1$, you have to turn on $2$ segments of the screen, and if you want to display $8$, all $7$ segments of some place to display a digit should be turned on.
You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than $n$ segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than $n$ segments.
Your program should be able to process $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases in the input.
Then the test cases follow, each of them is represented by a separate line containing one integer $n$ ($2 \le n \le 10^5$) β the maximum number of segments that can be turned on in the corresponding testcase.
It is guaranteed that the sum of $n$ over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the greatest integer that can be displayed by turning on no more than $n$ segments of the screen. Note that the answer may not fit in the standard $32$-bit or $64$-bit integral data type.
-----Example-----
Input
2
3
4
Output
7
11
|
t = int(input())
for _ in range(t):
n = int(input())
if n % 2 == 0:
print("1"*(n//2))
else:
print("7"+"1"*(n//2-1))
|
You have a large electronic screen which can display up to $998244353$ decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of $7$ segments which can be turned on and off to compose different digits. The following picture describes how you can display all $10$ decimal digits:
[Image]
As you can see, different digits may require different number of segments to be turned on. For example, if you want to display $1$, you have to turn on $2$ segments of the screen, and if you want to display $8$, all $7$ segments of some place to display a digit should be turned on.
You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than $n$ segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than $n$ segments.
Your program should be able to process $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases in the input.
Then the test cases follow, each of them is represented by a separate line containing one integer $n$ ($2 \le n \le 10^5$) β the maximum number of segments that can be turned on in the corresponding testcase.
It is guaranteed that the sum of $n$ over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the greatest integer that can be displayed by turning on no more than $n$ segments of the screen. Note that the answer may not fit in the standard $32$-bit or $64$-bit integral data type.
-----Example-----
Input
2
3
4
Output
7
11
|
t=int(input())
for _ in range(t):
n=int(input())
if n%2==0:
x=n//2
for i in range(x):
print(1,end='')
else:
x=n//2
x-=1
print(7,end='')
for i in range(x):
print(1,end='')
print()
|
You have a large electronic screen which can display up to $998244353$ decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of $7$ segments which can be turned on and off to compose different digits. The following picture describes how you can display all $10$ decimal digits:
[Image]
As you can see, different digits may require different number of segments to be turned on. For example, if you want to display $1$, you have to turn on $2$ segments of the screen, and if you want to display $8$, all $7$ segments of some place to display a digit should be turned on.
You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than $n$ segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than $n$ segments.
Your program should be able to process $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases in the input.
Then the test cases follow, each of them is represented by a separate line containing one integer $n$ ($2 \le n \le 10^5$) β the maximum number of segments that can be turned on in the corresponding testcase.
It is guaranteed that the sum of $n$ over all test cases in the input does not exceed $10^5$.
-----Output-----
For each test case, print the greatest integer that can be displayed by turning on no more than $n$ segments of the screen. Note that the answer may not fit in the standard $32$-bit or $64$-bit integral data type.
-----Example-----
Input
2
3
4
Output
7
11
|
t = int(input())
for y in range(t):
n = int(input())
s = ""
if(n%2 == 1):
s += '7'
n -= 3
s += (n//2)*'1'
print(int(s))
|
Young wilderness explorers set off to their first expedition led by senior explorer Russell. Explorers went into a forest, set up a camp and decided to split into groups to explore as much interesting locations as possible. Russell was trying to form groups, but ran into some difficulties...
Most of the young explorers are inexperienced, and sending them alone would be a mistake. Even Russell himself became senior explorer not long ago. Each of young explorers has a positive integer parameter $e_i$Β β his inexperience. Russell decided that an explorer with inexperience $e$ can only join the group of $e$ or more people.
Now Russell needs to figure out how many groups he can organize. It's not necessary to include every explorer in one of the groups: some can stay in the camp. Russell is worried about this expedition, so he asked you to help him.
-----Input-----
The first line contains the number of independent test cases $T$($1 \leq T \leq 2 \cdot 10^5$). Next $2T$ lines contain description of test cases.
The first line of description of each test case contains the number of young explorers $N$ ($1 \leq N \leq 2 \cdot 10^5$).
The second line contains $N$ integers $e_1, e_2, \ldots, e_N$ ($1 \leq e_i \leq N$), where $e_i$ is the inexperience of the $i$-th explorer.
It's guaranteed that sum of all $N$ doesn't exceed $3 \cdot 10^5$.
-----Output-----
Print $T$ numbers, each number on a separate line.
In $i$-th line print the maximum number of groups Russell can form in $i$-th test case.
-----Example-----
Input
2
3
1 1 1
5
2 3 1 2 2
Output
3
2
-----Note-----
In the first example we can organize three groups. There will be only one explorer in each group. It's correct because inexperience of each explorer equals to $1$, so it's not less than the size of his group.
In the second example we can organize two groups. Explorers with inexperience $1$, $2$ and $3$ will form the first group, and the other two explorers with inexperience equal to $2$ will form the second group.
This solution is not unique. For example, we can form the first group using the three explorers with inexperience equal to $2$, and the second group using only one explorer with inexperience equal to $1$. In this case the young explorer with inexperience equal to $3$ will not be included in any group.
|
import sys
input=sys.stdin.readline
for _ in range(int(input())):
N=int(input())
e=list(map(int,input().split()))
e.sort()
ans=0
val=0
g=0
for i in range(0,N):
g+=1
val=e[i]
if g>=val:
ans+=1
g=0
val=0
print(ans)
|
Young wilderness explorers set off to their first expedition led by senior explorer Russell. Explorers went into a forest, set up a camp and decided to split into groups to explore as much interesting locations as possible. Russell was trying to form groups, but ran into some difficulties...
Most of the young explorers are inexperienced, and sending them alone would be a mistake. Even Russell himself became senior explorer not long ago. Each of young explorers has a positive integer parameter $e_i$Β β his inexperience. Russell decided that an explorer with inexperience $e$ can only join the group of $e$ or more people.
Now Russell needs to figure out how many groups he can organize. It's not necessary to include every explorer in one of the groups: some can stay in the camp. Russell is worried about this expedition, so he asked you to help him.
-----Input-----
The first line contains the number of independent test cases $T$($1 \leq T \leq 2 \cdot 10^5$). Next $2T$ lines contain description of test cases.
The first line of description of each test case contains the number of young explorers $N$ ($1 \leq N \leq 2 \cdot 10^5$).
The second line contains $N$ integers $e_1, e_2, \ldots, e_N$ ($1 \leq e_i \leq N$), where $e_i$ is the inexperience of the $i$-th explorer.
It's guaranteed that sum of all $N$ doesn't exceed $3 \cdot 10^5$.
-----Output-----
Print $T$ numbers, each number on a separate line.
In $i$-th line print the maximum number of groups Russell can form in $i$-th test case.
-----Example-----
Input
2
3
1 1 1
5
2 3 1 2 2
Output
3
2
-----Note-----
In the first example we can organize three groups. There will be only one explorer in each group. It's correct because inexperience of each explorer equals to $1$, so it's not less than the size of his group.
In the second example we can organize two groups. Explorers with inexperience $1$, $2$ and $3$ will form the first group, and the other two explorers with inexperience equal to $2$ will form the second group.
This solution is not unique. For example, we can form the first group using the three explorers with inexperience equal to $2$, and the second group using only one explorer with inexperience equal to $1$. In this case the young explorer with inexperience equal to $3$ will not be included in any group.
|
from sys import stdin
for _ in range(int(stdin.readline())):
n = int(stdin.readline())
ans = 0
arr = sorted(list(map(int,stdin.readline().split())))
peo = 0
for i in range(n):
peo += 1
if peo == arr[i]:
ans += 1
peo = 0
print(ans)
|
Young wilderness explorers set off to their first expedition led by senior explorer Russell. Explorers went into a forest, set up a camp and decided to split into groups to explore as much interesting locations as possible. Russell was trying to form groups, but ran into some difficulties...
Most of the young explorers are inexperienced, and sending them alone would be a mistake. Even Russell himself became senior explorer not long ago. Each of young explorers has a positive integer parameter $e_i$Β β his inexperience. Russell decided that an explorer with inexperience $e$ can only join the group of $e$ or more people.
Now Russell needs to figure out how many groups he can organize. It's not necessary to include every explorer in one of the groups: some can stay in the camp. Russell is worried about this expedition, so he asked you to help him.
-----Input-----
The first line contains the number of independent test cases $T$($1 \leq T \leq 2 \cdot 10^5$). Next $2T$ lines contain description of test cases.
The first line of description of each test case contains the number of young explorers $N$ ($1 \leq N \leq 2 \cdot 10^5$).
The second line contains $N$ integers $e_1, e_2, \ldots, e_N$ ($1 \leq e_i \leq N$), where $e_i$ is the inexperience of the $i$-th explorer.
It's guaranteed that sum of all $N$ doesn't exceed $3 \cdot 10^5$.
-----Output-----
Print $T$ numbers, each number on a separate line.
In $i$-th line print the maximum number of groups Russell can form in $i$-th test case.
-----Example-----
Input
2
3
1 1 1
5
2 3 1 2 2
Output
3
2
-----Note-----
In the first example we can organize three groups. There will be only one explorer in each group. It's correct because inexperience of each explorer equals to $1$, so it's not less than the size of his group.
In the second example we can organize two groups. Explorers with inexperience $1$, $2$ and $3$ will form the first group, and the other two explorers with inexperience equal to $2$ will form the second group.
This solution is not unique. For example, we can form the first group using the three explorers with inexperience equal to $2$, and the second group using only one explorer with inexperience equal to $1$. In this case the young explorer with inexperience equal to $3$ will not be included in any group.
|
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int,input().split()))
a.sort()
ans = 0
sepa = -1
for i in range(n):
if i-sepa >= a[i]:
sepa = i
ans += 1
print(ans)
|
Young wilderness explorers set off to their first expedition led by senior explorer Russell. Explorers went into a forest, set up a camp and decided to split into groups to explore as much interesting locations as possible. Russell was trying to form groups, but ran into some difficulties...
Most of the young explorers are inexperienced, and sending them alone would be a mistake. Even Russell himself became senior explorer not long ago. Each of young explorers has a positive integer parameter $e_i$Β β his inexperience. Russell decided that an explorer with inexperience $e$ can only join the group of $e$ or more people.
Now Russell needs to figure out how many groups he can organize. It's not necessary to include every explorer in one of the groups: some can stay in the camp. Russell is worried about this expedition, so he asked you to help him.
-----Input-----
The first line contains the number of independent test cases $T$($1 \leq T \leq 2 \cdot 10^5$). Next $2T$ lines contain description of test cases.
The first line of description of each test case contains the number of young explorers $N$ ($1 \leq N \leq 2 \cdot 10^5$).
The second line contains $N$ integers $e_1, e_2, \ldots, e_N$ ($1 \leq e_i \leq N$), where $e_i$ is the inexperience of the $i$-th explorer.
It's guaranteed that sum of all $N$ doesn't exceed $3 \cdot 10^5$.
-----Output-----
Print $T$ numbers, each number on a separate line.
In $i$-th line print the maximum number of groups Russell can form in $i$-th test case.
-----Example-----
Input
2
3
1 1 1
5
2 3 1 2 2
Output
3
2
-----Note-----
In the first example we can organize three groups. There will be only one explorer in each group. It's correct because inexperience of each explorer equals to $1$, so it's not less than the size of his group.
In the second example we can organize two groups. Explorers with inexperience $1$, $2$ and $3$ will form the first group, and the other two explorers with inexperience equal to $2$ will form the second group.
This solution is not unique. For example, we can form the first group using the three explorers with inexperience equal to $2$, and the second group using only one explorer with inexperience equal to $1$. In this case the young explorer with inexperience equal to $3$ will not be included in any group.
|
import sys
def solve():
input = sys.stdin.readline
T = int(input())
Ans = [0] * T
for t in range(T):
N = int(input())
A = [int(a) for a in input().split()]
skillDict = dict()
for a in A:
if a in skillDict: skillDict[a] += 1
else: skillDict[a] = 1
for i in range(1, N+1):
if i in skillDict:
Ans[t] += skillDict[i] // i
if i+1 not in skillDict: skillDict[i+1] = 0
skillDict[i+1] += skillDict[i] % i
print("\n".join(map(str, Ans)))
return 0
def __starting_point():
solve()
__starting_point()
|
Young wilderness explorers set off to their first expedition led by senior explorer Russell. Explorers went into a forest, set up a camp and decided to split into groups to explore as much interesting locations as possible. Russell was trying to form groups, but ran into some difficulties...
Most of the young explorers are inexperienced, and sending them alone would be a mistake. Even Russell himself became senior explorer not long ago. Each of young explorers has a positive integer parameter $e_i$Β β his inexperience. Russell decided that an explorer with inexperience $e$ can only join the group of $e$ or more people.
Now Russell needs to figure out how many groups he can organize. It's not necessary to include every explorer in one of the groups: some can stay in the camp. Russell is worried about this expedition, so he asked you to help him.
-----Input-----
The first line contains the number of independent test cases $T$($1 \leq T \leq 2 \cdot 10^5$). Next $2T$ lines contain description of test cases.
The first line of description of each test case contains the number of young explorers $N$ ($1 \leq N \leq 2 \cdot 10^5$).
The second line contains $N$ integers $e_1, e_2, \ldots, e_N$ ($1 \leq e_i \leq N$), where $e_i$ is the inexperience of the $i$-th explorer.
It's guaranteed that sum of all $N$ doesn't exceed $3 \cdot 10^5$.
-----Output-----
Print $T$ numbers, each number on a separate line.
In $i$-th line print the maximum number of groups Russell can form in $i$-th test case.
-----Example-----
Input
2
3
1 1 1
5
2 3 1 2 2
Output
3
2
-----Note-----
In the first example we can organize three groups. There will be only one explorer in each group. It's correct because inexperience of each explorer equals to $1$, so it's not less than the size of his group.
In the second example we can organize two groups. Explorers with inexperience $1$, $2$ and $3$ will form the first group, and the other two explorers with inexperience equal to $2$ will form the second group.
This solution is not unique. For example, we can form the first group using the three explorers with inexperience equal to $2$, and the second group using only one explorer with inexperience equal to $1$. In this case the young explorer with inexperience equal to $3$ will not be included in any group.
|
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
e = list(map(int, input().split()))
e = sorted(e)
ans = 0
cnt = 0
max_ = 0
for i in range(n):
cnt += 1
max_ = max(e[i], max_)
if max_ <= cnt:
ans += 1
max_ = 0
cnt = 0
print(ans)
|
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.
Marmot brought Mole n ordered piles of worms such that i-th pile contains a_{i} worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a_1, worms in second pile are labeled with numbers a_1 + 1 to a_1 + a_2 and so on. See the example for a better understanding.
Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.
Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^5), the number of piles.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^3, a_1 + a_2 + ... + a_{n} β€ 10^6), where a_{i} is the number of worms in the i-th pile.
The third line contains single integer m (1 β€ m β€ 10^5), the number of juicy worms said by Marmot.
The fourth line contains m integers q_1, q_2, ..., q_{m} (1 β€ q_{i} β€ a_1 + a_2 + ... + a_{n}), the labels of the juicy worms.
-----Output-----
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number q_{i} is.
-----Examples-----
Input
5
2 7 3 4 9
3
1 25 11
Output
1
5
3
-----Note-----
For the sample input:
The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
|
n=int(input())
a=list(map(int,input().split()))
k=[]
for i in range(n):
for j in range(a[i]):
k.append(i+1)
m=int(input())
b=list(map(int,input().split()))
for i in b:
print(k[i-1])
|
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.
Marmot brought Mole n ordered piles of worms such that i-th pile contains a_{i} worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a_1, worms in second pile are labeled with numbers a_1 + 1 to a_1 + a_2 and so on. See the example for a better understanding.
Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.
Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^5), the number of piles.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^3, a_1 + a_2 + ... + a_{n} β€ 10^6), where a_{i} is the number of worms in the i-th pile.
The third line contains single integer m (1 β€ m β€ 10^5), the number of juicy worms said by Marmot.
The fourth line contains m integers q_1, q_2, ..., q_{m} (1 β€ q_{i} β€ a_1 + a_2 + ... + a_{n}), the labels of the juicy worms.
-----Output-----
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number q_{i} is.
-----Examples-----
Input
5
2 7 3 4 9
3
1 25 11
Output
1
5
3
-----Note-----
For the sample input:
The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
|
n, p, m, w = int(input()), list(map(int, input().split())), int(input()), sorted(enumerate(map(int, input().split())), key = lambda x: x[1])
ans, pos = [-1] * m, [0, 0]
for i, c in w:
while pos[0] + p[pos[1]] < c:
pos[0] += p[pos[1]]
pos[1] += 1
ans[i] = pos[1] + 1
print(*ans, sep = '\n')
|
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.
Marmot brought Mole n ordered piles of worms such that i-th pile contains a_{i} worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a_1, worms in second pile are labeled with numbers a_1 + 1 to a_1 + a_2 and so on. See the example for a better understanding.
Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.
Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^5), the number of piles.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^3, a_1 + a_2 + ... + a_{n} β€ 10^6), where a_{i} is the number of worms in the i-th pile.
The third line contains single integer m (1 β€ m β€ 10^5), the number of juicy worms said by Marmot.
The fourth line contains m integers q_1, q_2, ..., q_{m} (1 β€ q_{i} β€ a_1 + a_2 + ... + a_{n}), the labels of the juicy worms.
-----Output-----
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number q_{i} is.
-----Examples-----
Input
5
2 7 3 4 9
3
1 25 11
Output
1
5
3
-----Note-----
For the sample input:
The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
|
from sys import stdin, stdout
from bisect import *
input = stdin.read()
n, ai_str, m, qi_str = [_f for _f in input.split('\n') if _f]
a = list(map(int, ai_str.split()))
q = list(map(int, qi_str.split()))
assert len(a) > 0 and len(q) > 0
b = [0] * len(a)
for i, ai in enumerate(a):
b[i] = b[i-1] + ai
for qi in q:
print(bisect_left(b, qi) + 1)
|
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.
Marmot brought Mole n ordered piles of worms such that i-th pile contains a_{i} worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a_1, worms in second pile are labeled with numbers a_1 + 1 to a_1 + a_2 and so on. See the example for a better understanding.
Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.
Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^5), the number of piles.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^3, a_1 + a_2 + ... + a_{n} β€ 10^6), where a_{i} is the number of worms in the i-th pile.
The third line contains single integer m (1 β€ m β€ 10^5), the number of juicy worms said by Marmot.
The fourth line contains m integers q_1, q_2, ..., q_{m} (1 β€ q_{i} β€ a_1 + a_2 + ... + a_{n}), the labels of the juicy worms.
-----Output-----
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number q_{i} is.
-----Examples-----
Input
5
2 7 3 4 9
3
1 25 11
Output
1
5
3
-----Note-----
For the sample input:
The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
|
input()
heaps = list(map(int, input().split()))
input()
numbers = list(map(int, input().split()))
#heaps = [2, 7, 3, 4, 9]
#numbers = [1, 25, 11]
res = [0] * len(numbers)
sums = [heaps[0]]
mask = [1] * heaps[0]
for i in range(1, len(heaps)):
mask += [i+1] * (heaps[i])
sums.append(heaps[i] + sums[-1])
for i in range(len(numbers)):
print(mask[numbers[i]-1])
|
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.
Marmot brought Mole n ordered piles of worms such that i-th pile contains a_{i} worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a_1, worms in second pile are labeled with numbers a_1 + 1 to a_1 + a_2 and so on. See the example for a better understanding.
Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.
Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^5), the number of piles.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^3, a_1 + a_2 + ... + a_{n} β€ 10^6), where a_{i} is the number of worms in the i-th pile.
The third line contains single integer m (1 β€ m β€ 10^5), the number of juicy worms said by Marmot.
The fourth line contains m integers q_1, q_2, ..., q_{m} (1 β€ q_{i} β€ a_1 + a_2 + ... + a_{n}), the labels of the juicy worms.
-----Output-----
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number q_{i} is.
-----Examples-----
Input
5
2 7 3 4 9
3
1 25 11
Output
1
5
3
-----Note-----
For the sample input:
The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
|
num = int(input())
piles = list(map(int, input().split(' ')))
tuplex = []
curr = 1
for i in piles:
tuplex.append((curr, curr+i-1))
curr = curr+i
quer = int(input())
queries = list(map(int, input().split(' ')))
quer2 = [[queries[x], x, -1] for x in range(len(queries))]
quer2.sort(key = lambda x:x[0])
ind = 0
for i in range(len(quer2)):
while not (tuplex[ind][0] <= quer2[i][0] <= tuplex[ind][1]):
ind += 1
quer2[i][2] = ind
quer2.sort(key = lambda x:x[1])
for i in quer2:
print(i[2]+1)
|
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.
Marmot brought Mole n ordered piles of worms such that i-th pile contains a_{i} worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a_1, worms in second pile are labeled with numbers a_1 + 1 to a_1 + a_2 and so on. See the example for a better understanding.
Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.
Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^5), the number of piles.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^3, a_1 + a_2 + ... + a_{n} β€ 10^6), where a_{i} is the number of worms in the i-th pile.
The third line contains single integer m (1 β€ m β€ 10^5), the number of juicy worms said by Marmot.
The fourth line contains m integers q_1, q_2, ..., q_{m} (1 β€ q_{i} β€ a_1 + a_2 + ... + a_{n}), the labels of the juicy worms.
-----Output-----
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number q_{i} is.
-----Examples-----
Input
5
2 7 3 4 9
3
1 25 11
Output
1
5
3
-----Note-----
For the sample input:
The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
|
kheap=int(input())
heaps=list(map(int,input().split()))
kworms=int(input())
worms=list(map(int,input().split()))
d1={i:0 for i in range(1,sum(heaps)+1)}
prev=0
counter=1
for i in heaps:
start=prev+1
prev+=i
for i2 in range(start,prev+1):
d1[i2]=counter
counter+=1
for num in worms:
print(d1[num])
|
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.
Marmot brought Mole n ordered piles of worms such that i-th pile contains a_{i} worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a_1, worms in second pile are labeled with numbers a_1 + 1 to a_1 + a_2 and so on. See the example for a better understanding.
Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.
Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^5), the number of piles.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^3, a_1 + a_2 + ... + a_{n} β€ 10^6), where a_{i} is the number of worms in the i-th pile.
The third line contains single integer m (1 β€ m β€ 10^5), the number of juicy worms said by Marmot.
The fourth line contains m integers q_1, q_2, ..., q_{m} (1 β€ q_{i} β€ a_1 + a_2 + ... + a_{n}), the labels of the juicy worms.
-----Output-----
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number q_{i} is.
-----Examples-----
Input
5
2 7 3 4 9
3
1 25 11
Output
1
5
3
-----Note-----
For the sample input:
The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
|
'''
Created on Oct 6, 2014
@author: Ismael
'''
n = int(input())
A = list(map(int,input().split()))
q = int(input())
Q = list(map(int,input().split()))
ans = []
prec = 1
iStack = 0
for ai in A:
iStack += 1
for query in range(prec,prec+ai):
ans.append(iStack)
prec = ai
for query in Q:
print(ans[query-1])
|
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.
Marmot brought Mole n ordered piles of worms such that i-th pile contains a_{i} worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a_1, worms in second pile are labeled with numbers a_1 + 1 to a_1 + a_2 and so on. See the example for a better understanding.
Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.
Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^5), the number of piles.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^3, a_1 + a_2 + ... + a_{n} β€ 10^6), where a_{i} is the number of worms in the i-th pile.
The third line contains single integer m (1 β€ m β€ 10^5), the number of juicy worms said by Marmot.
The fourth line contains m integers q_1, q_2, ..., q_{m} (1 β€ q_{i} β€ a_1 + a_2 + ... + a_{n}), the labels of the juicy worms.
-----Output-----
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number q_{i} is.
-----Examples-----
Input
5
2 7 3 4 9
3
1 25 11
Output
1
5
3
-----Note-----
For the sample input:
The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
|
#class heap:
#def __init__(self, first, last):
#self.first = first
#self.last = last
#def __contains__(self, x):
#if self.first <= x <= self.last:
#return True
#else:
#return False
def borders(nums):
prev = 1
for x in nums:
yield prev, prev + x - 1
prev += x
def inside(x, first, last):
return first <= x <= last
#nums = list(int(x) for x in '2 7 3 4 9'.split(" "))
#print(nums)
#print(list(borders(nums)))
#j = list(int(x) for x in '1 25 11'.split(" "))
heapsamount = int(input())
nums = list(int(x) for x in input().split(" "))
jamount = int(input())
j = list(int(x) for x in input().split(" "))
#heapsamount = 5
#nums = list(int(x) for x in '2 7 3 4 9'.split(" "))
#jamount = 4
#j = [1, 25, 11, 4]
b= list(borders(nums))
#for hp, number in zip(hps, j):
#hps = list(heap(*args) for args in b)
#for number in j:
#for hp, hpnum in zip(hps, range(1,heapsamount+1)):
#if number in hp:
#print(hpnum)
sor = list([x, y, None] for x, y in zip(j, list(range(jamount))))
sor.sort(key=lambda x: x[0])
i=0
j=0
for number, index, n in sor:
bord = b[i]
while not inside(number, bord[0], bord[1]):
i+=1
bord = b[i]
#while inside(number, bord[0], bord[1]):
sor[j][2] = i+1
j+=1
sor.sort(key=lambda x:x[1])
for x in sor:
print(x[2])
|
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.
Marmot brought Mole n ordered piles of worms such that i-th pile contains a_{i} worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a_1, worms in second pile are labeled with numbers a_1 + 1 to a_1 + a_2 and so on. See the example for a better understanding.
Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.
Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^5), the number of piles.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^3, a_1 + a_2 + ... + a_{n} β€ 10^6), where a_{i} is the number of worms in the i-th pile.
The third line contains single integer m (1 β€ m β€ 10^5), the number of juicy worms said by Marmot.
The fourth line contains m integers q_1, q_2, ..., q_{m} (1 β€ q_{i} β€ a_1 + a_2 + ... + a_{n}), the labels of the juicy worms.
-----Output-----
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number q_{i} is.
-----Examples-----
Input
5
2 7 3 4 9
3
1 25 11
Output
1
5
3
-----Note-----
For the sample input:
The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
|
n,a = int(input()), list(map(int, input().split()))
m,q = int(input()), list(map(int, input().split()))
dp = []
for i in range(n):
dp += [i+1]*a[i]
for x in q:
print (dp[x-1])
|
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.
Marmot brought Mole n ordered piles of worms such that i-th pile contains a_{i} worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a_1, worms in second pile are labeled with numbers a_1 + 1 to a_1 + a_2 and so on. See the example for a better understanding.
Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.
Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^5), the number of piles.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^3, a_1 + a_2 + ... + a_{n} β€ 10^6), where a_{i} is the number of worms in the i-th pile.
The third line contains single integer m (1 β€ m β€ 10^5), the number of juicy worms said by Marmot.
The fourth line contains m integers q_1, q_2, ..., q_{m} (1 β€ q_{i} β€ a_1 + a_2 + ... + a_{n}), the labels of the juicy worms.
-----Output-----
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number q_{i} is.
-----Examples-----
Input
5
2 7 3 4 9
3
1 25 11
Output
1
5
3
-----Note-----
For the sample input:
The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
|
__author__ = 'hamed1soleimani'
import math
input()
p = input().split()
input()
q = input().split()
worms = list(range(10 ** 6))
m = 0
for i in range(len(p)):
for j in range(int(p[i])):
worms[m] = i
m += 1
for x in q:
print(worms[int(x) - 1] + 1)
|
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.
Marmot brought Mole n ordered piles of worms such that i-th pile contains a_{i} worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a_1, worms in second pile are labeled with numbers a_1 + 1 to a_1 + a_2 and so on. See the example for a better understanding.
Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.
Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^5), the number of piles.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^3, a_1 + a_2 + ... + a_{n} β€ 10^6), where a_{i} is the number of worms in the i-th pile.
The third line contains single integer m (1 β€ m β€ 10^5), the number of juicy worms said by Marmot.
The fourth line contains m integers q_1, q_2, ..., q_{m} (1 β€ q_{i} β€ a_1 + a_2 + ... + a_{n}), the labels of the juicy worms.
-----Output-----
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number q_{i} is.
-----Examples-----
Input
5
2 7 3 4 9
3
1 25 11
Output
1
5
3
-----Note-----
For the sample input:
The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
|
n=int(input())
a=list(map(int,input().split()))
m=int(input())
q=list(map(int,input().split()))
b=[]
for i in range(n):
b+=[i+1]*a[i]
for i in q:
print(b[i-1])
|
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.
Marmot brought Mole n ordered piles of worms such that i-th pile contains a_{i} worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a_1, worms in second pile are labeled with numbers a_1 + 1 to a_1 + a_2 and so on. See the example for a better understanding.
Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.
Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^5), the number of piles.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^3, a_1 + a_2 + ... + a_{n} β€ 10^6), where a_{i} is the number of worms in the i-th pile.
The third line contains single integer m (1 β€ m β€ 10^5), the number of juicy worms said by Marmot.
The fourth line contains m integers q_1, q_2, ..., q_{m} (1 β€ q_{i} β€ a_1 + a_2 + ... + a_{n}), the labels of the juicy worms.
-----Output-----
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number q_{i} is.
-----Examples-----
Input
5
2 7 3 4 9
3
1 25 11
Output
1
5
3
-----Note-----
For the sample input:
The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
|
n = int(input())
a = list(map(int, input().split()))
input()
queries = list(map(int, input().split()))
ans = []
for i in range(n):
ans += [i]*a[i]
for q in queries:
print(ans[q-1]+1)
|
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.
Marmot brought Mole n ordered piles of worms such that i-th pile contains a_{i} worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a_1, worms in second pile are labeled with numbers a_1 + 1 to a_1 + a_2 and so on. See the example for a better understanding.
Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.
Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^5), the number of piles.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^3, a_1 + a_2 + ... + a_{n} β€ 10^6), where a_{i} is the number of worms in the i-th pile.
The third line contains single integer m (1 β€ m β€ 10^5), the number of juicy worms said by Marmot.
The fourth line contains m integers q_1, q_2, ..., q_{m} (1 β€ q_{i} β€ a_1 + a_2 + ... + a_{n}), the labels of the juicy worms.
-----Output-----
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number q_{i} is.
-----Examples-----
Input
5
2 7 3 4 9
3
1 25 11
Output
1
5
3
-----Note-----
For the sample input:
The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
|
n = int(input())
pre = []
row = 1
for i in input().split(' '):
for j in range(int(i)):
pre.append(row)
row += 1
m = int(input())
tasty_worms = []
for i in input().split(' '):
i = int(i)
print(pre[i - 1])
|
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.
Marmot brought Mole n ordered piles of worms such that i-th pile contains a_{i} worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a_1, worms in second pile are labeled with numbers a_1 + 1 to a_1 + a_2 and so on. See the example for a better understanding.
Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.
Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^5), the number of piles.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^3, a_1 + a_2 + ... + a_{n} β€ 10^6), where a_{i} is the number of worms in the i-th pile.
The third line contains single integer m (1 β€ m β€ 10^5), the number of juicy worms said by Marmot.
The fourth line contains m integers q_1, q_2, ..., q_{m} (1 β€ q_{i} β€ a_1 + a_2 + ... + a_{n}), the labels of the juicy worms.
-----Output-----
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number q_{i} is.
-----Examples-----
Input
5
2 7 3 4 9
3
1 25 11
Output
1
5
3
-----Note-----
For the sample input:
The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
|
mp = {}
def main():
a,b =-1,0
n = int(input())
line = input() #Read the whole line
x = line.split()
for i in range(n):
a = b
b = b + int(x[i])
for k in range(a+1,b+1):
mp[k] = 1 + i
m = int(input())
line = input()
q = line.split()
for i in range(m):
print(mp[int(q[i])])
main()
|
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.
Marmot brought Mole n ordered piles of worms such that i-th pile contains a_{i} worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a_1, worms in second pile are labeled with numbers a_1 + 1 to a_1 + a_2 and so on. See the example for a better understanding.
Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.
Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^5), the number of piles.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^3, a_1 + a_2 + ... + a_{n} β€ 10^6), where a_{i} is the number of worms in the i-th pile.
The third line contains single integer m (1 β€ m β€ 10^5), the number of juicy worms said by Marmot.
The fourth line contains m integers q_1, q_2, ..., q_{m} (1 β€ q_{i} β€ a_1 + a_2 + ... + a_{n}), the labels of the juicy worms.
-----Output-----
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number q_{i} is.
-----Examples-----
Input
5
2 7 3 4 9
3
1 25 11
Output
1
5
3
-----Note-----
For the sample input:
The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
|
from itertools import accumulate
def bs(ws, w):
i, e = -1, len(ws)-1
while e-i > 1:
m = (e+i)//2
if w <= ws[m]:
e = m
else:
i = m
return e
input()
worms = list(accumulate(map(int, input().split())))
input()
tofind = list(map(int, input().split()))
print("\n".join(str(bs(worms, w)+1) for w in tofind))
|
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.
Marmot brought Mole n ordered piles of worms such that i-th pile contains a_{i} worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a_1, worms in second pile are labeled with numbers a_1 + 1 to a_1 + a_2 and so on. See the example for a better understanding.
Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.
Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^5), the number of piles.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^3, a_1 + a_2 + ... + a_{n} β€ 10^6), where a_{i} is the number of worms in the i-th pile.
The third line contains single integer m (1 β€ m β€ 10^5), the number of juicy worms said by Marmot.
The fourth line contains m integers q_1, q_2, ..., q_{m} (1 β€ q_{i} β€ a_1 + a_2 + ... + a_{n}), the labels of the juicy worms.
-----Output-----
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number q_{i} is.
-----Examples-----
Input
5
2 7 3 4 9
3
1 25 11
Output
1
5
3
-----Note-----
For the sample input:
The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
|
3
from bisect import bisect_left
n = int(input())
A = list(map(int, input().split()))
m = int(input())
Q = list(map(int, input().split()))
sum_A = list(A)
for i in range(1, n):
sum_A[i] += sum_A[i-1]
for q in Q:
print(bisect_left(sum_A, q) + 1)
|
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.
Marmot brought Mole n ordered piles of worms such that i-th pile contains a_{i} worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a_1, worms in second pile are labeled with numbers a_1 + 1 to a_1 + a_2 and so on. See the example for a better understanding.
Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.
Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^5), the number of piles.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^3, a_1 + a_2 + ... + a_{n} β€ 10^6), where a_{i} is the number of worms in the i-th pile.
The third line contains single integer m (1 β€ m β€ 10^5), the number of juicy worms said by Marmot.
The fourth line contains m integers q_1, q_2, ..., q_{m} (1 β€ q_{i} β€ a_1 + a_2 + ... + a_{n}), the labels of the juicy worms.
-----Output-----
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number q_{i} is.
-----Examples-----
Input
5
2 7 3 4 9
3
1 25 11
Output
1
5
3
-----Note-----
For the sample input:
The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
|
n = int(input())
a = [0]
b = list(map(int,input().split()))
for i in range(len(b)):
for j in range(b[i]):
a.append(i+1)
n = int(input())
b = list(map(int,input().split()))
for i in range(len(b)):
print(a[b[i]])
|
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.
Marmot brought Mole n ordered piles of worms such that i-th pile contains a_{i} worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a_1, worms in second pile are labeled with numbers a_1 + 1 to a_1 + a_2 and so on. See the example for a better understanding.
Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.
Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 10^5), the number of piles.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^3, a_1 + a_2 + ... + a_{n} β€ 10^6), where a_{i} is the number of worms in the i-th pile.
The third line contains single integer m (1 β€ m β€ 10^5), the number of juicy worms said by Marmot.
The fourth line contains m integers q_1, q_2, ..., q_{m} (1 β€ q_{i} β€ a_1 + a_2 + ... + a_{n}), the labels of the juicy worms.
-----Output-----
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number q_{i} is.
-----Examples-----
Input
5
2 7 3 4 9
3
1 25 11
Output
1
5
3
-----Note-----
For the sample input:
The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
|
# Codeforces contest 271d1 problem B
import bisect
n = int(input())
worms = [int(x) for x in input().split(' ')]
for i in range(n-1):
worms[i+1] += worms[i]
m = int(input())
v = [int(x) for x in input().split(' ')]
[(lambda x: print(bisect.bisect_left(worms, x)+1))(x) for x in v]
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.