Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
長さ N の整数列 A=(A_1,A_2,\dots,A_N) が与えられます。
長さ M の A の連続部分列 B=(B_1,B_2,\dots,B_M) に対する、\displaystyle \sum_{i=1}^{M} i \times B_i の最大値を求めてください。
注記
数列の連続部分列とは、数列の先頭から 0 個以上、末尾から 0 個以上の要素を削除して得られる列のことをいいます。
例えば (2, 3) や (1, 2, 3) は (1, 2, 3, 4) の連続部分列ですが、(1, 3) や (3,2,1) は (1, 2, 3, 4) の連続部分列ではありません。
制約
- 1 \le M \le N \le 2 \times 10^5
- - 2 \times 10^5 \le A_i \le 2 \times 10^5
- 入力は全て整数。
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
4 2 5 4 -1 8
出力例 1
15
B=(A_3,A_4) とした場合、\displaystyle \sum_{i=1}^{M} i \times B_i = 1 \times (-1) + 2 \times 8 = 15 となります。16 以上の値を達成することはできないため、解は 15 です。
B=(A_1,A_4) などを選ぶことができないことに注意してください。
入力例 2
10 4 -3 1 -4 1 -5 9 -2 6 -5 3
出力例 2
31
Score : 300 points
Problem Statement
You are given an integer sequence A=(A_1,A_2,\dots,A_N) of length N.
Find the maximum value of \displaystyle \sum_{i=1}^{M} i \times B_i for a contiguous subarray B=(B_1,B_2,\dots,B_M) of A of length M.
Notes
A contiguous subarray of a number sequence is a sequence that is obtained by removing 0 or more initial terms and 0 or more final terms from the original number sequence.
For example, (2, 3) and (1, 2, 3) are contiguous subarrays of (1, 2, 3, 4), but (1, 3) and (3,2,1) are not contiguous subarrays of (1, 2, 3, 4).
Constraints
- 1 \le M \le N \le 2 \times 10^5
- - 2 \times 10^5 \le A_i \le 2 \times 10^5
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_N
Output
Print the answer.
Sample Input 1
4 2 5 4 -1 8
Sample Output 1
15
When B=(A_3,A_4), we have \displaystyle \sum_{i=1}^{M} i \times B_i = 1 \times (-1) + 2 \times 8 = 15. Since it is impossible to achieve 16 or a larger value, the solution is 15.
Note that you are not allowed to choose, for instance, B=(A_1,A_4).
Sample Input 2
10 4 -3 1 -4 1 -5 9 -2 6 -5 3
Sample Output 2
31
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
文字列 S の各文字を並べ替えて作ることが可能な文字列を辞書順にすべて列挙したとき、前から K 番目にくる文字列を求めてください。
「各文字を並べ替えて作ることが可能な文字列」とは?
「文字列 A が文字列 B の各文字を並べ替えて作ることが可能な文字列である」とは、任意の文字が文字列 A と文字列 B に同数含まれるということを指します。制約
- 1 \le |S| \le 8
- S は英小文字のみからなる
- S の各文字を並べ替えてできる文字列は K 種類以上存在する
入力
入力は以下の形式で標準入力から与えられる。
S K
出力
答えを出力せよ。
入力例 1
aab 2
出力例 1
aba
文字列 aab の各文字を並べ替えて作ることが可能な文字列は \{ aab, aba, baa \} の 3 つですが、このうち辞書順で前から 2 番目にくるものは aba です。
入力例 2
baba 4
出力例 2
baab
入力例 3
ydxwacbz 40320
出力例 3
zyxwdcba
Score : 300 points
Problem Statement
Find the K-th lexicographically smallest string among the strings that are permutations of a string S.
What is a permutation of a string?
A string A is said to be a permutation of a string B when any character occurs the same number of times in A and B.Constraints
- 1 \le |S| \le 8
- S consists of lowercase English letters.
- There are at least K distinct strings that are permutations of S.
Input
Input is given from Standard Input in the following format:
S K
Output
Print the answer.
Sample Input 1
aab 2
Sample Output 1
aba
There are three permutations of a string aab: \{ aab, aba, baa \}. The 2-nd lexicographically smallest of them is aba.
Sample Input 2
baba 4
Sample Output 2
baab
Sample Input 3
ydxwacbz 40320
Sample Output 3
zyxwdcba
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
1 から N の番号がついた N 人のユーザが利用している SNS があります。
この SNS では 2 人のユーザが互いに友達になれる機能があります。
友達関係は双方向的です。すなわち、ユーザ X がユーザ Y の友達であるならば、必ずユーザ Y はユーザ X の友達です。
現在 SNS 上には M 組の友達関係が存在し、i 組目の友達関係はユーザ A_i とユーザ B_i からなります。
以下の操作を行える最大の回数を求めてください。
- 操作:3 人のユーザ X, Y, Z であって、X と Y は友達、Y と Z は友達であり、X と Z は友達でないようなものを選ぶ。X と Z を友達にする。
制約
- 2 \leq N \leq 2\times 10^5
- 0 \leq M \leq 2\times 10^5
- 1\leq A_i < B_i \leq N
- (A_i,B_i) は相異なる
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 \vdots A_M B_M
出力
答えを出力せよ。
入力例 1
4 3 1 2 2 3 1 4
出力例 1
3
次のようにして「友達の友達と新たに友達になる」という操作は 3 回行えます。
- ユーザ 1 が友達(ユーザ 2)の友達であるユーザ 3 と新たに友達になる
- ユーザ 3 が友達(ユーザ 1)の友達であるユーザ 4 と新たに友達になる
- ユーザ 2 が友達(ユーザ 1)の友達であるユーザ 4 と新たに友達になる
4 回以上行うことはできません。
入力例 2
3 0
出力例 2
0
もともと友達関係が存在しないとき、新たな友達関係は発生しません。
入力例 3
10 8 1 2 2 3 3 4 4 5 6 7 7 8 8 9 9 10
出力例 3
12
Score: 350 points
Problem Statement
There is an SNS used by N users, labeled with numbers from 1 to N.
In this SNS, two users can become friends with each other.
Friendship is bidirectional; if user X is a friend of user Y, user Y is always a friend of user X.
Currently, there are M pairs of friendships on the SNS, with the i-th pair consisting of users A_i and B_i.
Determine the maximum number of times the following operation can be performed:
- Operation: Choose three users X, Y, and Z such that X and Y are friends, Y and Z are friends, but X and Z are not. Make X and Z friends.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq A_i < B_i \leq N
- The pairs (A_i, B_i) are distinct.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 B_1 \vdots A_M B_M
Output
Print the answer.
Sample Input 1
4 3 1 2 2 3 1 4
Sample Output 1
3
Three new friendships with a friend's friend can occur as follows:
- User 1 becomes friends with user 3, who is a friend of their friend (user 2)
- User 3 becomes friends with user 4, who is a friend of their friend (user 1)
- User 2 becomes friends with user 4, who is a friend of their friend (user 1)
There will not be four or more new friendships.
Sample Input 2
3 0
Sample Output 2
0
If there are no initial friendships, no new friendships can occur.
Sample Input 3
10 8 1 2 2 3 3 4 4 5 6 7 7 8 8 9 9 10
Sample Output 3
12
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
AtCoder国には N 個の都市があり、都市 1 , 都市 2 , \ldots , 都市 N と番号付けられています。 最初、どの 2 つの相異なる都市の間も双方向に通れる道で結ばれていましたが、老朽化が進み、これらのうち M 本の道が使えなくなってしまいました。具体的には 1\leq i \leq M について都市 U_i と都市 V_i を結ぶ道が使えなくなってしまいました。
いま、高橋君は都市 1 で始まり、都市 1 で終わる K 日間の旅をしようと考えました。都市 1 で始まり、都市 1 で終わる K 日間の旅とは、 K+1 個の都市の列 (A_0, A_1, \ldots, A_K) であって、A_0=A_K=1 をみたし、 0\leq i\leq K-1 について A_i と A_{i+1} が相異なり、かつ都市 A_i と都市 A_{i+1} が現在も使用可能な道で結ばれているものを指します。
都市 1 で始まり、都市 1 で終わる K 日間の相異なる旅の数を 998244353 で割った余りを出力してください。ただし、 2 つの K 日間の旅 (A_0, A_1, \ldots, A_K) と (B_0, B_1, \ldots, B_K) が相異なるとは、ある i が存在して A_i\neq B_i となることを言います。
制約
- 2 \leq N \leq 5000
- 0 \leq M \leq \min\left( \frac{N(N-1)}{2},5000 \right)
- 2 \leq K \leq 5000
- 1 \leq U_i<V_i \leq N
- (U_i, V_i) は全て互いに相異なる。
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N M K U_1 V_1 : U_M V_M
出力
答えを出力せよ。
入力例 1
3 1 4 2 3
出力例 1
4
次のような 4 種類の旅が存在します。
- (1,2,1,2,1)
- (1,2,1,3,1)
- (1,3,1,2,1)
- (1,3,1,3,1)
これ以外に条件をみたすようなものは無いため、 4 を出力します。
入力例 2
3 3 3 1 2 1 3 2 3
出力例 2
0
使える道が 1 本も残っておらず、条件をみたすような旅は存在しません。
入力例 3
5 3 100 1 2 4 5 2 3
出力例 3
428417047
Score : 500 points
Problem Statement
The Republic of AtCoder has N cities, called City 1, City 2, \ldots, City N. Initially, there was a bidirectional road between every pair of different cities, but M of these roads have become unusable due to deterioration over time. More specifically, for each 1\leq i \leq M, the road connecting City U_i and City V_i has become unusable.
Takahashi will go for a K-day trip that starts and ends in City 1. Formally speaking, a K-day trip that starts and ends in City 1 is a sequence of K+1 cities (A_0, A_1, \ldots, A_K) such that A_0=A_K=1 holds and for each 0\leq i\leq K-1, A_i and A_{i+1} are different and there is still a usable road connecting City A_i and City A_{i+1}.
Print the number of different K-day trips that start and end in City 1, modulo 998244353. Here, two K-day trips (A_0, A_1, \ldots, A_K) and (B_0, B_1, \ldots, B_K) are said to be different when there exists an i such that A_i\neq B_i.
Constraints
- 2 \leq N \leq 5000
- 0 \leq M \leq \min\left( \frac{N(N-1)}{2},5000 \right)
- 2 \leq K \leq 5000
- 1 \leq U_i<V_i \leq N
- All pairs (U_i, V_i) are pairwise distinct.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M K U_1 V_1 : U_M V_M
Output
Print the answer.
Sample Input 1
3 1 4 2 3
Sample Output 1
4
There are four different trips as follows.
- (1,2,1,2,1)
- (1,2,1,3,1)
- (1,3,1,2,1)
- (1,3,1,3,1)
No other trip is valid, so we should print 4.
Sample Input 2
3 3 3 1 2 1 3 2 3
Sample Output 2
0
No road remains usable, so there is no valid trip.
Sample Input 3
5 3 100 1 2 4 5 2 3
Sample Output 3
428417047
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 550 点
問題文
座標平面上にタイルが敷き詰められています。 1\times1 の大きさの小タイルと K\times K の大きさの大タイルの 2 種類があり、次の規則に従って敷き詰められています。
- 整数の組 (i,j) に対し、正方形 \lbrace(x,y)\mid i\leq x\leq i+1\wedge j\leq y\leq j+1\rbrace は 1 つの小タイルもしくは 1 つの大タイルに含まれる。
- \left\lfloor\dfrac iK\right\rfloor+\left\lfloor\dfrac jK\right\rfloor が偶数のとき、小タイルに含まれる。
- そうでないとき、大タイルに含まれる。
ただし、タイルは境界を含むものとし、共通部分が正の面積をもつような 2 つの異なるタイルは存在しないとします。
例えば、K=3 のとき、タイルは以下のようになります。

