A - Shampoo

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

高橋君の家には、高橋君、高橋君の父、高橋君の母の 3 人が住んでおり、全員が毎晩風呂で髪を洗います。
風呂には、高橋君の父、高橋君の母、高橋君の順に入り、それぞれシャンプーを A,B,C ミリリットル使います。

今朝の時点で、ボトルには V ミリリットルのシャンプーが残っていました。このまま補充しない時、初めてシャンプーが不足するのは誰が使おうとした時ですか?

制約

  • 1 \leq V,A,B,C \leq 10^5
  • 入力に含まれる値は全て整数である

入力

入力は以下の形式で標準入力から与えられる。

V A B C

出力

初めてシャンプーが不足するのが、高橋君の父が使おうとしたときならば F、高橋君の母が使おうとしたときならば M、高橋君が使おうとしたときならば T を出力せよ。


入力例 1

25 10 11 12

出力例 1

T

シャンプーは 25 ミリリットル残っています。

  • まず高橋君の父が 10 ミリリットル使い、残りは 15 ミリリットルになります。
  • 次に高橋君の母が 11 ミリリットル使い、残りは 4 ミリリットルになります。
  • 最後に高橋君が 12 ミリリットル使おうとしますが、4 ミリリットルしか残っておらず、不足しています。

入力例 2

30 10 10 10

出力例 2

F

シャンプーは 30 ミリリットル残っています。

  • まず高橋君の父が 10 ミリリットル使い、残りは 20 ミリリットルになります。
  • 次に高橋君の母が 10 ミリリットル使い、残りは 10 ミリリットルになります。
  • 続いて高橋君が 10 ミリリットル使い、残りは 0 ミリリットルになります。
  • 翌日、高橋君の父が 10 ミリリットル使おうとしますが、0 ミリリットルしか残っておらず、不足しています。

入力例 3

100000 1 1 1

出力例 3

M

Score : 100 points

Problem Statement

Three people live in Takahashi's house: Takahashi, his father, and his mother. All of them wash their hair in the bathroom each night.
His father, his mother, and Takahashi take a bath in this order and use A, B, and C milliliters of shampoo, respectively.

This morning, the bottle contained V milliliters of shampoo. Without refilling, who will be the first to run short of shampoo to wash their hair?

Constraints

  • 1 \leq V,A,B,C \leq 10^5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

V A B C

Output

If the first person to run short of shampoo to wash their hair is Takahashi's father, print F; if it is Takahashi's mother, print M; if it is Takahashi, print T.


Sample Input 1

25 10 11 12

Sample Output 1

T

Now, they have 25 milliliters of shampoo.

  • First, Takahashi's father uses 10 milliliters, leaving 15.
  • Next, Takahashi's mother uses 11 milliliters, leaving 4.
  • Finally, Takahashi tries to use 12 milliliters and runs short of shampoo since only 4 is remaining.

Sample Input 2

30 10 10 10

Sample Output 2

F

Now, they have 30 milliliters of shampoo.

  • First, Takahashi's father uses 10 milliliters, leaving 20.
  • Next, Takahashi's mother uses 10 milliliters, leaving 10.
  • Then, Takahashi uses 10 milliliters, leaving 0.
  • Next day, Takahashi's father tries to use 10 milliliters and runs short of shampoo since only 0 is remaining.

Sample Input 3

100000 1 1 1

Sample Output 3

M
B - Good morning

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

ある日、高橋君は AB 分ちょうどに、青木君は CD1 秒に起きました。
高橋君の起床時刻が青木君より早いならば Takahashi を、そうでないならば Aoki を出力してください。

制約

  • 0 \leq A \leq 23
  • 0 \leq B \leq 59
  • 0 \leq C \leq 23
  • 0 \leq D \leq 59
  • 入力はすべて整数である。

入力

入力は以下の形式で標準入力から与えられる。

A B C D

出力

高橋君の起床時刻が青木君より早いならば Takahashi を、そうでないならば Aoki を出力せよ。


入力例 1

7 0 6 30

出力例 1

Aoki

高橋君は 7 時ちょうどに、青木君は 6301 秒に起きました。 青木君の起床時刻の方が早いため、Aoki を出力します。


入力例 2

7 30 7 30

出力例 2

Takahashi

高橋君は 730 分ちょうどに、青木君は 7301 秒に起きました。 高橋君の起床時刻の方が 1 秒だけ早いため、Takahashi を出力します。


入力例 3

0 0 23 59

出力例 3

Takahashi

ある日の 00 分ちょうどはその日の 01 分の 1 分前であり、 その日の 2359 分の 1 分後、すなわちいわゆる 24 時ちょうどのことではありません。 よって、高橋君の起床時刻の方が早く、Takahashi を出力します。

Score : 100 points

Problem Statement

