Time Limit: 2 sec / Memory Limit: 256 MB
配点 : 点
問題文
縦 行、横 列のマス目に区切られた盤面があります。 はじめ、駒が左上隅のマスに置かれていました。 シックはこの駒を マスずつ上下左右に動かし、右下隅のマスに移動させました。 このとき、駒が同じマスを複数回通った可能性もあります(左上隅や右下隅のマスも含む)。
縦横に並んだ文字 (, ) が与えられます。
が #
のとき、駒が移動する過程で 行 列目のマスを一度以上通ったことを表します(左上隅や右下隅のマスは必ず通ったものとして扱います)。
が .
のとき、駒が 行 列目のマスを一度も通らなかったことを表します。
移動する過程で、駒が常に右または下に動いていた可能性があるか判定してください。
制約
- は
#
または.
である。 - 問題文および で与えられる情報と整合するような駒の動き方が存在する。
入力
入力は以下の形式で標準入力から与えられる。
出力
移動する過程で、駒が常に右または下に動いていた可能性があれば Possible
、なければ Impossible
と出力せよ。
入力例 1
4 5 ##... .##.. ..##. ...##
出力例 1
Possible
右、下、右、下、右、下、右と駒が動くと、通るマスが与えられた情報と合致します。
入力例 2
5 3 ### ..# ### #.. ###
出力例 2
Impossible
入力例 3
4 5 ##... .###. .###. ...##
出力例 3
Impossible
Score : points
Problem Statement
We have a grid of rows and columns. Initially, there is a stone in the top left cell. Shik is trying to move the stone to the bottom right cell. In each step, he can move the stone one cell to its left, up, right, or down (if such cell exists). It is possible that the stone visits a cell multiple times (including the bottom right and the top left cell).
You are given a matrix of characters (, ). After Shik completes all moving actions, is #
if the stone had ever located at the -th row and the -th column during the process of moving. Otherwise, is .
. Please determine whether it is possible that Shik only uses right and down moves in all steps.
Constraints
- is either
#
or.
. - There exists a valid sequence of moves for Shik to generate the map .
Input
The input is given from Standard Input in the following format:
Output
If it is possible that Shik only uses right and down moves, print Possible
. Otherwise, print Impossible
.
Sample Input 1
4 5 ##... .##.. ..##. ...##
Sample Output 1
Possible
The matrix can be generated by a -move sequence: right, down, right, down, right, down, and right.
Sample Input 2
5 3 ### ..# ### #.. ###
Sample Output 2
Impossible
Sample Input 3
4 5 ##... .###. .###. ...##
Sample Output 3
Impossible
Time Limit: 2 sec / Memory Limit: 256 MB
Score : points
Problem Statement
You are given a permutation of the set {}. Please construct two sequences of positive integers , , ..., and , , ..., satisfying the following conditions:
- for all
Constraints
- is a permutation of the set {}
Input
The input is given from Standard Input in the following format:
Output
The output consists of two lines. The first line contains , , ..., seperated by a space. The second line contains , , ..., seperated by a space.
It can be shown that there always exists a solution for any input satisfying the constraints.
Sample Input 1
2 1 2
Sample Output 1
1 4 5 4
and . So this output satisfies all conditions.
Sample Input 2
3 3 2 1
Sample Output 2
1 2 3 5 3 1
Sample Input 3
3 2 3 1
Sample Output 3
5 10 100 100 10 1
配点 : 点
問題文
集合 {} の要素を並び替えた順列 が与えられます。以下の条件をすべて満たす つの正整数列 , , ..., および , , ..., を構成してください。
- すべての に対し、
制約
- は集合 {} の要素を並び替えた順列である。
入力
入力は以下の形式で標準入力から与えられる。
Output
行出力せよ。 行目に整数列 , , ..., を、 行目に整数列 , , ..., を、それぞれ空白区切りで出力せよ。
なお、制約を満たす任意の入力に対して解が存在することが示せる。
入力例 1
2 1 2
出力例 1
1 4 5 4
および より、すべての条件が満たされています。
入力例 2
3 3 2 1
出力例 2
1 2 3 5 3 1
入力例 3
3 2 3 1
出力例 3
5 10 100 100 10 1
Time Limit: 2 sec / Memory Limit: 256 MB
配点 : 点
問題文
個の球と 個の穴が一直線上に並んでいます。球には左から順に から の番号が、穴には左から順に から の番号が振られています。 番の球は 番の穴と 番の穴の間に置かれており、隣り合う穴と球の間の距離を左から順に並べて得られる数列を () とおきます。 の値とパラメータ が与えられており、 が任意の () に対して成り立っています。
これら 個の球を 個ずつ転がし、穴に落としていくことを考えます。球が穴の上を通ると、その穴にすでに別の球が入っていなければその穴に落ちます。すでに別の球がその穴に入っていた場合は、球は落ちずにそのまま転がり続けます。(なお、この問題で考える球の転がし方において、球どうしが衝突することはありません。)
球を転がす際は、まだ転がされていない球の中から等確率で つを選び、その球を左または右に等確率で転がします。これを 回繰り返してすべての球を転がすとき、すべての球が移動する距離の総和の期待値を求めてください。
以下に , , の場合の球の転がし方の例を挙げます。