高橋君は、はじめ座標平面上の点 (S _ x+0.5,S _ y+0.5) にいます。
高橋君は、次の移動を好きなだけ繰り返します。
- 上下左右の方向と正の整数 n を選ぶ。その方向に n だけ進む。
高橋君が異なるタイルを通るたび、高橋君は通行料を 1 だけ支払います。
高橋君が点 (T _ x+0.5,T _ y+0.5) にたどり着くために支払わなければならない通行料の最小値を求めてください。
制約
- 1\leq K\leq10 ^ {16}
- 0\leq S _ x\leq2\times10 ^ {16}
- 0\leq S _ y\leq2\times10 ^ {16}
- 0\leq T _ x\leq2\times10 ^ {16}
- 0\leq T _ y\leq2\times10 ^ {16}
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
K S _ x S _ y T _ x T _ y
出力
高橋君が支払わなければならない通行料の最小値を出力せよ。
入力例 1
3 7 2 1 6
出力例 1
5
例えば、以下のように移動することで支払う通行料を 5 にすることができます。

- 上に 3 進む。通行料を 1 支払う。
- 左に 2 進む。通行料を 1 支払う。
- 上に 1 進む。通行料を 1 支払う。
- 左に 4 進む。通行料を 2 支払う。
支払う通行料を 4 以下にすることはできないので、5 を出力してください。
入力例 2
1 41 42 13 56
出力例 2
42