One day, Takahashi got up at exactly B minutes past A o'clock (in 24-hour clock), and Aoki got up at exactly D minutes and 1 second past C o'clock.
If Takahashi got up earlier than Aoki, print Takahashi; otherwise, print Aoki.

Constraints

  • 0 \leq A \leq 23
  • 0 \leq B \leq 59
  • 0 \leq C \leq 23
  • 0 \leq D \leq 59
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

If Takahashi got up earlier than Aoki, print Takahashi; otherwise, print Aoki.


Sample Input 1

7 0 6 30

Sample Output 1

Aoki

Takahashi got up at 7 sharp, and Aoki got up at 30 minutes and 1 second past 6 o'clock. Aoki was the first to get up, so Aoki should be printed.


Sample Input 2

7 30 7 30

Sample Output 2

Takahashi

Takahashi got up at exactly half past 7, and Aoki got up at 30 minutes and 1 second past 7 o'clock. Just by one second, Takahashi was the first to get up, so Takahashi should be printed.


Sample Input 3

0 0 23 59

Sample Output 3

Takahashi

0:00 in a day is one minute before 0:01, not one minute past 23:59 ("24:00"). Thus, Takahashi was the first to get up, so Takahashi should be printed.

C - Couples

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 150

問題文

2N 人の人が横一列に並んでおり、左から i 番目の人は色 A_i の服を着ています。ここで、服の色は 1 から NN 色であり、それぞれの色についてちょうど 2 人の人がその色の服を着ています。

i=1,2,\ldots,N のうち、以下の条件を満たすものは何通りあるか求めてください。

  • i の服を着た二人の人の間にはちょうど一人いる。

制約

  • 2\leq N\leq 100
  • 1\leq A_i \leq N
  • A には 1 以上 N 以下の整数全てがそれぞれ 2 個ずつ含まれる
  • 入力される数値は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N 
A_1 A_2 \ldots A_{2N}

出力

答えを出力せよ。


入力例 1

3
1 2 1 3 2 3

出力例 1

2

条件を満たす i132 個です。

実際、色 1 の服を着ているのは左から 1 番目の人と左から 3 番目の人で、間にちょうど一人います。


入力例 2

2
1 1 2 2

出力例 2

0

条件を満たす i が存在しない場合もあります。


入力例 3

4
4 3 2 3 2 1 4 1

出力例 3

3

Score : 150 points

Problem Statement

There are 2N people standing in a row, and the person at the i-th position from the left is wearing clothes of color A_i. Here, the clothes have N colors from 1 to N, and exactly two people are wearing clothes of each color.

Find how many of the integers i=1,2,\ldots,N satisfy the following condition:

  • There is exactly one person between the two people wearing clothes of color i.

Constraints

  • 2 \leq N \leq 100
  • 1 \leq A_i \leq N
  • Each integer from 1 through N appears exactly twice in A.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_{2N}

Output

Print the answer.


Sample Input 1

3
1 2 1 3 2 3

Sample Output 1

2

There are two values of i that satisfy the condition: 1 and 3.

In fact, the people wearing clothes of color 1 are at the 1st and 3rd positions from the left, with exactly one person in between.


Sample Input 2

2
1 1 2 2

Sample Output 2

0

There may be no i that satisfies the condition.


Sample Input 3

4
4 3 2 3 2 1 4 1

Sample Output 3

3
D - Second Best

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

長さ N の整数列 A=(A_1,\ldots,A_N) が与えられます。ここで A_1,A_2,\ldots,A_N は相異なります。

A の中で二番目に大きい要素は A の何番目の要素でしょうか。

制約

  • 2\leq N\leq 100
  • 1\leq A_i \leq 10^9
  • A_1,A_2,\ldots,A_N は相異なる
  • 入力される数値は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N 
A_1 A_2 \ldots A_{N}

出力

A の中で二番目に大きい要素が AX 番目であるとき、X を整数として出力せよ。


入力例 1

4
8 2 5 1

出力例 1

3

A の中で二番目に大きい要素は A_3 なので 3 を出力してください。


入力例 2

8
1 2 3 4 5 10 9 11

出力例 2

6

Score : 200 points

Problem Statement

You are given an integer sequence A=(A_1,\ldots,A_N) of length N. Here, A_1, A_2, \ldots, A_N are all distinct.

Which element in A is the second largest?

Constraints

  • 2 \leq N \leq 100
  • 1 \leq A_i \leq 10^9
  • A_1, A_2, \ldots, A_N are all distinct.
  • 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

Print the integer X such that the X-th element in A is the second largest.


Sample Input 1

4
8 2 5 1

Sample Output 1

3

The second largest element in A is A_3, so print 3.


Sample Input 2

8
1 2 3 4 5 10 9 11

Sample Output 2

6
E - One More aab aba baa

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