- 番の球を左に転がす。球は 番の穴に落ちる。球が移動する距離は である。
- 番の球を右に転がす。球は 番の穴の上を通り、 番の穴に落ちる。球が移動する距離は である。
- 番の球を右に転がす。球は 番の穴に落ちる。球が移動する距離は である。
この例では、球が移動する距離の総和は となります。
なお、球をどのように転がしてもどの球も必ずいずれかの穴に落ち、最後に穴が一つだけ空のまま残ることになります。
制約
- 入力値はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
出力
答えを小数で出力せよ。絶対誤差または相対誤差のいずれかが 以内でなければならない。
入力例 1
1 3 3
出力例 1
4.500000000000000
球が 個、穴が 個あります。球から左の穴までの距離は 、球から右の穴までの距離は です。球を左に転がすか右に転がすかの 通りの転がし方があり、それぞれにおいて球が移動する距離は です。したがって、答えは となります。
入力例 2
2 1 0
出力例 2
2.500000000000000
入力例 3
1000 100 100
出力例 3
649620280.957660079002380
Score : points
Problem Statement
There are balls and holes in a line. Balls are numbered through from left to right. Holes are numbered through from left to right. The -th ball is located between the -th hole and -th hole. We denote the distance between neighboring items (one ball and one hole) from left to right as (). You are given two parameters and . is equal to for all ().
We want to push all balls into holes one by one. When a ball rolls over a hole, the ball will drop into the hole if there is no ball in the hole yet. Otherwise, the ball will pass this hole and continue to roll. (In any scenario considered in this problem, balls will never collide.)
In each step, we will choose one of the remaining balls uniformly at random and then choose a direction (either left or right) uniformly at random and push the ball in this direction. Please calculate the expected total distance rolled by all balls during this process.
For example, when , , and , the following is one possible scenario:

- first step: push the ball numbered to its left, it will drop into the hole numbered . The distance rolled is .
- second step: push the ball numbered to its right, it will pass the hole numbered and drop into the hole numbered . The distance rolled is .
- third step: push the ball numbered to its right, it will drop into the hole numbered . The distance rolled is .
So the total distance in this scenario is .
Note that in all scenarios every ball will drop into some hole and there will be a hole containing no ball in the end.
Constraints
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Print a floating number denoting the answer. The relative or absolute error of your answer should not be higher than .
Sample Input 1
1 3 3
Sample Output 1
4.500000000000000
The distance between the only ball and the left hole is and distance between the only ball and the right hole is . There are only two scenarios (i.e. push left and push right). The only ball will roll and unit distance, respectively. So the answer for this example is .
Sample Input 2
2 1 0
Sample Output 2
2.500000000000000
Sample Input 3
1000 100 100
Sample Output 3
649620280.957660079002380
Time Limit: 2 sec / Memory Limit: 256 MB
配点 : 点
問題文
一直線上でゲームを行います。はじめプレイヤーは座標 におり、キャンディを 個持っています。座標 に出口があります。プレイヤーの他に、この直線上には 匹のクマがおり、 匹目のクマは座標 で静止しています。プレイヤーは直線上を 以下の速度で動くことができます。
プレイヤーがクマにキャンディを 個与えると、クマは 単位時間後に 枚のコインをその場に吐き出します。すなわち、時刻 にクマにキャンディを 個与えると、時刻 にそのクマの位置に 枚のコインが出現します。このゲームの目的は、 匹すべてのクマにキャンディを与え、 枚のコインをすべて回収して出口から脱出することです。クマにキャンディを与えるためには、プレイヤーはクマと同じ位置にいなければなりません。また、 匹のクマに 回以上キャンディを与えることはできません。コインは、出現した瞬間以降にプレイヤーがコインと同じ位置にいれば回収できます。プレイヤーが回収する前にコインが消滅することはありません。
シックはこのゲームの達人です。シックがクマにキャンディを与えたり、コインを拾うのに必要な時間は極めて短く、無視することができます。ゲームの設定が与えられるので、シックがすべてのコインを集めて出口から脱出するまでに必要な最短時間を求めてください。
制約
- ()
- 入力値はすべて整数である。
部分点
- 点分のデータセットでは、 が成り立つ。
入力
入力は以下の形式で標準入力から与えられる。
出力
シックがすべてのコインを集めて出口から脱出するまでに必要な最短時間を表す整数を出力せよ。
入力例 1
3 9 1 1 3 8
出力例 1
12
出口に向かいながら、クマに会うたびにキャンディを与え、その場でコインが出るのを待つのが最適です。このとき、移動に 単位時間、 回の待機に 単位時間、合計で 単位時間を要します。
入力例 2
3 9 3 1 3 8
出力例 2
16
入力例 3
2 1000000000 1000000000 1 999999999
出力例 3
2999999996
Score : points
Problem Statement
Imagine a game played on a line. Initially, the player is located at position with candies in his possession, and the exit is at position . There are also bears in the game. The -th bear is located at . The maximum moving speed of the player is while the bears do not move at all.
When the player gives a candy to a bear, it will provide a coin after units of time. More specifically, if the -th bear is given a candy at time , it will put a coin at its position at time . The purpose of this game is to give candies to all the bears, pick up all the coins, and go to the exit. Note that the player can only give a candy to a bear if the player is at the exact same position of the bear. Also, each bear will only produce a coin once. If the player visits the position of a coin after or at the exact same time that the coin is put down, the player can pick up the coin. Coins do not disappear until collected by the player.
Shik is an expert of this game. He can give candies to bears and pick up coins instantly. You are given the configuration of the game. Please calculate the minimum time Shik needs to collect all the coins and go to the exit.
Constraints
- for
- All input values are integers.
Partial Scores
- In test cases worth points, .
Input
The input is given from Standard Input in the following format:
Output
Print an integer denoting the answer.
Sample Input 1
3 9 1 1 3 8
Sample Output 1
12
The optimal strategy is to wait for the coin after treating each bear. The total time spent on waiting is and moving is . So the answer is .
Sample Input 2
3 9 3 1 3 8
Sample Output 2
16
Sample Input 3
2 1000000000 1000000000 1 999999999
Sample Output 3
2999999996
Time Limit: 5 sec / Memory Limit: 256 MB
Score : points
Problem Statement
In the country there are cities numbered through , which are connected by bidirectional roads. In terms of graph theory, there is a unique simple path connecting every pair of cities. That is, the cities form a tree. Besides, if we view city as the root of this tree, the tree will be a full binary tree (a tree is a full binary tree if and only if every non-leaf vertex in the tree has exactly two children). Roads are numbered through . The -th road connects city and city . When you pass through road , the toll you should pay is (if is , it indicates the road does not require a toll).
A company in city will give each employee a family travel and cover a large part of the tolls incurred. Each employee can design the travel by themselves. That is, which cities he/she wants to visit in each day and which city to stay at each night. However, there are some constraints. More details are as follows:
-
The travel must start and end at city . If there are leaves in the tree formed by the cities, then the number of days in the travel must be . At the end of each day, except for the last day, the employee must stay at some hotel in a leaf city. During the whole travel, the employee must stay at all leaf cities exactly once.
-
During the whole travel, all roads of the country must be passed through exactly twice.
-
The amount that the employee must pay for tolls by him/herself is the maximum total toll incurred in a single day during the travel, except the first day and the last day. The remaining tolls will be covered by the company.
Shik, as an employee of this company, has only one hope for his travel. He hopes that the amount he must pay for tolls by himself will be as small as possible. Please help Shik to design the travel which satisfies his hope.
Constraints
- for all
- is an integer
- The given tree is a full binary tree
Input
The input is given from Standard Input in the following format:
Output
Print an integer denoting the minimum amount Shik must pay by himself for tolls incurred during the travel.
Sample Input 1
7 1 1 1 1 2 1 2 1 3 1 3 1
Sample Output 1
4
There are leaves in the tree formed by the cities (cities , , , and ), so the travel must be days long. There are possible arrangements of the leaf cities to stay during the travel, but some of them are invalid. For example, and are valid orders, but is invalid because the route passes times through the road connecting city and city . The figure below shows the routes of the travel for these arrangements.