高橋君が最短距離で移動するとき、どのように移動しても通行料を 42 だけ支払います。
支払う通行料を 41 以下にすることはできないので、42 を出力してください。
入力例 3
100 100 99 199 1
出力例 3
0
通行料を支払わなくてよい場合もあります。
入力例 4
96929423 5105216413055191 10822465733465225 1543712011036057 14412421458305526
出力例 4
79154049
Score: 550 points
Problem Statement
Tiles are laid out on a coordinate plane. There are two types of tiles: small tiles of size 1\times1 and large tiles of size K\times K, laid out according to the following rules:
- For each pair of integers (i,j), the square \lbrace(x,y)\mid i\leq x\leq i+1\wedge j\leq y\leq j+1\rbrace is contained within either one small tile or one large tile.
- If \left\lfloor\dfrac iK\right\rfloor+\left\lfloor\dfrac jK\right\rfloor is even, it is contained within a small tile.
- Otherwise, it is contained within a large tile.
Tiles include their boundaries, and no two different tiles have a positive area of intersection.
For example, when K=3, tiles are laid out as follows:

Takahashi starts at the point (S_x+0.5,S_y+0.5) on the coordinate plane.
He can repeat the following movement any number of times:
- Choose a direction (up, down, left, or right) and a positive integer n. Move n units in that direction.
Each time he crosses from one tile to another, he must pay a toll of 1.
Determine the minimum toll Takahashi must pay to reach the point (T_x+0.5,T_y+0.5).
Constraints
- 1\leq K\leq10^{16}
- 0\leq S_x\leq2\times10^{16}
- 0\leq S_y\leq2\times10^{16}
- 0\leq T_x\leq2\times10^{16}
- 0\leq T_y\leq2\times10^{16}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
K S_x S_y T_x T_y
Output
Print the minimum toll Takahashi must pay.
Sample Input 1
3 7 2 1 6
Sample Output 1
5
For example, he can move as follows, paying a toll of 5.

- Move up by 3. Pay a toll of 1.
- Move left by 2. Pay a toll of 1.
- Move up by 1. Pay a toll of 1.
- Move left by 4. Pay a toll of 2.
The toll paid cannot be 4 or less, so print 5.
Sample Input 2
1 41 42 13 56
Sample Output 2
42

When he moves the shortest distance, he will always pay a toll of 42.
The toll paid cannot be 41 or less, so print 42.
Sample Input 3
100 100 99 199 1
Sample Output 3
0
There are cases where no toll needs to be paid.
Sample Input 4
96929423 5105216413055191 10822465733465225 1543712011036057 14412421458305526
Sample Output 4
79154049