実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 150 点
問題文
高橋村の長老はすぐに寝てしまいます。具体的には、最後に肩を叩かれてから S+0.5 秒以上経過すると長老は寝てしまいます。
現在、長老は起きており、付き人がちょうど長老の肩を叩きました。
これから付き人はちょうど N 回長老の肩を叩きます。i 回目の肩叩きは現在から T_i 秒後に行われます。
長老が現在から T_N 秒後まで連続して起きているかを判定してください。
制約
- 1 \leq N \leq 100
- 1 \leq S \leq 100
- 1 \leq T_i \leq 1000 (1 \leq i \leq N)
- T_i < T_{i+1} (1 \leq i \leq N-1)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N S T_1 T_2 \ldots T_N
出力
長老が現在から T_N 秒後まで連続して起きているならば Yes を、そうでないならば No を出力せよ。
入力例 1
5 10 6 11 21 22 30
出力例 1
Yes
付き人は時系列順に以下のように肩を叩きます。
- 6 秒後に肩を叩きます。このとき、長老は最後に肩を叩かれてから 6 秒しか経過していないので起きています。
- 11 秒後に肩を叩きます。このとき、長老は最後に肩を叩かれてから 5 秒しか経過していないので起きています。
- 21 秒後に肩を叩きます。このとき、長老は最後に肩を叩かれてから 10 秒しか経過していないので起きています。
- 22 秒後に肩を叩きます。このとき、長老は最後に肩を叩かれてから 1 秒しか経過していないので起きています。
- 30 秒後に肩を叩きます。このとき、長老は最後に肩を叩かれてから 8 秒しか経過していないので起きています。
現在から 30 秒後まで長老は起きているので、Yes と出力します。
入力例 2
2 100 1 200
出力例 2
No
長老は現在から 101.5 秒後に寝てしまいます。そのため No と出力します。
入力例 3
10 22 47 81 82 95 117 146 165 209 212 215
出力例 3
No
Score : 150 points
Problem Statement
The elder of Takahashi Village falls asleep immediately. Specifically, if S+0.5 seconds or more have passed since the elder was last tapped on the shoulder, the elder falls asleep.
Currently, the elder is awake, and an attendant has just tapped the elder on the shoulder.
From now on, the attendant will tap the elder's shoulder exactly N times. The i-th shoulder tap will be performed T_i seconds from now.
Determine whether the elder remains awake continuously from now until T_N seconds later.
Constraints
- 1 \leq N \leq 100
- 1 \leq S \leq 100
- 1 \leq T_i \leq 1000 (1 \leq i \leq N)
- T_i < T_{i+1} (1 \leq i \leq N-1)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N S T_1 T_2 \ldots T_N
Output
If the elder remains awake continuously from now until T_N seconds later, output Yes; otherwise, output No.
Sample Input 1
5 10 6 11 21 22 30
Sample Output 1
Yes
The attendant taps the shoulder in chronological order as follows:
- Taps after 6 seconds. At this time, only 6 seconds have passed since the elder was last tapped on the shoulder, so the elder is awake.
- Taps after 11 seconds. At this time, only 5 seconds have passed since the elder was last tapped on the shoulder, so the elder is awake.
- Taps after 21 seconds. At this time, only 10 seconds have passed since the elder was last tapped on the shoulder, so the elder is awake.
- Taps after 22 seconds. At this time, only 1 second has passed since the elder was last tapped on the shoulder, so the elder is awake.
- Taps after 30 seconds. At this time, only 8 seconds have passed since the elder was last tapped on the shoulder, so the elder is awake.
Since the elder is awake from now until 30 seconds later, output Yes.
Sample Input 2
2 100 1 200
Sample Output 2
No
The elder falls asleep 101.5 seconds from now. Therefore, output No.
Sample Input 3
10 22 47 81 82 95 117 146 165 209 212 215
Sample Output 3
No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
消毒液の入ったボトルがあり、その消毒液によってちょうど M 本の手を消毒することができます。
N 人の宇宙人が順に手の消毒を行いに来ます。
i 人目 (1\leq i\leq N) の宇宙人は H_i 本の手を持っており、それぞれ自身のすべての手を 1 回ずつ消毒したいと考えています。
何人目の宇宙人までがすべての手を消毒できるか求めてください。
ただし、ある宇宙人が消毒を始める時点で、自身のすべての手を消毒する分の消毒液が残っていなかったとしても、その宇宙人はその消毒液を使い切ってしまうものとします。
制約
- 1\leq N,M\leq 100
- 1\leq H_i\leq 100
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M H_1 H_2 \ldots H_N
出力
何人目の宇宙人までが自身のすべての手を消毒できるか出力せよ。
入力例 1
5 10 2 3 2 5 3
出力例 1
3
次の手順で宇宙人は自身の手を消毒します。
- 1 人目の宇宙人は自身の 2 本の手を消毒します。残りの消毒液によって、10-2=8 本の手を消毒できます。
- 2 人目の宇宙人は自身の 3 本の手を消毒します。残りの消毒液によって、8-3=5 本の手を消毒できます。
- 3 人目の宇宙人は自身の 2 本の手を消毒します。残りの消毒液によって、5-2=3 本の手を消毒できます。
- 4 人目の宇宙人は 5 本の手を持っていますが、消毒液は 3 本分しかないため消毒液を使い切り、かつ自身のすべての手を消毒できません。
よって、3 人目の宇宙人までが自身のすべての手を消毒できるため、3 を出力します。
入力例 2
5 10 2 3 2 3 5
出力例 2
4
入力例 3
1 5 1
出力例 3
1
すべての宇宙人が自身の手を消毒することができます。
Score : 100 points
Problem Statement
There is a bottle of disinfectant that can disinfect exactly M hands.
N aliens come one by one to disinfect their hands.
The i-th alien (1 \leq i \leq N) has H_i hands and wants to disinfect all of their hands once.
Determine how many aliens can disinfect all of their hands.
Here, even if there is not enough disinfectant left for an alien to disinfect all of their hands when they start, they will use up the remaining disinfectant.
Constraints
- 1 \leq N, M \leq 100
- 1 \leq H_i \leq 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M H_1 H_2 \ldots H_N
Output
Print the number of aliens who can disinfect all of their hands.
Sample Input 1
5 10 2 3 2 5 3
Sample Output 1
3
The aliens disinfect their hands in the following steps:
- The first alien disinfects their two hands. The remaining disinfectant can disinfect 10-2=8 hands.
- The second alien disinfects their three hands. The remaining disinfectant can disinfect 8-3=5 hands.
- The third alien disinfects their two hands. The remaining disinfectant can disinfect 5-2=3 hands.
- The fourth alien has five hands, but there is only enough disinfectant for three hands, so they use up the disinfectant without disinfecting all of their hands.
Thus, the first three aliens can disinfect all of their hands, so print 3.
Sample Input 2
5 10 2 3 2 3 5
Sample Output 2
4
Sample Input 3
1 5 1
Sample Output 3
1
All aliens can disinfect their hands.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
非負整数 X に対し、 i=0,1,\dots,K-1 の順に次の操作を行ったとき、操作を全て終えた時点での X を求めてください。
- X の 10^i の位以下を四捨五入する。
- 厳密には、 X を「 |Y-X| が最小となる 10^{i+1} の倍数のうち最大のもの」である Y に置き換える。
- 具体例を挙げる。
- 273 の 10^1 の位以下を四捨五入すれば 300 となる。
- 999 の 10^2 の位以下を四捨五入すれば 1000 となる。
- 100 の 10^9 の位以下を四捨五入すれば 0 となる。
- 1015 の 10^0 の位以下を四捨五入すれば 1020 となる。
制約
- X,K は整数
- 0 \le X < 10^{15}
- 1 \le K \le 15
入力
入力は以下の形式で標準入力から与えられる。
X K
出力
答えを整数として出力せよ。
入力例 1
2048 2
出力例 1
2100
操作の過程で、 X は 2048 \rightarrow 2050 \rightarrow 2100 と変化します。
入力例 2
1 15
出力例 2
0
入力例 3
999 3
出力例 3
1000
入力例 4
314159265358979 12
出力例 4
314000000000000
X は 32bit 整数型に収まらない可能性があります。
Score : 200 points
Problem Statement
Given a non-negative integer X, perform the following operation for i=1,2,\dots,K in this order and find the resulting X.
- Round X off to the nearest 10^i.
- Formally, replace X with Y that is "the largest multiple of 10^i that minimizes |Y-X|."
- Here are some examples:
- Rounding 273 off to the nearest 10^2 yields 300.
- Rounding 999 off to the nearest 10^3 yields 1000.
- Rounding 100 off to the nearest 10^{10} yields 0.
- Rounding 1015 off to the nearest 10^1 yields 1020.
Constraints
- X and K are integers.
- 0 \le X < 10^{15}
- 1 \le K \le 15
Input
The input is given from Standard Input in the following format:
X K
Output
Print the answer as an integer.
Sample Input 1
2048 2
Sample Output 1
2100
X changes as 2048 \rightarrow 2050 \rightarrow 2100 by the operations.
Sample Input 2
1 15
Sample Output 2
0
Sample Input 3
999 3
Sample Output 3
1000
Sample Input 4
314159265358979 12
Sample Output 4
314000000000000
X may not fit into a 32-bit integer type.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
文字列 S が与えられます。S の各文字を並び替えて得られる文字列 S' のうち、辞書順で最小のものを出力してください。
なお、相異なる 2 つの文字列 s = s_1 s_2 \ldots s_n と t = t_1 t_2 \ldots t_m について、それらが以下の条件のいずれかを満たすとき、辞書順で s \lt t であるとします。
- ある整数 i\ (1 \leq i \leq \min(n,m)) が存在し、s_i \lt t_i かつすべての整数 j\ (1 \leq j \lt i) について s_j=t_j
- すべての整数 i\ (1 \leq i \leq \min(n,m)) について s_i = t_i かつ、n \lt m
制約
- S は英小文字のみからなる長さ 1 以上 2 \times 10^5 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S の各文字を並び替えて得られる文字列 S' のうち、辞書順で最小のものを出力せよ。
入力例 1
aba
出力例 1
aab
S= aba を並び替えて得られる文字列は以下の 3 つが考えられます。
abaaabbaa
この中で辞書順で最小のものは、aab です。
入力例 2
zzzz
出力例 2
zzzz
Score : 200 points
Problem Statement
You are given a string S. Find the lexicographically smallest string S' obtained by permuting the characters of S.
Here, for different two strings s = s_1 s_2 \ldots s_n and t = t_1 t_2 \ldots t_m, s \lt t holds lexicographically when one of the conditions below is satisfied.
- There is an integer i\ (1 \leq i \leq \min(n,m)) such that s_i \lt t_i and s_j=t_j for all integers j\ (1 \leq j \lt i).
- s_i = t_i for all integers i\ (1 \leq i \leq \min(n,m)), and n \lt m.
Constraints
- S is a string of length between 1 and 2 \times 10^5 (inclusive) consisting of lowercase English letters.
Input
Input is given from Standard Input in the following format:
S
Output
Print the lexicographically smallest string S' obtained by permuting the characters in S.
Sample Input 1
aba
Sample Output 1
aab
Three strings can be obtained by permuting aba:
abaaabbaa
The lexicographically smallest among them is aab.
Sample Input 2
zzzz
Sample Output 2
zzzz
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
整数の多重集合 S があります。はじめ S は空です。
Q 個のクエリが与えられるので順に処理してください。 クエリは次の 3 種類のいずれかです。
-
1 x: S に x を 1 個追加する。 -
2 x c: S から x を \mathrm{min}(c, (S に含まれる x の個数 )) 個削除する。 -
3: (S の最大値 )- (S の最小値 ) を出力する。このクエリを処理するとき、 S が空でないことが保証される。
制約
- 1 \leq Q \leq 2\times 10^5
- 0 \leq x \leq 10^9
- 1 \leq c \leq Q
3のクエリを処理するとき、S は空でない。- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q
i 番目のクエリを表す \mathrm{query}_i は以下の 3 種類のいずれかである。
1 x
2 x c
3
出力
3 のクエリに対する答えを順に改行区切りで出力せよ。
入力例 1
8 1 3 1 2 3 1 2 1 7 3 2 2 3 3
出力例 1
1 5 4
多重集合 S は以下のように変化します。
- 1 番目のクエリ : S に 3 を追加する。S は \lbrace3 \rbrace となる。
- 2 番目のクエリ : S に 2 を追加する。S は \lbrace2, 3\rbrace となる。
- 3 番目のクエリ : S = \lbrace 2, 3\rbrace の最大値は 3 、最小値は 2 なので、 3-2=1 を出力する。
- 4 番目のクエリ : S に 2 を追加する。S は \lbrace2,2,3 \rbrace となる。
- 5 番目のクエリ : S に 7 を追加する。S は \lbrace2, 2,3, 7\rbrace となる。
- 6 番目のクエリ : S = \lbrace2,2,3, 7\rbrace の最大値は 7 、最小値は 2 なので、 7-2=5 を出力する。
- 7 番目のクエリ : S に含まれる 2 の個数は 2 個なので、 \mathrm{min(2,3)} = 2 個の 2 を S から削除する。S は \lbrace3, 7\rbrace となる。
- 8 番目のクエリ : S = \lbrace3, 7\rbrace の最大値は 7 、最小値は 3 なので、 7-3=4 を出力する。
入力例 2
4 1 10000 1 1000 2 100 3 1 10
出力例 2
クエリ 3 が含まれない場合、何も出力してはいけません。
Score : 300 points
Problem Statement
We have a multiset of integers S, which is initially empty.
Given Q queries, process them in order. Each query is of one of the following types.
-
1 x: Insert an x into S. -
2 x c: Remove an x from S m times, where m = \mathrm{min}(c,( the number of x's contained in S)). -
3: Print ( maximum value of S)-( minimum value of S). It is guaranteed that S is not empty when this query is given.
Constraints
- 1 \leq Q \leq 2\times 10^5
- 0 \leq x \leq 10^9
- 1 \leq c \leq Q
- When a query of type
3is given, S is not empty. - All values in input are integers.
Input
Input is given from Standard Input in the following format:
Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q
\mathrm{query}_i, which denotes the i-th query, is in one of the following formats:
1 x
2 x c
3
Output
Print the answers for the queries of type 3 in the given order, separated by newlines.
Sample Input 1
8 1 3 1 2 3 1 2 1 7 3 2 2 3 3
Sample Output 1
1 5 4
The multiset S transitions as follows.
- 1-st query: insert 3 into S. S is now \lbrace 3 \rbrace.
- 2-nd query: insert 2 into S. S is now \lbrace 2, 3 \rbrace.
- 3-rd query: the maximum value of S = \lbrace 2, 3\rbrace is 3 and its minimum value is 2, so print 3-2=1.
- 4-th query: insert 2 into S. S is now \lbrace 2,2,3 \rbrace.
- 5-th query: insert 7 into S. S is now \lbrace 2, 2,3, 7\rbrace.
- 6-th query: the maximum value of S = \lbrace 2,2,3, 7\rbrace is 7 and its minimum value is 2, so print 7-2=5.
- 7-th query: since there are two 2's in S and \mathrm{min(2,3)} = 2, remove 2 from S twice. S is now \lbrace 3, 7\rbrace.
- 8-th query: the maximum value of S = \lbrace 3, 7\rbrace is 7 and its minimum value is 3, so print 7-3=4.
Sample Input 2
4 1 10000 1 1000 2 100 3 1 10
Sample Output 2
If the given queries do not contain that of type 3, nothing should be printed.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋君は風船で空を飛ぶことにしました。高橋君は時刻 0 (単位は秒) の時点で高度 H にいて、これから 10^9 秒間飛行します。
高橋君は自身が飛んでいる高度を 1 秒あたり 1 まで変化させることが出来ます。ただし、高度を 0 以下にすることはできません。
- 言い換えると、高橋君の時刻 t における高度を F(t) とした時、F(t) は以下の条件を全て満たします。
- F(0) = H
- 0 \leq t \lt u \leq 10^9 において -1 \leq \dfrac{F(u) - F(t)}{u - t} \leq 1
- 0 \leq t \leq 10^9 において F(t) \gt 0
N 個の高度に関する目標があります。i 個目の目標は時刻 t_i 時点での高度を l_i 以上 u_i 以下にすることです。
高橋君は目標を全て達成するような飛行が可能ですか?
T 個のテストケースが与えられるので、それぞれに対して答えを求めてください。
制約
- 1 \leq T \leq 10^5
- 1 \leq N \leq 10^5
- 1 \leq H \leq 10^9
- 1 \leq t_1 \lt t_2 \lt \dots \lt t_N \leq 10^9
- 1 \leq l_i \leq u_i \leq 10^9
- 全てのテストケースにおける N の総和は 10^5 以下
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。ここで \mathrm{case}_i は i 番目のテストケースを意味する。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられる。
N H t_1 l_1 u_1 t_2 l_2 u_2 \vdots t_N l_N u_N
出力
T 行出力せよ。i 行目には、i 番目のテストケースにおいて目標を全て達成するような飛行が可能である場合 Yes を、そうでない場合 No を出力せよ。
入力例 1
3 2 5 3 1 4 8 9 11 2 6 1 1 4 3 5 8 10 36 27 37 38 30 34 54 38 20 77 45 1 36 49 38 51 52 31 58 65 43 60 71 14 42 73 36 38 85 14 29
出力例 1
Yes No Yes
1 番目のテストケースについて、高橋君は以下の方法で飛行することで全ての目標を達成することが出来ます。
- 時刻 0 の時点で高橋君は高度 5 にいる。
- 時刻 0 から時刻 2 にかけて高橋君は秒速 0.5 で高度を下げる。
- 時刻 2 の時点で高橋君は高度 5 - 0.5 \times 2 = 4 にいる。
- 時刻 2 から時刻 3 の間、高橋君は同じ高度にとどまる。
- 時刻 3 の時点で高橋君は高度 4 にいる。高橋君は 1 番目の目標を満たす。
- 時刻 3 から時刻 8 にかけて高橋君は秒速 1 で高度を上げる。
- 時刻 8 の時点で高橋君は高度 4 + 1 \times 5 = 9 にいる。高橋君は 2 番目の目標を満たす。
2 番目のテストケースについて、高橋君はどのように飛行しても目標を全て達成することは出来ません。
Score : 300 points
Problem Statement
Takahashi has decided to fly in the sky with balloons. Takahashi is at altitude H at time 0 (the unit is seconds), and will fly for 10^9 seconds from now.
Takahashi can change his flying altitude by up to 1 per second. However, he cannot make his altitude 0 or less.
- In other words, if F(t) denotes Takahashi's altitude at time t, then F(t) satisfies all of the following conditions:
- F(0) = H
- -1 \leq \dfrac{F(u) - F(t)}{u - t} \leq 1 for 0 \leq t \lt u \leq 10^9.
- F(t) \gt 0 for 0 \leq t \leq 10^9.
There are N goals regarding altitude. The i-th goal is to make the altitude at time t_i at least l_i and at most u_i.
Is it possible for Takahashi to fly in a way that achieves all goals?
You are given T test cases; solve each of them.
Constraints
- 1 \leq T \leq 10^5
- 1 \leq N \leq 10^5
- 1 \leq H \leq 10^9
- 1 \leq t_1 \lt t_2 \lt \dots \lt t_N \leq 10^9
- 1 \leq l_i \leq u_i \leq 10^9
- The sum of N over all test cases is at most 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format, where \mathrm{case}_i denotes the i-th test case.
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Each test case is given in the following format:
N H t_1 l_1 u_1 t_2 l_2 u_2 \vdots t_N l_N u_N
Output
Output T lines. The i-th line should contain Yes if it is possible to fly in a way that achieves all goals in the i-th test case, and No otherwise.
Sample Input 1
3 2 5 3 1 4 8 9 11 2 6 1 1 4 3 5 8 10 36 27 37 38 30 34 54 38 20 77 45 1 36 49 38 51 52 31 58 65 43 60 71 14 42 73 36 38 85 14 29
Sample Output 1
Yes No Yes
For the first test case, Takahashi can achieve all goals by flying as follows:
- At time 0, he is at altitude 5.
- From time 0 to time 2, he descends at a rate of 0.5 per second.
- At time 2, he is at altitude 5 - 0.5 \times 2 = 4.
- From time 2 to time 3, he stays at the same altitude.
- At time 3, he is at altitude 4. He satisfies the first goal.
- From time 3 to time 8, he ascends at a rate of 1 per second.
- At time 8, he is at altitude 4 + 1 \times 5 = 9. He satisfies the second goal.
For the second test case, he cannot achieve all goals no matter how he flies.
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
N 以下の正整数のうち、 a<b<c なる 素数 a,b,c を用いて a^2 \times b \times c^2 と表せるものはいくつありますか?
制約
- N は 300 \le N \le 10^{12} を満たす整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを整数として出力せよ。
入力例 1
1000
出力例 1
3
1000 以下で条件を満たす整数は以下の 3 つです。
- 300 = 2^2 \times 3 \times 5^2
- 588 = 2^2 \times 3 \times 7^2
- 980 = 2^2 \times 5 \times 7^2
入力例 2
1000000000000
出力例 2
2817785
Score : 400 points
Problem Statement
How many positive integers no greater than N can be represented as a^2 \times b \times c^2 with three primes a,b, and c such that a<b<c?
Constraints
- N is an integer satisfying 300 \le N \le 10^{12}.
Input
The input is given from Standard Input in the following format:
N
Output
Print the answer as an integer.
Sample Input 1
1000
Sample Output 1
3
The conforming integers no greater than 1000 are the following three.
- 300 = 2^2 \times 3 \times 5^2
- 588 = 2^2 \times 3 \times 7^2
- 980 = 2^2 \times 5 \times 7^2
Sample Input 2
1000000000000
Sample Output 2
2817785
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
N 個の頂点と M 本の辺からなる無向グラフが与えられます。
i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結ぶ無向辺であり、
a_i = 1 ならばはじめは通行可能、a_i = 0 ならばはじめは通行不能です。
また、頂点 s_1 、頂点 s_2 、\ldots 、頂点 s_K の K 個の頂点にはスイッチがあります。
高橋君は、はじめ頂点 1 におり、「下記の移動とスイッチを押すの 2 つの行動のどちらかを行うこと」を好きなだけ繰り返します。
- 移動 : いまいる頂点と通行可能な辺を介して隣接する頂点を 1 つ選び、その頂点に移動する。
- スイッチを押す : いまいる頂点にスイッチがあるならば、そのスイッチを押す。その結果、グラフ上のすべての辺の通行可能・通行不能の状態が反転する。すなわち、通行可能である辺は通行不能に、通行不能である辺は通行可能に変化する。
高橋君が頂点 N に到達することが可能かどうかを判定し、可能な場合は頂点 N に到達するまでに行う移動の回数としてあり得る最小値を出力してください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 0 \leq K \leq N
- 1 \leq u_i, v_i \leq N
- u_i \neq v_i
- a_i \in \lbrace 0, 1\rbrace
- 1 \leq s_1 \lt s_2 \lt \cdots \lt s_K \leq N
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M K u_1 v_1 a_1 u_2 v_2 a_2 \vdots u_M v_M a_M s_1 s_2 \ldots s_K
出力
高橋君が頂点 N に到達することが不可能な場合は -1 を、 可能な場合は頂点 N に到達するまでに行う移動の回数としてあり得る最小値を出力せよ。
入力例 1
5 5 2 1 3 0 2 3 1 5 4 1 2 1 1 1 4 0 3 4
出力例 1
5
高橋君は、下記の手順で行動することで頂点 N に到達できます。
- 頂点 1 から頂点 2 に移動する。
- 頂点 2 から頂点 3 に移動する。
- 頂点 3 にあるスイッチを押す。これによって、グラフ上のすべての辺の通行可能・通行不能の状態が反転する。
- 頂点 3 から頂点 1 に移動する。
- 頂点 1 から頂点 4 に移動する。
- 頂点 4 にあるスイッチを押す。これによって、グラフ上のすべての辺の通行可能・通行不能の状態が再び反転する。
- 頂点 4 から頂点 5 に移動する。
この手順における移動の回数は 5 回であり、これが考えられる最小の回数です。
入力例 2
4 4 2 4 3 0 1 2 1 1 2 0 2 1 1 2 4
出力例 2
-1
与えられるグラフは、連結でないことや、多重辺を含むことがあります。 この入力例では、高橋君はどのように行動しても頂点 N に到達することはできないので、-1 を出力します。
Score : 500 points
Problem Statement
You are given an undirected graph consisting of N vertices and M edges.
For i = 1, 2, \ldots, M, the i-th edge is an undirected edge connecting vertex u_i and v_i that is initially passable if a_i = 1 and initially impassable if a_i = 0.
Additionally, there are switches on K of the vertices: vertex s_1, vertex s_2, \ldots, vertex s_K.
Takahashi is initially on vertex 1, and will repeat performing one of the two actions below, Move or Hit Switch, which he may choose each time, as many times as he wants.
- Move: Choose a vertex adjacent to the vertex he is currently on via an edge, and move to that vertex.
- Hit Switch: If there is a switch on the vertex he is currently on, hit it. This will invert the passability of every edge in the graph. That is, a passable edge will become impassable, and vice versa.
Determine whether Takahashi can reach vertex N, and if he can, print the minimum possible number of times he performs Move before reaching vertex N.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 0 \leq K \leq N
- 1 \leq u_i, v_i \leq N
- u_i \neq v_i
- a_i \in \lbrace 0, 1\rbrace
- 1 \leq s_1 \lt s_2 \lt \cdots \lt s_K \leq N
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M K u_1 v_1 a_1 u_2 v_2 a_2 \vdots u_M v_M a_M s_1 s_2 \ldots s_K
Output
If Takahashi cannot reach vertex N, print -1; if he can, print the minimum possible number of times he performs Move before reaching vertex N.
Sample Input 1
5 5 2 1 3 0 2 3 1 5 4 1 2 1 1 1 4 0 3 4
Sample Output 1
5
Takahashi can reach vertex N as follows.
- Move from vertex 1 to vertex 2.
- Move from vertex 2 to vertex 3.
- Hit the switch on vertex 3. This inverts the passability of every edge in the graph.
- Move from vertex 3 to vertex 1.
- Move from vertex 1 to vertex 4.
- Hit the switch on vertex 4. This again inverts the passability of every edge in the graph.
- Move from vertex 4 to vertex 5.
Here, Move is performed five times, which is the minimum possible number.
Sample Input 2
4 4 2 4 3 0 1 2 1 1 2 0 2 1 1 2 4
Sample Output 2
-1
The given graph may be disconnected or contain multi-edges. In this sample input, there is no way for Takahashi to reach vertex N, so you should print -1.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
N 個の一次関数 f_1,f_2,\ldots,f_N が与えられます。f_i(x)=A_i x+B_i です。
1 以上 N 以下の相異なる K 個の整数からなる長さ K の数列 p=(p_1,p_2, \ldots p_K) について、f_{p_1}(f_{p_2}(\ldots f_{p_K}(1)\ldots )) としてありえる最大値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^{5}
- 1 \leq K \leq \text{min}(N,10)
- 1 \leq A_i,B_i \leq 50 (1 \leq i \leq N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 B_1 A_2 B_2 \vdots A_N B_N
出力
答えを整数として出力せよ。
入力例 1
3 2 2 3 1 5 4 2
出力例 1
26
ありえるすべての p とそれに対応する f_{p_1}(f_{p_2}(1)) の値は以下の通りです。
- p= ( 1,2 ) : f_1(f_2(1))=15
- p= ( 1,3 ) : f_1(f_3(1))=15
- p= ( 2,1 ) : f_2(f_1(1))=10
- p= ( 2,3 ) : f_2(f_3(1))=11
- p= ( 3,1 ) : f_3(f_1(1))=22
- p= ( 3,2 ) : f_3(f_2(1))=26
よって、 26 と出力します。
入力例 2
10 3 48 40 34 22 24 37 45 40 48 31 49 44 45 40 44 6 35 22 39 28
出力例 2
216223
Score : 500 points
Problem Statement
You are given N linear functions f_1, f_2, \ldots, f_N, where f_i(x) = A_i x + B_i.
Find the maximum possible value of f_{p_1}(f_{p_2}(\ldots f_{p_K}(1) \ldots )) for a sequence p = (p_1, p_2, \ldots, p_K) of K distinct integers between 1 and N, inclusive.
Constraints
- 1 \leq N \leq 2 \times 10^{5}
- 1 \leq K \leq \text{min}(N,10)
- 1 \leq A_i, B_i \leq 50 (1 \leq i \leq N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K A_1 B_1 A_2 B_2 \vdots A_N B_N
Output
Print the answer as an integer.
Sample Input 1
3 2 2 3 1 5 4 2
Sample Output 1
26
Here are all possible p and the corresponding values of f_{p_1}(f_{p_2}(1)):
- p= ( 1,2 ) : f_1(f_2(1))=15
- p= ( 1,3 ) : f_1(f_3(1))=15
- p= ( 2,1 ) : f_2(f_1(1))=10
- p= ( 2,3 ) : f_2(f_3(1))=11
- p= ( 3,1 ) : f_3(f_1(1))=22
- p= ( 3,2 ) : f_3(f_2(1))=26
Therefore, print 26.
Sample Input 2
10 3 48 40 34 22 24 37 45 40 48 31 49 44 45 40 44 6 35 22 39 28
Sample Output 2
216223