For all valid orders, day is the day with the maximum total incurred toll of , passing through roads.
Sample Input 2
9 1 2 1 2 3 2 3 2 5 2 5 2 7 2 7 2
Sample Output 2
6
The following figure shows the route of the travel for one possible arrangement of the stayed cities for the solution. Note that the tolls incurred in the first day and the last day are always covered by the company.

Sample Input 3
15 1 2 1 5 3 3 4 3 4 3 6 5 5 4 5 3 6 3 3 2 11 5 11 3 13 2 13 1
Sample Output 3
15
Sample Input 4
3 1 0 1 0
Sample Output 4
0
配点 : 点
問題文
ある国には 個の都市があり、それらは 本の道路で結ばれています。道路は双方向に通行できます。便宜上、都市には から の、道路には から の番号が振られています。グラフ理論の用語を用いると、任意の二つの都市に対し、それらを結ぶ単純道がちょうど一つ存在します。すなわち、都市と道路から構成されるグラフは木です。また、 番の都市をこの木の根とみなすと、この木は全二分木となっています。(全二分木とは、葉以外の任意の頂点がちょうど二つの子を持つような根付き木のことをいいます。) 番の道路は 番の都市と 番の都市を結び、一回の通行ごとに の通行料が発生します。( が であるような道路では通行料は発生しません。)
番の都市に、社員の旅行を奨励していることで有名な会社があります。この会社には道路通行料補助制度という制度があり、社員の旅行中に発生する道路の通行料のほとんどを会社が負担します。旅行がこの制度の適用対象となるためにはいくつかの制約を満たす必要があり、その範囲内であれば好きなように旅程を決めることができます。これらの詳細は以下の通りです。
-
制度の適用対象となるためには、旅行の出発点と終着点はともに 番の都市でなければならない。また、この国の都市と道路を 番の都市を根とする根付き木とみなしたとき、この木の葉の個数を とすると、旅行日程は 泊 日でなければならない。これらの 回の宿泊は、木の葉に相当する都市のすべてで一度ずつ行わなければならない。
-
旅行全体を通じて、この国のすべての道路をそれぞれちょうど二度ずつ通行しなければならない。
-
旅行中に発生する道路の通行料のうち、社員自身が負担しなければならない金額は、発生する通行料の合計が最大であるような日(ただし旅行初日および最終日を除く)に発生する通行料の合計である。残りの金額は会社の負担となる。
シックはこの会社の従業員です。道路通行料補助制度のもとで行う今度の旅行では、発生する通行料のうち自分自身で支払わなければならない金額を最小にすることだけを考えています。そのような旅程を組む手伝いをしてあげてください。
制約
- すべての に対し、
- は整数である。
- 与えられる木は全二分木である。
入力
入力は以下の形式で標準入力から与えられる。
出力
道路通行料補助制度のもとで旅行を行うとき、発生する通行料のうち自分自身で負担しなければならない金額の最小値を出力せよ。
入力例 1
7 1 1 1 1 2 1 2 1 3 1 3 1
出力例 1
4
都市と道路を 番の都市を根とする根付き木とみなしたとき、この木には 個の葉が存在するため( 番の都市に相当する頂点)、 旅行日程は 泊 日となります。これらの つの都市に宿泊する順序は 通り存在しますが、そのうちの一部では道路通行料補助制度の対象外となってしまいます。例えば、 や の順に都市を訪れると制度の対象になりますが、 の順に訪れると経路中に 番の都市と 番の都市を結ぶ道路を 回通ってしまい、制度の対象外となってしまいます。下図にこれらの訪問順序に対応する旅行の経路を示します。

