Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
1 から N の番号がついた N 人の人がいます。人 i は数 A_i を持っています。
「自分と同じ数を持っている人が自分以外の N-1 人の中に存在しない」という条件を満たす人のうち、持っている数が最大である人の番号を求めてください。
条件を満たす人が存在しない場合、代わりにそのことを報告してください。
制約
- 1 \leq N \leq 3\times 10^5
- 1 \leq A_i \leq 10^9
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
「自分と同じ数を持っている人が自分以外の N-1 人の中に存在しない」という条件を満たす人が存在しない場合、-1
と出力せよ。
条件を満たす人が存在する場合、そのうち、持っている数が最大である人の番号を出力せよ。
入力例 1
9 2 9 9 7 9 2 4 5 8
出力例 1
9
「自分と同じ数を持っている人が自分以外の N-1 人の中に存在しない」という条件を満たすのは人 4,7,8,9 の 4 人です。
これらの人が持っている数はそれぞれ 7,4,5,8 であり、最大の数を持っているのは人 9 です。
よって答えは 9 となります。
入力例 2
4 1000000000 1000000000 998244353 998244353
出力例 2
-1
条件を満たす人が存在しない場合、-1
を出力してください。
Score : 300 points
Problem Statement
There are N people, labeled 1 to N. Person i has an integer A_i.
Among the people who satisfy the condition "None of the other N-1 people has the same integer as themselves," find the one with the greatest integer, and print that person's label.
If no person satisfies the condition, report that fact instead.
Constraints
- 1 \leq N \leq 3\times 10^5
- 1 \leq A_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N
Output
If no person satisfies the condition "None of the other N-1 people has the same integer as themselves," print -1
.
Otherwise, among those who satisfy it, print the label of the person whose integer is the largest.
Sample Input 1
9 2 9 9 7 9 2 4 5 8
Sample Output 1
9
Those who satisfy the condition are the persons labeled 4, 7, 8, and 9.
Their integers are 7, 4, 5, and 8, respectively, and the person with the largest integer is the person labeled 9.
Thus, the answer is 9.
Sample Input 2
4 1000000000 1000000000 998244353 998244353
Sample Output 2
-1
If no person satisfies the condition, print -1
.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
正整数 N,Q と、長さ N の英小文字からなる文字列 S が与えられます。
以下で説明されるクエリを Q 個処理してください。クエリは次の 2 種類のいずれかです。
1 x
: 「S の末尾の文字を削除し、先頭に挿入する」という操作を x 回連続で行う。2 x
: S の x 番目の文字を出力する。
制約
- 2 \le N \le 5 \times 10^5
- 1 \le Q \le 5 \times 10^5
- 1 \le x \le N
- |S|=N
- S は英小文字からなる。
2 x
の形式のクエリが 1 個以上与えられる。- N,Q,x はすべて整数。
入力
入力は以下の形式で標準入力から与えられる。
N Q S \mathrm{query}_1 \mathrm{query}_2 \vdots \mathrm{query}_Q
それぞれのクエリは以下の形式で与えられる。ここで、t は 1 または 2 である。
t x
出力
2 x
の形式の各クエリについて、答えを一行に出力せよ。
入力例 1
3 3 abc 2 2 1 1 2 2
出力例 1
b a
1 個目のクエリのとき、S は abc
なので 2 文字目の b
を出力します。
2 個目のクエリのとき、S は abc
から cab
に変わります。
3 個目のクエリのとき、S は cab
なので 2 文字目の a
を出力します。
入力例 2
10 8 dsuccxulnl 2 4 2 7 1 2 2 7 1 1 1 2 1 3 2 5
出力例 2
c u c u
Score : 300 points
Problem Statement
You are given positive integers N and Q, and a string S of length N consisting of lowercase English letters.
Process Q queries. Each query is of one of the following two types.
1 x
: Perform the following x times in a row: delete the last character of S and append it to the beginning.2 x
: Print the x-th character of S.
Constraints
- 2 \le N \le 5 \times 10^5
- 1 \le Q \le 5 \times 10^5
- 1 \le x \le N
- |S|=N
- S consists of lowercase English letters.
- At least one query in the format
2 x
. - N, Q, x are all integers.
Input
Input is given from Standard Input in the following format:
N Q S \mathrm{query}_1 \mathrm{query}_2 \vdots \mathrm{query}_Q
Each query is in the following format, where t is 1 or 2:
t x
Output
For each query in the format 2 x
, print the answer in a single line.
Sample Input 1
3 3 abc 2 2 1 1 2 2
Sample Output 1
b a
In the 1-st query, S is abc
, so the 2-nd character b
should be printed.
In the 2-nd query, S is changed from abc
to cab
.
In the 3-rd query, S is cab
, so the 2-nd character a
should be printed.
Sample Input 2
10 8 dsuccxulnl 2 4 2 7 1 2 2 7 1 1 1 2 1 3 2 5
Sample Output 2
c u c u
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君は何も書かれていないたくさんのボールと 1 つの袋を持っています。 最初、袋は空で、高橋君は Q 回の操作を行います。 それぞれの操作は以下の 3 種類のうちのいずれかです。
- 操作 1 : まだ何も書かれていないボール 1 つに整数 X_i を書き込み、袋に入れる。
- 操作 2 : 袋に入っているすべてのボールについて、そこに書かれている数を、それに X_i を加えたものに書き換える。
- 操作 3 : 袋に入っているボールのうち書かれている数が最小のもの(複数ある場合はそのうちの 1 つ)を取り出し、そこに書かれている数を記録する。その後、そのボールを捨てる。
1\leq i\leq Q について i 回目の操作の種類 P_i および操作 1 , 2 における X_i の値が与えられるので、操作 3 において記録された数を順に出力してください。
制約
- 1 \leq Q \leq 2\times 10^5
- 1 \leq P_i \leq 3
- 1 \leq X_i \leq 10^9
- 入力は全て整数である。
- P_i=3 であるような i が 1 つ以上存在する。
- P_i=3 であるとき、 i 回目の操作の直前の時点で、袋には 1 つ以上のボールが入っている。
入力
入力は以下の形式で標準入力から与えられる。
Q query_1 query_2 : query_Q
2 行目から Q+1 行目の各 query_i は次のいずれかの形で与えられる。
1 X_i
2 X_i
3
まず、1\leq P_i\leq 3 が与えられる。これは操作の種類を表す。 P_i=1 または P_i=2 ならば、その後に空白区切りで X_i が与えられる。
出力
Q 回の操作のうち操作の種類が P_i=3 であるような各操作について、記録された数を改行区切りで出力せよ。
入力例 1
5 1 3 1 5 3 2 2 3
出力例 1
3 7
高橋君は次のように操作を行います。
- 3 の書かれたボールを袋に入れる。
- 5 の書かれたボールを袋に入れる。
- 今、袋には 3 の書かれたボールと 5 の書かれたボールが入っているため、このうち小さい 3 の書かれたボールを取り出し、 3 を記録した後に捨てる。
- 今、袋には 5 の書かれたボールのみが入っているため、この数を 5+2=7 に書き換える。
- 今、袋には 7 の書かれたボールのみが入っているため、このボールを取り出し、 7 を記録した後に捨てる。
よって、記録された順に 3 , 7 を出力します。
入力例 2
6 1 1000000000 2 1000000000 2 1000000000 2 1000000000 2 1000000000 3
出力例 2
5000000000
答えが 32 bit整数に収まらないことがある事に注意してください。
Score : 400 points
Problem Statement
Takahashi has many balls, on which nothing is written, and one bag. Initially, the bag is empty. Takahashi will do Q operations, each of which is of one of the following three types.
- Type 1: Write an integer X_i on a blank ball and put it in the bag.
- Type 2: For each ball in the bag, replace the integer written on it with that integer plus X_i.
- Type 3: Pick up the ball with the smallest integer in the bag (if there are multiple such balls, pick up one of them). Record the integer written on this ball and throw it away.
For each 1\leq i\leq Q, you are given the type P_i of the i-th operation and the value of X_i if the operation is of Type 1 or 2. Print the integers recorded in the operations of Type 3 in order.
Constraints
- 1 \leq Q \leq 2\times 10^5
- 1 \leq P_i \leq 3
- 1 \leq X_i \leq 10^9
- All values in input are integers.
- There is one or more i such that P_i=3.
- If P_i=3, the bag contains at least one ball just before the i-th operation.
Input
Input is given from Standard Input in the following format:
Q query_1 query_2 : query_Q
Each query_i in the 2-nd through (Q+1)-th lines is in the following format:
1 X_i
2 X_i
3
The first number in each line is 1\leq P_i\leq 3, representing the type of the operation. If P_i=1 or P_i=2, it is followed by a space, and then by X_i.
Output
For each operation with P_i=3 among the Q operations, print the recorded integer in its own line.
Sample Input 1
5 1 3 1 5 3 2 2 3
Sample Output 1
3 7
Takahashi will do the following operations.
- Write 3 on a ball and put it in the bag.
- Write 5 on a ball and put it in the bag.
- The bag now contains a ball with 3 and another with 5. Pick up the ball with the smaller of them, or 3. Record 3 and throw it away.
- The bag now contains just a ball with 5. Replace this integer with 5+2=7.
- The bag now contains just a ball with 7. Pick up this ball, record 7, and throw it away.
Therefore, we should print 3 and 7, in the order they are recorded.
Sample Input 2
6 1 1000000000 2 1000000000 2 1000000000 2 1000000000 2 1000000000 3
Sample Output 2
5000000000
Note that the outputs may not fit into a 32-bit integer.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
\displaystyle \sum_{k=0}^{10^{100}} \left \lfloor \frac{X}{10^k} \right \rfloor を求めてください。
注釈
\lfloor A \rfloor は、 A の小数点以下を切り捨てた値を指します。
制約
- X は整数
- 1 \le X < 10^{500000}
入力
入力は以下の形式で標準入力から与えられる。
X
出力
答えを整数として出力せよ。
但し、たとえ答えが大きな整数であっても、求める答えを正確に整数として出力する必要がある。たとえば、 2.33e+21
のような指数表記や、 0523
のような先頭に不要な 0 を付けたような表記は許されない。
入力例 1
1225
出力例 1
1360
求める値は、 1225+122+12+1+0+0+\dots+0=1360 となります。
入力例 2
99999
出力例 2
111105
繰り上がりに注意してください。
入力例 3
314159265358979323846264338327950288419716939937510
出力例 3
349065850398865915384738153697722542688574377708317
入力される値も出力すべき値も非常に大きくなる場合があります。
Score : 500 points
Problem Statement
Find \displaystyle \sum_{k=0}^{10^{100}} \left \lfloor \frac{X}{10^k} \right \rfloor.
Notes
\lfloor A \rfloor denotes the value of A truncated to an integer.
Constraints
- X is an integer.
- 1 \le X < 10^{500000}
Input
Input is given from Standard Input in the following format:
X
Output
Print the answer as an integer.
Here, the answer must be precisely printed as an integer, even if it is large. It is not allowed to use exponential notation, such as 2.33e+21
, or print unnecessary leading zeros, as in 0523
.
Sample Input 1
1225
Sample Output 1
1360
The value we seek is 1225+122+12+1+0+0+\dots+0=1360.
Sample Input 2
99999
Sample Output 2
111105
Beware of carries.
Sample Input 3
314159265358979323846264338327950288419716939937510
Sample Output 3
349065850398865915384738153697722542688574377708317
The values in input and output can both be enormous.
Time Limit: 6 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 次元空間上の 2 点 x=(x_1, x_2, \dots, x_N), y = (y_1, y_2, \dots, y_N) のマンハッタン距離 d(x,y) は次の式で定義されます。
また、座標成分 x_1, x_2, \dots, x_N がすべて整数であるような点 x=(x_1, x_2, \dots, x_N) を格子点と呼びます。
N 次元空間上の格子点 p=(p_1, p_2, \dots, p_N), q = (q_1, q_2, \dots, q_N) が与えられます。
d(p,r) \leq D かつ d(q,r) \leq D であるような格子点 r としてあり得るものは全部で何個ありますか?答えを 998244353 で割ったあまりを求めてください。
制約
- 1 \leq N \leq 100
- 0 \leq D \leq 1000
- -1000 \leq p_i, q_i \leq 1000
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N D p_1 p_2 \dots p_N q_1 q_2 \dots q_N
出力
答えを出力せよ。
入力例 1
1 5 0 3
出力例 1
8
N=1 の場合は 1 次元空間、すなわち数直線上の点に関する問題になります。
条件を満たす点は -2,-1,0,1,2,3,4,5 の 8 個です。
入力例 2
3 10 2 6 5 2 1 2
出力例 2
632
入力例 3
10 100 3 1 4 1 5 9 2 6 5 3 2 7 1 8 2 8 1 8 2 8
出力例 3
145428186
Score : 500 points
Problem Statement
In an N-dimensional space, the Manhattan distance d(x,y) between two points x=(x_1, x_2, \dots, x_N) and y = (y_1, y_2, \dots, y_N) is defined by:
A point x=(x_1, x_2, \dots, x_N) is said to be a lattice point if the components x_1, x_2, \dots, x_N are all integers.
You are given lattice points p=(p_1, p_2, \dots, p_N) and q = (q_1, q_2, \dots, q_N) in an N-dimensional space.
How many lattice points r satisfy d(p,r) \leq D and d(q,r) \leq D? Find the count modulo 998244353.
Constraints
- 1 \leq N \leq 100
- 0 \leq D \leq 1000
- -1000 \leq p_i, q_i \leq 1000
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N D p_1 p_2 \dots p_N q_1 q_2 \dots q_N
Output
Print the answer.
Sample Input 1
1 5 0 3
Sample Output 1
8
When N=1, we consider points in a one-dimensional space, that is, on a number line.
8 lattice points satisfy the conditions: -2,-1,0,1,2,3,4,5.
Sample Input 2
3 10 2 6 5 2 1 2
Sample Output 2
632
Sample Input 3
10 100 3 1 4 1 5 9 2 6 5 3 2 7 1 8 2 8 1 8 2 8
Sample Output 3
145428186