制度の対象となるような都市の訪問順序すべてにおいて、対応する旅程では 日目に 本の道路を通行して合計で の通行料が発生し、発生する通行料の合計が最大であるような日はこの日となります。
入力例 2
9 1 2 1 2 3 2 3 2 5 2 5 2 7 2 7 2
出力例 2
6
下図に負担金額が最小となるような旅行の経路をひとつ示します。

負担金額を算出する際に、旅行初日および最終日に発生する通行料は考えないことに注意してください。
入力例 3
15 1 2 1 5 3 3 4 3 4 3 6 5 5 4 5 3 6 3 3 2 11 5 11 3 13 2 13 1
出力例 3
15
入力例 4
3 1 0 1 0
出力例 4
0
Time Limit: 2 sec / Memory Limit: 256 MB
Score : points
Problem Statement
Shik's job is very boring. At day , his boss gives him a string of length which consists of only lowercase English letters. In the -th day after day , Shik's job is to copy the string to a string . We denote the -th letter of as .
Shik is inexperienced in this job. In each day, when he is copying letters one by one from the first letter to the last letter, he would make mistakes. That is, he sometimes accidentally writes down the same letter that he wrote previously instead of the correct one. More specifically, is equal to either or . (Note that always equals to .)
You are given the string and another string .
Please determine the smallest integer such that could be equal to . If no such exists, please print -1
.
Constraints
- The lengths of and are both .
- Both and consist of lowercase English letters.
Input
The input is given from Standard Input in the following format:
Output
Print the smallest integer such that could be equal to . If no such exists, print -1
instead.
Sample Input 1
5 abcde aaacc
Sample Output 1
2
= abcde
, = aaccc
and = aaacc
is a possible sequence such that .
Sample Input 2
5 abcde abcde
Sample Output 2
0
Sample Input 3
4 acaa aaca
Sample Output 3
2
Sample Input 4
5 abcde bbbbb
Sample Output 4
-1
配点 : 点
問題文
シックの仕事はコピー取りです。ある日、シックは上司から英小文字からなる長さ の文字列 を受け取りました(この日を 日目とします)。これ以降、 日目の仕事は、文字列 を にコピーすることです。以下、 の 番目の文字を と表します。
シックはまだこの仕事に慣れていません。毎日、文字列を先頭の文字から順に書き写していくのですが、正しい文字の代わりに誤って直前に書いた文字と同じ文字を書いてしまうことがあります。すなわち、 は または のどちらかと等しくなります。(ただし、文字列の先頭の文字を書き間違えることはありません。すなわち、 は必ず と等しくなります。)
二つの文字列 と が与えられます。 が と等しくなる可能性があるような最小の整数 を求めてください。もしそのような が存在しなければ、代わりに -1
と答えてください。
制約
- と の長さはともに である。
- と はともに英小文字のみからなる。
入力
入力は以下の形式で標準入力から与えられる。
出力
が と等しくなる可能性のあるような が存在するならば、そのような のうち最小のものを出力せよ。そのような が存在しなければ、代わりに -1
と出力せよ。
入力例 1
5 abcde aaacc
出力例 1
2
= abcde
, = aaccc
, = aaacc
のように、 となる可能性が存在します。
入力例 2
5 abcde abcde
出力例 2
0
入力例 3
4 acaa aaca
出力例 3
2
入力例 4
5 abcde bbbbb
出力例 4
-1