group_id
stringlengths
12
18
problem_description
stringlengths
85
3.02k
candidates
listlengths
3
20
aoj_0259_cpp
すべての数は6174に通ず 0 から 9 の数字からなる四桁の数 N に対して以下の操作を行う。 N の桁それぞれの数値を大きい順に並べた結果得た数を L とする N の桁それぞれの数値を小さい順に並べた結果得た数を S とする 差 L-S を新しい N とする(1回分の操作終了) 新しい N に対して 1. から繰り返す このとき、全桁が同じ数字(0000, 1111など)である場合を除き、あらゆる四桁の数はいつかは 6174になることが知られている。例えば N = 2012の場合 1回目 (N = 2012): L = 2210, S = 0122, L-S = 2088 2回目 (N = 2088): L = 8820, S = 0288, L-S = 8532 3回目 (N = 8532): L = 8532, S = 2358, L-S = 6174 となり、3 回の操作で 6174 に到達する。 0 から 9 の数字からなる四桁の数が与えられたとき、何回の操作で 6174 に到達するか計算するプログラムを作成して下さい。 入力 入力は複数のデータセットからなる。入力の終わりは 0000 が1つの行で示される。各データセットは以下の形式で与えられる。 N データセットは1行であり、N (1 ≤ N ≤ 9999) は四桁の数を示す。N < 1000 の場合は上の桁は 0 で埋められている。 データセットの数は 10000 を超えない。 出力 各データセットごとに何回の操作で 6174 に到達したかを1行に出力する。ただし全桁が同じ数字である数が入力として与えられた場合は NA と出力する。 入力例 6174 2012 3333 0000 出力例 0 3 NA
[ { "submission_id": "aoj_0259_9621832", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define MAX 1000000007\n\nint main(void){\n string s;\n while(true){\n cin >> s;\n if(s == \"0000\") break;\n if(s[0] == s[...
aoj_0260_cpp
パイプつなぎ職人の給料 ぼくはパイプつなぎ職人です。パイプをつなぐジョイントとパイプさえもらえれば、どんなパイプだってつないでみせます。ぼくは毎日、親方からパイプとジョイントを渡されて、それをつないで親方に渡します。でも、パイプの数が多すぎるときは、1日でそれを全部つなげることはできません。そんなときでも親方はにっこり笑って、ぼくに給料を渡してくれます。 ところで、あるとき変なことに気がつきました。全部のパイプをつなげられたときより、つなげられなかったときの方が給料が多いことがしょっちゅうあるんです。あんまり変なので、ある日、親方が来たときに、給料の計算方法が書いてあるメモをこっそり見ちゃいました。そしたら、なんと "給料は「パイプの本数×パイプの長さの総和」で支払う。ただし、ジョイントでつなげて、ひとつながりになったものは、それを1本のパイプとみなす。" って書いてありました。これで全部つなげた方が給料が安くなることがある理由がわかりました。たとえば下図のように、長さ 1 のパイプ 3 本と長さ 2 のジョイント 2 本を全部つなげると長さ 1+2+1+2+1 = 7 のパイプが 1 本できるので、1 × (7) = 7 です。でも、ジョイントを一つだけ使って長さ 1+2+1 = 4のパイプと長さ 1 のパイプの 2 本にすると2 × (4+1) = 10なので、全部つなげるよりいっぱい給料がもらえます。 親方がなんでこんな方法で給料を決めてるかわからないけど、これでぼくもどうすればもらえる給料を多くできるかわかりました! それでは、パイプの本数が与えられたとき、もらえる給料の最大の金額を計算するプログラムを作成してください。 入力 入力は複数のデータセットからなる。入力の終わりはゼロ1つの行で示される。各データセットは以下の形式で与えられる。 n p 1 ... p n j 1 ... j n-1 1行目にパイプの本数 n (2 ≤ n ≤ 65000) が与えられる。2行目は1つの空白で区切られた n 個の整数からなる。p i (1 ≤ p i ≤ 1000) はi番目のパイプの長さを示す。3行目は1つの空白で区切られた n-1 個の整数からなる。j i (1 ≤ j i ≤ 1000) は i 番目のジョイントの長さを示す。 i番目のジョイントは、i 番目と i+1 番目のパイプだけをつなげることができる。つなげたパイプの長さは、p i + j i + p i+1 になる。 データセットの数は 100 を超えない。 出力 各データセットごとに、得られる給料の最大の金額を1行に出力する。入力として与えられるデータセットでは、出力される値は必ず32ビット符号無し整数の範囲に収まるものとする。 入力例1 3 1 1 1 3 3 4 3 3 3 3 1 1 1 5 1 2 3 4 5 4 3 2 1 0 出力例 12 48 76
[ { "submission_id": "aoj_0260_10867597", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define FOR(i,s,t) for(int i = s; i < t ; ++i)\n#define ALL(a) a.begin(),a.end()\n#define SORT(a) sort(ALL(a))\n#define UNIQ(a) a.erase(unique(ALL(a)),a.end())\n#define debug(a) cerr<<#a\":=\"<<a<<endl;\n...
aoj_0262_cpp
すごろくを作る 太郎君は、子供会の催しでみんなで遊べるようにすごろくを作りました。ゲームをおもしろくするために、「ふりだし」と「あがり」以外のすごろくのマスのいくつかに「6つ進む」、「5つ戻る」のように指示を書き込んでいきました。ルーレットを回して出た数だけ進み、止まったマスに指示が書き込んであれば、その指示に従って移動します。ただし、指示に従って進んだ先のマスの指示には従いません。 ルーレットは1からある数までの間の数を等確率で出すことができるものとします。また、「あがり」に達するより大きな数が出たときや、指示に従うと「あがり」より先に進んでしまうときは「あがり」に移動します。指示に従って戻るときに「ふりだし」より前に戻ってしまうときは「ふりだし」に戻ることにします。 ところが、ルーレットとマスの指示によっては「あがり」にたどりつけない場合が出てきてしまいます。たとえば、図のようなすごろくを作ったとしましょう。1と2しか出ないルーレットを使うと、1,2の順に出れば「あがり」に行けますが、はじめに2が出たらその後は何が出ても永久に「あがり」にはたどり着けません。太郎君は、そうとは知らずに調子に乗ってあちこちのマスに指示を書き込んでしまいました。 そこで、太郎君に代わって、ルーレットとマスの指示によっては、「あがり」にたどり着けない場合が生じるかどうか判定するプログラムを作成してください。 入力 入力は複数のデータセットからなる。入力の終わりはゼロ1つの行で示される。各データセットは以下の形式で与えられる。 max n d 1 d 2 . . . d n 1行目にルーレットが出す数の最大値 max (2 ≤ max ≤ 250) が与えられ、2行目に「ふりだし」と「あがり」以外のマスの数 n (2 ≤ n ≤ 250) が与えられる。続く n 行に各マスの指示を表す数 d i (-n ≤ d i ≤ n) が与えられる。d i がゼロのときは指示が書いていないことを、正の数のときは |d i | 進む指示を、負の数のときは |d i | 戻る指示を表す(ここで、|x| は x の絶対値を表す)。入力される値はすべて整数である。 データセットの数は 100 を超えない。 出力 各データセットごとに判定結果を1行に出力する。ルーレットとマスの指示によっては、「あがり」にたどり着けない場合が生じるときは「NG」、そうでなければ「OK」を出力する。 入力例 3 3 -2 1 0 2 4 2 0 -1 -2 2 2 -2 -2 0 出力例 OK NG NG
[ { "submission_id": "aoj_0262_9597880", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define Rrep(i,n) for(int i=n-1;i>=0;i--)\n#define Rrep1(i,n) for(int i=n;i>0;i--)\n#define all(a) a.begin(),a.end()\nusin...
aoj_0263_cpp
ビートパネル 遊太君は、近所のゲームセンターで人気のゲーム「ビートパネル」にはまっています。このゲームは図のようなグリッド状に並べられた 4×4 の計16個のパネル型のボタンからなります。 図のように、左上から右下にボタン1, ボタン2, …, ボタン16の順にボタンが並んでいます。ゲームでは一定間隔でビート音が鳴り最後に終了音が鳴ります。ビート音が鳴ると同時に複数のボタンが光ります。1個も光らない場合もあります。プレイヤーは、ビート音が鳴った直後から次の音が鳴るまでの間に両手の指を使って複数のボタンを1度だけ同時に押すことができます。何も押さないということも可能です。終了音が鳴ったと同時にゲームは終了します。 遊太君は c 通りの押し方を習得しており、ビート音が鳴る度に、それらの押し方の中から1つ決めてボタンを押します。押したボタンが光っていれば、それらのボタンの光は消え、消えたボタンの数がプレイヤーのスコアに加算されます。また、一度光ったボタンはそのボタンが押されるまで光は消えることはありません。 ビート音が n 回鳴るときのボタンの光り方と遊太君が習得している c 通りのボタンの押し方を入力とし、遊太君が獲得することのできる得点の最大値を出力するプログラムを作成してください。 入力 入力は複数のデータセットからなる。入力の終わりはゼロ2つの行で示される。各データセットは以下の形式で与えられる。 n c a 1,1 a 1,2 ... a 1,16 a 2,1 a 2,2 ... a 2,16 ... a n,1 a n,2 ... a n,16 b 1,1 b 1,2 ... b 1,16 b 2,1 b 2,2 ... b 2,16 ... b c,1 b c,2 ... b c,16 1行目は1つの空白で区切られた2つの整数からなる。n (1 ≤ n ≤ 30) はビート音の数、c (1 ≤ c ≤ 30) は習得しているボタンの押し方の数を示す。続く n+c 行にボタンの光り方とボタンの押し方が与えられる。行中の入力項目の区切りは1つの空白である。a k,i は k 回目のビート音が鳴ったときのボタン i の光り方を、b j,i は c 通り中 j 番目の押し方でボタン i が押されるかどうかを表す。a k,i の値は 0 が「光らない」、1 が「光る」を表す。b j,i の値は 0 が「押さない」、1 が「押す」を表す。 データセットの数は 20 を超えない。 出力 各データセットごとに、得点の最大値を1行に出力する。 入力例 2 2 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 2 2 0 0 1 0 1 1 1 1 0 0 1 0 0 0 1 0 0 1 0 1 0 1 1 1 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 5 3 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 出力例 8 7 16
[ { "submission_id": "aoj_0263_10848448", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing VS = vector<string>; using LL = long long;\nusing VI = vector<int>; using VVI = vector<VI>;\nusing PII = pair<int, int>; using PLL = pair<LL, LL>;\nusing VL = vector<LL>; using ...
aoj_0264_cpp
有限体電卓 0以外のすべての実数の逆数は実数になりますが、整数の逆数が整数になるとは限りません。C言語などで 3.0/2.0*2.0 = 3.0 なのに 3/2*2 = 2 になってしまうのはこのためです。ですが、素数で割った余りが等しい整数を同じものとみなすと、0以外のすべての整数が逆数をもつようにできます。 整数 x と y を p で割った余りが等しいとき、x ≡ y (mod p) と書きます。p が素数のとき、このような x と y を同じものとみなすなら、すべての整数 n は0から p−1 までのどれかの整数 x に対して x ≡ n (mod p) となるので、けっきょく { 0, 1, 2, ..., p−1 } という集合だけからなる世界を考えればよいことがわかります。 この世界での加減乗除は以下のようになります。 加算 x+y の値は、x+y ≡ z (mod p) となる 0 から p−1 までの数 z になります。 減算 x−yの値は、y+m ≡ 0 (mod p) となる 0 から p−1 までの数 m(これが y に相当します)について、x−y ≡ x + m (mod p) となることから得られます。たとえば、p = 5のとき、4+1 ≡ 0 (mod 5) となります。このとき、2−4 ≡ 2+1 ≡ 3 (mod 5) より、2−4の値は3になります。 乗算 x*y の値は、x*y ≡ z (mod p) となる 0 から p−1 までの数 z になります。 除算 x/y の値は、y*d ≡ 1 (mod p)となる 0 から p−1 までの数 d (これがyの逆数になります)について、x/y ≡ x*d (mod p) となることから得られます。たとえば、p = 5 のとき、2*3 ≡ 1 (mod 5) より 3 は 2 の逆数になります。このとき、1/2 ≡ 1*3 ≡ 3 (mod 5) より、1/2 の値は 3 になります。 このようにして加減乗除の四則演算がすべて0からp−1の範囲に収まります。このとき、集合 { 0,1,...,p−1 } を p の有限体といいます。この有限体の中で、加減乗除、0 から p−1 までの数、カッコを使って 算術式を構成することができます。 p の有限体の 0 以外のすべての要素が逆数をもつことは、フェルマーの小定理と呼ばれる有名な定理 a p−1 ≡ 1 (mod p) からわかります(ただし、p は素数、a と p は互いに素)。p の有限体のすべての要素 x は p と互いに素なので、この定理から x*x p-2 ≡ 1 (mod p) となり x p-2 が x の逆数になるからです。 では、素数と式が与えられたときに、その素数の有限体で式を計算する電卓プログラムを作成してください。 入力 入力は複数のデータセットからなる。入力の終わりは0の後にコロンが1つ続くだけの行 0: で示される。各データセットは以下の形式で与えられる。 p:exp データセットは1行であり、p (2 ≤ p ≤ 46000) は素数、exp は算術式を示す。exp は加減乗除(それぞれ +, -, *, /)とカッコ、0 から p−1 までの数から構成される。演算子、数、カッコなどの前後には1つ以上の空白が現れる可能性がある。exp の長さは 100,000 を超えない。 データセットの数は 1000 を超えない。 出力 データセットごとに、計算結果を出力する。出力形式は以下の通りである。 exp = val (mod p) val は、p の有限体での算術式 exp の計算結果である。ただし、0 での除算が含まれるときは NG と出力する。また、exp に現れる加減乗除、カッコ、数の前後には空白を空けないこと。ただし、= の前後には空白を一つずつ空ける。また、valと開きカッコの間、modとp の間にも空白を一つずつ空ける。 入力例 5: 2 - 3 17: 1 + 3 * (2 + 3 / 5 * 2) + 7 11: 1 / 8 - 5 - 8 * 2 19: 8 / (2 - 3 * 7) 1153: 10 * 3 / 7 + ( 50 + 81 / 22 ) + 11 0: 出力例 2-3 = 4 (mod 5) 1+3*(2+3/5*2)+7 = 4 (mod 17) 1/8-5-8*2 = 8 (mod 11) NG 10*3/7+(50+81/22)+11 = 915 (mod 1153)
[ { "submission_id": "aoj_0264_10865771", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define FOR(i,s,t) for(int i = s; i < t ; ++i)\n#define ALL(a) a.begin(),a.end()\n#define SORT(a) sort(ALL(a))\n#define UNIQ(a) a.erase(unique(ALL(a)),a.end())\n#define debug(a) cerr<<#a\":=\"<<a<<endl;\n...
aoj_0267_cpp
ブロックの三角形 図a のように積まれたブロックに対し、以下の並べ替え操作を繰り返す。 一番下のブロック全て(図a 中の白のブロック)を右端に新しく積み上げる(残りのブロックは自動的に1段下に落ち、図b のようになる)。 ブロックの間に隙間ができたら、左に詰めて隙間をなくす(図 b から図c のようになる)。 1 以上の整数 k に対して、k×(k+1)/2 で表される数 (例:1, 3, 6, 10, ...)を三角数という。ブロックの総数が三角数の場合、上記の並べ替えを繰り返すと、左端の高さが1 で右に向かって1つずつ高くなっていくような三角形になると予想されている(図d は総数が15 個の場合)。 ブロックの最初の並びが与えられたとき、あらかじめ決められた回数以下の操作で、上で説明したようなブロックの三角形ができるとき、三角形が得られるまでの最小の操作回数を出力するプログラムを作成してください。 入力 入力は複数のデータセットからなる。入力の終わりはゼロ1つの行で示される。各データセットは以下の形式で与えられる。 N b 1 b 2 ... b N 各データセットは2行であり、ブロックの最初の並びを表す。N (1 ≤ N ≤ 100)は、一番下の段にあるブロックの数を示す。b i (1 ≤ bi ≤ 10000) は左から i 番目の位置に積まれているブロックの数を示す。b i と b i+1 は1つの空白で区切られている。ブロックの総数は 3 以上である。 データセットの数は 20 を超えない。 出力 データセットごとに、三角形ができるまでに行った並べ替え操作の回数を1行に出力する。ただし、三角形が作れない場合や、操作回数が 10000 回を超える場合は -1 を出力する。 入力例 6 1 4 1 3 2 4 5 1 2 3 4 5 10 1 1 1 1 1 1 1 1 1 1 9 1 1 1 1 1 1 1 1 1 12 1 4 1 3 2 4 3 3 2 1 2 2 1 5050 3 10000 10000 100 0 出力例 24 0 10 -1 48 5049 -1 最初のデータセットが、図に示した場合に対応する。 4つ目のデータセットが、ブロックの総数が三角数でないため、三角形が作れない場合に対応する。 最後のデータセットが、ブロックの総数は三角数だが、操作回数が 10000 回を超える場合に対応する。
[ { "submission_id": "aoj_0267_9568704", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\n\nconst int MAX_CNT = 10000;\nconst int MAX_SET = 20;\n\nint main()\n{\n int N;\n int setcount = 0;\n\n\n while (cin >> N && N != 0 && setcount < MAX_SET)\n {\n if (N < 1 ...
aoj_0265_cpp
ねこまっしぐら あるところに、高い塀に囲まれた大きなお屋敷がありました。そのお屋敷の主人は猫がとても大好きで、時折やってくる猫たちのために、いつもおいしいごはんを用意していました。お腹を空かせた猫たちは、高い塀をひょいと飛び越え、お屋敷の至る所に置いてあるごはんにまっしぐらに駆け寄るのでした。 ある日主人は、屋敷の中で何匹かの猫が倒れているのを見つけました。猫たちはごはんを探して屋敷の中を縦横無尽に走り回ったので、ぶつかったり転んだりしてしまったのです。主人は猫たちの安全を考えて、ごはんの置き場所を工夫することにしました。 上空から見ると、このお屋敷の塀は多角形をなしています。主人は猫たちがごはんを見つけやすいように、敷地内の中でも、多角形の頂点にあたる場所だけにごはんを置くことにしました。また猫たちは気まぐれなので、多角形の周上のどの点からお屋敷に入ってくるかは予想できません。そこで主人は、猫がどの点から入ったとしても、その点から直進していずれかのごはんにたどり着けるように、ごはんを配置することも決めました。 すべての頂点にごはんを配置すれば、この条件を満たすことができます。しかし、ごはんを補充して回るのは大変なので、主人はできるだけ少ない数の頂点にごはんを配置したいと思いました。さて、主人は最低何箇所にごはんを配置する必要があるでしょうか。 お屋敷の塀を表す多角形を入力とし、ごはんを配置する頂点の数の最小値を求めるプログラムを作成して下さい。ただし、猫は多角形の内部のみを直進できるものとします(辺は多角形の内部に含めるものとします)。 入力 入力は複数のデータセットからなる。入力の終わりはゼロ1つの行で示される。各データセットは以下の形式で与えられる。 n x 1 y 1 x 2 y 2 ... x n y n 1行目に多角形の頂点の数 n (3 ≤ n ≤ 16) が与えられる。続く n 行に多角形の頂点の座標が与えられる。n 行のそれぞれは、1つの空白で区切られた2つの整数からなる。x i (-1000 ≤ x i ≤ 1000) は i 番目の頂点の x 座標、y i (-1000 ≤ y i ≤ 1000) は i 番目の頂点の y 座標を示す。多角形の頂点は、隣り合った頂点を反時計回りに訪問するような順番で与えられる。 データセットの数は 20 を超えない。 出力 各データセットごとに、ご飯を配置する頂点の数を1行に出力する。 入力例 8 0 0 3 2 6 2 8 6 6 5 7 7 0 4 3 4 8 0 0 5 3 5 2 4 1 6 1 8 6 6 4 2 4 0 出力例 1 2
[ { "submission_id": "aoj_0265_5433557", "code_snippet": "#include<bits/stdc++.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに注意\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n\n\n#defi...
aoj_0268_cpp
金剛の型 以下のように、右から 7 ビット分が小数部、続く24 ビット分が整数部で、一番左の 1 ビット分を符号部とする 32 ビット実数型を考える(b 1 , ... , b 32 は 0 か1 を表す)。 この形式を、人が理解しやすい 10 進数表現に直すときは、以下のように解釈する。 (-1) 符号部 × (整数部 + 小数部) 上の表現で、整数部の値は b 8 + 2 1 ×b 9 + 2 2 ×b 10 + ... + 2 23 ×b 31 になる。例えば整数部が のようになっていた場合、整数部の値は以下のように計算される。 1 + 2 1 + 2 3 = 1 + 2 + 8 = 11 一方、小数部の値は(0.5) 1 ×b 7 +(0.5) 2 ×b 6 + ... + (0.5) 7 × b 1 になる。例えば小数部が のようになっていた場合、小数部は以下のように計算される。 0.5 1 + 0.5 3 = 0.5 + 0.125 = 0.625 さらに、符号部、整数部、小数部を合わせた以下のようなビット列の場合、 ビット列全体が表す10 進数は、以下のようになる(ただし、-1 の0 乗は1)。 (-1) 0 × (1 + 2 + 8 + 0.5 + 0.125) = 1 × (11.625) = 11.625 32 ビットのビット列を Q 個入力すると、それらのビット列が表す実数の10 進表記を一切の誤差無く出 力するプログラムを作成してください。 入力 入力は1つのデータセットからなる。入力データは以下の形式で与えられる。 Q s 1 s 2 . . . s Q 1 行目にビット列の数 Q (1 ≤ Q ≤ 10000)が与えられる。続くQ 行に入力ビット列 s i が与えられる。 入力ビット列は 16 進数表記で与えられるとする (例: 0111 1111 1000 1000 0000 0000 0000 0000 は 7f880000 と与えられる) 。つまり、Q 行のそれぞれには、2 進数を 4 ビット分ずつまとめた 16 進数が 8 個、空白をはさまずに並んでいる。10 進数、2 進数、16 進数の対応は以下の表のとおりである。 16 進数の英字 a から f までは小文字で与えられる。 出力 各ビット列が表す実数の 10 進表記を 1 行ずつ出力する。ただし、小数部において、ある桁以下がすべて 0 の場合、その桁以下は省略するものとするが、例外として小数部が 0 の場合は小数部には 0 ひとつを出力する。 入力例 8 00000000 80000000 00000080 00000040 000000c0 00000100 80000780 80000f70 出力例 0.0 -0.0 1.0 0.5 1.5 2.0 -15.0 -30.875
[ { "submission_id": "aoj_0268_10848234", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nint main() {\n int Q;\n cin >> Q;\n while(Q--) {\n string s;\n cin >> s;\n int n = 0;\n for(int i=0; i<8; ++i) {\n n <<= 4;\n ...
aoj_0269_cpp
風よ、私の梅の香りを届けておくれ! 引っ越しが決まり、この地を去ることになった。この土地自体に未練は無いが、1つだけ気になることがある。それは、庭に植えた梅の木のことだ。私は毎年、この梅が花を咲かすことを楽しみにしていた。ここを離れた後は春の楽しみが1つ減ってしまう。私の梅の香りだけでも風に乗って引っ越し先の家まで届き、春を楽しませてはくれないものか。 日本には春を象徴する3つの花がある。梅・桃・桜の3つだ。引っ越し先には、私の梅以外にも、これらの花の香りが届くだろう。しかし、私の梅の香りだけが届く日数が最も多い家に住みたい。 図のように、花の香りは扇状に広がり、その領域は風の向かう方向と強さによって決まる。扇形は風の向かう方向 w を中心にして対称に広がり、風の強さ a を半径とする領域をもつ。香りが広がる角度 d は花の種類によって決まっているが、風の向かう方向と強さは日によって異なる。ただし、同じ日では、すべての場所で風の向かう方向と強さは同じである。 手元には、私の梅以外の梅・桃・桜の位置と花の種類ごとの香りが広がる角度、引っ越し先の家の候補のデータがある。さらに、数日分の風の向かう方向と強さのデータもある。私の梅以外の梅・桃・桜の木と家の位置は、私の梅の位置を原点とした座標で示されている。 これらのデータを使って、私の梅の香りだけが届く日数の最も多い家を探すためのプログラムを書いてみることとしよう。私は有能なプログラマーなのだから! 入力 入力は複数のデータセットからなる。入力の終わりはゼロ2つの行で示される。各データセットは以下の形式で与えられる。 H R hx 1 hy 1 hx 2 hy 2 : hx H hy H U M S du dm ds ux 1 uy 1 ux 2 uy 2 : ux U uy U mx 1 my 1 mx 2 my 2 : mx M my M sx 1 sy 1 sx 2 sy 2 : sx S sy S w 1 a 1 w 2 a 2 : w R a R 各行で与えられる数値は1つの空白で区切られている。 1行目に引っ越し先の家の候補の数 H (1 ≤ H ≤ 100) と風の記録の数 R (1 ≤ R ≤ 100) が与えられる。続くH 行に引っ越し先の家の位置が与えられる。hx i とhy i は i 番目の家の x 座標と y 座標を示す-1000 以上1000 以下の整数である。 続く1行に、私の梅以外の梅の木の数 U と、桃・桜の木の数 M、S、梅・桃・桜の香りが広がる角度du、dm、ds が与えられる。U、M、S の範囲は 0 以上10 以下である。角度の単位は度であり、1 以上180 未満の整数である。 続く U 行に私の梅以外の梅の木の位置、続く M 行に桃の木の位置、続く S 行に桜の木の位置が与えられる。uxi と uyi、mxi と myi、sxi と syi はそれぞれ、i 番目の梅・桃・桜の木の x 座標と y 座標を示す、-1000 以上 1000 以下の整数である。 続く R 行に風の記録が与えられる。w i (0 ≤ w i < 360) と a i (0 < a i ≤ 100) は i 日目の風の向かう方向と強さを表す整数である。風の向かう方向は、x 軸の正の向きから反時計回りに測った角度で表し、単位は度とする。 入力は以下の条件を満たすと考えてよい。 入力される座標はすべて異なるものとする。 原点には私の梅以外無い。 どの花についても、花の香りが届く領域の境界から距離が 0.001 以内には家は無い。 データセットの数は 50 を超えない。 出力 データセットごとに、私の梅の香りだけが届く日数の最も多い全ての家の番号を、小さい順に1行に出力する。家の番号の間は空白1つで区切る。行の終わりには空白文字を出力しない。 ただし、どの家についても、私の梅の花の香りだけが届く日が1日もなければ、NA と出力する。 入力例 6 3 2 1 1 2 5 2 1 3 1 5 -2 3 1 1 1 90 30 45 3 -4 -3 0 2 -2 45 6 90 6 135 6 2 1 1 3 5 2 0 1 1 90 30 45 -3 0 2 -2 45 6 0 0 出力例 5 6 NA
[ { "submission_id": "aoj_0269_4985319", "code_snippet": "#include <cmath>\n#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\nconst double pi = acos(-1.0);\nclass point2d {\npublic:\n\tdouble x, y;\n\tpoint2d() : x(0.0), y(0.0) {};\n\tpoint2d(double x_, double y_) : x(x_), y(...
aoj_0272_cpp
寂しがり屋のついた嘘 パケットモンスターというゲームがあります。モンスターを捕まえ、育て、戦わせたり交換したりするゲームで、日本中で大流行しています。もちろん、わたしのクラスでも。だからわたしもこのゲームでみんなと遊べるようにモンスターを育てているけど、悲しいことに対戦する相手がいません。わたしは積極的に人に話しかけるのが得意なタイプではないからです。だから今日も、クラスメイトが誘ってくれるのを待ちながら、独り寂しくモンスターのレベルを上げています。 そしてついに、待ち望んでいた瞬間が訪れました。クラスメイトの一人が対戦しないかとわたしに話を持ちかけてきたのでした。彼女は自慢気に、彼女のモンスターたちを見せてくれました。 一般的なパケットモンスターの対戦ルールでは、各プレイヤーがそれぞれN 匹のモンスターを用意し、互いに1 匹ずつ戦わせます。各モンスターには成長の度合いを示す「レベル」という数値があり、レベルが大きいモンスターが必ず勝ちます。同レベルのときは引き分けです。一度戦ったモンスターは、それ以上戦わせることができません。こうして一対一の戦いをN 回行ったあと、過半数の勝利を収めたプレイヤーが勝者となります。 わたしは、彼女のモンスターを見た瞬間、嫌な予感がしました。彼女のモンスターはどれも、とても強そうなのです。わたしと戦ってくれるのなら負けてもかまいません。でも、あまりにもひどい負け方をしたら、"こいつと戦っても面白くないな"とか思われて、もう遊んでくれなくなるんじゃないかな。それは、とても嫌です。だから、 「ごめん、わたしまだモンスターをN 匹持ってなくて」 嘘をつきました。 N 回勝負では勝ち目がなくても、それより少ない数のモンスターで勝負する特別ルールなら、もしかしたら勝てるかもしれません。さっきは負けたってかまわないと言ったけど、やっぱり勝てたら嬉しいです。 彼女はこの特別ルールを受け入れてくれました。つまり、二人が持っているモンスターからお互いに k 匹選んで一匹ずつ順番に戦わせ、過半数の(つまり、k/2 より大きな数の) 勝利を収めた方が勝ちとなります。 さっきわたしは、彼女のモンスターのレベルを知ってしまいました。彼女がどのモンスターを選び、どんな順番で出してくるのかはわかりません。でも、対戦させるモンスターの数kをわたしがうまく決めれば、彼女がどんな選択をしようとわたしが勝つことができるかもしれません。 皆さんにお願いです。モンスターの数 N と、二人が持っているモンスターのレベルを入力すると、彼女がどんな選択をしようとわたしが勝てるような最小のモンスターの数 k を出力するプログラムを作成して下さい。 入力 入力は複数のデータセットからなる。入力の終わりはゼロ1つの行で示される。各データセットは以下の形式で与えられる。 N a 1 a 2 ... a N b 1 b 2 ... b N 1行目にモンスターの数 N (1 ≤ N ≤ 40000) が与えられる。2行目に自分のモンスターのレベル a i (1 ≤ a i ≤ 100000) が1つの空白区切りで与えられる。3行目にクラスメイトのモンスターのレベル b i (1 ≤ b i ≤ 100000) が1つの空白区切りで与えられる。 データセットの数は 50 を超えない。 出力 データセットごとに、k (1 ≤ k < N) が存在すれば最小値を1行に出力する。もし、k が存在しないか、k が N に等しければ NA と出力する。 入力例 10 4 9 1 9 5 9 2 3 2 1 8 7 6 5 10 5 5 4 7 6 5 4 3 2 5 1 4 4 4 4 4 4 4 1 3 2 4 3 2 1 0 出力例 3 1 NA 1つ目のデータセットでは、わたしは3匹での対戦を提案し、レベルが [9, 9, 9] のモンスターを選べば良い。相手のレベル 10 以外のどのモンスターにも勝てるので、少なくとも2勝できる。2匹での対戦では 1勝1敗になる可能性があり、これは過半数の勝利とは言えないので、2 は答えにならない。 2つ目のデータセットでは、わたしは最強のモンスターを1匹選べば良い。 3つ目のデータセットでは、わたしがどの k 匹を選び、どの順番で出したとしても、彼女が全く同じモンスターを同じ順番で出してきて引き分けになる可能性がある。したがって、「彼女がどんな選択をしようとわたしが勝つことができる」とは言い切れない。
[ { "submission_id": "aoj_0272_10774072", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nconstexpr int N = 40000;\nint a[N], b[N];\nint cnt[N];\nint n;\n\nvoid solve() {\n for (int i = 0; i < n; ++i) cin >> a[i];\n for (int i = 0; i < n; ++i) cin >> b[i];\n sort(a, a + n);\n ...
aoj_0270_cpp
モジュロ・クエリ あなたに N 枚のカードを渡します。どのカードにも一つだけ自然数が書いてあります。 これから質問として、適当な自然数を言います。あなたが持っているカードに書いてある数を私が言った数で割ったときに得られる余りのうち最も大きなものを答えてください。 たとえば、あなたは 3 枚のカードを持っていて、それぞれ 9、3、8 と書いてあるとします。私が「4」と言ったら、9 と 3 と 8 をそれぞれ4 で割った余りを求めてください。余りはそれぞれ 1、3、0 ですが、この中でもっとも大きな余りは3 なので、3 が正しい答えになります。 では始めましょうか。え? カードがいっぱいあるとたいへんだ? しょうがないですね。それではコ ンピュータを使って最大の余りを見つけることにしましょう。カードに書いてある数を、質問された数で割った余りのうち、最大のものを見つけるプログラムを作成してください。なお、質問は1回だけでなく何度もしますが、同じ数を 2 回以上質問することはありません。 入力 入力は1つのデータセットからなる。入力データは以下の形式で与えられる。 N Q c 1 c 2 ... c N q 1 q 2 : q Q 1行目にカードの枚数 N (2 ≤ N ≤ 300000) と質問の回数 Q (2 ≤ Q ≤ 100000) が1つの空白区切りで与えられ、2行目にカードに書かれた数 c i (1 ≤ c i ≤ 300000) が1つの空白区切りで与えられる。続くQ 行に質問として与えられる数 q i (1 ≤ q i ≤ 300000) が与えられる。 出力 質問ごとに最大の余りを1行に出力する。 入力例 3 3 9 3 8 4 6 5 出力例 3 3 4
[ { "submission_id": "aoj_0270_10853187", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int N, Q;\n cin >> N >> Q;\n vector<int> c(N);\n int size = 0;\n for(int i=0; i<N; ++i) {\n cin >> c[i];\n size = max(size, c[i]);\n }\n size += 1;\n ...
aoj_0261_cpp
マヤの大予言 真也君はテレビで「マヤの大予言!2012年で世界が終る?」という番組を見ました。結局、世界が終るかどうかはよくわかりませんでしたが、番組で紹介されていたマヤの「長期暦」という暦に興味を持ちました。その番組では以下のような説明をしていました。 マヤ長期暦は、右の表のような単位からなる、全部で13バクトゥン(187万2000日)で構成される非常に長い暦である。ある計算法では、この暦は紀元前3114年8月11日に始まり2012年12月21日に終わると考えられていて、このため今年の12月21日で世界が終るという説が唱えられている。しかし、13バクトゥンで1サイクルとなり、今の暦が終わったら新しいサイクルに入るだけという考えもある。 1キン=1日 1ウィナル=20キン 1トゥン=18ウィナル 1カトゥン=20トゥン 1バクトゥン=20カトゥン 「ぼくの二十歳の誕生日はマヤ暦だと何日になるのかな?」真也君はいろいろな日をマヤ長期暦で表してみたくなりました。 では、真也君の代わりに、西暦とマヤ長期暦とを相互変換するプログラムを作成してください。 入力 入力は複数のデータセットからなる。入力の終わりは#1つの行で示される。各データセットは以下の形式で与えられる。 b.ka.t.w.ki または y.m.d データセットは1つの文字列を含む1行からなる。b.ka.t.w.ki はマヤ長期暦の日付、y.m.d は西暦の日付である。与えられる単位は以下の通りである。 マヤ長期暦 b バクトゥン (0 ≤ b< 13) ka カトゥン (0 ≤ ka < 20) t トゥン (0 ≤ t< 20) w ウィナル (0 ≤ w < 18) ki キン (0 ≤ ki < 20) 西暦 y 年 (2012 ≤ y ≤ 10,000,000) m 月 (1 ≤ m ≤ 12) d 日 (1 ≤ d ≤ 31) 西暦の日の最大値は、大の月、小の月、うるう年かどうかで変わる(うるう年は 4 の倍数の年のうち、100 で割り切れない年か、400 で割り切れる年である)。マヤ長期暦の日付の範囲は 0.0.0.0.0 から 12.19.19.17.19 までとする。ただし、マヤ長期暦の 0.0.0.0.0 は西暦の 2012.12.21 に対応する。また、西暦の日付の範囲は 2012.12.21から 10000000.12.31 までとする。 データセットの数は 500 を超えない。 出力 入力が西暦のときはマヤ長期暦を、マヤ長期暦のときは西暦を、それぞれ入力と同じ形式で出力する。 入力された西暦を換算した結果、マヤ長期暦の次のサイクルに入った場合でも b.ka.t.w.ki の形式で出力してよい。 入力例 2012.12.31 2.12.16.14.14 7138.5.13 10.5.2.1.5 10000000.12.31 # 出力例 0.0.0.0.10 3054.8.15 0.0.0.0.10 6056.2.29 8.19.3.13.2
[ { "submission_id": "aoj_0261_9597837", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i,n) for(int i=0;i<n;i++)\n#define rep1(i,n) for(int i=1;i<=n;i++)\n#define Rrep(i,n) for(int i=n-1;i>=0;i--)\n#define Rrep1(i,n) for(int i=n;i>0;i--)\n#define all(a) a.begin(),a.end()\nusin...
aoj_0274_cpp
図画工作 イヅア大学附属小学校は日本有数の競技プログラマー養成校として有名です。この学校の教員は幅広い アルゴリズムの知識を持ち、日々それを活用しています。教員であるあなたは、今年は図画工作の授業 を担当することになりました。この授業では、児童全員がそれぞれ1年間で一つの課題を完成させることになっています。授業の概要は以下のとおりです。 1年間で授業は D 回(同じ日に2 回以上授業はない)あり、その全てが課題制作に充てられる。 制作する課題は M 種類用意されている。 それぞれの児童に、M 種類の中から課題を1つずつ割り当てる。 児童は N 人であり、N 人それぞれに異なる課題が割り当てられる。 児童は、K 種類ある部品のうちいくつかの種類の部品を使用して課題を完成させます。課題の制作の概要は以下のとおりです。 課題ごとに、使用すべき部品の種類と数はあらかじめ決められている。 課題が完成するまでに使用する部品の種類と数は、その課題で使用すべき部品の種類と数に、過不足なく一致していなければならない。 異なる課題でも、使われる部品の種類と数がすべて同じ場合もある。 どの課題も、同じ種類の部品は2個までしか使用できない。 部品を使用する順序は課題の完成に影響を与えない。 いくつかの部品が入っている袋が事前に P 個用意されている。ただし、異なる袋でも、入っている部品の種類と数がすべて同じ場合もある。 教員は、児童1人につき袋を1つだけ渡すことができる(袋を渡さない児童がいてもよい)。 2人以上の児童に同じ袋を渡すことはできない(反対に、誰にも渡されない袋があってもよい)。 袋を渡された児童は、袋の中に入っている部品をすべて、自分が制作する課題に使わなければならない。 袋に入っている部品以外で課題に使用する部品は、別途購入する必要があります。部品の購入に関する条件は以下のとおりです。 部品は授業の日だけ購入でき、その日にしか使えない。 それぞれの課題について、1回の授業でL個までの部品を購入することができる。 部品の種類によって価格が設定されており、購入する日によって価格が変動する。ただし、どの種類も品切れになることはない。 あなたは、このような条件下で、授業にかかる経費をなるべく抑えたいと考えています。そこで、児童全員の部品購入費の合計の最小値を計算するプログラムを作成することにしました。 入力 入力は複数のデータセットからなる。入力の終わりはゼロ3つの行で示される。各データセットは以下の形式で与えられる。 D K L c 1,1 c 1,2 ... c 1,K c 2,1 c 2,2 ... c 2,K : c D,1 c D,2 ... c D,K M N P r 1,1 r 1,2 ... r 1,K r 2,1 r 2,2 ... r 2,K : r M,1 r M,2 ... r M,K t 1,1 t 1,2 ... t 1,K t 2,1 t 2,2 ... t 2,K : t P,1 t P,2 ... t P,K 各行で与えられる数値は1つの空白で区切られている。 D(1 ≤ D ≤ 8)は授業の回数、K(1 ≤ K ≤ 8)は部品の種類の数、L(1 ≤ L ≤ 8)は1日に購入することができる部品の数を示す。c d,k (1 ≤ c d,k ≤ 100)は d 日目の部品k の価格を示す。 M(1 ≤ M ≤ 200)は課題の種類の数、N(1 ≤ N ≤ M)は生徒の人数、P(1 ≤ P ≤ 200)は袋の数を示す。 r m,k (0 ≤ r m,k ≤ 2)は課題 m に必要な部品 k の個数、t p,k (0 ≤ t p,k ≤ 2)は袋 p に入っている部品 k の個数を示す。 部品を1つも必要としない課題や、空の袋は与えられないものとする。 データセットの数は20 を超えない。 出力 データセットごとに、生徒全員の部品購入費の合計の最小値を1行に出力する。N種類の作品を完成させることができない場合は -1 を出力する。 入力例 2 2 1 5 3 2 2 3 3 1 2 0 1 2 2 2 1 1 2 2 1 5 3 2 2 3 2 1 2 0 1 2 2 2 1 1 2 2 2 5 3 2 2 3 2 1 2 0 1 2 2 2 1 1 4 3 1 2 2 1 3 2 2 2 3 3 1 2 2 5 4 3 1 1 0 1 0 1 1 0 2 1 1 2 2 2 2 1 0 1 2 0 2 1 1 1 0 0 0 出力例 -1 9 6 7
[ { "submission_id": "aoj_0274_9267197", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <set>\nusing namespace std;\ntypedef long long ll;\n#define rep(i, n) for(int i = 0; i < (n); i++)\n\ntemplate<class T>\nusing vi = vector<T>;\n\nt...
aoj_0273_cpp
ねこまっしぐら2 あるところに大きな古いお屋敷があり、1匹のねこが住み着いていました。図のように、お屋敷は上空から見ると凸多角形になっており、まっすぐな壁で囲まれたいくつかの部屋で造られています。1枚の壁はその両端にある柱によって支えられています。お屋敷はとても古いため、どの壁にもねこが通ることができるくらいの穴が1つ空いています。ねこは壁の穴を通ってお屋敷の部屋と部屋または部屋と外を行き来することができます。 お屋敷のご主人は、ねこに餌をあげるために、口笛を吹いてねこをお屋敷の外へ呼び出します。ねこはとても賢く、ご主人が口笛を吹くと、「最良の選択」でお屋敷の外へ抜け出します。つまり、最も少ない回数だけ穴を通って外へ出ます。 ねこはとても気まぐれで、どの部屋にいるか分かりません。そのため、ねこがお屋敷の外へ出るのにかかる時間は日によって異なり、ご主人はどれだけ待てばよいかわからず困っていました。ある時ご主人は、ねこが穴を通り抜けるときに、とても時間がかかっていることに気が付きました。ということは、ねこが外に出るのにかかる時間は穴を通る回数によって決まることになります。ご主人は、ねこが「最良の選択」をした場合の穴を通る回数の最大値が分かれば、ねこがどの部屋にいたとしても、最大どれだけ待てばよいか分かるのではないかと考えました。ご主人はお屋敷の間取りを知っていますが、お屋敷はとても大きく、自分では計算することができません...。 お屋敷の間取りを読み込み、ねこが「最良の選択」をした場合、外に出るまでに通る穴の数の最大値を 出力するプログラムを作成して下さい。 入力 入力は複数のデータセットからなる。入力の終わりはゼロ2つの行で示される。各データセットは以下の形式で与えられる。 C W x 1 y 1 x 2 y 2 : x C y C s 1 t 1 s 2 t 2 : s W t W 各行で与えられる数値は1つの空白で区切られている。 1行目に柱の数 C (3 ≤ C ≤ 100) と壁の数 W (3 ≤ W ≤ 300) が与えられる。続く C 行に柱の座標が与えられる。x i (-1000 ≤ x i ≤ 1000) は i 番目の柱の x 座標、y i (-1000 ≤ y i ≤ 1000) は i 番目の柱の y 座標をそれぞれ示す整数である。柱にはそれぞれ 1 から C までの番号が割り当てられている。 続く W 行に壁の情報が与えられる。 s i (1 ≤ s i ≤ C)、t i (1 ≤ t i ≤ C) は壁の両端を支える柱の番号を示す。 入力は以下の条件を満たすと考えてよい。 異なる柱が同じ位置を占めることはない。 異なる壁が重なったり、柱以外で交差することはない。 1つの柱は2つ以上の壁を支えている。 壁の長さは 0 より大きい。 柱は壁の両端だけにある。つまり、柱が壁の途中にあることはない。 壁は異なる2つの部屋、あるいは部屋と外とを仕切る。 任意の異なる2つの柱を選んだとき、互いの柱には壁をたどって到達できる。 データセットの数は 50 を超えない。 出力 データセットごとに、ねこが「最良の選択」をした場合、外に出るまでに通る穴の数の最大値を1行に出力する。 入力例 4 4 0 0 1 0 1 1 0 1 1 2 1 4 2 3 3 4 12 22 2 0 4 0 8 0 4 2 6 2 8 2 2 4 4 4 6 4 0 6 8 6 0 8 1 2 2 3 1 4 1 7 1 10 2 4 2 5 3 5 3 6 4 5 4 8 5 6 5 9 6 9 6 11 7 10 7 8 8 9 9 11 10 11 10 12 11 12 0 0 出力例 1 3 2つ目のデータセットが、冒頭の図に示したお屋敷の間取りに対応する。図のように、薄く色が塗られた部屋にねこがいるときは、「最良の選択」で外に出るまでに通り抜ける穴の数は3つである。それ以外の部屋にねこがいるときは、2つ以下で外に出られる。したがって、ねこが「最良の選択」をしたとき、外に出るまでに通り抜ける穴の数の最大値は 3 になる。
[ { "submission_id": "aoj_0273_10848620", "code_snippet": "#include <bits/stdc++.h>\n\n#define REP(i,n) for(int i=0;i<(int)(n);i++)\n#define ALL(x) (x).begin(),(x).end()\n\nusing namespace std;\n\nusing ll = long long;\nusing ld = long double;\n\ntemplate <typename T> T &chmin(T &a, const T &b) { return a = m...
aoj_0282_cpp
プログラミングコンテスト 白虎大学では毎年プログラミングコンテストが開催されています。チームの総数をNとしたとき、チームには1からNのチームIDがそれぞれ割り当てられています。コンテストは全てのチームの得点が0の状態から開始し、L秒間連続して行われます。 今年のコンテストは、テレビで中継されることになりました。コンテストの間テレビに映るのは、その時点で最も得点の高いチームだけです。ただし、該当するチームが複数いるときは、その中でチームIDの一番小さいチームが映ります。映すべきチームが変わると、瞬時にカメラが切り替わります。 あなたはコンテストのログを入手しました。ログには、あるチームの得点が変化した時のレコードがすべて含まれています。各レコードは「チーム d がコンテスト開始 t 秒後の時点で x 点獲得した」という形式で与えられています。なお、減点される場合もあるので、現在の得点が0より小さくなることもあります。 コンテストのログを入力とし、コンテスト開始から終了までの間にテレビに映った時間が最も長いチームのIDを報告するプログラムを作成してください。 入力 入力は1つのデータセットからなる。入力は以下の形式で与えられる。 N R L d 1 t 1 x 1 d 2 t 2 x 2 : d R t R x R 1行目は3つの整数からなる。 N (2 ≤ N ≤ 100000)はチーム数、 R (0 ≤ R ≤ 1000000)はログに含まれるレコードの数である。 L (1 ≤ L ≤ 1000000)はコンテストの時間(長さ)を表す。続く R 行に各レコードの情報が与えられる。各レコードはチーム d i (1 ≤ d i ≤ N )がコンテスト開始 t i (0 < t i < L )秒後の時点で x i (-1000 ≤ x i ≤ 1000)点獲得あるいは減点されたことを示す。ただし、 t i , x i は整数であり、 t i-1 ≤ t i である。 出力 コンテスト開始から終了までの間にテレビに映った時間が最も長いチームのIDを1行に出力する。ただし、最も長いチームが複数あるときは、その中でチームIDの一番小さいチームを出力する。 入力例 1 3 4 600 3 100 5 1 200 10 2 400 20 3 500 20 出力例 1 1 入力例 2 3 5 600 3 100 5 1 200 10 2 300 30 1 400 -8 2 400 -27 出力例 2 3
[ { "submission_id": "aoj_0282_10853284", "code_snippet": "// 7\n#include <bits/stdc++.h>\nusing namespace std;\n#define FOR(i,a,b) for (int i =(a); i <(b); i++)\n#define REP(i,n) FOR(i,0,n)\n#define ALL(v) (v).begin(), (v).end()\n#define fi first\n#define se second\ntemplate<typename A, typename B> inline bo...
aoj_0275_cpp
鉄道路線 ある国の鉄道網に、自動改札を導入することになりました。導入にあたって難しい問題の一つは、与えられた切符で、指定された駅の間を移動できるかどうかを判定することです。それぞれの切符には乗車駅と降車駅が記載されています。この切符でできるのは、「乗車駅で乗って、降車駅で降りる」ことだけではなく、途中乗車や途中下車も許されています。 この鉄道網にはS 個の駅があり、そのうちR 組の駅は隣り合っていて、他の駅を経由せずに双方向に鉄道で移動することができます。隣り合った駅を結ぶ線路はひとつしかありません。隣り合った駅の間の距離は、この線路に沿って測った距離です。ある駅からある駅までの経路は鉄道網の形状によっては複数通り考えられますが、そのうち最も距離が短くなるような経路を最短経路と呼ぶことにします。そのような経路が複数ある場合、どちらも最短経路として認められます。 乗車駅 a、降車駅 b の切符で駅 c から駅 d まで移動できるのは、以下の条件をすべて満たす経路 p が存在するときです。 経路 p は、駅 a から駅 b への最短経路である。 経路 p は、駅 a から出発し、駅 c、駅 d の順に経由し、駅 b で終わる経路である。また、駅 c から駅 d の区間はこの2 駅の最短経路になっている。 路線図と切符の情報が与えられます。次に、始点と終点の組がいくつか与えられるので、その切符で始点から終点へ移動できるかどうかを判定するプログラムを作成してください。 入力 入力は1つのデータセットからなる。入力データは以下の形式で与えられる。 S R u 1 v 1 w 1 u 2 v 2 w 2 : u R v R w R a b Q c 1 d 1 : c Q d Q 各行で与えられる数値は1つの空白で区切られている。 1行目は2つの整数からなる。S (2 ≤ S ≤ 100000) は鉄道路線図に現れる駅の数、R (1 ≤ R ≤ 200000) は隣り合った駅の組の数である。続く R 行に、隣り合った駅の間を直接つなぐ線路の情報が与えられる。u i と v i (1 ≤ u i , v i ≤ S) は i 番目の線路の両端の駅の番号を示す。w i (1 ≤ w i ≤ 1000) はこれらの駅の間の距離を表す整数である。ただし、各駅には 1 から S までの番号が重複なく割り振られており、u i ≠ v i とする。 続く1行は3つの整数からなる。最初の2つの整数は切符の区間を表し、a は乗車駅、b は降車駅 (1 ≤ a, b ≤ S) である。3つ目の整数 Q (1 ≤ Q ≤ 40000) は質問の数を示す。続く Q 行に質問が与えられる。 c i とd i (1 ≤ c i , d i ≤ S)は i 番目の質問の乗車駅と降車駅を示す。ただし、a ≠ b、c i ≠ d i とする。 出力 質問ごとに、与えられた切符で移動できるなら Yes を、できないなら No を1行に出力する。 入力例 6 7 1 2 3 1 4 1 2 3 5 4 3 1 3 6 2 4 5 2 5 6 1 1 6 6 1 6 4 3 4 6 5 6 2 6 2 5 出力例 Yes Yes Yes Yes No No
[ { "submission_id": "aoj_0275_10170526", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\n#define rep(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)\n#define drep(i, m, n) for (ll i = m - 1; i >= n; i--)\n#define ...
aoj_0281_cpp
陣形 プログラマー養成校であるアカベ高校では、チーム戦の競技プログラマーの役割を以下の3タイプに分けています。 C: コーダー 言語を熟知しており、コーディングを行う。 A: アルゴリズマー 論理的思考が得意であり、アルゴリズムを考える。 N: ナビゲーター 読解力に長けており、問題の分析・デバッグをする。 この高校では以下のいずれかの陣形で3人一組のチームを構成しています。 CCA: バランスがとれている安定型 CCC: スピードを見込めるがリスクの高い速攻型 CAN: 正確に問題を解く慎重型 競技プログラミング部のコーチであるあなたは、これらの部員をうまく組み合わせ、できるだけ多くのチームを結成できるよう、毎年気を配っています。そこで、コーダーの人数、アルゴリズマーの人数、ナビゲーターの人数が入力として与えられたとき、最大何チーム作れるかを出力するプログラムを作成してください。 入力 入力は1つのデータセットからなる。入力データは以下の形式で与えられる。 Q c 1 a 1 n 1 c 2 a 2 n 2 : c Q a Q n Q 1行目の Q (0 ≤ Q ≤ 100)はチーム数を求めたい年数である。続く Q 行に各年の役割別の人数が与えられる。各行にはコーダーの人数 c i (0 ≤ c i ≤ 1000)、アルゴリズマーの人数 a i (0 ≤ a i ≤ 1000)、ナビゲーターの人数 n i (0 ≤ n i ≤ 1000)が与えられる。 出力 年ごとに、作成できる最大のチーム数を1行に出力する。 入力例 4 3 0 0 1 1 1 9 4 1 0 1 2 出力例 1 1 4 0
[ { "submission_id": "aoj_0281_9637717", "code_snippet": "#include<bits/stdc++.h>\n\n#define ll long long\nusing namespace std;\n\n\n\nint main() {\n//#ifndef ONLINE_JUDGE\n// freopen(\"Input.txt\", \"r\", stdin);\n// freopen(\"Output.txt\", \"w\", stdout);\n//#endif\n int t;\n cin >> t ;\n whi...
aoj_0279_cpp
おそろいの景品 ジョウ君とヤエさんは仲の良いカップルです。ジョウ君はカプセル玩具自販機(ガチャポン)の景品を集めており、二人で出かけたときも、ガチャポンを見つけると何度かやってみるほどの熱の入りようです。ヤエさんは楽しそうなジョウ君をそばで見ているだけでしたが、ジョウ君の今度の誕生日プレゼントにガチャポンの景品をひとつあげることにしました。ヤエさんはガチャポン自体にはあまり興味がわきませんでしたが、できればジョウ君とおそろいの景品が欲しいと思っています。 ヤエさんがやってみようと思うガチャポンは、1回のチャレンジで景品がひとつ出ます。品切れのものも含め景品が何種類あるのかと、それぞれの景品がいくつ残っているのかはわかります。しかし、1回のチャレンジでどの景品が出るかはわかりません。そこで、景品が出る順番にかかわらず、ヤエさんが同じ景品を必ず2つ得るために最低限必要なチャレンジの回数を出力するプログラムを作成してください。 入力 入力は複数のデータセットからなる。入力の終わりはゼロ1つの行で示される。各データセットは以下の形式で与えられる。 N k 1 k 2 ... k N 各データセットは2行であり、1行目に景品が何種類あるかを表す整数 N (1 ≤ N ≤ 10000)が与えられる。続く1行に各景品がいくつ残っているのかを表す整数 k i (0 ≤ k i ≤ 10000)が与えられる。 データセットの数は100を超えない。 出力 データセットごとに、同じ景品を必ず2つ得るために最低限必要なチャレンジの回数を出力する。ただし、不可能な場合はNAと出力する。 入力例 2 3 2 3 0 1 1 1 1000 0 出力例 3 NA 2 1つ目のデータセットでは、運良く1種類目か2種類目の景品が連続で出て2回で済む可能性はあるが、同じ種類の景品を必ず2つ得るためには3回チャレンジする必要がある。 2つ目のデータセットでは、2つ以上残っている景品がもともと無いので不可能である。 3つ目のデータセットでは、景品は1種類だけなので2回のチャレンジでその景品が必ず2つ得られる。
[ { "submission_id": "aoj_0279_10867650", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <map>\n#include <numeric>\n#include <bitset>\n#include <vector>\n#include <queue>\n\nusing namespace std;\n\ntypedef long long ll;\n#define FOR...
aoj_0283_cpp
勉強会 プログラマー養成校のアカベ高校には、生徒自身で運営するユニークな勉強会があります。プログラマーは新しい技術を常に取り入れることが大切なので、この勉強会を通して自学自習の習慣を身につけることがこの活動のねらいです。 生徒は全部で N 人おり、それぞれが入学時のプログラミングコンテストの結果で得られたスコアを持っています。勉強会では N 人の生徒のうち何人かがリーダーになり、各リーダーがそれぞれのグループを運営するとともに、自らの運営するグループに参加します。 リーダー以外の生徒は、自分のスコアよりも低いスコアのリーダーが運営するグループには参加できません。また、0以上のある値 r を1つ決め、グループに参加する生徒とリーダーのスコアの差が r 以内となるようにしています。つまり、グループのリーダーのスコアが s のとき、自分のスコアが s を超えているか、あるいは s - r 未満ならば、そのグループには参加できません。 あなたは勉強会の実行委員長であり、運営準備のためにシミュレーションを行うことにしました。シミュレーションでは、リーダーが誰もいない状態から始め、以下の操作を何回か繰り返します。 生徒をリーダーに加える。 生徒をリーダーから外す。 要求時点でのリーダーの組み合わせについて、どのグループにも参加できない生徒が x 人以下になるような、最小の r を求める。 このようなシミュレーションを行うプログラムを作成してください。 入力 入力は1つのデータセットからなる。入力は以下の形式で与えられる。 N Q s 1 s 2 : s N QUERY 1 QUERY 2 : QUERY Q 1行目に生徒の数 N (1 ≤ N ≤ 1000000)、処理要求の数 Q (0 ≤ Q ≤ 1000)が与えられる。 続く N 行に i 番目の生徒のスコアを示す整数 s i (0 ≤ s i ≤ 1,000,000,000)が与えられる。生徒は1,2,..., N で番号付けされているものとする。 続く Q 行に処理要求 QUERY i が与えられる。処理要求は時系列順に与えられる。処理要求はADD, REMOVE, CHECKの3種類あり、各 QUERY i は以下のいずれかの形式で与えられる。 ADD a または REMOVE a または CHECK x ADD a は番号 a (1 ≤ a ≤ N )の生徒をリーダーに加えることを表す。 REMOVE a は番号 a (1 ≤ a ≤ N )の生徒をリーダーから外すことを表す。 CHECK x は出力要求を表す。どのグループにも参加できない生徒の数の上限 x (0 ≤ x ≤ N )が与えられる。 なお、入力は以下の条件を満たすものとする。 どの時点でも、リーダーの人数が100人を超えることはない。 その時点でリーダーである生徒をリーダーに加えることはない。 その時点でリーダーでない生徒をリーダーから外すことはない。 出力 時系列順に各出力要求の時点で、どのグループにも参加できない生徒がx人以下になるような最小の r を1行に出力する。ただし、どのような r を選んでも x 人以下にすることが不可能であればNAと出力する。 入力例 1 5 8 5 10 8 7 3 ADD 1 ADD 3 CHECK 0 CHECK 1 CHECK 2 CHECK 3 CHECK 4 CHECK 5 出力例 1 NA 2 1 0 0 0 入力例 2 5 28 5 10 8 7 3 CHECK 0 CHECK 1 CHECK 2 CHECK 3 CHECK 4 CHECK 5 ADD 1 CHECK 0 CHECK 1 CHECK 2 CHECK 3 CHECK 4 CHECK 5 REMOVE 1 ADD 3 CHECK 0 CHECK 1 CHECK 2 CHECK 3 CHECK 4 CHECK 5 ADD 1 CHECK 0 CHECK 1 CHECK 2 CHECK 3 CHECK 4 CHECK 5 出力例 2 NA NA NA NA NA 0 NA NA NA 2 0 0 NA 5 3 1 0 0 NA 2 1 0 0 0
[ { "submission_id": "aoj_0283_10852139", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n// #include<atcoder/all>\n// using mint = atcoder::modint998244353;\nusing ld = long double;\n#define fi first\n#define se second\n#define all(x) x.begin(),x.end()\n#define rep(i,n) for(int i=0;i<(int)(n...
aoj_0284_cpp
ハッピーエンド問題 「ハッピーエンド問題」と呼ばれる数学の未解決問題に関連したプログラムを書いてみましょう。平面上に与えられた N 個の点から、ちょうど k 個の点を結んでできる凸多角形のうち、最も面積の小さいものを見つけるプログラムを作成してください。ただし、N個の点の座標を与えられた後、質問として凸多角形の角の個数 k がいくつか与えられます。 (補足:ハッピーエンド問題について) 平面上にどの3点も同じ直線上に乗らないように N 個の点を置きます。そのとき、どのように点を置いても、k個の点をうまく選ぶとk個の角をもつ凸多角形が必ず作れると予想されています。 今のところ、三角形ならば N =3、四角形ならば N =5、五角形ならば N =9、六角形ならば N =17であればよいことが、2006年までに証明されています。また、三角形以上のすべてのk角形に対して、 N =1+2 k -2 という予想がありますが、いまだ証明されていません。これは100年にわたり研究が進められている難問です。 この問題には、「ハッピーエンド問題」という素敵な名前がつけられています。ある男女がこの問題を研究しているうちに仲良くなって、ついに結婚したことにちなんで、友人の数学者が名付けたそうです。ロマンチックですね。 入力 入力は1つのデータセットからなる。入力データは以下の形式で与えられる。 N x 1 y 1 : x N y N Q k 1 : k Q 1行目に平面上の点の個数 N (3 ≤ N ≤ 40)が与えられる。続く N 行に各点の座標が与えられる。各点には1から N までの番号が、入力される順番に付けられている。 x i と y i (-10000 ≤ x i , y i ≤ 10000)は i 番目の点のそれぞれ x 座標と y 座標を表す整数である。 x 軸の正方向は右向き, y 軸の正方向は上向きに取るものとする。 続く1行に質問の数 Q (1 ≤ Q ≤ N )が与えられる。続く Q 行に質問が与えられる。 k i (3 ≤ k i ≤ N )は i 番目の質問である凸多角形の角の個数を表す。 なお、入力は以下の条件を満たすものとする。 入力される点の座標はすべて異なる。 どの3点も同じ直線上には乗らない。 各質問に対して面積最小の凸多角形は1つであり、2番目に小さい凸多角形との面積の差は 0.0001以上。 出力 質問ごとに、面積が最小の凸多角形の全頂点を1行に出力する。凸多角形の頂点で最も下にあるものの中で最も左にある頂点から順に、反時計周りで頂点の番号を出力する。頂点の番号の間は空白1つで区切る。行の終わりには空白文字を出力しない。ただし、凸多角形が作れない場合はNAと出力する。 入力例 1 5 0 0 3 0 5 2 1 4 0 3 3 3 4 5 出力例 1 1 4 5 1 2 4 5 1 2 3 4 5 入力例 2 6 0 0 3 0 5 2 1 4 0 3 3 2 4 3 4 5 6 出力例 2 6 3 5 6 3 4 5 1 2 6 4 5 NA
[ { "submission_id": "aoj_0284_10864669", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ld=long double;\n#define fi first;\n#define se second;\n#define i128 __int128_t\n#define all(x) x.begin(),x.end()\n#define rep(i,n) for(int i=0;i<(int)(n);++i)\n#define si(a) (long)(a.size())\ntemp...
aoj_0271_cpp
イヅア国語辞典 あなたはイヅア国の公用語であるイヅア語の国語辞典と、イヅア語のアルファベット(文字の一覧表) を手に入れました。イヅア語のアルファベットにはN 種類の文字があります。イヅア語の国語辞典に現れる単語の順番は、イヅア語のアルファベット順に並んでいます。 辞典を見ると、載っているどの単語もN 文字で、しかも、N 種類の文字をひとつずつ含んでいることがわかりました。さらに調べてみると、辞典にはN 種類の文字の可能な並び方がすべて書かれていることを発見しました。 この発見から、あなたはある単語が辞典の何番目に出てくるかわかるようになりました。この知識を利用してイヅア国の人を驚かせてみましょう。まず、N 種類の文字をひとつずつアルファベット順に並べます。次に、任意の2文字の順番を入れ替える操作を R 回繰り返してもらいます。あなたは、出来上がった単語がイヅア国語辞典の何番目に出てくるか当てて見せます。そのための準備として、国語辞典中の単語の場所を求めるプログラムを作成してください。ただし、アルファベット順で最初の単語を 0 番目の単語とします。 入力 入力は複数のデータセットからなる。入力の終わりはゼロ1つの行で示される。各データセットは以下の形式で与えられる。 N R s 1 t 1 s 2 t 2 : s R t R 1行目にアルファベットを構成する文字の数 N (1 ≤ N ≤ 100000) が与えられ、2行目に文字を入れ替えてもらう回数 R (0 ≤ R ≤ 50) が与えられる。続く R 行に、入れ替えられる文字の位置の組が与えられる。s i と t i (1 ≤ s i < t i ≤ N) は、先頭から数えて s i 番目と t i 番目の文字を i 回目に入れ替えることを表す。s i と t i は1つの空白で区切られている。 データセットの数は100 を超えない。 出力 データセットごとに、入れ替えが終わった時点で得られた単語が国語辞典の何番目に現れるかを示す数を1行に出力する。ただし、出力すべき値は非常に大きくなりうるので、代わりに 1,000,000,007 で割った余りを出力する。 入力例 3 2 1 2 2 3 4 2 2 3 2 4 0 出力例 3 4 入出力例の最初のデータセットについて説明する。説明のため、アルファベットは小さい方から順番に、'A', 'B', 'C' の3文字からなるとする。これらの文字をアルファベット順に並べると "ABC" という文字列が得られる。この文字列の1番目の文字 'A' と2番目の文字 'B' を入れ替えると、"BAC" という文字列が得られる。次に、得られた文字列の2番目の文字 'A' と3番目の文字 'C' を入れ替えると、単語 "BCA" が得られる。辞典には、先頭から順に "ABC", "ACB", "BAC", "BCA", "CAB", "CBA" の6種類の単語が載っている。"BCA" はこの中で3番目(最初の"ABC"が0番目であることに注意)に現れるので、3 を 1,000,000,007 で割った余りである 3 が答えになる。
[ { "submission_id": "aoj_0271_10568001", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nconst int mod = 1e9 + 7;\n\nint mul(int x, int y) {\n return (1ll * x * y) % mod;\n}\n\nconst int MAXN = 1e5 + 5;\n\nint fact[MAXN];\n\nvoid precalc() {\n fact[0] = 1;\n for (int i = 1; i < ...
aoj_0287_cpp
塵劫記 大きな数を表そうとすると、文字数も多くなるし、位取りがわからなくなってしまうので、なかなか面倒です。大きな数をわかりやすく表すために、人々は数の単位を使ってきました。江戸時代に書かれた「塵劫記」という本の中では、数の単位が次のように書かれています。 たとえば、2の100乗のようなとても大きな数は、126穣7650(じょ)6002垓2822京9401兆4967億320万5376と表せます。それでは、正の整数 m と n が与えられたとき、 m の n 乗を塵劫記の単位を使って上のように表すプログラムを作成してください。 入力 入力は複数のデータセットからなる。入力の終わりはゼロ2つの行で示される。各データセットは以下の形式で与えられる。 m n m (2 ≤ m ≤ 20) が基数、 n (1 ≤ n < 240) が指数を表す。ただし、 m n は 10 72 未満である。 データセットの数は 100 を超えない。 出力 データセットごとに、 m n を塵劫記の単位で表した文字列を1行に出力する。ただし、各単位は以下の表記で出力する。 m n を表す文字列は、1 から 9999 までの数と上の表に現れる単位を表す文字列からなる。文字列には、余分な 0 や単位を含めない。 入出力例 入力例 2 10 5 20 10 8 20 17 0 0 出力例 1024 95Cho3674Oku3164Man625 1Oku 131Gai720Kei
[ { "submission_id": "aoj_0287_3201960", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstring>\n#include<vector>\n#include<set>\n#include<cmath>\nusing namespace std;\ntypedef vector<int> vi;\nconst string units[]={\n \"\",\"Man\",\"Oku\",\"Cho\",\"Kei\",\"Gai\",\"Jo\",\"Jou\",\"Ko\",\"Kan...
aoj_0286_cpp
親方の給料計算 ワシはパイプつなぎ組合の親方じゃ。職人を工事現場に派遣し、現場でパイプをつながせておる。去年は工事が増えて大儲けするかと思ったのじゃが、ちょっと給料の出し方がまずくてのぅ。ウチとしては大赤字になってしまったのじゃよ…。そこで、今年は職人たちへの給料の出し方を工夫したいのじゃ。 職人たちの給料は、工事の種類とこなした回数で決めておる。つまり、 職人の給料 = 種類 1 の単価 × 種類 1 をこなした回数 + 種類 2 の単価 × 種類 2 をこなした回数 .... + 種類 M の単価 × 種類 M をこなした回数 となるのじゃ。これを計算するために、ウチでは、どの職人がどの種類の工事を何回こなしたかを次のような表に記録しておる。 例えば、上の表では、職人1が工事2を2回、工事4を1回こなしたことを示しておる。 職人たちがこなした仕事の回数はもう変えられんが、やつらは工事の単価を知らんので、単価をいろいろと変えながら皆の不満が出ぬよう給料を調整しようと思うておる。じゃが、ワシがこしらえたプログラムが今もって動かなくてのぅ。ちょっとみてくれんかね。 //省略 int i, j; for ( i = 0; i < N; i++ ){ c[i] = 0; for ( j = 0; j < M; j++ ){ c[i] = c[i] + a[i][j]*b[j]; } } //省略 N は職人の数で M は工事の種類の数じゃ。変数 a[i][j] に職人iが工事 j をこなした回数を、 b[j] に工事 j の単価をいれて、 c[i] に職人 i の給料を格納しておる。合っているはずなのに、うんともすんとも言わん!そろそろ今年の給料を職人たちに払わないとまずいのじゃが・・・・・なんとかならんかのぅ。 それでは、職人のこなした仕事の回数と各工事の単価の情報が与えられたとき、各職人の給料を計算するプログラムを作成してください。 入力 入力は1つのデータセットからなる。入力データは以下の形式で与えられる。 N M s 1 t 1 e 1 s 2 t 2 e 2 : 0 0 0 L b 11 b 12 ... b 1M b 21 b 22 ... b 2M : b L1 b L2 ... b LM 1行目は職人の数 N (1 ≤ N ≤ 10000)と工事の種類の数 M (1 ≤ M ≤ 10000)。 続いて、工事の記録として、職人の番号 s i (1 ≤ s i ≤ N ) と工事の種類の番号 t i (1 ≤ t i ≤ M )、および職人 s i が工事 t i をこなした回数 e i (1 ≤ e i ≤ 50000) からなる行が1行以上与えられる。工事の記録はゼロ3つの行で終わる。ただし、 e i の合計は 1 以上 50000 以下である。また、工事の記録には、どの職人と工事の種類の組も2度以上現れない。工事をこなした回数が 0 である職人と工事の種類の組は与えられない。 続く 1 行は給料の算出を行う回数 L (1 ≤ L ≤ 100) 。 続く L 行は、 i 回目の給料の算出に必要な、工事 j の単価 b ij (0 ≤ b ij ≤ 10000) の並び。 出力 以下の形式で、 i 回目の給料の算出によって得られた職人 j の給料 c ij を順番に出力する。各給料の間は空白1つで区切る。 c 11 c 12 ... c 1N c 21 c 22 ... c 2N : c L1 c L2 ... c LN 入出力例 入力例 4 6 1 2 1 2 3 2 2 4 3 3 1 5 4 2 4 4 3 2 4 6 10 0 0 0 3 1 3 2 4 0 10 6 5 4 3 2 1 5 1 1 5 5 1 出力例 3 16 5 116 5 17 30 38 1 17 25 16
[ { "submission_id": "aoj_0286_11031173", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)\n\nint main() {\n\tcin.tie(nullptr);\n\tios_base::sync_with_stdio(false);\n\t\n\tint N, M;\n\tcin >> N >> M;\n\tvecto...
aoj_0291_cpp
古代遺跡の謎 古代遺跡から超大型あみだくじが発見された。学者達はこれをVLA(Very Long Amidakuji)と命名した。分析の結果、VLAは N 本の縦棒を持ついくつかの部品の組み合わせで構成され、多くの場合繰り返しを含んでいることがわかった。例えば、図のVLAは2種類の部品AとBを使った4つの部品から構成されている。 このVLAの上端に左から順番に 1,2,3,4,5 と番号をつけ、それぞれの番号からVLAをたどり(あみだくじのたどり方については問題文最後の補足を参照してください)、下端にたどり着いた場所の縦棒にその番号を振る。すべての番号についてたどり終えた後で、下端に振られた番号を左から読むと 4,2,5,1,3 になる。このように、1,2,3,4,5 という番号の列が 4,2,5,1,3 という列に変換される。 彼らはVLAによって 1 から N までの番号の列がどのように変換されるのかを知りたいが、手作業でたどるのは大変そうだ。そこでパソコン甲子園出場者である優秀なプログラマーの君たちに、VLAのシミュレーションを依頼した。 VLAはとても長いため、そのままの状態ではプログラマーたちに情報を伝えることができない。そこで学者たちは、部品の種類を英大文字1つで表し、VLAを式で表現することにした。 部品は同じ位置関係である縦棒が一致するように、左から順番に + 演算子で接続される。たとえば、上からA,B,C,D という順番で連結された部品の列は A+B+C+D という式で表記される。 部品の列を表す式 seq の m 回の繰り返しは、 m ( seq ) と表記できる。たとえば、 A+B の2回の繰り返しは 2(A+B) と表せ、これは A+B+A+B と同じである。また、 seq が1つの部品 a の場合は、 a を囲むかっこを省略しても構わない。 4(A+B) と 3(X+Y) を、この順番で連結したVLAは 4(A+B)+3(X+Y) と表せる。また、かっこは 2(4(A+B)+3(X+Y))+10C のように入れ子にしても構わない。 それでは、縦棒の数 N 、各部品の情報、複数のVLAの式を入力とし、各式が表すVLAによって1から N までの番号の列がどのように変換されるかを出力するプログラムを作成しなさい。 (※補足:あみだくじのたどり方について) あみだくじのある縦棒の上端から出発して上から下へ進む。ただし、横棒がある地点ではその横棒でつながった別の縦棒に移動する。これを、縦棒の下端にたどり着くまで繰り返す。 入力 入力は1つのデータセットからなる。入力データは以下の形式で与えられる。 N K p 1 h 1 g 1 (1,1) g 1 (1,2) .. g 1 (1, N -1) g 1 (2,1) g 1 (2,2) .. g 1 (2, N -1) : g 1 ( h 1 -1,1) g 1 ( h 1 -1,2) .. g 1 ( h 1 -1, N -1) p 2 h 2 g 2 (1,1) g 2 (1,2) .. g 2 (1, N -1) g 2 (2,1) g 2 (2,2) .. g 2 (2, N -1) : g 2 ( h 2 -1,1) g 2 ( h 2 -1,2) .. g 2 ( h 2 -1, N -1) : p K h K g K (1,1) g K (1,2) .. g K (1, N -1) g K (2,1) g K (2,2) .. g K (2, N -1) : g K ( h K -1,1) g K ( h K -1,2) .. g K ( h K -1, N -1) E expression 1 expression 2 : expression E 1行目に縦棒の数を表す整数 N (2 ≤ N ≤ 12) と部品の種類の数 K (1 ≤ K ≤ 26)が与えられる。 2行目以降に K 種類の部品の情報が与えられる。各部品は以下の形式で与えられる。 最初の行は部品名を表す1つの英大文字 p i と部品 i の縦棒の長さを表す整数 h i (1 ≤ h i ≤ 20)を含む。 続く h i -1 行で部品 i の横棒の情報が与えられる。 g i ( r, c ) は部品 i の上端から長さ r の位置において、左から c 番目の縦棒と c +1 番目の縦棒をつなぐ横棒の有無を表す。横棒があるときは g i ( r , c ) = 1、横棒がないときは g i ( r , c ) = 0である。 続く1行に式の数 E (1 ≤ E ≤ 100) が与えられる。続く E 行にVLAの式を表す文字列 expression i (1文字以上1000文字以下)が与えられる。 入力は以下の条件を満たす。 異なる部品に同じ名前が使われることはない。 各部品 i について、 g i ( r , c )と g i ( r , c +1)が同時に 1 となることはない。 式に含まれる整数は 1 以上 1,000,000,000 以下である。 式には空白は含まれない。 出力 式ごとに、VLAによる1からNまでの列の変換結果を1行に出力する。それぞれの数字の間は空白1つで区切る。 入出力例 入力例 5 2 A 5 1 0 0 1 0 1 0 0 1 0 1 0 0 1 0 1 B 4 1 0 0 1 0 1 0 0 0 0 1 0 2 2(A+B) 1000000000(1000000000A+1000000000B) 出力例 4 2 5 1 3 1 2 3 4 5
[ { "submission_id": "aoj_0291_10852950", "code_snippet": "#include <iostream>\n#include <vector>\n#include <string>\nusing namespace std;\n\nint H, N, Q, p[30][30]; vector<int>r[30], J; string c[30]; string S;\n\nvector<int>amida_add(vector<int>a, vector<int>b) {\n\tvector<int>c = a, p = a, q = a;\n\tfor (in...
aoj_0288_cpp
巨樹の刻み手 あなたは N 種類の道具を使って、目の前の巨樹を切り倒そうとしています。はじめは、樹の耐久力は D 、あなたの経験値は 0 ですが、道具 i で1回樹を叩くと樹の耐久力は a i 減り、あなたは e i の経験値を得ます。ただし、道具 i を使うためには、あなたは r i 以上の経験値を持っていなければなりません。樹の耐久力が 0 以下になると樹は倒れます。 樹の耐久力と道具についての情報が与えられたとき、樹を切り倒すには最低何回樹を叩かなければいけないかを求めるプログラムを作成してください。 入力 入力は複数のデータセットからなる。入力の終わりはゼロ2つの行で示される。各データセットは以下の形式で与えられる。 D N a 1 e 1 r 1 a 2 e 2 r 2 : a N e N r N 1 行目に樹の耐久力を表す整数 D (1 ≤ D ≤ 100) と道具の種類の数 N (1 ≤ N ≤ 100) が与えられる。続く N 行に道具 1 から N までの情報が与えられる。 a i (0 ≤ a i ≤ 100) と e i (0 ≤ e i ≤ 100) は、道具iを1回使うことで減る樹の耐久力とあなたが得ることのできる経験値をそれぞれ表す。 r i (0 ≤ r i ≤ 100) は道具を使うために必要な経験値を表す。 a i , e i , r i はすべて整数である。 データセットの数は 50 を超えない。 出力 樹を切り倒すのに必要な、樹を叩く最小の回数を1行に出力する。ただし、樹を切り倒すことが不可能な場合は NA と出力する。 入出力例 入力例 15 4 1 1 0 1 2 2 5 10 5 8 1 15 60 4 5 2 0 8 8 2 3 5 0 49 0 18 100 1 1 1 1 0 0 出力例 6 4 NA
[ { "submission_id": "aoj_0288_10848293", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing P = pair<int, int>;\n\nconstexpr int INF = 1e9;\n\nint main() {\n int D, N;\n while(cin >> D >> N, D) {\n using tool = tuple<int, int, int>;\n vector<tool> v(N);\n for(...
aoj_0294_cpp
アカ・ベコ捕獲作戦 怪盗アカ・ベコは大胆にも、ツルガジョーから銀のシャチホコを盗み去った。警部であるあなたは、アカ・ベコが仲間のコボウ氏にシャチホコを渡すらしいという情報を手に入れた。ただ、コボウ氏はアカ・ベコのアジトまで出向けないので、アジトの外で渡すらしい。あなたはアカ・ベコを待ち伏せして捕まえることにした。この町は古い城下町なので、道が複雑に入り組んでいて、どの道も一方通行になっている。アカ・ベコがアジトから受け渡し場所までどの経路を通るのかはわからないが、人員不足のため、待ち伏せ場所は1つに設定しなければならない。 あなたは、アジトを含むいくつかの地点を選び、それらをつなぐ道を調べ、地図を作った。地図上の地点のどこかで受け渡しが行われるらしい。受け渡し場所が判明次第、以下のように待ち伏せ場所を選ぶ。 アジトは危険なのでアジト以外の地点。 アカ・ベコがアジトから受け渡し場所までどのような経路を通っても必ず通る地点。 1, 2 の両方を満たす地点のうち、受け渡し場所にたどり着くまでに通過しなければならない地点の数がより少ない地点。ただし、コボウ氏に見つからないようにするために、1, 2 を満たす地点が他にない場合のみ、受け渡し場所を待ち伏せ場所にする。 アジトと待ち伏せ場所の候補からなる地点をつなぐ道の情報のあとに、質問として受け渡し場所が複数入力されたとき、それぞれの受け渡し場所について待ち伏せ場所を出力するプログラムを作成してください。 入力 入力は1つのデータセットからなる。入力データは以下の形式で与えられる。 N M s 1 t 1 : s M t M Q r 1 : r Q 1行目は2つの整数からなる。 N (3 ≤ N ≤ 100000) は地点の数、 M ( N -1 ≤ M ≤ 300000) は地図に書いたすべての道の数を表す。続く M 行に隣り合った地点の間を直接つなぐ道が与えられる。 s i と t i (1 ≤ s i ≠ t i ≤ N ) は i 番目の道のそれぞれ始点、終点となる地点の番号を表す。ただし、アジトの番号は1とし、アジトからはすべての地点へ到達できる。 続く1行に質問の数 Q (1 ≤ Q < N ) が与えられる。続く Q 行に質問が与えられる。 i 番目の質問として、受け渡し場所の番号 r i (2 ≤ r i ≤ N ) が与えられる。 出力 質問ごとに、待ち伏せ場所の番号を1行に出力する。 入出力例 入力例 1 6 7 1 2 2 3 1 3 1 4 4 5 5 6 6 1 5 2 3 4 5 6 出力例 1 2 3 4 4 5 入力例 2 11 15 1 2 1 3 1 4 2 5 3 6 3 7 3 9 4 7 4 10 4 11 6 2 6 8 7 9 8 11 9 6 10 6 2 10 8 9 3 5 11 4 7 出力例 2 6 2 4 6 9 3 2 11 4 7
[ { "submission_id": "aoj_0294_8775198", "code_snippet": "#include <iostream>\n#include <string.h>\n#include <vector>\n#include <queue>\nusing namespace std;\nconst int LIMIT=100003;\nint ans[LIMIT];\nint ans2[LIMIT];\nint count[LIMIT];\nvector<int> vs[LIMIT];\nqueue<int> qs;\nint main() {\n\t// your code goe...
aoj_0292_cpp
壁 2XXX年、突然出現した天敵の侵入を防ぐために、人類は壁を作りその中に逃げ込んだ。その結果、人類の活動領域はその壁で囲まれた範囲に限定されてしまった。この領域は、上空から見ると W × H の長方形である。領域内部には x 軸あるいは y 軸に対して平行な壁がいくつか設置されている。活動領域の例を下図に示す。 人類は活動領域内を自由に移動することができるが、壁を越えるためには一定量の資源を消費しなければならない。ただし、壁を越えることはできるが(図中の (1))、壁の交点を越えること (2)、壁や活動領域の境界の上を移動すること (3)、活動領域外に出ること (4) はできない。 領域内部の壁の情報といくつかの始点と終点の組を入力し、始点から終点へ移動するために越えなければならない壁の数の最小値を計算するプログラムを作成しなさい。ただし、壁は幅のない線分とします。 入力 入力は1つのデータセットからなる。入力データは以下の形式で与えられる。 W H M px 1 py 1 qx 1 qy 1 px 2 py 2 qx 2 qy 2 : px M py M qx M qy M Q sx 1 sy 1 gx 1 gy 1 sx 2 sy 2 gx 2 gy 2 : sx Q sy Q gx Q gy Q 1行目に領域の横の長さと縦の長さを表す整数 W , H (2 ≤ W,H ≤ 1,000,000,000) と壁の数 M (0 ≤ M ≤ 100) が与えられる。 続く M 行に壁を表す線分の情報が与えられる。各行に与えられる4つの整数 px i , py i , qx i , qy i (0 ≤ px i , qx i ≤ W , 0 ≤ py i , qy i ≤ H ) はそれぞれ i 番目の線分の端点の x 座標、 y 座標、もうひとつの端点の x 座標、 y 座標を表す。 続く1行に質問の数 Q (1 ≤ Q ≤ 100) が与えられる。続く Q 行に各質問が与えられる。各質問に含まれる4つの整数 sx i , sy i , gx i , gy i (0 < sx i , gx i < W , 0 < sy i , gy i < H ) はそれぞれ始点の x 座標、 y 座標、終点の x 座標、 y 座標を表す。 入力は以下の条件を満たす。 各線分は x 軸あるいは y 軸に対して平行であり、長さは1以上である。 2つの互いに平行な線分が同じ点あるいは線分を共有することはない。 スタート地点、ゴール地点は壁上にあることはない。 どの線分も活動領域の境界と線分を共有することはない。 出力 質問ごとに、壁を越える回数の最小値を出力する。 入出力例 入力例 1 5 6 5 0 2 5 2 0 3 5 3 0 4 3 4 1 4 1 6 3 0 3 6 2 2 5 4 1 2 5 4 5 出力例 1 3 1 入力例 2 4 4 0 1 1 1 2 2 出力例 2 0 入力例 3 4 7 3 0 2 2 2 3 3 4 3 0 5 4 5 3 1 1 1 3 1 1 1 4 1 1 1 6 出力例 3 0 0 1
[ { "submission_id": "aoj_0292_5958818", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nvector<int> dx = {1, 0, -1, 0};\nvector<int> dy = {0, 1, 0, -1};\nint main(){\n int W, H, M;\n cin >> W >> H >> M;\n vector<int> px(M), py(M), qx(M), qy(M);\n for (int i = 0; i < M; i++){\n cin >> ...
aoj_0293_cpp
アルゴリズム検定試験 あなたはイズア地区におけるアルゴリズム検定試験を運営しており、運営費を最小にしたいと考えています。運営費は、会場使用料、バスの使用料、移動補助金の総和です。 今年は N 人の受験者登録があり、それぞれ c i 人まで受け入れ可能な M 個の会場を確保しました。あなたは各受験者を必ずどこかの会場に割り当てなければなりません。1 人以上割り当てた会場については、人数にかかわらず f i 円の会場使用料を支払う必要があります。 イズア地区は図に示すように東西方向と南北方向に 1 km 間隔で道が走っており、受験者の家と会場は交差点上にあると考えます。各受験者は家から会場まで道に沿って徒歩で移動できます。また、この検定試験では、1 人以上受験者を受け入れる各会場につき 1 台のシャトルバスが運行されるので、受験者はバスを利用することもできます。ただし、自分が受験する会場行きのバスにしか乗れません。 各シャトルバスは、会場からの東西方向の距離と南北方向の距離の和が D 以内のすべての交差点に停車します(図は D = 3 の場合)。バスの使用料は D に比例し、 D が 1 km 増えると B 円上がります。つまり、シャトルバスを運行するには 1 会場あたり D × B 円の費用を支払う必要があります。なお、 D 及び B はすべての会場で共通の値を用います。 移動補助金とは、各受験者に必要な最低限の徒歩での移動に対して運営者が受験者に支払う費用で、1 km について 1 円を払う必要があります。 あなたは受験者の家と会場の位置、会場の受け入れ可能人数と使用料、 D が 1 km 増えたときに加算される料金 B の情報を入力データとして持っており、各受験者への会場割り当てと D を決定することができます(ただし、 D は 0 以上の整数)。このとき、運営費の最小値を求めてください。 入力 入力は複数のデータセットからなる。入力の終わりはゼロ3つの行で示される。各データセットは以下の形式で与えられる。 N M B ax 1 ay 1 ax 2 ay 2 : ax N ay N bx 1 by 1 c 1 f 1 bx 2 by 2 c 2 f 2 : bx M by M c M f M 1 行目は3つの整数からなる。 N (1 ≤ N ≤ 100) は受験者の人数、 M (1 ≤ M ≤ 5) は会場数である。 B (0 ≤ B ≤ 1000) はシャトルバスを運行する際に、 D が 1 km 増えたときに加算される料金である。続く N 行に各受験者の家の座標が与えられる。 ax i , ay i (-1000 ≤ ax i , ay i ≤ 1000) はそれぞれ受験者 i の家の x 座標と y 座標を示す。続く M 行に各会場の情報が与えられる。 bx i , by i (-1000 ≤ bx i , by i ≤ 1000) はそれぞれ会場 i の x 座標と y 座標を示す。 c i (1 ≤ c i ≤ 100) は会場 i の受け入れ可能人数、 f i (0 ≤ f i ≤ 100000) は会場 i の使用料を示す。ただし、 c 1 から c M までの合計は N 以上である。 入力は以下の条件を満たすと考えてよい。 使用する会場を決めたとき、 D = i におけるそれらの会場への受験者の割り当て方のうち移動補助金が最小となるときの金額を F ( i ) とする。このとき、 F ( i +2) - F ( i +1) ≥ F ( i +1)- F ( i ) が成立する。 データセットの数は 10 を超えない。 出力 データセットごとに試験の運営費の最小値を 1 行に出力する。 入出力例 入力例 1 1 1 0 0 0 0 1 0 1 1 0 0 0 -3 0 2 3 1 3 1 0 0 -3 0 2 3 0 -5 2 0 4 0 2 1 4 3 1 0 0 0 0 0 0 0 0 -3 0 2 3 0 -5 2 0 4 0 2 1 6 3 1 0 0 0 0 0 0 0 0 0 0 0 0 -3 0 2 3 0 -5 2 0 4 0 2 1 6 3 2 0 0 0 0 0 0 0 0 0 0 0 0 -3 0 2 3 0 -5 2 0 4 0 2 1 10 5 1 0 0 2 0 4 0 8 0 100 0 100 0 100 0 100 0 100 0 100 0 -3 0 1 0 1 0 1 0 3 0 2 0 15 0 1 0 105 0 6 0 10 5 2 0 0 2 0 4 0 8 0 100 0 100 0 100 0 100 0 100 0 100 0 -3 0 1 0 1 0 1 0 3 0 2 0 15 0 1 0 105 0 6 0 0 0 0 出力例 0 3 5 11 18 28 20 38
[ { "submission_id": "aoj_0293_9263225", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <map>\n#include <queue>\n#include <set>\nusing namespace std;\ntypedef long long ll;\n#define rep(i, n) for(int i = 0; i < (n); i++)\n\ntemplate<class T>\nusing vi = vector<T>;\n\nt...
aoj_0290_cpp
微生物発電 飯沼博士は磐梯山の噴気孔でふしぎな微生物を見つけました。この微生物の雄と雌1体ずつが合体すると、電気エネルギーを放出します。この微生物を研究すれば、将来のエネルギー危機から我々を救えるかもしれません。 観察を続けると、微生物は合体したときだけ電気エネルギーを発生させることと、合体した微生物のさらなる合体はないことがわかりました。さらに観察を続けると、合体で放出される電気エネルギーは、微生物が体内に持つ未知の粒子(博士はこれを磐梯山にちなんでB粒子と名づけました)の量で決まることがわかりました。合体する雄と雌が体内に持つB粒子の量をそれぞれ bm と bw とすると、合体により放出される電気エネルギーは | bm - bw | × (| bm - bw | - 30) 2 という式で計算できます。 この発見により、微生物の集団から得られる最大の電気エネルギーが計算できるようになりました。それでは、微生物の集団に含まれる雄と雌の数と、各個体が持つB粒子の量が与えられたとき、この微生物の集団から得られる最大の電気エネルギーを計算するプログラムを作成してください。 入力 入力は複数のデータセットからなる。入力の終わりはゼロ2つの行で示される。入力データは以下の形式で与えられる。 M W bm 1 bm 2 ... bm M bw 1 bw 2 ... bw W 1行目の M と W (1 ≤ M,W ≤ 12) はそれぞれ雄と雌の微生物の数である。2行目に、 i 番目の雄が体内に持つB粒子の量 bm i (0 ≤ bm i ≤ 50) が与えられる。3行目に、 i 番目の雌が体内に持つB粒子の量 bw i (0 ≤ bw i ≤ 50) が与えられる。 データセットの数は 20 を超えない。 出力 データセットごとに、微生物の集団から得られる電気エネルギーの最大値を1行に出力する。 入出力例 入力例 3 3 0 20 30 10 20 30 10 10 32 10 15 8 20 10 6 45 50 41 18 0 37 25 45 11 25 21 32 27 7 3 23 14 39 6 47 16 23 19 37 8 0 0 出力例 12000 53906 11629
[ { "submission_id": "aoj_0290_5934872", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\nint dp[13][(1<<12)+2];\nvoid solve(int m,int w){\n vector<int>bm(max(m,w)),bw(max(m,w));\n for(int i = 0; max(m,w) > i; i++){\n bm[i] = bw[i] = -1;\n }\n for(int i = 0; m > i; i++){\n...
aoj_0299_cpp
鉄道路線II わたしの住む街には、図のような N 個の駅からなる円環状の鉄道路線があります。この路線の各駅には 0 から N -1 までの番号が順番に割り当てられていて、隣の駅まで100 円で移動することができます。移動はどちらの方向にでも可能です。 わたしはこの路線上のいくつかの駅で買い物をしたいと思っています。そこで、プログラムを作成して移動費を計算することにしました。ある駅を始点として、買い物をするすべての駅を訪問する最小の費用(円)を求めるプログラムを作成してください。ただし、同じ駅を何度訪問しても良く、どのような順番で駅を訪問しても構いません。 入力 入力は以下の形式で与えられる。 N M p d 1 d 2 : d M 1行目に駅の数 N (2 ≤ N ≤ 100000)、買い物をする駅の数 M (1 ≤ M ≤ 10000 かつ M < N )、出発する駅の番号 p (0 ≤ p ≤ N -1) が与えられる。続く M 行に買い物をする駅の番号 d i (0 ≤ d i ≤ N -1) が与えられる。ただし、 d i は全て異なり、 p もまたどの d i とも異なるものとする。 出力 最小の費用を1行に出力する。 入出力例 入力例1 5 4 0 1 2 3 4 出力例1 400 入力例2 7 2 1 6 2 出力例2 400
[ { "submission_id": "aoj_0299_4988855", "code_snippet": "#include <iostream>\n#include <stdio.h>\n\nusing namespace std;\n\nint main()\n\n \n\n{ \n int N, M, p;\n int d[100000];\n int i, j, tmp;\n \n scanf(\"%d %d %d\", &N, &M, &p);\n for (i=0; i<M; ++i)\n {\n scanf(\"%d\", &...
aoj_0300_cpp
フロッピーキューブ フロッピーキューブをプログラミングで解いてみましょう。フロッピーキューブは図のように表面に色のついた9個の立方体から構成されている立体パズルで、キューブの列を回転させることによって、6つの各面の色をそろえます。 フロッピーキューブに対しては下図のような4種類の操作を行うことができ、一回の操作で、端にある3つの隣接したキューブを180度回転することができます。わかりやすいように、図では、上面に+(赤色)、下面に*(緑色)、右前面に□(黄色)、左前面に●(青色)、右奥面に○(水色)、左奥面に■紫色) の記号が付いている状態を初期状態としています。 フロッピーキューブの初期状態が与えられるので、パズルを解くために必要な最小の操作回数を求めるプログラムを作成してください。 入力 入力は以下の形式で与えられる。 N puzzle 1 puzzle 2 : puzzle N 1行目の N (1 ≤ N ≤ 30) は操作回数を計算したいパズルの数である。続くN行に各フロッピーキューブの初期状態 puzzle i が与えられる。 puzzle i は以下の形式で与えられる。 p 1 p 2 p 3 p 4 p 5 p 6 p 7 p 8 p 9 p 10 p 11 p 12 p 13 p 14 p 15 p 16 p 17 p 18 p 19 p 20 p 21 p 22 p 23 p 24 p 25 p 26 p 27 p 28 p 29 p 30 各フロッピーキューブの情報は 30 個の整数 p i (1 ≤ p i ≤ 6) からなる。 p i は、下図のようにフロッピーキューブの各面に番号 i を振ったときの、そのキューブの面の色を表す。 パズルは、多くとも8回の操作で解くことができると仮定してよい。 出力 パズルごとに、最小の操作回数を1行に出力する。 入出力例 入力例 4 1 1 1 1 1 1 1 1 1 2 2 2 4 4 4 6 6 6 5 5 5 3 3 3 3 3 3 3 3 3 3 3 3 1 1 1 1 1 1 2 2 2 4 4 6 4 6 6 5 5 5 3 3 3 3 3 3 1 1 1 3 3 3 1 1 3 1 1 1 2 2 5 6 4 4 4 6 6 2 5 5 3 3 3 1 3 3 1 1 1 1 3 1 3 1 3 3 1 3 2 2 2 6 4 4 6 6 4 5 5 5 1 3 1 1 3 1 3 1 3 出力例 0 1 2 7
[ { "submission_id": "aoj_0300_10850143", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n// #include<atcoder/all>\n// using mint = atcoder::modint998244353;\nusing ld = long double;\n#define fi first\n#define se second\n#define all(x) x.begin(),x.end()\n#define rep(i,n) for(int i=0;i<(int)(n...
aoj_0308_cpp
デッドロックを検出せよ コンピュータにおける「データベース」とは、情報を管理するための入れ物で、「データベースマネジメントシステム(DBMS)」とは、その管理をする仕組みです。複数のユーザが利用するデータベースでは、DBMS は慎重に構築する必要があります。 例えば、倉庫から商品を1つ取り出した人が、データベースに対して以下の処理を行うとしましょう。 (1) 商品の個数 N をデータベースから読む。 (2) 新たな商品の個数 N -1 をデータベースに書き込む。 ユーザ1が(1)を終えて(2)を始める前に、別のユーザ2が倉庫から商品を取り出して(1)を行ったとします。ユーザ2もユーザ1と同じ個数を読むので、2人が(2)を終えたときには商品は2個減るのにデータベース上では1個しか減らないというおかしな結果になります。このような問題を防ぐために、DBMS は特定のデータを操作中のユーザに、そのデータを「ロック」する権利を与えます。ロックされていれば、他のユーザはその値を操作できなくなるので、おかしな結果を返すことはなくなります。 これで安全に操作できることは保証されますが、今度は別の問題が起こります。例えば、ユーザ1とユーザ2が以下のような順番でデータAとBをロックしようとしたらどうなるでしょうか? (1) ユーザ1がデータAのロックを試みる → 成功(データAがロック中になる) (2) ユーザ2がデータBのロックを試みる → 成功(データBがロック中になる) (3) ユーザ1がデータBのロックを試みる → データBがロック中なのでユーザ1は待つ (4) ユーザ2がデータAのロックを試みる → データAがロック中なのでユーザ2は待つ (4)を実行した時点では、ユーザ1がA、ユーザ2がBをロックしているので、(3)(4)は永久に成功しませ ん。これを「デッドロック」と呼びます。DBMS はこれを検出しなければなりません。 ある時点でデッドロックが起きているかどうかは、その時点でのすべてのユーザとデータの依存関係を書き、循環ができているかどうかで判断できます。依存関係は,ユーザがデータをロック済みの場合はデータからユーザの向きに矢印を、ユーザがデータのロックを試行していて待ち状態になっている場合はユーザからデータの向きに矢印を書くことで表します。 上の(1)から(4)の例であれば、(4)を実行した時点での依存関係は右上のような図になります。このとき、矢印の方向に進むと、ユーザ1→データB→ユーザ2→データA→ユーザ1という循環ができているため、デッドロックが起きていることがわかります。DBMS の苦労を体験するため、あなたにここでやってもらいたいのは、このようなデッドロックを検出するプログラムを作成することです。 入力 入力は以下の形式で与えられる。 N rel 1 rel 2 : rel N 1行目にユーザとデータの依存関係の数 N (1 ≤ N ≤ 1000) が与えられる。続く N 行に、ユーザとデータの依存関係 rel i が与えられる。依存関係は lock と wait の2種類あり、各 rel i は以下のいずれかの形式で与えられる。 u lock d または u wait d u lock d は、ユーザ u (1 ≤ u ≤ 100) が、データ d (1 ≤ d ≤ 100) をロック済みであることを表す。 u wait d は、ユーザ u (1 ≤ u ≤ 100) が、データ d (1 ≤ d ≤ 100) のロックを試行していて待ち状態であることを表す。 ただし、入力は以下の条件を満たしていると仮定してよい。 ロックされていないデータに対して、ユーザが待ち状態ではない。 二人以上のユーザにロックされているデータはない。 ユーザ自身がロックしているデータに対して、待ち状態ではない。 同じ依存関係は与えられない。 出力 デッドロックが発生しているなら 1、発生していないなら 0 を1行に出力する。 入出力例 入力例1 4 1 lock 1 2 lock 2 1 wait 2 2 wait 1 出力例1 1 入力例2 4 3 lock 3 2 wait 3 3 lock 4 2 wait 4 出力例2 0
[ { "submission_id": "aoj_0308_2719446", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define MAX_V 10000000\n#define MAX_E 10000000\n#define INF 1111111111\nstruct edge{int from,to,cost;};\n\nedge es[MAX_E]; \nint d[MAX_V]; \nint V=202,E;\n\nbool find_negative_loop(){\n memset(d,0,sizeo...
aoj_0303_cpp
力持ち 力持ちたちが集う力持ち学園がありました。力持ち学園の運動会では、力持ちたちが隊列を組んで行進します。 力持ちたちは常に自分たちの力を誇示したい一方で、彼らの大半は自分で歩きたくありません。そこで彼らの一部が一番下になり、その上に大勢の人を縦一列に持ちあげて歩くことで、実際に歩く人数を減らそうと考えました。 はじめに、 N 人の力持ちは地面の上に横一列に並んでいて、それぞれ左側から 1,2,3,..., N と通し番号で呼ばれています。通し番号 i の力持ちの体重は w i で最大 c i の重量まで持つことができます。 力持ちは、以下の条件をすべて満たすときだけ、隣に立っている左右どちらかの力持ちを持ちあげることができます。 自分の上下には力持ちはいない。つまり、誰かに持ち上げられてもいないし、誰かを持ちあげてもいない。 隣の力持ちの体重が、自分が持つことのできる最大の重量以下である。ただし、隣の力持ちが既に誰かを持ち上げているなら、縦に積み上がった力持ちたちの体重の合計が、自分が持つことのできる最大の重量以下でなければならない。 例えば、次のような3人の力持ちの隊列を考えてみましょう。 下図のように、2の力持ちが持つことのできる重さが w 3 以上のとき、2の力持ちは3の力持ちを持ちあげることができます。続いて、1の力持ちが持つことのできる重さが w 2 + w 3 以上のとき、1の力持ちは2の力持ちを持ちあげることができます。 → また、下図のように3の力持ちが2の力持ちを持ちあげた場合は、1の力持ちの隣が、2の力持ちを持った3の力持ちになるので、1の力持ちは3の力持ちを持ちあげることができます。 → 2の力持ちが、下の図のように1と3の力持ちを両方持つことはできません。 力持ち学園の専属プログラマーとして、一番下で歩くことになる最小の人数を求めてください。 入力 入力は以下の形式で与えられる。 N c 1 w 1 c 2 w 2 : c N w N 1 行目に力持ちの人数 N (1 ≤ N ≤ 1000) が与えられる。続く N 行に i 番目の力持ちが持てる重量の最大値 c i (1 ≤ c i ≤ 100000) と体重 w i (1 ≤ w i ≤ 100000) が与えられる。 出力 最小の人数を1行に出力する。 入出力例 入力例1 3 150 120 100 50 80 100 出力例1 1 入力例2 8 50 100 20 20 30 20 90 50 140 30 30 60 59 120 10 10 出力例2 3
[ { "submission_id": "aoj_0303_10691213", "code_snippet": "// competitive-verifier: PROBLEM\n#pragma GCC optimize(\"Ofast,fast-math,unroll-all-loops\")\n#include <bits/stdc++.h>\n#if !defined(ATCODER) && !defined(EVAL)\n#pragma GCC target(\"sse4.2,avx2,bmi2\")\n#endif\ntemplate <class T, class U>\nconstexpr b...
aoj_0304_cpp
学食 今日はZ大学のオープンキャンパスです。毎年この日の昼休みには、大勢の高校生たちが学食に列をつくります。そこでZ大学の事務局は、行列の長さが最大でどのくらいの距離になるかを予測することにしました。事前調査の結果で、以下のことが分かっています。 行列にはそれぞれ 1 から N までの番号が振られた N 人が並びます。 C 個の高校生のペア ( a i , b i ) それぞれについて、以下の2種類の制約があります: 1つ目の制約は順序に関するもので以下のいずれかです: a i は b i よりも先、または同じ位置に並ばなくてはならない a i は b i よりも後、または同じ位置に並ばなくてはならない a i は b i より先でも、同じ位置でも、後でもよい 2つ目の制約は距離に関するもので以下のいずれかです: a i と b i は d i メートル以上離れなければならない a i と b i は d i メートル以内に並ばなければならない また、先頭から同じ距離の場所に複数の人が並ぶことができ、行列の先頭には常に番号 1 の人が並ぶことが分かっています。 与えられた C 個の制約をすべて満たす行列について、先頭から最後尾までの距離が最大となるような並び方をした場合の距離を求めるプログラムを作成してください。ただし、どこまでも離れることができる場合は inf と、制約を満たす並び方が不可能な場合は -1 と出力してください。 入力 入力は以下の形式で与えられる。 N C constraint 1 constraint 2 : constraint C 1 行目に行列に並ぶ高校生の人数 N (2 ≤ N ≤ 100) と制約の数 C (0 ≤ C ≤ 200) が与えられる。続く C 行に各制約 constraint i が次の形式で与えられる。 a i o i b i s i d i 制約には空白は含まれない。 a i , o i , b i , s i , d i の意味を以下に示す。 a i と b i (1 ≤ a i , b i ≤ N かつ a i ≠ b i ) は高校生の番号、 d i は距離 (0 ≤ d ≤ 10000) を表す整数である。 o i は順序の制約を指定する <= 、 >= 、 * のいずれかの文字列であり、 <= の場合「 a i は b i よりも先、または同じ位置に並ばなくてはならない」、 >= の場合「 a i は b i よりも後、または同じ位置に並ばなくてはならない」、 * の場合「 a i は b i より先でも、同じ位置でも、後でもよい」ことを意味する。ただし o i が * となる制約は7個以上与えられることはない。 s i は距離の制約を指定する + または - の文字であり、 + の場合「 a i と b i は d i メートル以上離れなければならない」、 - の場合「 a i と b i は d i メートル以内に並ばなければならない」ことを意味する。 ただし、あるペアに対して複数の制約が与えられることはないものとする。 出力 先頭から最後尾までの距離を1行に出力する。 入出力例 入力例1 3 2 1<=2-1 2<=3-2 出力例1 3 入力例2 3 3 1<=2-1 2<=3-2 1<=3+4 出力例2 -1 入力例3 3 2 1<=2-2 2*3+1 出力例3 inf
[ { "submission_id": "aoj_0304_5931301", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for(long long i=0;i<(long long)(n);i++)\n#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)\n#define pb emplace_back\n#define lb(v,k) (lower_bound(all(v),(k))-v.begin())\n#defin...
aoj_0301_cpp
バトンリレーゲーム アカベ高校では、毎年全校生徒が参加するゲームを行っています。まず、校庭に N 人の全校生徒が円形に並びます。図のように、各生徒は 0 から N -1 までの番号が書かれたゼッケンを付けています。 ゲームではバトンを1本使い、最初はゼッケン 0 番の生徒がバトンを持っています。そこから、以下の手順を M 回繰り返します。まず、現時点でバトンを持っている生徒が適当な正の整数 a を宣言します。 a が偶数のときは時計回り、奇数のときは反時計回りに隣の生徒にバトンを渡していき、 a 番目にバトンを受け取った生徒が脱落します。脱落した生徒は、時計回りで隣の生徒にバトンを渡し、円から抜けます。 ゲームが終わった後に円に残った生徒は、放課後の掃除が1年間免除されます。しかし、ここ数年は生徒数が増えたため、全校生徒を集めるのが難しくなってきています。そこで、競技プログラミング部のあなたは、シミュレーションで掃除が免除される生徒を求めるプログラムを作成するよう頼まれました。 指定した生徒が掃除を免除されているかどうかを質問したとき、それに答えるプログラムを作成してください。 入力 入力は以下の形式で与えられる。 N M Q a 1 a 2 ... a M q 1 q 2 ... q Q 入力は3行であり、1行目に生徒の人数 N (10 ≤ N ≤ 200000)、繰り返し回数 M (5 ≤ M < N )、生徒が掃除を免除されるかどうかを問い合わせる質問の個数 Q (1 ≤ Q ≤ 1000) が与えられる。続く1行に、 i 回目の繰り返しで最初にバトンを持っている生徒が宣言する整数 a i (1 ≤ a i ≤ 100) が与えられる。続く1行に、質問としてゼッケン番号 q i (0 ≤ q < N ) が与えられる。 出力 質問ごとに、ゼッケン番号 q i の生徒が掃除を免除されるかどうかを i 行目に出力する。掃除が免除されるなら 1 を、されないなら 0 を出力する。 入出力例 入力例 10 5 3 2 6 5 18 3 3 0 5 出力例 1 0 1
[ { "submission_id": "aoj_0301_10876569", "code_snippet": "#include <bits/stdc++.h>\n#include <iomanip>\n#include <numbers>\n#include <cassert>\n#include <numeric>\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing pint = pair<int...
aoj_0313_cpp
未知の病原菌 英世博士は未知の病原菌を発見しました。この病原菌は、アクダマキンとゼンダマキンと呼ばれる二種類の菌が、一直線に連なった鎖状の構造をしています。人類のために、この病原菌を無害化したいと考えています。 この病原菌は、長さが2以下になると力が弱まり、免疫力によって無害化されることが分かっています。英世博士は、この病原菌を任意の場所で切断して、前半と後半の2つの鎖にすることができます。また、2つの鎖を連結して1つの鎖にすることもできます。 しかし注意しなければいけないのは、アクダマキンの数が多い鎖はきわめて有害だということです。ある鎖においてアクダマキンの数がゼンダマキンの数よりも多くなると、その瞬間アクダマキンは無制限に増殖を始めます。これは長さ2以下の鎖についても例外ではないので、慎重に鎖を切断していかなければなりません。 どの瞬間においてもアクダマキンの数の方が多いような鎖を作ることなく、一本の鎖を長さ2以下にして無害化することは可能でしょうか。英世博士は、助手であるあなたに無害化が可能かどうか判定するプログラムを作成するよう指示しました。無害化が可能ならばその操作を出力し、不可能ならば不可能であると出力するプログラムを作成してください。ただし、その操作のステップ数が最小である必要はありません。 入力 入力は以下の形式で与えられる。 Q str 1 str 2 : str Q 1 行目に病原菌の数 Q (1 ≤ Q ≤ 300) が与えられる。続く Q 行に各病原菌の初期状態を表す ' o ' (ゼンダマキン) および ' x ' (アクダマキン) からなる1つの文字列 str i が与えられる。文字列 str i の長さは 1 以上 100 以下である。 出力 str i について、要求を満たす切断・結合操作の列が存在する場合、出力の最初の行にはその操作列の長さを表す整数 n を出力し、続く n 行に操作の内容を1行に1操作ずつ、最初の操作から順に出力する。 切断操作は以下の形式で表すこと。 split a p a は切断する鎖の識別番号を表す整数であり、 p はその鎖を切断する位置である。この操作の結果、鎖 a は先頭から p 番目(先頭を 0 から始める通し番号)の菌の直後で切断される。新しくできる2つの鎖のうち、前半のものに識別番号 m +1 が、後半のものに識別番号 m +2 が付与される(ここで、 m はこれまでに付与された最も大きな識別番号を表す)。また、鎖 a は消滅する。 結合操作は以下の形式で表すこと。 join a b a , b は結合する鎖の識別番号を表す整数である。この操作の結果、鎖 a の末尾に鎖 b の先頭を結合した新しい鎖が作られる。新しく作られた鎖には、識別番号 m +1 が付与される(ここで、 m はこれまでに付与された最も大きな識別番号を表す)。また、鎖 a , b は消滅する。 入力として与えられる最初の鎖には、識別番号 0 が付与されている。 操作の結果、問題の要求を満たすように鎖が分解されていた場合、どのような操作でも正答と判定される。操作列の長さも必ずしも最短である必要はない。ただし、操作列の長さは 20000 以下でなければならない。データセットにおいて、鎖が分解可能な場合、必ずこの条件を満たす操作列が存在することが保証される。 不正な操作列が出力された場合、誤答と判定される。不正な操作列には、以下の場合が含まれる。 切断操作 split a p において、0 ≤ p < (鎖 a の長さ)-1 が満たされていない場合。 切断・結合操作の対象となる鎖の識別番号がまだ生成されていないものである場合や、既に別の操作の対象となったため消滅している場合。 結合操作 join a b において、 a と b が等しい場合。 要求を満たす切断・結合操作の列が存在しない場合、" -1 "と出力する。 入出力例 入力例 6 oooxxxx ooooxxx oxxooxxo ooxx oo ooo 出力例 -1 7 split 0 0 join 2 1 split 3 4 split 4 0 join 7 6 split 8 2 split 9 0 3 split 0 1 split 2 1 split 4 1 -1 0 1 split 0 0 例えば、入力例の2番目の病原菌 ooooxxx は、 split 0 0 により o (1) と oooxxx (2) ができる。ここで、()内の数字は識別番号を表す。 join 2 1 により oooxxxo (3) ができ 1 と 2 は消滅する。 split 3 4 により oooxx (4) と xo (5) ができる。このとき{ oooxx (4), xo (5) }の鎖が存在する。 split 4 0 により o (6) と ooxx (7) ができる。{ xo (5), o (6), ooxx (7)} join 7 6 により ooxxo (8) ができる。{ xo (5), ooxxo (8)} split 8 2 により oox (9) と xo (10) ができる。{ xo (5), oox (9), xo (10) } split 9 0 により { xo (5), xo (10), o (11), ox (12) } となって終了する。
[ { "submission_id": "aoj_0313_10205416", "code_snippet": "// AOJ #313\n// Unknown Germ 2025.2.9\n\n#include <bits/stdc++.h>\nusing namespace std;\n \nstruct Chain {\n int id;\n string s;\n};\n \nbool isSafe(const string &s) {\n int countO = 0, countX = 0;\n for(char c : s) {\n if(c=='o') c...
aoj_0306_cpp
対称3進数 1 グラム、3 グラム、9 グラム、27 グラムのおもりが1つずつあれば、天びんを使って 1 グラムから 40グラムまで 1 グラム刻みで量れることが知られています。たとえば、天びんの一方の皿に重さを量りたいものと 3 グラムのおもりを載せ、もう一方の皿に 27 グラムと 1 グラムのおもりを載せて釣り合えば、量りたいものの重さは 27-3+1=25 グラムだとわかります。 さらに、1(=3 0 )グラム、3 1 グラム、... 、3 n-1 グラム、3 n グラムまでのおもりが1つずつあれば、天びんを使って(3 n+1 -1)/2グラムまで量れることが知られています。また、天びんが釣り合うようなおもりの置き方は一通りしかないことも知られています。 量りたいものとおもりを天びんに置いて、釣り合うようなおもりの置き方を文字列で表すことができます。3 i グラムのおもりを量りたいものと同じ皿に載せたときは「 - 」、もう一方の皿に載せたときは「 + 」、どちらにも載せなかったときは「0」を文字列の右端からi番目に書きます(右端を0番目と数えます)。たとえば、先ほどの 25 グラムの例は +0-+ と表わせます。 それでは、量りたいものの重さが与えられたとき、天びんが釣り合うようなおもりの置き方を表す文字列を出力するプログラムを作成してください。ただし、3のべき乗グラムのおもりは、どのような重さのものでも必ず1つあるものとします。 (補足: 対称3進数について) 量りたいものの重さがwのとき、おもりの置き方を表す文字列はwの対称3進数になっています。対称3進数とは、3のべき乗の数で位取りを行い、各位に数 1、0、-1 を表す文字を書くことで表した数のことです。上の文字列では、文字「 + 」、「 0 」、「 - 」がそれぞれ数 1、0、-1 に対応します。たとえば、25 グラムのものを量るときのおもりの置き方が +0-+ である対称3進数が表す数は、1 × 3 3 + 0 × 3 2 - 1× 3 1 + 1 × 3 0 = 25 となります。 入力 入力は以下の形式で与えられる。 w w (1 ≤ w ≤ 100000) は量りたいものの重さを表す整数である。 出力 おもりの置き方を表す文字列を出力する。ただし文字列の左端を 0 にしてはならない。 入出力例 入力例1 25 出力例1 +0-+ 入力例2 2 出力例2 +- 入力例3 5 出力例3 +--
[ { "submission_id": "aoj_0306_8406601", "code_snippet": "#include <algorithm>\n#include <array>\n#include <iostream>\n#include <stack>\n#include <string>\n#include <vector>\n\n\nnamespace {\n const std::array<int, 12> digits{\n 1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683, 59049, 177147 };\n std...
aoj_0310_cpp
枠 画像の中から有益な情報を抽出する画像認識はコンピュータサイエンスの中でも重要な研究テーマのひとつです。デジタルカメラ、運転支援システム、防犯システムなどに幅広く応用されています。 このような研究のおかげで、私たちは画像解析を行うための多くのソフトウェアやプログラム集を使い様々な処理を行うことができます。一方、自力でプログラムを書いて解析することで、その仕組みを知り、楽しい時間を過ごすことができます。ここでは、一風変わった画像認識をしてみましょう。 画像として次のような各ピクセルが整数の値を持つ N × N のピクセルが入力として与えられます。この画像の中から、線の太さが1ピクセルの長方形の枠(わく)を1つ抽出します。 枠が覆うピクセルの値の和が最大となるような枠を抽出して、その和を報告するプログラムを作成して 下さい。ただし、下の図のように、縦、横のピクセル数が1つや2つの場合も枠とみなすものとします。 入力 入力は以下の形式で与えられる。 N p 1,1 p 1,2 ... p 1,N p 2,1 p 2,2 ... p 2,N : p N,1 p N,2 ... p N,N 1行目に縦と横のピクセル数 N (1 ≤ N ≤ 300) が与えられる。続く N 行に、 i 行 j 列目のピクセルの値を表す整数 p i,j (-1000 ≤ p i,j ≤ 1000)が与えられる。 出力 ピクセル値の和が最大となるような枠の、ピクセル値の和を1行に出力する。 入出力例 入力例1 5 2 0 0 2 0 0 1 0 2 0 0 0 0 -1 0 0 4 0 3 0 -1 0 0 1 0 出力例1 12 入力例2 3 0 0 0 0 -1 0 0 0 0 出力例2 0
[ { "submission_id": "aoj_0310_11026928", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)\n\nint main() {\n\tcin.tie(nullptr);\n\tios_base::sync_with_stdio(false);\n\t\n\tint N;\n\tcin >> N;\n\tvector P(N, v...
aoj_0311_cpp
かぐや いよいよ今月末に「はやぶさ2」が打ち上げられます。4年前に「はやぶさ」が帰ってきたときは、日本中が盛り上がったのを覚えている人も多いと思います。7年前には、「かぐや」が打ち上げられ、月の周りを回りながらたくさんの鮮明な画像を地球に送ってくれました。 上の図は、地球を原点とした空間座標(z軸は紙面から垂直に下から上に向いているものとします)に、月の軌道といくつかの月の位置、月の周りを回るかぐやの軌道を描いたものです。月の軌道は、x軸とy軸を通る平面上にある、原点を中心とする円とします。月の周りを回るかぐやの軌道は、x軸とz軸を通る平面に平行な平面上にある円とし、その中心は月の中心と一致します。月は、その軌道に沿って描かれた矢印の方向に回っているものとします。 右の図で、月の位置はA,B,Cの3つが描かれています。月を横切る直線がかぐやの軌道です。かぐやは月の周りを回っているので軌道は円ですが、z軸の正の方向から見ているので、図のx軸に平行な直線に見えます(月の位置が変わっても、つねにx軸に平行であることに注意してください)。かぐやは、その軌道上に描かれた矢印の方向に回っているものとします。 かぐやが地球から見て月の裏側に隠れてしまうと、地球と直接通信ができなくなります。かぐやの管制を担当するあなたは、かぐやが月の裏側に隠れる時間が、与えられた時間の中でどれだけになるかをプログラムで求めようとしています。 月の地球に対する位置と分単位での時間tが与えられたとき、その位置から始めてt分後までの間に、かぐやが月の裏側に隠れる時間を求めるプログラムを作成してください。ただし、地球とかぐやは点、月は半径 1800 km の球と考えます。月は半径 380000 km の軌道を 2500000 秒で一周し、かぐやは月の表面から高度 100 km の円上を2時間で一周するものとします。かぐやの最初の位置は、かぐやの軌道がz座標で最大の値をとる位置とします。 入力 入力は以下の形式で与えられる。 m t m (0 ≤ m < 360) は月の位置を上の図のx軸の正の部分からy軸の正の部分に向かって反時計回りに測った角度を整数で表したものである。 t (1 ≤ t ≤ 10000) は分で測った時間を表す整数である。 出力 はじめの位置から t 分経過するまでの間に、かぐやが月の裏側に隠れる時間(分)を実数で出力する。ただし、誤差がプラスマイナス 1.0 分を超えてはならない。この条件を満たせば小数点以下何桁表示してもよい。 入出力例 入力例1 90 10 出力例1 0.0 入力例2 0 120 出力例2 47.73
[ { "submission_id": "aoj_0311_8296988", "code_snippet": "#include <cmath>\n#include <iostream>\nusing namespace std;\n\nuint64_t seed = 1234567891234567891;\nuint64_t xorshift64() {\n\tseed ^= seed << 13;\n\tseed ^= seed >> 7;\n\tseed ^= seed << 17;\n\treturn seed;\n}\nfloat rand_float() {\n\treturn xorshift...
aoj_0312_cpp
ネットカフェ あなたはネットカフェを経営しています。今日あなたは、顧客に指摘され続けている問題を解決しようと取り組んでいます。その問題とは、店舗の本棚の単行本が巻数順に並んでおらず、目的の単行本を探しだすのが面倒だという苦情です。 あなたの店舗で一番巻数の多い単行本は「名探偵 赤ベコ」(通称「赤ベコ」)です。あまりに長編なので、特別な本棚を「赤ベコ」のために用意しました。 単行本の各巻の重さと厚さは様々で、本棚の各段の幅と、各段に並べることができる本の重さの上限も様々です。あなたは、次の条件を満足するように本棚に本を並べることにしました。 1 巻からある巻までの「赤ベコ」が本棚に並んでいる。 それぞれの段には、巻数順に(途中で抜けている巻がないように)本が並ぶ。 各段に並べる本の重さの合計が、その段で定められた重さの上限を超えない。 各段に並べる本の厚さの合計が、その段の幅を超えない。 これらの条件を満たしたとき,この本棚に最大で何巻まで「赤ベコ」を並べることができるかを求めるプログラムを作成してください。 入力 入力は以下の形式で与えられる。 M N w 1 t 1 w 2 t 2 : w M t M c 1 b 1 c 2 b 2 : c N b N 最初の1行に「赤ベコ」の巻数 M (1 ≤ M ≤ 200000) と本棚の段数 N (1 ≤ N ≤ 15) が与えられる。続く M 行に、「赤ベコ」の単行本 i 巻目の重さ w i (1 ≤ w i ≤ 100) と厚さ t i (1 ≤ t i ≤ 100) を表す整数が与えられる。続く N 行に、本棚の i 段目の重さの上限 c i (1 ≤ c i ≤ 10 8 )と幅 b i (1 ≤ b i ≤ 10 8 ) を表す整数が与えられる。 出力 本棚に並べることができる最大の「赤ベコ」巻数を1行に出力する。 入出力例 入力例1 3 4 2 2 3 3 4 4 3 3 4 4 1 1 2 2 出力例1 3 入力例2 2 2 1 2 2 1 2 1 2 1 出力例2 0 入力例3 3 2 1 2 2 2 2 1 3 3 2 2 出力例3 2 入力例4 3 2 1 2 2 1 2 2 2 2 3 3 出力例4 3
[ { "submission_id": "aoj_0312_11026966", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)\n\nint main() {\n\tcin.tie(nullptr);\n\tios_base::sync_with_stdio(false);\n\t\n\tint M, N;\n\tcin >> M >> N;\n\tvecto...
aoj_0302_cpp
天体観測 ある晴れた夜の帰り道、空を見上げるとそこには無数の星。強く輝く星々、かすかに見える星々、いろ いろな明るさの星々が夜空を彩っています。 あなたはふと思いました。この星空に自分の星座があったらどんなに素敵だろうと。そこであなたはひとつの基準を決め、部屋の窓から見える星々を適当につなげて自分の星座を作ることにしました。その基準とは、「どの2つの星を選んでも、それらの明るさの差がある定数 d 以下になるような星のグループを作り、その中で一番大きいものを自分の星座にしよう!」というものです。例えば、図のような窓から見える夜空を考えてみましょう(外側の長方形は窓枠です)。 この夜空には、明るさがそれぞれ 1,12, 2,4,1,8, 3,5,4 の9つの星がありますが、 d = 2 とすると例えば以下のような3つの星座 A, B, C ができます。 大きさが 12 の星座 A 大きさが 10 の星座 B 大きさが 16 の星座 C 星座の大きさを次のように決めることにしました。ある星座の星をすべて含むような、窓枠に平行な辺からなる長方形のうち、面積が最も小さいものを選びます。この長方形の面積をその星座の大きさとします。例えば、上の夜空では星座 A, B, Cの大きさはそれぞれ12, 10, 16になるので、星座Cが最も大きい星座となります。 N 個の星の位置と明るさ、および整数 d が与えられたとき、一番大きい星座の面積を求めるプログラムを作成してください。星の位置は窓枠の左下隅を原点とした座標で与えられ、軸は図のような向きとします。星座を構成する星が1つの場合や、星々が軸に平行な直線上にある場合は、その星座の面積は 0 となることに注意してください。 入力 入力は以下の形式で与えられる。 N d x 1 y 1 b 1 x 2 y 2 b 2 : x N y N b N 1 行目に星の数 N (1 ≤ N ≤ 200000) と整数 d (0 ≤ d ≤ 10 9 ) が与えられる。続く N 行に、 i 番目の星の座標を表す整数 x i (0 ≤ x i ≤ 2000000) と y i (0 ≤ y i ≤ 2000000)、明るさを表す整数 b i (0 ≤ b i ≤ 10 9 ) が与えられる。入力される星の座標はすべて異なる。 出力 一番大きい星座の面積を1行に出力する。 入出力例 入力例 9 2 1 1 1 1 5 12 2 3 2 3 2 4 4 4 1 5 1 3 5 3 8 6 5 5 7 2 4 出力例 16
[ { "submission_id": "aoj_0302_10876657", "code_snippet": "#include <bits/stdc++.h>\n#include <iomanip>\n#include <numbers>\n#include <cassert>\n#include <numeric>\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing pint = pair<int...
aoj_0314_cpp
アカベコ国王の配慮 アカベコ国の国王には2人の王子がいます。国王は自分が退位するときに国を2つに分割し、それぞれの王子に一つずつ国を治めさせることにしました。新しい国の名前はアカ国とベコ国です。アカベコ国には N 個の町と、2つの町を繋ぐ M 本の道があります。国王は、以下の手順でアカベコ国の町と一部の道を2つの国に配分することにしました。 (1)   町を2つ選び、それぞれアカ国とベコ国に配分する。 (2)   すでに配分された町sを選ぶ。さらに、町 s から1本の道で繋がっている、まだ配分されていない町 t を選ぶ。そして、町 s 、 t 間の道と町 t を、町 s が配分された国に配分する。 (3)   (2)を、行えなくなるまで繰り返す。 実は2人の王子はあまり仲が良くないので、国王は2つの国の距離をなるべく大きくしたいと考えています。ここで、2つの国の距離とは、アカ国の町とベコ国の町を繋ぐ道の中で、最も短い道の長さです。 アカベコ国の町と道の情報が与えられたとき、分配後のアカ国とベコ国の距離の最大値と、そのような距離になる配分が何通りあるかを求めるプログラムを作成してください。ただし、2つの配分結果は、アカ国とベコ国に異なる町か道が配分された場合に区別されます。 入力 入力は以下の形式で与えられる。 N M s 1 t 1 d 1 s 2 t 2 d 2 : s M t M d M 1行目は2つの整数からなる。 N (2 ≤ N ≤ 100) は町の数、 M ( N -1 ≤ M ≤ N ( N -1)/2) は道の数を表す。続く M 行に2つの町を繋ぐ道が与えられる。 s i と t i (1 ≤ s i ≠ t i ≤ N ) は i 番目の道が繋ぐ2つの町の番号を表す。 d i (1 ≤ d i ≤ 10 9 ) は i 番目の道の長さを表す。 入力は以下の条件を満たす。 どの2つの町もいくつかの道を使い行き来が可能である。 どの2つの町の間にも2本以上の道はない。 同じ長さの道は5本以下である。 出力 分配後のアカ国とベコ国の距離の最大値と組み合わせの数を、空白区切りで1行に出力する。ただし、分配後の組み合わせの数は非常に大きくなりうるので、代わりに 1,000,000,007 で割った余りを出力する。 入出力例 入力例 6 7 1 2 1 2 3 2 3 1 3 4 5 4 5 6 5 6 4 6 1 4 7 出力例 7 18
[ { "submission_id": "aoj_0314_10454075", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst int MOD = 1e9 + 7;\n\nll modinv(ll a) {\n ll res = 1, b = MOD - 2;\n while (b > 0) {\n if (b & 1) res = res * a % MOD;\n a = a * a % MOD;\n b >>= 1...
aoj_0322_cpp
Slates Numerous stone slates have been discovered in the ruins of the ancient nation of Iwashiro. Researchers have found that each slate has a single word inscribed on it. However, due to weathering over the years, some of the slates seem to be difficult to decipher for the following reasons. In some cases, only one of the letters of the word on the slate is covered with moss, making it impossible to figure out the word. The left side of the slate is missing (chipped), and there may have been some text written there (we cannot grasp more than or equal to 0 letters on the left side of the slate). The right side of the slate is missing (chipped), and there may have been some text written there (we cannot grasp more than or equat to 0 letters on the right side of the slate). There is at most one place where moss grows on the slate. In addition, although moss may grow on the chipped slate, both sides of the slate are not chipped at the same time. Researchers have a dictionary of words known from surveys prior to the discovery of the slates. However, when guessing the original word from a slate with moss and chips due to the effects of weathering, it is not immediately clear how many words in the dictionary apply. Write a program to count how many words in the given dictionary might fit for given a slate. Input The input is given in the following format. N M word 1 word 2 : word N slate 1 slate 2 : slate M In the first line, the number of words in the dictionary N (1 ≤ N ≤ 50000), the number of slates M (1 ≤ M ≤ 50000) are given. The following N line gives the words word i . A word is a character string with a length of 1 or more and 200 or less, including only lowercase letters. Note that the N words are all different. The following M lines give the string slate i that represents the information for each slate. slate i is a character string with a length of 1 or more and 200 or less, including lowercase letters, "?", And "*". ? represents a moss-covered character. At most one ? appears in one string. If the string starts with *, it means that the left side of the slate is missing. If the string ends with *, it indicates that the right side of the slate is missing. * does not appear except at the beginning or end of the string, and does not appear on both sides at the same time. A string with only one * is never given. The total number of characters in the string given in the input does not exceed 3000000. Output For each slate, output the number of words on a single line. Sample Input 1 5 4 aloe apple apricot cactus cat apple ap* *e ca?* Sample Output 1 1 2 2 2
[ { "submission_id": "aoj_0322_10958576", "code_snippet": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\...
aoj_0321_cpp
Investigation of Club Activities Akira, the student council president of A High School, decided to investigate which club activities the A High School students belong to. A High School has N students numbered from 1 to N and M types of club activities numbered from 1 to M . There is no limit to the number of students in each club, and it is possible to have zero students in a club. However, according to the school rules of A High School, students can only belong to one club activity. All students follow this school rule. Akira asked a student member to do some research and obtained a record of K lines such that each line is one of the following. Student a and student b belong to the same club activity. Student c belongs to club activity x . However, there may be discrepancies in this record that would cause someone to violate the school rules. Akira decided to look at it from the first line and look for the first line where he could determine that there was a discrepancy. Write a program to find the number of the first line that can be determined to be inconsistent from the given the record of K lines. Input Input is given in the following format. N M K record 1 record 2 : record K In the first line, the number of students N (1 ≤ N ≤ 100000), the number of types of club activities M (1 ≤ M ≤ 100000), and the number of lines recorded K (1 ≤ K ≤ 200000) are given. Each record i of the record is given in the following K line in one of the following formats: 1 a b or 2 c x When the first number is "1", student a (1 ≤ a ≤ N ) and student b (1 ≤ b ≤ N ) belongs to the same club activity. Note that a ≠ b . When the first number is "2", the student c (1 ≤ c ≤ N ) is in the club activity x (1 ≤ x ≤ M ). Output Output the number of the first line that can be determined to be inconsistent in a single line. If none is found, output 0 in a single line. Sample Input 1 3 2 5 1 1 2 1 2 3 2 1 1 2 3 2 2 2 1 Sample Output 1 4 Sample Input 2 3 2 4 1 1 2 2 1 1 2 3 2 2 2 1 Sample Output 2 0 Sample Input 3 3 2 2 1 1 2 2 1 1 Sample Output 3 0
[ { "submission_id": "aoj_0321_10884265", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int, int>;\n#define rep(i, n) for (int i = 0; i < (int) (n); i++)\n\n// Union-Find\nstruct UnionFind {\n vector<int> par, rank, siz;\n\n // 構造体の初期化\n Unio...
aoj_0320_cpp
Quality Management The fabric coasters produced and sold by Aizu Takada City are known for their symmetrical design and beauty. As part of quality control, Aizu Takada has installed cameras on the production line to automatically verify that the images obtained from each coaster are symmetrical. Each coaster is represented as a N × N pixel square black and white image. Each pixel takes a value of 0 or 1, corresponding to a white or black image. We have decided to update the software of our image analysis system in conjunction with the equipment upgrade of our production line. The new system has been devised to reduce the amount of communication data, and the data is sent from the camera to the analysis system in the following way. Information about the first coaster flowing into the line is sent to the system as an image of N × N pixels. For the second and subsequent coasters, only the difference from the previous image is sent. The difference is given as a set of pixel positions that have changed from "0 to 1" or "1 to 0". Write a program that reads C coasters by the pixel information of the first image and the difference information of the following C - 1 sheet, and reports the number of coasters of vertically symmetrical and horizontally symmetrical. Input The input is given in the following format. C N p 11 p 12 ... p 1N p 21 p 22 ... p 2N : p N1 p N2 ... p NN diff 1 diff 2 : diff C−1 In the first line, the number of coasters C (1 ≤ C ≤ 10000) and the number of vertical and horizontal pixels in the image N (2 ≤ N ≤ 1000 and N is even) are given. From the second line to the line N + 1 represent the pixels of the first coaster image N row × N elements p ij ( p ij is 0 or 1) are given. After the line N + 2, the difference diff i representing the information of the second and subsequent coasters is given in the following format. The number of changed pixels D (0 ≤ D ≤ 100) is given in the first line. In the following D lines, the row and column numbers of the changed pixels r i and c i are given, respectively. (1 ≤ r i , c i ≤ N ) . The same position cannot be given more than once in diff i . Output Output the number of coasters that are vertically symmetrical and horizontally symmetrical in a line. Sample Input 1 7 8 00100000 00011000 10111101 01100110 01000110 10111101 00011000 00100100 2 5 3 1 6 1 6 8 3 6 8 3 3 3 6 2 6 3 6 6 0 2 3 8 6 8 Sample Output 1 3 The image of the coaster for input example 1 is shown below. In this case, the second, fifth, and sixth coasters are symmetrical vertically and horizontally, so they are reported as 3. Sample Input 2 1 6 000000 000000 010010 010010 000000 000000 Sample Output 2 1 Sample Input 3 2 2 00 00 4 1 1 1 2 2 1 2 2 Sample Output 3 2
[ { "submission_id": "aoj_0320_10884337", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nconst int INF = 1 << 30;\n\nint C, n;\nvector<vector<int>> p;\nint check(int x, int y) {\n if (p[x][y] == p[x][n-y-1] && p[x][y] == p[n-x-1][y] && p[x][y] == p[n-x-1][n-y-1]) ...
aoj_0323_cpp
Ruins The Aizu Archaeological Society has set out to investigate the ruin of the ancient nation of Iwashiro, which is submerged in the Hibarra Sea. The ruin exists somewhere in the Hibara Sea. We decided to use radar to get a rough idea of the location of the ruin from the shoreline, and to estimate how many meters away from the shoreline we would have to go to investigate. The observation points are placed on the coastline represented by the straight line as shown in the figure above, and the exploration radars are placed there. The radar can only tell us that there is the ruin within a semicircle of a certain size centered on the observation point. However, by combining multiple observation data, it is possible to narrow down the area to a smaller one. Given some observation data, consisting of the location of the observation points and the radius indicated by the exploration radars, write a program to find out how far from the coastline we need to survey at most. Input The input is given in the following format. N x 1 r 1 x 2 r 2 : x N r N The first line gives the number of data measured by the exploration radar N (1 ≤ N ≤ 100000). The following N lines are integers x i that represents the position of the observed data i on the coastline in meters (0 ≤ x i ≤ 1,000,000) and integers r i that represents the number of meters within a radius of the site (1 ≤ r i ≤ 1,000,000). Two or more observation data with the same observation point may be given. If there are multiple observational data given, it can be considered that there is always a point included in all of those semicircles. Output Outputs the actual number of meters that need to be investigated from the coastline. However, the error must not exceed plus or minus 0.001 meters. If this condition is satisfied, any number of digits after the decimal point may be displayed. Sample Input 1 2 0 2 1 2 Sample Output 1 1.936 Sample Input 2 3 0 3 1 2 2 1 Sample Output 2 1.0 Sample Input 3 2 0 1 3 2 Sample Output 3 0.0
[ { "submission_id": "aoj_0323_10873523", "code_snippet": "#include <bits/stdc++.h>\n#include <iomanip>\n#include <numbers>\n#include <cassert>\n#include <numeric>\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing pint = pair<int...
aoj_0324_cpp
Downhill Race You are participating in a ski competition held at Mt. Bandai. In this competition, each skier will ski down the slope twice and compete for the shortest total time. There are a number of flags on the slope, and a line is set up between them for the competitors to follow. Competitors follow the lines from the start point to the finish point. The lines are set up as follows. One or more lines extend from a flag except at the finish point. There is only one line at most that directly connects one flag to another. The line can only be slid down in a certain direction. You can always reach any flag from the start, and you can reach the goal from any flag. No matter how you follow the line, you will never return to the same flag. The athlete can decide which flag to go to next by choosing a line that extends from the flag they are currently on. The choice of line is free, so players can follow a different line to the finish point for each descent. The night before the competition, Mr. Salt, the sports doctor, predicted the condition of the slope when you skied. According to him, the line you took on the first descent will change the snow quality due to the passage, so if you take the same line on the second descent, the time it will take may change. Salt told you how long it takes to go through each line the first time and how long it takes to go through it the second time. You must use this information to find a way to ski the line that minimizes the total time of the two descents. Given the conditions of the slope, write a program that calculates the shortest total time for the two descents. Input The input is given in the following format. N P s 1 e 1 t 1,1 t 1,2 s 2 e 2 t 2,1 t 2,2 : s P e P t P,1 t P,2 On the first line, the number of flags N (2 ≤ N ≤ 1000) and the number of lines connecting the two flags P ( 1 ≤ P ≤ 2000) are given. The flags are numbered from 1 to N , the starting point flag number is 1 and the goal point flag number is N . The following P lines give information about the line connecting the two flags. Each line has the source flag number s i (1 ≤ s i < N ), the destination flag number e i (1 < e i ≤ N ) , time required for the first pass t i, 1 (1 ≤ t i, 1 ≤ 100000), the time required to pass the same line at the second time t i, 2 (1 ≤ t i, 2 ≤ 100000). Output Output the shortest value in the total time of two downhills in one line. Sample Input 1 3 3 1 2 1 2 2 3 1 2 1 3 1 3 Sample Output 1 3 Sample Input 2 3 3 1 2 1 2 2 3 1 2 1 3 1 1 Sample Output 2 2 Sample Input 3 4 5 1 2 3 5 1 3 1 3 3 2 2 5 2 4 6 1 3 4 5 5 Sample Output 3 13
[ { "submission_id": "aoj_0324_10852452", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n// #include<atcoder/all>\n// using mint = atcoder::modint998244353;\nusing ld = long double;\n#define fi first\n#define se second\n#define all(x) x.begin(),x.end()\n#define rep(i,n) for(int i=0;i<(int)(n...
aoj_0326_cpp
関連商品 インターネット通販サイトでは、ユーザが現在見ている商品と同じページに、過去に他のユーザによって、現在見ている商品と一緒に買われた別の商品をいくつか表示してくれます。関連性の高いと思われる商品を提示することで、売り上げを伸ばすことができると考えられているからです。 似たようなことは、一緒に買われることが多い商品を近くに配置する、という工夫として、近所のスーパーマーケットでも目にすることができます(例えば、パンとジャムのような)。あなたの仕事は、商品配置の工夫を助けるプログラムを書くことです。今回は、ある基準となる回数を設定し、一緒に買われた回数が基準回数以上である、2つの商品の組み合わせを求めたいと思います。 一緒に買われた商品の情報と基準回数が与えられたとき、基準回数以上一緒に買われた商品2つの組み合わせを出力するプログラムを作成せよ。 Input 入力は以下の形式で与えられる。 N F info 1 info 2 : info N 1行目に、一緒に買われた商品の情報の数 N (1 ≤ N ≤ 100) と、基準回数 F (1 ≤ F ≤ 100) が与えられる。続く N 行に、一緒に買われた商品の情報が与えられる。一緒に買われた商品の情報 info i は、以下の形式で与えられる。 M item 1 item 2 ... item M M (1 ≤ M ≤ 10) は、この情報がいくつの商品を含むかを表す。 item j は、この買い物で買われた商品の名前であり、英小文字だけから成る長さ 1 以上 30 以下の文字列である。 info i の中に同じ商品が与えられることはない。 Output 1行目に基準回数以上一緒に買われた商品2つの組み合わせの数を出力し、2行目以降に組み合わせをすべて出力する。ただし、組み合わせが一つもない場合は2行目以降には何も出力しない。 出力の順番は、組み合わせ内の商品名どうしを、辞書式順序(英和辞書で単語が並んでいる順番)で並べたあと、組み合わせどうしについては以下のようにする。 一つ目の商品名どうしを比較して、辞書式順序で早いほうが先。 同じ場合は、二つ目の商品名どうしを比較して、辞書式順序で早いほうが先。 商品名はスペース一つで区切り、商品の組み合わせは改行一つで区切る。 Sample Input 1 5 2 3 bread milk banana 2 milk cornflakes 3 potato bread milk 4 cornflakes bread milk butter 2 potato bread Sample Output 1 3 bread milk bread potato cornflakes milk Sample Input 2 5 5 3 bread milk banana 2 milk cornflakes 3 potato bread milk 4 cornflakes bread milk butter 2 potato bread Sample Output 2 0
[ { "submission_id": "aoj_0326_9447550", "code_snippet": "#include <bits/stdc++.h>\n\n\nusing namespace std;\n//make -f ../makefile SRC=\n/*\nWA on test 3\n*/\n\n\n//------------------------------------------------------------------------------\nbool DEBUG = false;\nconst int INF = 1000000000;\n\nstatic char ...
aoj_0327_cpp
虫食い算 足し算は筆算で簡単に計算できますが、もし、いくつかの数字が欠けていたら、欠けている数字を埋めるのは簡単でしょうか? 例えば、以下のような筆算において、1から9の数字が一度しか現れないという条件があるとき、CとEのマスに入る数字はいくつになるでしょう? この場合、Cに入るのは8,Eに入るのは5が正解となります。このように、数字がいくつか欠けている演算を虫食い算と呼びます。 1から9の数字が一度しか現れないという条件はそのままで、以下のようにもっとたくさんの数字が欠けていたら、正解となる数字の埋め方は一通りしかないのでしょうか? 実は、必ず一通りに決まるとは限りません。 上の図のような形をした虫食い算の、AからIの各マスの情報が与えられたとき、正しい埋め方が何通りあるかを出力するプログラムを作成せよ。 Input 入力は以下の形式で与えられる。 A B C D E F G H I 1行に、虫食い算の A から I のマスに入っている数字の情報が与えられる。ただし、与えられた値が -1 のときは、そのマスの数字が欠けていることを表す。-1 以外の値は、1 から 9 の整数のいずれかで、それらの間に重複はない。 Output 正しい埋め方が何通りあるかを1行に出力する。 Sample Input 1 7 6 -1 1 -1 9 2 3 4 Sample Output 1 1 Sample Input 2 7 6 5 1 8 9 2 3 4 Sample Output 2 0 Sample Input 3 -1 -1 -1 -1 -1 -1 8 4 6 Sample Output 3 12 Sample Input 4 -1 -1 -1 -1 -1 -1 -1 -1 -1 Sample Output 4 168
[ { "submission_id": "aoj_0327_11004401", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, l, n) for(int i = int(l); i < int(n); i++)\n#define ll long long\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n\ntemplate<class T> bool chmin(T &a, T...
aoj_0328_cpp
貴金属リサイクル 会津特産の貴金属であるアイヅニウムをリサイクルするPCK社は、全国各地にネットワークを持ち、たくさんの回収車でアイヅニウムを集めてきます。この会社は、処理の効率化のために、塊の重さと個数の単位を規格で定めています。 塊の重さには「ボッコ」という単位を使います。 x ボッコのアイヅニウムの重さは 2 x グラムです。宝石で例えると、「カラット」のようなものです。また、塊の個数には「マルグ」という単位を使います。 y マルグは 2 y 個です。1箱に入っている品物の個数である「ダース」のようなものです。ただし、 x と y は 0 以上の整数でなければいけません。 回収車 i は、 a i ボッコの重さのアイヅニウムを b i マルグずつ集めます。こうして集まったアイヅニウムを、炉の中に入れて溶かし、いくつかのアイヅニウムの塊を再生しますが、なるべくアイヅニウムの塊の数が少なくなるようにします。このとき、集めてきたアイヅニウムの重さの合計と、再生してできるアイヅニウムの重さの合計は変わりません。 回収車が集めたアイヅニウムの塊のボッコ単位の重さとマルグ単位の個数が与えられたとき、再生後のアイヅニウムの塊の数が最小になるような結果を求めるプログラムを作成せよ。 Input 入力は以下の形式で与えられる。 N a 1 b 1 a 2 b 2 : a N b N 1行目に、回収車の数 N (1 ≤ N ≤ 100000) が与えられる。続く N 行に、回収車 i が回収したアイヅニウムの塊の、「ボッコ」単位の重さを表す整数 a i (0 ≤ a i ≤ 100000) と「マルグ」単位の個数を表す整数 b i (0 ≤ b i ≤ 100000) が与えられる。 Output 再生した後に得られるアイヅニウムの塊の数が最小になるような、ボッコ単位の重さとマルグ単位の個数を、重さの小さい順に出力する。 Sample Input 1 3 2 1 1 3 2 2 Sample Output 1 3 0 5 0 Sample Input 2 1 100000 2 Sample Output 2 100002 0
[ { "submission_id": "aoj_0328_10945978", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)\n#define REP(i,n) FOR(i,0,n)\n#define ALL(a) a.begin(), a.end()\n#define MS(m,v) memset(m,v,sizeof(m))\ntypedef long long ll;\ntypedef long dou...
aoj_0329_cpp
完全平等二国間貿易 サイバースペースにあるアイヅ国はワカマツ国と情報貿易を行っています。2つの国はお互いに有用なデータを交換することで経済発展を遂げています。博愛と平等、そして何よりも会津地方の古い言葉である、「ならぬことはならぬものです」を国是とする両国は、定期的に貿易状況の調査を行っています。 調査では、バイト単位でアイヅ国から見たデータ流入量から流出量を引いた値を、1ナノ秒ごとに求めた表が与えられます。その表から、値の総和が0になる最長の区間を見つけます。この区間が長いほど、平等性が保たれていると判断します。 貿易状況が記録された表が与えられたとき、値の総和が0になる最長の区間の長さを求めるプログラムを作成せよ。 Input 入力は以下の形式で与えられる。 N d 1 d 2 : d N 1行目に、表に書かれた値の数 N (1 ≤ N ≤ 200000) が与えられる。続く N 行に、表の i 行目に書かれた値を示す整数 d i (-10 9 ≤ d i ≤ 10 9 ) が与えられる。 Output 表から得られる、総和が0になる最長の区間の長さを1行に出力する。そのような区間が存在しない場合、「0」を1行に出力する。 Sample Input 1 5 18 102 -155 53 32 Sample Output 1 3 入力例1では、2行目から4行目までの値の総和が0になるので、最長の区間の長さが3になる。 Sample Input 2 4 1 1 -1 -1 Sample Output 2 4 入力例2では、2行目から3行目の総和が0になるが、1行目から4行目までの値の総和も0になるので、最長の区間の長さが4になる。
[ { "submission_id": "aoj_0329_11030754", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)\n\nint main() {\n\tcin.tie(nullptr);\n\tios_base::sync_with_stdio(false);\n\t\n\tint N;\n\tcin >> N;\n\tvector<ll> d(...
aoj_0333_cpp
線分配置 A大学は今年もプログラミングコンテストを開催する。作題チームの一員であるあなたは、計算幾何学の問題の入力データの作成を担当することになった。あなたが作りたい入力データは、 x 軸または y 軸に平行で、互いに触れ合うことのない線分の集合である。あなたは、次のアルゴリズムに基づいたデータ生成プログラムを開発して、入力データを生成する。 xy 平面上の線分の集合 T を空にする。 次の処理を N 回繰り返す。 x 軸または y 軸に平行な適当な線分 s を作る。 s が T 内のどの線分にも触れない場合は s を T に追加し、触れる場合は s を追加しない。 x 軸または y 軸に平行な N 本の線分を順番に入力し、各線分が平面上に追加されるかどうかを判定するプログラムを作成せよ。 Input 入力は以下の形式で与えられる。 N px 1 py 1 qx 1 qy 1 px 2 py 2 qx 2 qy 2 : px N py N qx N qy N 1行目に線分の数 N (1 ≤ N ≤ 100000) が与えられる。続く N 行に、 i 番目に追加したい線分の情報が与えられる。各行に与えられる4つの整数 px i , py i , qx i , qy i (0 ≤ px i , py i , qx i , qy i ≤ 10 9 ) は、それぞれ i 番目の線分の端点の x 座標、 y 座標、もう一つの端点の x 座標、 y 座標を表す。ただし、線分の長さは1以上である。 Output 各線分について、追加される場合「1」を、追加されない場合「0」を1行に出力する。 Sample Input 1 9 0 2 5 2 1 3 1 7 0 6 3 6 2 4 8 4 4 0 4 5 6 3 6 0 5 6 7 6 8 3 8 7 6 5 11 5 Sample Output 1 1 1 0 1 0 1 1 0 1
[ { "submission_id": "aoj_0333_10203845", "code_snippet": "// AOJ #333\n// Line Segment Arrangement 2025.2.8\n\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() { // 整数の入力\n\tint n = 0, c = gc();\n\tif (c == '-') {\tc = gc();\n\...
aoj_0331_cpp
スケジューラ あなたはユニークなオペレーティングシステム「ウンズグネ15」の開発に取り組んでおり、性能を決定付けるスケジューラの設計に頭を悩ませている。スケジューラとは、実行すべき処理をタスクという単位で表現し、それらをどの順序で実行するかを決定するプログラムである。スケジューラはタスクに1から N の番号をつけて管理する。全てのタスクは K 個の属性 f 1 , f 2 ,..., f K を持ち、各属性にはそれぞれ固有の値が設定されている。ただし、ある2つのタスクについて、対応する属性の値すべてが同じになることはない。 あるタスクには、そのタスクの実行を始める前までに実行を完了していなければならないタスクが与えられることがある。タスクAがタスクBの前に完了していなければならないことを「タスクA → タスクB」と表す。例えば、タスク1 → タスク2、タスク3 → タスク2という関係があれば、タスク2を処理する前にタスク1とタスク3の両方の処理が終わっていなければならない。このような関係をタスク間の依存関係という。ただし、あるタスクから依存関係をたどっていって、そのタスクにたどり着くことはない。 スケジューラは依存関係に従って、実行順序を決定する。しかし、依存関係だけでは順序が一通りに定まらない場合がある。そのような場合は、各タスクが持つ属性の値によって、次に処理するタスクを選択する。 ウンズグネ15のタスクは属性を複数もつため、すべての属性の値を考慮して実行順序を決定する必要がある。そのために、属性を比較する順番を定める評価順序を用いる。評価順序が最も先の属性を比較し、その属性の値が最も大きいタスクを選択する。そのようなタスクが複数ある場合は、評価順序がその次の属性で比較し、以下同様な手順を繰り返す。例えば、以下の3 つの属性を持つ3 つのタスクについて考える。 タスク\属性 f 1 f 2 f 3 X 3 3 2 Y 3 2 2 Z 3 1 3 評価順序が f 1 f 2 f 3 、 f 2 f 1 f 3 、または f 2 f 3 f 1 に設定されている場合は、タスクX が選ばれる。また、評価順序が f 1 f 3 f 2 、 f 3 f 1 f 2 、または f 3 f 2 f 1 に設定されている場合はタスクZ が選ばれる。 ウンズグネ15のスケジューラの特徴は、属性の評価順序が途中で何度でも変更できることである。評価順序は、ある個数のタスクの実行が完了した時点で変更できる。ただし、スケジューラが最初に使う評価順序はあらかじめ決まっている。 各タスクの属性の値、タスクの依存関係、評価順序の変更情報が与えられたとき、タスクを実行する順序を報告するプログラムを作成せよ。 Input 入力は以下の形式で与えられる。 N K f 1,1 f 1,2 ... f 1,K f 2,1 f 2,2 ... f 2,K : f N,1 f N,2 ... f N,K D a 1 b 1 a 2 b 2 : a D b D e 0,1 e 0,2 ... e 0,K R m 1 e 1,1 e 1,2 ...… e 1,K m 2 e 2,1 e 2,2 ...… e 2,K : m R e R,1 e R,2 ...… e R,K 1行目に、タスクの数 N (2 ≤ N ≤ 50000) と、各タスクが持つ属性の数 K (1 ≤ K ≤ 4) が与えられる。続く N 行に、タスク i が持つ属性の値 f i,j (1 ≤ f i,j ≤ 100000) が与えられる。続く1行に、依存関係の個数 D (0 ≤ D ≤ 200000) が与えられる。続く D 行に依存関係 a i → b i (1 ≤ a i , b i ≤ N ) が与えられる。 続く1行に、最初の評価順序 e 0,j (1 ≤ e 0,j ≤ K ) が与えられる。続く1行に、評価順序の変更回数 R (0 ≤ R < N ) が与えられる。続く R 行に、評価順序の変更情報が与えられる。 i 回目の変更情報は、実行が完了したタスクの個数 m i (1 ≤ m i < N ) と評価順序 e i,j (1 ≤ e i,j ≤ K ) からなり、全部で m i 個のタスクの実行が完了した時点で、評価順序を e i,1 , e i,2 ,... , e i,K に変更することを示す。 評価順序の変更情報は以下の条件を満たす。 e i,1 , e i,2 ,..., e i,K 中に同じ値は2つ以上現れない。 i < j のとき、 m i < m j である。 Output スケジューラが処理する順番に、タスクの番号を出力する。 Sample Input 1 5 3 1 5 2 3 8 5 1 2 3 5 5 5 4 8 2 0 1 2 3 2 2 2 3 1 4 3 1 2 Sample Output 1 4 5 2 1 3 Sample Input 2 5 2 1 1 2 1 3 1 4 4 5 2 3 1 4 2 4 2 5 1 2 1 3 2 1 Sample Output 2 3 2 5 1 4
[ { "submission_id": "aoj_0331_11030956", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)\n\nint main() {\n\tcin.tie(nullptr);\n\tios_base::sync_with_stdio(false);\n\t\n\tint N, K;\n\tcin >> N >> K;\n\tint f...
aoj_0332_cpp
消える数列、消えない数列 ただお君は頭の体操をするために、数列を使ったゲームをしています。このゲームでは、はじめに、1から9までの数字がランダムに並んだ列が与えられます。ただお君は、数列からその一部分を消していきます。ルールは、以下の通りです。 数列から、同じ数字が2つ以上並んでいる部分を適当に選ぶ。その部分を含み、連続して現れている同じ数字をすべて消す。 消した部分の右側に数列が残っていた場合は、それを左に詰めて、数列を1つにまとめる。 上の2つの操作を繰り返した結果、すべての数字が消えればゲームクリアとなる。 例えば、上の図のような 1,2,3,3,2,2,1,2,2 という数列の場合、 左から数えて、3番目、4番目の3を消すと 1,2,2,2,1,2,2 左から数えて、2番目から4番目の2を消すと 1,1,2,2 左から数えて、1番目と2番目の1を消すと 2,2 左から数えて、1番目と2番目の2を消すと、ゲームクリアとなります。 ただし、どのように数字を消してもクリアできない数列があります。たとえば、1,2,3,3,1,2 や 1,2,3,1,2,3 などの数列です。短い数列であれば、ただお君でもクリアできるかどうかがすぐに分かり、クリアできないと分かれば違う数列にチャレンジできますが、長い数列になるとそう簡単にはいきません。 与えられた数列が上のゲームをクリアできるかどうか判定するプログラムを作成せよ。 Input 入力は以下の形式で与えられる。 N c 1 c 2 ... c N 1行目の N (1 ≤ N ≤ 100) は、数列の長さを表す整数である。2行目には1つの空白で区切られた N 個の整数 c i (1 ≤ c i ≤ 9) が与えられる。 c i は数列の i 番目の数字を示す。 Output 上に示されたルールで数列を消すことができる場合は「yes」、できない場合は「no」を出力する。 Sample Input 1 8 1 2 3 3 2 1 2 2 Sample Output 1 yes Sample Input 2 7 1 2 2 1 1 3 3 Sample Output 2 yes Sample Input 3 16 9 8 8 7 7 6 5 4 4 5 1 1 2 2 3 3 Sample Output 3 no Sample Input 4 5 1 1 2 2 1 Sample Output 4 yes
[ { "submission_id": "aoj_0332_9099687", "code_snippet": "#pragma GCC optimize(\"Ofast\")\n#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\nll myRand(ll B) { return (ull)rn...
aoj_0334_cpp
あみだくじ PCK 君はみんなでゲーム大会をしています。このゲーム大会では、大会の最後にあみだくじで順位を入れ替えます。大会には N 人のプレイヤーが参加しており、あみだくじには N 本の縦棒があります。 あみだくじは、図のように N - 1 段の部品からできており、それぞれ 1 から N -1 の番号が割り当てられています。各部品は、あみだくじの一部を横方向に切り取った部分です。各部品にはいくつかの横棒が引かれていますが、部品の中の横棒はすべて同じ高さにあります。横棒同士がつながることはありません。 大会の最後に、順位の高い人から右から左の順に縦棒が割り当てられます。PCK 君は現時点で最下位なので、左端からスタートです。例えば、上図の組み立て方では、6位だったPCK 君は、このあみだくじによって4位(右から4番目の棒)に浮上することができます。 このゲームでは、最下位の人にあみだくじを組み立てる権利が与えられます。PCK 君はうまくあみだくじの部品の順番を決めて、逆転優勝を狙っています。ただし、部品を回転することはできません。 (※補足:あみだくじのたどり方について) あみだくじのある縦棒の上端から出発して上から下へ進む。ただし、横棒がある地点ではその横棒でつながった別の縦棒に移動する。これを、縦棒の下端にたどり着くまで繰り返す。 ゲームの参加人数とあみだくじの部品の情報を入力し、PCK 君が優勝できるかどうか判定するプログラムを作成せよ。優勝できる場合、そのあみだくじの部品の並びを1つ出力せよ。ただし、そのような並べ方が複数ある場合は、与えられた部品の番号で辞書順最小のものを出力せよ。 Input 入力は以下の形式で与えられる。 N b 1,1 b 1,2 ... b 1,N−1 b 2,1 b 2,2 ... b 2,N−1 : b N−1,1 b N−1,2 ... b N−1,N−1 1行目に大会の参加者数 N (2 ≤ N ≤ 500) が与えられる。続く N -1 行に i 番目の部品の横棒の情報が与えられる。 b i,j が 1 であるとき、 i 番目の部品の、左から j 本目の縦棒から j +1 番目の縦棒へ横棒が引かれていることを表す。 b i,j が 0 であるとき、 i 番目の部品の、左から j 本目の縦棒から j +1 番目の縦棒へ横棒は引かれていないことを表す。 b i,j が 1 であるとき、 b i,j+1 が 1 となるような部品は与えられない。また、横棒の総数は 10000 を越えない。 Output PCK 君が優勝できる場合,1行目に「yes」と出力する。続く N -1 行に、あみだくじの上から順に、部品の番号の並びを出力する。そのような並びが複数ある場合、辞書順最小である並びを出力する。PCK 君が優勝できない場合、1行に「no」と出力する。 Sample Input 1 6 1 0 0 0 1 1 0 1 0 1 0 1 0 1 0 0 0 0 1 0 0 1 0 0 1 Sample Output 1 yes 1 3 2 4 5 Sample Input 2 5 0 1 0 1 0 1 0 1 1 0 1 0 1 0 0 1 Sample Output 2 yes 4 1 3 2 4 1 3 2 と 4 2 3 1 の2通りの組み立て方が可能だが、辞書順で小さい方の 4 1 3 2 を出力する。 Sample Input 3 5 1 0 0 1 0 1 0 1 1 0 0 0 0 1 0 1 Sample Output 3 no
[ { "submission_id": "aoj_0334_10853299", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <climits>\n#include <cfloat>\n#include <cstring>\n#include <map>\n#include <utility>\n#include <set>\n#include <iostream>\n#include <memory>\n#include <string>\n#include <vector>\n#incl...
aoj_0330_cpp
プログラム停止判定 皆さんは、苦労して作ったプログラムを実行してみたら、無限ループになってしまった経験はありませんか? プログラムの実行が停止するかどうかを、実行しなくても事前に判定できると便利ですよね。 残念ながら、皆さんがふだん使っているプログラミング言語では、あらゆるプログラムに対してそのような判定をすることは不可能です。しかし、それよりもはるかに計算能力の低いプログラミング言語なら、その言語で書いたプログラムが停止するかどうかを判定するプログラムを書ける場合があります。 TinyPowerというプログラミング言語を考えます。この言語のプログラムは行の並びです。プログラムの各行には、先頭に行番号を書き、その後ろに文を一つ書きます。この言語で書ける文の種類は以下の通りです。 文の種類 動作 ADD var 1 var 2 var 3 変数 var 2 の値と var 3 の値を加算した結果を変数 var 1 に代入する ADD var 1 var 2 con 変数 var 2 の値と定数 con を加算した結果を変数 var 1 に代入する SUB var 1 var 2 var 3 変数 var 2 の値から var 3 の値を減算した結果を変数 var 1 に代入する SUB var 1 var 2 con 変数 var 2 の値から定数 con を減算した結果を変数 var 1 に代入する SET var 1 var 2 変数 var 2 の値を変数 var 1 に代入する SET var 1 con 定数 con を変数 var 1 に代入する IF var 1 dest 変数 var 1 の値が0でないときだけ、行番号 dest にジャンプする HALT プログラムを停止させる 行番号は正の整数で、プログラム中に同じ行番号が2つ以上現れることはありません。変数は英小文字一文字で表し、定数と変数の値は整数です。変数の宣言は不要で、変数の初期値は0です。 プログラムの実行は先頭の文から始まり、並んでいる順に文が実行されます。ただし、上の表に書かれたように、IF文の変数の値が0でないときは、変数の後ろに書かれた行番号で指定される行にジャンプし、その行に書かれた文から実行を続けます。プログラムは以下のときに停止します。 HALT文を実行したとき。 負の整数または16以上の整数を変数に代入しようとしたとき(変数の値は更新されない)。 プログラムに現れない行番号にジャンプしようとしたとき。 プログラムの最後の文を実行した後、そこからどの行にもジャンプしないとき。 TinyPowerのプログラムが与えられたとき、それが停まるかどうかを判定するプログラムを作成せよ。 Input 入力は以下の形式で与えられる。 N stmt 1 stmt 2 : stmt N 1行目にプログラムの行数 N (1 ≤ N ≤ 50) が与えられる。続く N 行に、TinyPowerプログラムの文 stmt i が与えられる。 stmt i は、以下のいずれかの形式で与えられる。 line ADD var 1 var 2 var 3 または line ADD var 1 var 2 con または line SUB var 1 var 2 var 3 または line SUB var 1 var 2 con または line SET var 1 var 2 または line SET var 1 con または line IF var 1 dest または line HALT line , dest (1 ≤ line , dest ≤ 1000) は行番号、 var j (英小文字1文字)は変数、 con (0 ≤ con ≤ 15) は定数を表す。 stmt i 中の区切りは空白1文字とする。なお、プログラム中に変数は必ず1つ以上現れ、異なる変数名は5つまでしか現れないものとする。 Output プログラムが停止するときは、プログラムに現れる変数の結果を、変数名の辞書順に改行区切りで出力し、停止しないときは「inf」を出力する。変数の結果は、変数名と変数の値を「=」で区切って出力する。 Sample Input 1 6 10 SET c 1 20 SET i 5 100 ADD s s i 110 SUB i i c 120 IF i 100 200 HALT Sample Output 1 c=1 i=0 s=15 入力例1は、1から5までの整数の和を計算し、その結果を変数sに格納したあと、HALT文の実行で停止する。 Sample Input 2 3 10 SET c 1 120 IF c 10 20 HALT Sample Output 2 inf 入力例2は、行番号10でcに1を代入し、次の行番号120のIF文で行番号10に戻ることを繰り返すので、停止しない。 Sample Input 3 3 111 SET c 1 12 SUB c c 2 777 SET a 4 Sample Output 3 a=0 c=1 入力例3は、行番号111でcに1を代入し、次の行番号12でcに-1を代入しようとするので、停止する。このときcの値は-1に更新されない。行番号777は実行されないので、aの値は初期値0のままである。
[ { "submission_id": "aoj_0330_10946325", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <climits>\n#include <cfloat>\n#include <cstring>\n#include <map>\n#include <utility>\n#include <set>\n#include <iostream>\n#include <memory>\n#include <string>\n#include <vector>\n#incl...
aoj_0350_cpp
実数既約分数化 実数のうち、小数部が循環するものと有限桁のものは分数として表すことができます。 分数で表すことができる実数が与えられたとき、その実数と等しい既約分数(それ以上約分できない分数)を出力するプログラムを作成せよ。 Input 入力は以下の形式で与えられる。 str 1行に、変換したい実数を表す文字列 str が与えられる。実数の値は 0 より大きい。文字列は数字か「.」、「(」、「)」を含む、長さが 3 以上 8 以下の文字列である。「.」は小数点、「(」は数字の循環の始まり、「)」は数字の循環の終わりを示す。整数部にも小数部にも、必ず1桁以上の数字が与えられるとする。ただし、循環小数が与えられた場合、文字列は以下の条件を満たす。 循環の始まりと終わりのペアは、小数点の右側に一度だけ現れる。 循環の終わりを示す「)」は、文字列の末尾に現れる。 循環の始まりと終わりの間には、必ず1桁以上の数字が与えられる。 Output 実数を既約分数で表した形式(分子の整数に続けて「/」区切りで分母の整数を並べたもの)で出力する。 Sample Input 1 0.(3) Sample Output 1 1/3 Sample Input 2 1.0 Sample Output 2 1/1 Sample Input 3 5.2(143) Sample Output 3 52091/9990 Sample Input 4 0.0739 Sample Output 4 739/10000
[ { "submission_id": "aoj_0350_3592199", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <cstring>\n#include <climits>\n#include <vector>\n#include <map>\n#include <set>\n#include <list>\n#include <stack>\n#include <queue>\n#include <algorithm>\n#include <iostream>\n#include...
aoj_0348_cpp
必勝7並べ トランプを使ったゲームに「7並べ」があります。ここではそれを簡単にしたゲームを考えます。1から13の番号がそれぞれ書かれた13枚のカードを使って7並べをします。対戦は、2者だけで次のようにゲームを進めます。 「場」に7のカードを置きます。 2者には、残りのカードがランダムに6枚ずつ配布されます。 先手の手持ちのカードのうち、場にあるカードの番号と連続する番号のカードがあれば、そのうちの1枚を場に置きます。プレイヤーはカードが置ける場合には必ず置かなければいけません。無いときに限り、カードを出さずに相手の番になります。 後手も同じ要領で、手持ちのカードを場に置きます。 手順3と4を繰り返して、一方の手持ちのカードがなくなるまで続けます。先に手持ちのカードをすべて場に置けた方が勝者となります。 先手のカードの番号が与えられたとき、後手がどのようにカードを出してきても、先手が勝つ手順が少なくとも一つあるかを判定して出力するプログラムを作成せよ。 Input 入力は以下の形式で与えられる。 N game 1 game 2 : game N 1行目には、ゲームを行う回数 N (1 ≤ N ≤ 100) が与えられる。続く N 行に、 i 回目のゲームの情報 game i が与えられる。各 game i は、以下の形式で与えられる。 f 1 f 2 f 3 f 4 f 5 f 6 f j (1 ≤ f j ≤ 13, f j ≠ 7) は先手に配られるカードの番号である。ただし、同じ行に番号が重複して現れることはない( j ≠ k について f j ≠ f k )。 Output 各ゲームについて、後手がどのようにカードを出してきても、先手が勝つ手順が少なくとも一つある場合「yes」、そうでない場合「no」と1行に出力する。 Sample Input 1 5 1 2 3 4 5 6 1 3 5 6 8 4 1 2 3 4 5 8 1 2 4 5 10 11 1 2 3 6 9 11 Sample Output 1 yes yes no yes no
[ { "submission_id": "aoj_0348_3683470", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <set>\nusing namespace std;\n\nbool judge(vector<int> A, vector<int> B){\n do{\n int i = 0, j = 0;\n int mi = 7, ma = 7;\n while(i < 6 and j < 6){\n if(A[i] == mi-1 o...
aoj_0343_cpp
Programming Contest II Every year, Byakko University holds a programming contest. The contest starts with all teams having zero points, and points are added according to their answers. In this contest, the teams are ranked in order of their scores. When the total number of teams is N , each team is assigned a number from 1 to N . If the teams have the same score, the team with the smaller number will be ranked higher. Byakko University is developing a ranking system for spectators to make the contest more exciting. As a development team member, you are asked to make a program that is part of this system. Make a program to update the scores, and report the numbers and scores of the teams in a given rank, according to the given command. Input The input is given in the following format. N C command 1 command 2 : command C In the first line, the number of teams N (2 ≤ N ≤ 100000) and the number of commands C (1 ≤ C ≤ 100000) are given. In the following C lines, commands are given. Each command is given in the following format. 0 t p or 1 m When the first number is 0, it indicates an update command, and when it is 1, it indicates a report command. In the update command, add the score p (1 ≤ p ≤ 10 9 ), given as an integer, to the team with the given number t (1 ≤ t ≤ N ). In the report command, report the number and score of the team in a given rank m (1 ≤ m ≤ N ). However, the report command must appear at least once. Output For each report command, output the number and score of the team in a given rank in a line, separated by space. Sample Input 1 3 11 0 2 5 0 1 5 0 3 4 1 1 1 2 1 3 0 3 2 1 1 0 2 1 1 2 1 3 Sample Output 1 1 5 2 5 3 4 3 6 3 6 1 5 Sample Input 2 5 2 1 1 1 2 Sample Output 2 1 0 2 0
[ { "submission_id": "aoj_0343_10867159", "code_snippet": "#include <bits/stdc++.h>\n#include <iomanip>\n#include <numbers>\n#include <cassert>\n#include <numeric>\n#ifdef _MSC_VER\n#include <intrin.h>\n#endif\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\nusing pint = pair<int...
aoj_0341_cpp
Repeated Spell After discovering and investigating the ancient nation of Iwashiro, our researchers finally found a temple in the center of Iwashiro. The temple housed a stone tablet dedicated to the god of Iwashiro. On the tablet, there are two strings of text, one sentence and one spell. In Iashiro, The number of times a spell appears in a sentence has an important meaning. However, even if the spell appears sporadically in the sentence, it is considered to appear once. For example, if the sentence is "abab" and the spell is "ab", then "ab" appears three times in "abab", including non-consecutive occurrences (three patterns: ab ab, ab ab , a ba b .) Given a sentence and a spell, make a program to output the number of times the spell appears in the sentence. Input The input is given in the following format. t b In the first line, the string t represents the text written on the tablet is given. In the second line, the string b represents the spell written on the tablet is given. Both strings are made up of only lowercase letters and are between 1 and 1000 characters long. Output Output the number of times the spell appears in a sentence in a line. However, the value can be very large, so output the remainder divided by 1,000,000,007 instead. Sample Input 1 abab ab Sample Output 1 3 Sample Input 2 aaaabaaaabaaaabaaaab aaaaa Sample Output 2 4368 Sample Input 3 data structure Sample Output 3 0
[ { "submission_id": "aoj_0341_9605582", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define int long long\n\nint mod=1000000007;\n\nint dp[1009][1009];\n\nsigned main(){\n\tdp[0][0]=1;\n\tstring s,t;cin>>s>>t;\n\tint a=s.size(),b=t.size();\n\n\tfor(int i=0;i<a;i++){\n\t\tfor(int j=0;j<=...
aoj_0347_cpp
有理式最大化 N 個の異なる自然数が与えられる。その中から異なる4つを選んで、それらを $A$, $B$, $C$, $D$ としたとき、次の数式 $\frac{A + B}{C - D}$ の最大値を求めたい。 N 個の異なる自然数が与えられたとき、その中から異なる4つを選んで、上の数式の最大値を求めるプログラムを作成せよ。 Input 入力は以下の形式で与えられる。 N a 1 a 2 ... a N 1行目に自然数の個数 N (4 ≤ N ≤ 1000) が与えられる。2行目に各自然数の値 a i (1 ≤ a i ≤ 10 8 ) が与えられる。ただし、同じ自然数が重複して現れることはない( i ≠ j について a i ≠ a j )。 Output 与えられた N 個の自然数に対して、上の数式の最大値を実数で出力する。ただし、誤差がプラスマイナス 10 -5 を超えてはならない。 Sample Input 1 10 1 2 3 4 5 6 7 8 9 10 Sample Output 1 19.00000 入力例1では、$A=9$, $B=10$, $C=2$, $D=1$ などの組み合わせで最大になる。 Sample Input 2 5 22 100 42 3 86 Sample Output 2 9.78947 入力例2では、$A=100$, $B=86$, $C=22$, $D=3$ などの組み合わせで最大になる。 Sample Input 3 6 15 21 36 10 34 5 Sample Output 3 18.00000 入力例3では、$A=21$, $B=15$, $C=36$, $D=34$ などの組み合わせで最大になる。 Sample Input 4 4 100000 99999 8 1 Sample Output 4 28571.285714
[ { "submission_id": "aoj_0347_11017140", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\nsigned main(){\n int n;\n cin>>n;\n vector<int> s(n);\n for(int i=0;i<n;i++) cin>>s[i];\n sort(s.begin(),s.end());\n multiset<int> st;\n for(int i=0;i<n;i++) s...
aoj_0344_cpp
Game Strategy You have found an old board game in the club room of your programming club. It looks interesting, so you decide to play it. The game consists of M events, and you have to capture event i at time t i . However, your strength must be at least s i at that time. Otherwise, the game is over. At the beginning of the game (time 0), your strength is 0, but you can increase it by buying items. Your money is also 0 at the beginning of the game, but it increases by 1 per unit of time. There are N items on the board, numbered 1 to N . Item i costs v i , and buying it increases your strength by h i . You can buy as many items as you want at any time, as long as you have enough money. However, you must choose the item with the lowest number among the remaining items. Each item will be disappeared after it is bought. You can also buy multiple items in a row at the same time, and get a bonus equal to the sum of the differences in h i of the adjacent items. For example, if you buy items 1, 2, 3 at the same time, your strength will increase by | h 1 - h 2 | + | h 2 - h 3 | in addition to h 1 + h 2 + h 3 . You want to maximize the amount of money you have, after capturing all the events. Make a program to output the maximum amount of money you have after capturing all the events, using the information of items and events given in input. Input The input is given in the following format. N M v 1 h 1 v 2 h 2 : v N h N t 1 s 1 t 2 s 2 : t M s M In the first line, the number of items N (1 ≤ N ≤ 3000) and the number of events M (1 ≤ M ≤ 1000) are given. In the following N lines, the cost v i and the increase in strength h i (1 ≤ v i , h i ≤ 100000) of the item i are given. In the following M lines, the time t i and the condition s i (1 ≤ t i , s i ≤ 100000) of the event i are given. However, if i < j , then t i < t j . All inputs are given as integers. Output Output the maximum amount of money you have in a line. However, if it is not possible to capture the events, output -1. Sample Input 1 5 4 3 3 2 1 1 5 4 2 2 6 4 1 8 2 10 4 12 17 Sample Output 1 2 Sample Input 2 5 4 3 3 2 1 1 5 4 2 2 6 4 1 8 2 10 4 12 30 Sample Output 2 -1
[ { "submission_id": "aoj_0344_10862866", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ld=long double;\n#define fi first;\n#define se second;\n#define i128 __int128_t\n#define all(x) x.begin(),x.end()\n#define rep(i,n) for(int i=0;i<(int)(n);++i)\n#define si(a) (long)(a.size())\ntemp...
aoj_0349_cpp
環状すごろく ユキさんは、子供会の催しでみんなで遊べるようにすごろくを作りました。このすごろくでは、環状にマスが並んでいて、それぞれのマスには1以上の整数が書いてあります。 プレイヤーは出発点としてどこかのマスを選んで自分の駒を置きます。そのマスに書いてある数だけ、時計回りに駒を進めます。止まったマスに書いてある数だけ、再び時計回りに駒を進めます。これを繰り返して、出発点に選んだマスの上で駒が止まったら「あがり」です。 実際には、マスの選び方によっては絶対に「あがり」にならない場合もあります。ユキさんは、このすごろくで「あがり」にたどり着けるマスの個数を数えようとしています。 すごろくの情報を入力とし、「あがり」にたどり着けるマスの個数を報告するプログラムを作成せよ。 Input 入力は以下の形式で与えられる。 N a 1 a 2 ... a N 1行目にすごろくに含まれるすべてのマスの個数 N (1 ≤ N ≤ 100000) が与えられる。2行目に、それぞれのマスに書かれた数 a i (1 ≤ a i ≤ 10 9 ) が、時計回りに順番に与えられる。 Output 「あがり」にたどり着けるマスの個数を1行に出力する。 Sample Input 1 3 1 1 1 Sample Output 1 3 Sample Input 2 3 1 1 2 Sample Output 2 2 Sample Input 3 8 2 3 7 3 3 3 4 4 Sample Output 3 6
[ { "submission_id": "aoj_0349_11030870", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define int long long\n#define ll long long\n#define ld long double\n#define vec vector\n#define vi vec<int>\n#define vll vec<ll>\n#define vld vec<ld>\n#define pq priority_queue\n#define rep(i, l, r) fo...
aoj_0351_cpp
静かな町 アイヅ国では、毎年、駅伝大会が開催されています。アイヅ国には N 個の町が点在しており、それぞれ 1 から N までの番号が付いています。いくつかの町の間は、互いに直接行き来できる道でつながっています。また、どの町の間もいくつかの道を辿って行き来ができます。大会のコースは、次のようにして決めます。 2つの町のすべての組み合わせについて、最短経路の距離を求める。 それらの中で、最短経路の距離が最大になるような2つの町を、スタートの町とゴールの町とする。町の組み合わせが複数ある場合、そのうちのどれか一つを選ぶ。 選ばれたスタートの町とゴールの町の間の最短経路を大会のコースとする。最短経路が複数ある場合、そのうちのどれか一つを選ぶ。 ヤマト国から来たお坊さんのトクイツは、アイヅ国のできるだけ静かな町に新しいお寺を開きたいと考えています。そのため、駅伝大会のコースに使われる可能性がない町を知りたがっています。 アイヅ国の町を結ぶ道の情報が与えられたとき、駅伝大会のコースに使われる可能性がない町を求めるプログラムを作成せよ。 Input 入力は以下の形式で与えられる。 N R s 1 t 1 d 1 s 2 t 2 d 2 : s R t R d R 1行目に町の数 N (2 ≤ N ≤ 1500) と、町の間を直接つなぐ道の数 R (1 ≤ R ≤ 3000) が与えられる。続く R 行に2つの町を双方向に直接つなぐ道が与えられる。 s i と t i (1 ≤ s i < t i ≤ N ) は i 番目の道がつなぐ2つの町の番号を表し、 d i (1 ≤ d i ≤ 1000) はその道の長さを表す。ただし、どの2つの町についても、それらを直接つなぐ道は高々1本とする。 Output 与えられた道の情報に対して、駅伝大会のコースになることがない町をすべて出力する。1行目に駅伝大会のコースになることがない町の数 M を出力する。続く M 行に、そのような町の番号を昇順で出力する。 Sample Input 1 4 5 1 2 2 1 3 2 2 3 1 2 4 2 3 4 1 Sample Output 1 1 2 Sample Input 2 7 11 1 2 2 1 3 1 2 4 2 2 5 2 2 6 1 3 4 1 3 5 1 3 6 1 4 7 2 5 7 2 6 7 1 Sample Output 2 3 2 4 5
[ { "submission_id": "aoj_0351_11017195", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\nsigned main(){\n int N,M;\n cin>>N>>M;\n vector<vector<pair<int,int>>> G(N);\n for(int i=0;i<M;i++){\n int a,b,c;\n cin>>a>>b>>c;\n a--;\n b...
aoj_0360_cpp
Reservation System The supercomputer system L in the PCK Research Institute performs a variety of calculations upon request from external institutes, companies, universities and other entities. To use the L system, you have to reserve operation time by specifying the start and end time. No two reservation periods are allowed to overlap each other. Write a program to report if a new reservation overlaps with any of the existing reservations. Note that the coincidence of start and end times is not considered to constitute an overlap. All the temporal data is given as the elapsed time from the moment at which the L system starts operation. Input The input is given in the following format. a b N s_1 f_1 s_2 f_2 : s_N f_N The first line provides new reservation information, i.e., the start time a and end time b (0 ≤ a < b ≤ 1000) in integers. The second line specifies the number of existing reservations N (0 ≤ N ≤ 100). Subsequent N lines provide temporal information for the i -th reservation: start time s_i and end time f_i (0 ≤ s_i < f_i ≤ 1000) in integers. No two existing reservations overlap. Output Output "1" if the new reservation temporally overlaps with any of the existing ones, or "0" otherwise. Sample Input 1 5 7 3 1 4 4 5 7 10 Sample Output 1 0 Sample Input 2 3 7 3 7 10 1 4 4 5 Sample Output 2 1
[ { "submission_id": "aoj_0360_9615818", "code_snippet": "#include<bits/stdc++.h>\n#include <iostream>\nusing namespace std;\nint main(void){\n\tint a,b,n,s[101],f[101],cnt=0;\n\tcin>>a>>b>>n;\n\tfor(int i=0;i<n;i++){\n\t\tcin>>s[i];\n\t}\n\tfor(int i=0;i<n;i++){\n\t\tcin>>f[i];\n\t}\n\tfor(int i=a+1;i<b;i++)...
aoj_0352_cpp
勢力の予想 信夫くんと静夫くんは長方形の島で領地を取り合うゲームをしています。下の図①のように、島全体は格子状に区切られた正方形の区画でできており、区画にはそこから生じる損得が整数で示されています。 このゲームでは1つの駒を動かして領地の境界線を決めていきます。ゲーム開始時、駒は島の北西端にあります(図①)。この駒を島の南東端まで動かしていったときの駒の軌跡が領地の境界線となります。二人のプレーヤーは交互に駒を動かします。駒は南隣の格子点か東隣の格子点にのみ動かすことができます(図②)。駒が島の端に到達した場合は、南か東のうち動かせる方向に動かします。駒が南東端に到達したらゲーム終了です。 ゲーム終了後の境界線から北東側の領域が先攻プレーヤーの領域、南西側の領域が後攻プレーヤーの領 域です(図③)。各プレーヤーの領域内に含まれる、区画から生じる損得の合計がそのプレーヤーのス コアとなります。二人ともゲームにはかなり慣れており、最終的に自分のスコアから相手のスコアをひ いた値が一番大きくなるように正確に駒を動かします。 島の大きさとそれぞれの区画から生じる損得が与えられたとき、ゲーム終了時の結果を計算するプログラムを作成せよ。結果は、信夫くんと静夫くんのスコアの差の絶対値とする。 Input 入力は以下の形式で与えられる。 W H s 1,1 s 1,2 ... s 1,W s 2,1 s 2,2 ... s 2,W : s H,1 s H,2 ... s H,W 1行目に島に含まれる東西方向と南北方向の区画の数 W , H (1 ≤ W , H ≤ 1000) が与えられる。続く H 行に i 行 j 列目の区画から生じる損得 s i,j (-1000 ≤ s i,j ≤ 1000) が与えられる。ただし、 i の値が増加する方向を南、 j の値が増加する方向を東とする。 Output 信夫くんと静夫くんのスコアの差の絶対値を1行に出力する。 Sample Input 1 2 1 -2 1 Sample Output 1 1 Sample Input 2 2 2 2 -3 3 -1 Sample Output 2 3 Sample Input 3 5 4 5 3 2 -5 2 2 -4 2 8 -4 2 3 -7 6 7 3 -4 10 -3 -3 Sample Output 3 5
[ { "submission_id": "aoj_0352_11017206", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\nsigned main(){\n int H,W;\n cin>>W>>H;\n vector<vector<int>> A(H,vector<int>(W));\n vector<vector<int>> R(H+1,vector<int>(W));\n for(int i=0;i<H;i++)for(int j=0;j<W;...
aoj_0356_cpp
夕暮れ AIZU 国の夕暮れ時は、スマートフォンを西の空へ構えて立ち止まる観光客で賑わう。AIZU 国は無数のビルが建ち並ぶ大都市であり、ビルの谷間が長く連なる西の空には、ビルのシルエットとそれらから漏れ出す太陽の光による絶景が広がる。 AIZU 観光協会の若松氏によると、太陽を表す円が、その面積のちょうど半分だけ遮られているとき、格段の絶景になるという。 図のように、西の空を、地平線を x 軸、太陽の中心の軌跡を y 軸とするような x - y 平面で表し、太陽を半径 R の円、それぞれのビルのシルエットを長方形とする。 各ビルのシルエットの底辺は x 軸上にあり、太陽は十分高い位置から地平線に対して垂直に沈んでいく。太陽はビルのシルエットあるいは地平線を上端とする地面に遮られ、やがて地平線の下へ消えてゆく。 太陽の半径と各ビルのシルエットの情報が与えられたとき、太陽の面積のちょうど半分が遮られるような、最も高い太陽の高さ(中心の y 座標)を求めるプログラムを作成せよ。 Input 入力は以下の形式で与えられる。 N R x 1 w 1 h 1 x 2 w 2 h 2 : x N w N h N 1行目にビルの数 N (0 ≤ N ≤ 100) と太陽を表す円の半径 R (1 ≤ R ≤ 100) が与えられる。続く N 行に i 番目のビルのシルエットの左下角の x 座標 x i (-100 ≤ x i ≤ 100, x i < x i+1 )、幅 w i (1 ≤ w i ≤ 100)、高さ h i (1 ≤ h i ≤ 100) が与えられる。入力はすべて整数で与えられる。 入力は以下の条件を満たす。 ビルのシルエットが重なることはない( x i + w i ≤ x i+1 )。 2つの隣接するビルのシルエットの高さの差は、 R を超えない。 ビルの左側( x 軸の負の方向)または右側( x 軸の正の方向)に隣接するビルがない場合、その高さは R を超えない。 Output 太陽の高さ(円の中心の y 座標)を1行に出力する。ただし、誤差がプラスマイナス 10 -6 を超えてはならない。 Sample Input 1 0 2 Sample Output 1 0.00000000 Sample Input 2 3 2 -2 1 1 -1 1 2 0 2 1 Sample Output 2 1.25065774
[ { "submission_id": "aoj_0356_10203506", "code_snippet": "// AOJ #356\n// Evening 2025.2.8\n\n#include <bits/stdc++.h>\nusing namespace std;\nconst double eps = 1e-10;\nconst double PI = acos(-1.0);\n \ndouble groundIntersection(double Y, double R) {\n double circleArea = PI * R * R;\n if(Y >= R) retur...
aoj_0354_cpp
蟻 大きなチェス盤上に、それぞれ1から N までの番号が割り振られた N 匹の蟻がいます。図のように、チェス盤は H×W のマスからなる長方形で、北西角を白として、白マスと黒マスが交互に並んでいます。 最初、どの蟻もチェス盤のマスの中にいて、東または南を向いています。1つのマスの中に2匹以上の蟻がいることはありません。 今、蟻たちが一斉に動き出します。すべての蟻は 1 単位時間に向いている方向のマスに1つ移動します。ただし、移動先がチェス盤の外の場合、落下してチェス盤から姿を消します。 チェス盤上で2匹の蟻が同じマスに入ると、それらの蟻は以下のような行動をとります: そのマスの色が白ならば、東方向に進んでいた蟻は南方向へ、南方向に進んでいた蟻は東方向へ向きを変える。 そのマスの色が黒ならば、それぞれの蟻は進む方向を維持する。 チェス盤の大きさと蟻の情報が与えられたとき、落下する順番に蟻の番号を報告するプログラムを作成せよ。ただし、同じ時刻に複数の蟻が落下する場合は、番号がより小さい方を先に報告する。 Input 入力は以下の形式で与えられる。 W H N x 1 y 1 d 1 x 2 y 2 d 2 : x N y N d N 1行目にチェス盤の東西方向のマスの数 W と南北方向のマスの数 H (2 ≤ W , H ≤ 10 9 ) と蟻の数 N (1 ≤ N ≤ 200000) が与えられる。続く N 行に i 番目の蟻の東西方向の位置 x i (1 ≤ x i ≤ W )、南北方向の位置 y i (1 ≤ y i ≤ H )、向きを表す文字 d i (東向きの場合「E」、南向きの場合「S」)が与えられる。ここで、チェス盤の北西角のマスを (1,1)、 x が増加する方向を東、 y が増加する方向を南とする。 Output 落下する順番に、蟻の番号を1行ずつ出力する。 Sample Input 1 3 3 3 2 1 S 1 2 E 2 2 E Sample Output 1 3 1 2 Sample Input 2 5 4 3 3 1 S 2 2 E 1 3 E Sample Output 2 2 3 1
[ { "submission_id": "aoj_0354_10865835", "code_snippet": "#include <bits/stdc++.h>\n#define int long long\n#define INF 2e+9\n#define mp make_pair\nusing namespace std;\n\ntypedef pair<int,int> P;\ntypedef pair<int,P> PP;\n\nint w,h,n,x[200000],y[200000],ans[200000];\nstring d[200000];\nvector<int> vec[200000...
aoj_0353_cpp
ソート 高校入学後、プログラミング部に入部したタケ子さんは、次第にアルゴリズムの面白さにのめりこんでいきました。いまでは、2年生になったらプログラミング甲子園に出場してみたいと考えています。 あるとき、ソート・アルゴリズムについて学んだタケ子さんは、ソート・アルゴリズムを自分で設計してみました。タケ子さんが作ったソート・アルゴリズムでは、入力として要素の間に重複のない、1個以上の自然数からなる列が与えられたとき、以下の処理を実行します。 はじめに、列の先頭の要素を選ぶ。 選んだ要素の直前に要素があるとき、選んだ要素とその直前の要素を比べる。直前の要素のほうが大きいなら、それを列の末尾の直後に移動させる(図)。この操作を、選んだ要素が列の先頭になるか、選んだ要素よりその直前の要素の方が小さくなるまで続ける。 選んだ要素が列の末尾なら終了。そうでなければ、選んだ要素の直後の要素を新たに選び、2 へ戻る。 タケ子さんはこのアルゴリズムがどのくらいの計算時間を必要とするか見積もるために、要素を列の末尾の直後に移動させる操作の回数を数えることにしました。 列の情報を入力とし、要素を列の末尾の直後に移動させる操作の回数を報告するプログラムを作成せよ。 Input 入力は以下の形式で与えられる。 N a 1 a 2 ... a N 1行目に列に含まれる要素の個数 N (1 ≤ N ≤ 200000) が与えられる。2行目に列の要素 a i (1 ≤ a i ≤ 10 9 ) が先頭から順番に与えられる。要素 a i に重複はない。 Output 要素を列の末尾の直後に移動させる操作の回数を1行に出力する。 Sample Input 1 6 1 3 6 5 8 2 Sample Output 1 10 入力例1では、要素の移動が行われるたびに、以下のように列が変化していく。 0回目: 1 3 6 5 8 2 1回目: 1 3 5 8 2 6 2回目: 1 3 5 2 6 8 3回目: 1 3 2 6 8 5 4回目: 1 2 6 8 5 3 5回目: 1 2 6 5 3 8 6回目: 1 2 5 3 8 6 7回目: 1 2 3 8 6 5 8回目: 1 2 3 6 5 8 9回目: 1 2 3 5 8 6 10回目: 1 2 3 5 6 8 Sample Input 2 4 4 3 2 1 Sample Output 2 6
[ { "submission_id": "aoj_0353_11019202", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <deque>\nusing namespace std;\n\nint find_parent(vector<int>& parent, int x) {\n if (parent[x] < 0) return x;\n return parent[x] = find_parent(parent, parent[x]);\n}\n\nvoid ...
aoj_0362_cpp
Trampoline A plurality of trampolines are arranged in a line at 10 m intervals. Each trampoline has its own maximum horizontal distance within which the jumper can jump safely. Starting from the left-most trampoline, the jumper jumps to another trampoline within the allowed jumping range. The jumper wants to repeat jumping until he/she reaches the right-most trampoline, and then tries to return to the left-most trampoline only through jumping. Can the jumper complete the roundtrip without a single stepping-down from a trampoline? Write a program to report if the jumper can complete the journey using the list of maximum horizontal reaches of these trampolines. Assume that the trampolines are points without spatial extent. Input The input is given in the following format. N d_1 d_2 : d_N The first line provides the number of trampolines N (2 ≤ N ≤ 3 × 10 5 ). Each of the subsequent N lines gives the maximum allowable jumping distance in integer meters for the i -th trampoline d_i (1 ≤ d_i ≤ 10 6 ). Output Output " yes " if the jumper can complete the roundtrip, or " no " if he/she cannot. Sample Input 1 4 20 5 10 1 Sample Output 1 no Sample Input 2 3 10 5 10 Sample Output 2 no Sample Input 3 4 20 30 1 20 Sample Output 3 yes
[ { "submission_id": "aoj_0362_10873479", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ld=long double;\n#define fi first\n#define se second\n#define i128 __int128_t\n#define all(x) x.begin(),x.end()\n#define rep(i,n) for(int i=0;i<(int)(n);++i)\n#define si(a) (long)(a.size())\ntempla...
aoj_0355_cpp
文字列ゲーム あなたは、双子のアイ君とヅー君に文字列を使ったゲームのプログラムをプレゼントしました。このゲームでは、アイ君とヅー君が文字列の中から部分文字列をそれぞれ選び、それらを比較して小さい方を選んだ人に得点が加算されます。二人は競い合い、何度もゲームを行いました。ところが、同じ文字列に対して何度もゲームをしているうちに飽きてしまいました。そこであなたは、文字列が変化するようにプログラムを修正することにしました。 長さ N の文字列 U と Q 個の命令文が与えられたとき、以下の命令を処理するプログラムを作成せよ。 文字列 U の指定された範囲にあるすべての文字を、指定された文字に置き換える。 文字列 U の指定された2つの部分文字列 S , T を辞書順で比較して、それらの大小関係を出力する。 Input 入力は以下の形式で与えられる。 N U Q query 1 query 2 : query Q 1行目に文字列の長さ N (1 ≤ N ≤ 200000)、2行目に文字列 U (英小文字のみを含む文字列)が与えられる。3行目に命令の数 Q (1 ≤ Q ≤ 100000) が与えられる。続く Q 行に i 番目の命令 query i が与えられる。各 query i は、以下のいずれかの形式で与えられる。 set x y z または comp a b c d set x y z は文字列 U の x 文字目から y 文字目までを、指定された文字 z に置き換えることを表す。ただし、1 ≤ x ≤ y ≤ N であり、 z は英小文字である。 comp a b c d は文字列 U の a 文字目から b 文字目までの部分文字列を S 、文字列 U の c 文字目から d 文字目までの部分文字列を T としたとき、文字列 S と文字列 T を辞書順で比較することを表す。ただし、1 ≤ a ≤ b ≤ N かつ 1 ≤ c ≤ d ≤ N である。 Output 各 comp 命令について、 S の方が小さいならば「s」、 T の方が小さいならば「t」、両者が一致しているならば「e」と1行に出力する。 Sample Input 1 13 aizualgorithm 9 comp 1 1 4 5 comp 2 6 1 5 set 9 12 b comp 9 9 10 10 comp 5 8 1 4 set 1 10 z set 11 13 x comp 8 10 1 5 comp 1 5 1 5 Sample Output 1 s t e t s e
[ { "submission_id": "aoj_0355_10867757", "code_snippet": "#include <bits/stdc++.h>\n#define N 500500\n\ntypedef long long ll;\nusing namespace std;\n\nconst int MOD = 1e9 + 7;\n\nchar c[N * 4];\n\nint mult(ll x, ll y) {\n return (x * y) % MOD;\n}\n\nint sm(ll x, ll y) {\n return (x + y) % MOD;\n}\n\nin...
aoj_0370_cpp
Age Difference A trick of fate caused Hatsumi and Taku to come to know each other. To keep the encounter in memory, they decided to calculate the difference between their ages. But the difference in ages varies depending on the day it is calculated. While trying again and again, they came to notice that the difference of their ages will hit a maximum value even though the months move on forever. Given the birthdays for the two, make a program to report the maximum difference between their ages. The age increases by one at the moment the birthday begins. If the birthday coincides with the 29 th of February in a leap year, the age increases at the moment the 1 st of March arrives in non-leap years. Input The input is given in the following format. y_1 m_1 d_1 y_2 m_2 d_2 The first and second lines provide Hatsumi’s and Taku’s birthdays respectively in year y_i (1 ≤ y_i ≤ 3000), month m_i (1 ≤ m_i ≤ 12), and day d_i (1 ≤ d_i ≤ D max ) format. Where D max is given as follows: 28 when February in a non-leap year 29 when February in a leap-year 30 in April, June, September, and November 31 otherwise. It is a leap year if the year represented as a four-digit number is divisible by 4. Note, however, that it is a non-leap year if divisible by 100, and a leap year if divisible by 400. Output Output the maximum difference between their ages. Sample Input 1 1999 9 9 2001 11 3 Sample Output 1 3 In this example, the difference of ages between them in 2002 and subsequent years is 3 on the 1 st of October, but 2 on the 1 st of December. Sample Input 2 2008 2 29 2015 3 1 Sample Output 2 8 In this example, the difference of ages will become 8 on the 29 th of February in and later years than 2016.
[ { "submission_id": "aoj_0370_3737126", "code_snippet": "#include <iostream>\n#include <cstdio>\n#include <string>\n#include <cstring>\n#include <deque>\n#include <list>\n#include <queue>\n#include <stack>\n#include <vector>\n#include <utility>\n#include <algorithm>\n#include <map>\n#include <set>\n#include ...
aoj_0363_cpp
Loading Aizu Ocean Transport Company (AOTC) accepted a new shipping order. The pieces of the cargo included in this order all have the same square footprint (2m x 2m). The cargo room has a rectangular storage space of 4m width with various longitudinal extents. Each pieces of cargo must be placed in alignment with partition boundaries which form a 1m x 1m square grid. A straddling arrangement with a neighboring partition (for example, protrusion by 50 cm) is not allowed. Neither an angled layout nor stacked placement are allowed. Note also that there may be some partitions on which any cargo loading is prohibited. AOTC wishes to use the currently available ship for this consignment and needs to know how many pieces of cargo it can accommodate. Make a program to report the maximum cargo capacity of the cargo space given the following information: the depth (m) of the cargo space and the loading-inhibited partitions. Input The input is given in the following format. H N x_1 y_1 x_2 y_2 : x_N y_N The first line provides the longitudinal depth H (2 ≤ H ≤ 10 4 ) of the cargo space in meters and the number of load-inhibited partitions N (0 ≤ N ≤ 4 × 10 4 ). Each of the subsequent N lines defines the position of the i -th load-inhibited partition x_i (0 ≤ x_i ≤ 3) and y_i (0 ≤ y_i ≤ H -1) in integers, where x = 0 and y = 0 indicate the bottom-left corner partition. A load-inhibited partition appears only once in the list. Output Output the maximum number of cargo pieces loaded into the cargo space. Sample Input 1 5 3 0 1 1 2 3 3 Sample Output 1 2 Input example 1 corresponds to the cargo layout shown in the left-most figure. Sample Input 2 6 4 0 2 1 3 3 4 0 5 Sample Output 2 4
[ { "submission_id": "aoj_0363_10866776", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define rep(i,N) for(int i=0;i<(ll)(N);i++)\nll gcd(ll a,ll b){\n if(b==0)return a;\n return gcd(b,a%b);\n}\nint main(){\n ll H,N;cin>>H>>N;\n vector<vector<int>>G(4,vect...
aoj_0366_cpp
Road Improvement Aizu is a country famous for its rich tourism resources and has N cities, each of which is uniquely identified with a number (0 to N -1). It has a road network consisting of M one-way roads connecting a city to another. All the roads connecting the cities in Aizu have a row of cherry trees along their routes. For enhancing the cherry-viewing experience, a proposal was made to modify the road network so that a tourist can travel around all the roads. To achieve this target, it was decided to construct several one-way roads, each connecting two cities and abiding by the following rules. The newly constructed road is for one-way traffic Starting from any city, a tourist is able to make a roundtrip and return to the city, whereby he/she drives all the roads exhaustively, including the newly constructed ones. Multiple passages of some of the roads are allowed. You, as a tourism promotion officer, are assigned with the task of writing a program for the road construction project. Write a program to determine the minimum number of roads to be constructed given the road network information in Aizu. Input The input is given in the following format. N M s_1 t_1 s_2 t_2 : s_M t_M The first line provides the number of cities N (1 ≤ N ≤ 10 4 ) and roads M (0 ≤ M ≤ 10 5 ). Each of the subsequent M lines provides the numbers assigned to start and destination cities for the i -th road: s_i , t_i (0 ≤ s_i , t_i ≤ N -1) , where s_i ≠ t_i . (no duplicate appearance of a road) Output Output the minimum number of roads to be newly constructed. Sample Input 1 6 7 0 2 2 1 1 0 2 3 4 3 4 5 5 4 Sample Output 1 2 Sample Input 2 6 9 0 2 2 1 1 0 2 3 4 3 4 5 5 4 5 2 3 4 Sample Output 2 0
[ { "submission_id": "aoj_0366_10873655", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ld=long double;\n#define fi first\n#define se second\n#define i128 __int128_t\n#define all(x) x.begin(),x.end()\n#define rep(i,n) for(int i=0;i<(int)(n);++i)\n#define si(a) (long)(a.size())\ntempla...
aoj_0365_cpp
Swapping Characters You are given a string and a number k . You are suggested to generate new strings by swapping any adjacent pair of characters in the string up to k times. Write a program to report the lexicographically smallest string among them. Input The input is given in the following format. s k The first line provides a string s . The second line provides the maximum number of swapping operations k (0 ≤ k ≤ 10 9 ). The string consists solely of lower-case alphabetical letters and has a length between 1 and 2 × 10 5 . Output Output the lexicographically smallest string. Sample Input 1 pckoshien 3 Sample Output 1 ckopshien Sample Input 2 pckoshien 10 Sample Output 2 cekophsin
[ { "submission_id": "aoj_0365_11011873", "code_snippet": "// #ifndef ONLINE_JUDGE\n// #define _GLIBCXX_DEBUG//[]で配列外参照をするとエラーにしてくれる。上下のやつがないとTLEになるので注意 ABC311Eのサンプル4みたいなデバック中のTLEは防げないので注意\n// #endif\n\n#include <bits/stdc++.h>\nusing namespace std;\n// #include <atcoder/all>\n// using namespace atcoder;\n\n\...
aoj_0364_cpp
Dungeon Bob is playing a popular game called "Dungeon". The game is played on a rectangular board consisting of W × H squares. Each square is identified with its column and row number, thus the square located in the x -th column and the y -th row is represented as ( x , y ). The left-most square in the top row is (0, 0) and the right-most square in the bottom row is ( W -1, H -1). Bob moves a character "BomBom" to clear the game. BomBom is initially located at (0, 0). The game is won if Bob successfully destroys all the enemy characters on the board by manipulating BomBom cleverly. The enemy characters are fixed on specific squares, and Bob can manipulate BomBom using the following two operations any number of times. One-square movement in the up, down, left, or right direction within the board Using a bomb, eliminate all the enemy characters that are located in the same column and row as that of BomBom BomBom consumes a Cost when it moves from one square to another. BomBom can use a bomb any number of times without consuming a Cost. Use of a bomb has no effect on BomBom’s behavior and it can move even to a square where an enemy character is located. Given the board size and enemy information, make a program to evaluate the minimum Cost BomBom consumes before it destroys all enemy characters. Input The input is given in the following format. W H N x_1 y_1 x_2 y_2 : x_N y_N The first line provides the number of squares in the horizontal direction W (1 ≤ W ≤ 10 5 ), in the vertical direction H (1 ≤ H ≤ 10 5 ), and the number of enemy characters N (1 ≤ N ≤ 10 5 ). Each of the subsequent N lines provides location information of the i -th enemy, column x_i (0 ≤ x_i ≤ W -1) and row y_i (0 ≤ y_i ≤ H -1). The number of enemy characters in a specific square can be either one or zero. Output Output the minimum Cost in a line. Sample Input 1 5 4 4 0 3 1 1 2 2 2 3 Sample Output 1 2 Sample Input 2 6 6 5 2 1 5 2 3 3 1 4 1 5 Sample Output 2 4 Sample Input 3 8 8 4 6 0 7 0 0 6 0 7 Sample Output 3 0
[ { "submission_id": "aoj_0364_10866790", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define rep(i,N) for(int i=0;i<(ll)(N);i++)\n#define fi first \n#define se second\n#define all(X) (X).begin(),(X).end()\nll gcd(ll a,ll b){\n if(b==0)return a;\n return gcd(b,a...
aoj_0375_cpp
Lake Survery The Onogawa Expedition is planning to conduct a survey of the Aizu nature reserve. The expedition planner wants to take the shortest possible route from the start to end point of the survey, while the expedition has to go around the coast of the Lake of Onogawa en route. The expedition walks along the coast of the lake, but can not wade across the lake. Based on the base information including the start and end point of the survey and the area of Lake Onogawa as convex polygon data, make a program to find the shortest possible route for the expedition and calculate its distance. Note that the expedition can move along the polygonal lines passing through the nodes, but never enter within the area enclosed by the polygon. Input The input is given in the following format. x_s y_s x_g y_g N x_1 y_1 x_2 y_2 : x_N y_N The first line provides the start point of the survey x_s,y_s (0≤ x_s,y_s ≤10 4 ), and the second line provides the end point x_g,y_g (0 ≤ x_g,y_g ≤ 10 4 ) all in integers. The third line provides the number of apexes N (3 ≤ N ≤ 100) of the polygon that represents the lake, and each of the subsequent N lines provides the coordinate of the i -th apex x_i,y_i (0 ≤ x_i,y_i ≤ 10 4 ) in counter-clockwise order. These data satisfy the following criteria: Start and end points of the expedition are not within the area enclosed by the polygon nor on boundaries. Start and end points of the expedition are not identical, i.e., x_s ≠ x_g or y_s ≠ y_g . No duplicate coordinates are given, i.e., if i ≠ j then x_i ≠ x_r or y_i ≠ y_j . The area enclosed by the polygon has a positive value. Any three coordinates that define an area are not aligned on a line. Output Output the distance of the shortest possible expedition route. Any number of decimal places can be selected as long as the error does not exceed ± 10 -3 . Sample Input 1 0 0 4 0 4 1 1 2 1 3 3 1 2 Sample Output 1 4.472136 Sample Input 2 4 4 0 0 4 1 1 3 1 3 3 1 3 Sample Output 2 6.32455
[ { "submission_id": "aoj_0375_3240241", "code_snippet": "#include <cmath>\n#include <vector>\n#include <iomanip>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\nclass point2d {\npublic:\n\tdouble x, y;\n\tpoint2d() : x(0), y(0) {};\n\tpoint2d(double x_, double y_) : x(x_), y(y_) {};\n\tbool...
aoj_0371_cpp
Electric Metronome A boy named PCK is playing with N electric metronomes. The i -th metronome is set to tick every t_i seconds. He started all of them simultaneously. He noticed that, even though each metronome has its own ticking interval, all of them tick simultaneously from time to time in certain intervals. To explore this interesting phenomenon more fully, he is now trying to shorten the interval of ticking in unison by adjusting some of the metronomes’ interval settings. Note, however, that the metronomes do not allow any shortening of the intervals. Given the number of metronomes and their preset intervals t_i (sec), write a program to make the tick-in-unison interval shortest by adding a non-negative integer d_i to the current interval setting of the i -th metronome, and report the minimum value of the sum of all d_i . Input The input is given in the following format. N t_1 t_2 : t_N The first line provides the number of metronomes N (1 ≤ N ≤ 10 5 ). Each of the subsequent N lines provides the preset ticking interval t_i (1 ≤ t_i ≤ 10 4 ) of the i -th metronome. Output Output the minimum value. Sample Input 1 3 3 6 8 Sample Output 1 3 If we have three metronomes each with a ticking interval of 3, 6, and 8 seconds, respectively, simultaneous activation of these will produce a tick in unison every 24 seconds. By extending the interval by 1 second for the first, and 2 seconds for the second metronome, the interval of ticking in unison will be reduced to 8 seconds, which is the shortest possible for the system. Sample Input 2 2 10 10 Sample Output 2 0 If two metronomes are both set to 10 seconds, then simultaneous activation will produce a tick in unison every 10 seconds, which is the shortest possible for this system.
[ { "submission_id": "aoj_0371_11026002", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\nsigned main(){\n int N;\n cin>>N;\n vector<int> A(N);\n for(int i=0;i<N;i++) cin>>A[i];\n sort(A.begin(),A.end());\n int ans = 0;\n int now = 1;\n for(int x...
aoj_0374_cpp
Paper Fortune If you visit Aizu Akabeko shrine, you will find a unique paper fortune on which a number with more than one digit is written. Each digit ranges from 1 to 9 (zero is avoided because it is considered a bad omen in this shrine). Using this string of numeric values, you can predict how many years it will take before your dream comes true. Cut up the string into more than one segment and compare their values. The difference between the largest and smallest value will give you the number of years before your wish will be fulfilled. Therefore, the result varies depending on the way you cut up the string. For example, if you are given a string 11121314 and divide it into segments, say, as 1,11,21,3,14, then the difference between the largest and smallest is 21 - 1 = 20. Another division 11,12,13,14 produces 3 (i.e. 14 - 11) years. Any random division produces a game of luck. However, you can search the minimum number of years using a program. Given a string of numerical characters, write a program to search the minimum years before your wish will be fulfilled. Input The input is given in the following format. n An integer n is given. Its number of digits is from 2 to 100,000, and each digit ranges from 1 to 9. Output Output the minimum number of years before your wish will be fulfilled. Sample Input 1 11121314 Sample Output 1 3 Sample Input 2 123125129 Sample Output 2 6 Sample Input 3 119138 Sample Output 3 5
[ { "submission_id": "aoj_0374_10874359", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, l, n) for(int i = int(l); i < int(n); i++)\n#define ll long long\n#define all(x) (x).begin(), (x).end()\n#define rall(x) (x).rbegin(), (x).rend()\n\ntemplate<class T> bool chmin(T &a, T...
aoj_0373_cpp
Checkered Pattern You have a cross-section paper with W x H squares, and each of them is painted either in white or black. You want to re-arrange the squares into a neat checkered pattern, in which black and white squares are arranged alternately both in horizontal and vertical directions (the figure shown below is a checkered patter with W = 5 and H = 5 ). To achieve this goal, you can perform the following two operations as many times you like in an arbitrary sequence: swapping of two arbitrarily chosen columns, and swapping of two arbitrarily chosen rows. Create a program to determine, starting from the given cross-section paper, if you can re-arrange them into a checkered pattern. Input The input is given in the following format. W H c 1,1 c 1,2 ... c 1,W c 2,1 c 2,2 ... c 2,W : c H,1 c H,2 ... c H,W The first line provides the number of squares in horizontal direction W (2≤ W ≤1000) and those in vertical direction H (2≤ H ≤1000). Each of subsequent H lines provides an array of W integers c i,j corresponding to a square of i -th row and j -th column. The color of the square is white if c i,j is 0, and black if it is 1. Output Output "yes" if the goal is achievable and "no" otherwise. Sample Input 1 3 2 1 1 0 0 0 1 Sample Output 1 yes Sample Input 2 2 2 0 0 1 1 Sample Output 2 no
[ { "submission_id": "aoj_0373_11022526", "code_snippet": "#include <iostream>\n#include <vector>\n#include <set>\n#include <map>\n#include <algorithm>\n#include <iomanip>\n#include <numeric>\n#include <queue>\n#include <stack>\nusing namespace std;\nusing ll = long long;\nint main() {\n\tint W, H;cin >> W >>...
aoj_0376_cpp
Lottery Box A lottery is being held in a corner of the venue of the Aizu festival. Several types of balls are inside the lottery box and each type has its unique integer printed on the surfaces of the balls. An integer T is printed on the lottery box. In the lottery, you first declare two integers A and B , and draw up to M balls from the box. Let the sum of the integers printed on the balls be S . You can get a wonderful gift if the following two criteria are met: S divided by T gives a remainder greater than or equal to A , and S divided by T gives a quotient (fractional portion dropped) greater than or equal to B . Write a program to determine if you have any chance of getting the gift given the following information: the number of ball types, ball-type specific integers, the maximum number of balls to be drawn from the box, the integer printed on the lottery box, and two integers declared before drawing. Assume that each ball type has sufficient (≥M) population in the box. Note also that there may be a chance of getting the gift even without drawing any ball. Input The input is given in the following format. N M T a_1 a_2 : a_N Q A_1 B_1 A_2 B_2 : A_Q B_Q The first line provides the number of ball types N (1≤ N ≤10 5 ), the maximum number of balls you can draw from the box M (1≤ M ≤10 5 ), and the integer printed on the box T (1≤ T ≤1000). Each of the subsequent N lines provides an integer a_i (1≤ a_i ≤10 9 ) printed on the i -th ball type. The next line following these provides the number of declarations Q (1≤ Q ≤10 5 ). Each of the Q lines following this provides a pair of integers A_i (0 ≤ A_i < T ), B_i (0 ≤ B_i ≤ 10 9 ) that constitute the i -th declaration. Output Output a line for each pair of declaration A and B that contains " yes " if there is a chance of getting the gift or " no " otherwise. Sample Input 1 3 2 7 8 3 6 5 2 2 3 2 4 1 6 1 6 0 Sample Output 1 yes no yes no yes
[ { "submission_id": "aoj_0376_10203930", "code_snippet": "// AOJ #376\n// Lottery Box 2025.2.8\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() { // 整数の入力\n\tint n = 0, c = gc();\n\tif (c == '-') {\tc ...
aoj_0377_cpp
Party The students in a class in Akabe high-school define the relation “acquaintance” as: If A and B are friends, then A is acquainted with B. If A and B are friends and B is acquainted with C, then A is acquainted with C. They define the relation “companion” as: Suppose A is acquainted with B, and two classmates who have been friend distance. If A is still acquainted with B, then A and B are companions. A boy PCK joined the class recently and wishes to hold a party inviting his class fellows. He wishes to invite as many boys and girls as possible to the party and has written up an invitation list. In arranging the list, he placed the following conditions based on the acquaintanceship within the class before he joined. When T is in the list: U is listed if he/she is a companion of T. If U is not a companion of T, U is not listed if he/she and T are friends, or he/she and some of T’s companions are friends. PCK made the invitation list so that the maximum number of his classmates is included. Given the number of classmates N and the friendships among them, write a program to estimate the number of boys and girls in the list. All the classmates are identified by an index that ranges from 0 to N -1. Input The input is given in the following format. N M s_1 t_1 s_2 t_2 : s_M t_M The first line provides the number of classmates N (2 ≤ N ≤ 10 5 ) and the number of friendships M (1 ≤ M ≤ 2×10 5 ). Each of the M subsequent lines provides two of the classmates s_i, t_i (0 ≤ s_i,t_i ≤ N -1) indicating they are friends. No duplicate relationship appears among these lines. Output Output the maximum number of classmates PCK invited to the party. Sample Input 1 7 8 0 1 1 2 1 3 2 3 0 4 4 5 4 6 5 6 Sample Output 1 6 Sample Input 2 3 2 0 1 1 2 Sample Output 2 2
[ { "submission_id": "aoj_0377_10205417", "code_snippet": "// AOJ #377\n// Party 2025.2.9\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() { // 整数の入力\n\tint n = 0, c = gc();\n\tif (c == '-') {\tc = gc()...
aoj_0379_cpp
Iron Bars A boy PCK had N straight iron bars, which were serially indexed. Unfortunately, the first M bars (0 ≤ M ≤ N ) among them were bent during transportation. They all suffered a perpendicular bend at one point. He is planning to make a cube using a set of bars selected using the following rules: X bars from bent ones, Y bars from straight ones, where 2X + Y = 12 . Any two bars can be jointed only at the apexes of the cube. He wants to count how many types of rectangular parallelepipeds (hereafter RP) he can make using these bars. Make a program to count out types (different shapes) of RPs that PCK can make using the following information: the number of bars and length of each one, position of the bend, and the number of bars to be used to construct an RP. Note that any two RPs similar in shape are considered identical: namely if the length of three defining sides of two RPs coincide if arranged in increasing/decreasing order (e.g., three sides of RP i and j are A_i, B_i, C_i , and A_j, B_j and C_j in increasing order, then the relations A_i = A_j, B_i = B_j , and C_i = C_j hold. Note also that the bars are sufficiently thin to allow you to consider them as idealized lines. Input The input is given in the following format. N M X Y a_1 a_2 : a_N b_1 b_2 : b_M The first line provides the total number of iron bars and bent bars, and those straight and bent bars used to construct an RP: N (6 ≤ N ≤ 6000), M (0 ≤ M ≤ N ), X (0 ≤ X ≤ 6), and Y (0 ≤ Y ≤ 12). The following relations always hold for them: 2X+Y=12 , X+Y ≤ N , X ≤ M . Each of the subsequent N lines provides the length of the i -th bar a_i (1 ≤ a_i ≤ 6000) in integers. Furthermore, each of the subsequent M lines provides the location at which the i -th bent bar suffered a perpendicular bend b_i (1 ≤ b_i ≤ 3000) in centimeters from one end of the bar (note: 1 ≤ a_i-b_i ≤ 3000). Output Output the number of constructible rectangular parallelepipeds. Sample Input 1 18 8 3 6 4 3 3 3 3 2 2 2 1 1 1 1 1 2 2 3 3 3 1 1 1 1 1 1 1 1 Sample Output 1 3
[ { "submission_id": "aoj_0379_10205452", "code_snippet": "// AOJ #379\n// Iron Bars 2025.2.9\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\n\nstruct FastBitset {\n vector<ull> data;\n int size;\n FastBitset() {}\n FastBitset(int n) : siz...
aoj_0378_cpp
Aerial Photos Hideyo has come by two aerial photos of the same scale and orientation. You can see various types of buildings, but some areas are hidden by clouds. Apparently, they are of the same area, and the area covered by the second photograph falls entirely within the first. However, because they were taken at different time points, different shapes and distribution of clouds obscure identification where the area in the second photograph is located in the first. There may even be more than one area within the first that the second fits equally well. A set of pixel information is given for each of the photographs. Write a program to extract candidate sub-areas within the first photograph that compare with the second equally well and output the number of the candidates. Input The input is given in the following format. AW AH BW BH arow 1 arow 2 : arow AH brow 1 brow 2 : brow BH The first line provides the number of pixels in the horizontal and the vertical direction AW (1 ≤ AW ≤ 800) and AH (1 ≤ AH ≤ 800) for the first photograph, followed by those for the second BW (1 ≤ BW ≤ 100) and BH (1 ≤ BH ≤ 100) ( AW ≥ BW and AH ≥ BH ). Each of the subsequent AH lines provides pixel information ( arow i ) of the i -th row from the top of the first photograph. Each of the B H lines that follows provides pixel information ( brow i ) of the i -th row from the top of the second photograph. Each of the pixel information arow i and brow i is a string of length AW and BW , respectively, consisting of upper/lower-case letters, numeric characters, or " ? ". Each one character represents a pixel, whereby upper/lower-case letters and numeric characters represent a type of building, and " ? " indicates a cloud-covered area. Output Output the number of the candidates. Sample Input 1 5 5 3 3 AF??z W?p88 ???Hk pU?m? F???c F?? p?8 ?H? Sample Output 1 4 Sample Input 2 6 5 4 2 aaaaaa aaaaaa aaaaaa aaaaaa aaaaaa aaaa aaaa Sample Output 2 12
[ { "submission_id": "aoj_0378_10205430", "code_snippet": "// AOJ #378\n// Aerial Photo 2025.2.9\n\n#include <iostream>\n#include <vector>\n#include <string>\n#include <emmintrin.h> // SSE2 ヘッダ\nusing namespace std;\nusing ll = long long;\n\nstatic inline bool checkRowSSE(const char* a, const char* b, int le...
aoj_0367_cpp
Charging System for Network There is a network consisting of N machines (sequentially numbered from 0 to N -1) interconnected through N -1 bidirectional communication cables. Any two machines can perform bidirectional communication through one or more cables. From time to time, machines in the network are renewed. When a new machine is introduced, the cables directly connected to it are also replaced with thicker ones to cope with the increased volume of communication traffic. The communication fee arising from communication traffic between any two machines is calculated by summing the charges assigned to all the cables routing via the two. A unique charging scheme is employed in this system: if the size of a cable is a multiple of K , then the cable is not charged (free of charge). Other cables are charged according to their sizes. Based on the given information on the network topology and Q instructions, write a program to execute each instruction. Increase the sizes of all cables directly connected to the machine x by d . Report the communication charge between the machines s and t . Input The input is given in the following format. N K a_1 b_1 c_1 a_2 b_2 c_2 : a_{N−1} b_{N−1} c_{N−1} Q query_1 query_2 : query_Q The first line provides the number of machines N (2 ≤ N ≤ 10 5 ) and the cable size K (1 ≤ K ≤ 10 5 ) (the reference value for determining free of charge cables). Each of subsequent N -1 lines provides the i -th cable information that directly connects two machines a_i and b_i (0 ≤ a_i < b_i ≤ N -1), followed by the cable’s initial size c_i (1 ≤ c_i ≤ 10 5 ). For any pair of machines, the number of cables directly connecting them is either one or zero. The next line following them provides the number of instructions Q (1 ≤ Q ≤ 10 5 ). Each of the Q lines following it provides the i -th instruction query_i , which is either one of the following two: add x d or send s t The instruction add x d increase the size of all cables directly connected to machine x (0 ≤ x ≤ N -1) by d (1 ≤ d ≤ 10 5 ). The instruction send s t reports the charge imposed to the communication between the two machines s (0 ≤ s ≤ N -1) and t (0 ≤ t ≤ N -1), where s ≠ t . At least one send instruction is included in the input information. Output For each send command, output the communication charge between s and t . Sample Input 1 6 3 0 1 1 0 2 1 0 3 1 2 4 1 2 5 1 3 send 1 4 add 2 2 send 1 4 Sample Output 1 3 1
[ { "submission_id": "aoj_0367_10423002", "code_snippet": "// AOJ #0367 Charging System for Network\n// 2025.4.26\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing pii = pair<int, int>;\nconst int NMAX = 200000;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c...
aoj_0385_cpp
Bozosort Bozosort, as well as Bogosort, is a very inefficient sort algorithm. It is a random number-based algorithm that sorts sequence elements following the steps as below: Randomly select two elements and swap them. Verify if all the elements are sorted in increasing order. Finish if sorted, else return to 1. To analyze Bozosort, you have decided to simulate the process using several predetermined pairs of elements. You are given several commands to swap two elements. Make a program to evaluate how many times you have to run the command before the sequence is aligned in increasing order. Input The input is given in the following format. $N$ $a_1$ $a_2$ ... $a_N$ $Q$ $x_1$ $y_1$ $x_2$ $y_2$ $...$ $x_Q$ $y_Q$ The first line provides the number of sequence elements $N$ ($2 \leq N \leq 300,000$). The second line provides an array of integers $a_i$ ($1 \leq a_i \leq 10^9$) that constitutes the sequence. Each of the subsequent $Q$ lines provides a pair of integers $x_i,y_i$ ($1 \leq x_i,y_i \leq N$) that represent the $i$-th command, which swaps the two elements indicated by $x_i$ and $y_i$ ($x_i \ne y_i$). Output If neat alignment in increasing order is realized the first time after execution of multiple commands, output at what time it was. Output 0 if the initial sequence is aligned in increasing order, and -1 if exhaustive execution still failed to attain the goal. Sample Input 1 6 9 7 5 6 3 1 3 1 6 2 5 3 4 Sample Output 1 2 Sample Input 2 4 4 3 2 1 2 1 2 3 4 Sample Output 2 -1 Sample Input 3 5 1 1 1 2 2 1 1 2 Sample Input 3 0
[ { "submission_id": "aoj_0385_10874892", "code_snippet": "#include <bits/stdc++.h>\n\n#define rep(i, n) for (int i = 0; i < (int)(n); ++i)\n#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)\n#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)\n#define rrep2(i, a, b) for (int i = (int)(b)...
aoj_0372_cpp
Three Meals You are running a restaurant that serves dishes only three times a day. Each of your customer has his/her own separate timeframes for eating breakfast, lunch and supper. Thus, your customer enjoys your dish only when he/she visits your restaurant to meet your serving time settings. Your wish is to modify your serving time arrangement so that as many as possible of your customers can enjoy your meals three times a day. Write a program to enable as many as possible of customers can enjoy your service three times a day, i.e. breakfast, lunch, and supper. A list of customers eating timeframes is given, which you cannot change. Settings of your service time are your option. Coincidence between your service time and either edge of customer’s timframe (start or end time) does not hinder you to provide your service. Input The input is given in the following format. N ast_1 aet_1 hst_1 het_1 bst_1 bet_1 ast_2 aet_2 hst_2 het_2 bst_2 bet_2 : ast_N aet_N hst_N het_N bst_N bet_N The first line provides the number of customers N (1≤ N ≤50). Each of subsequent N lines provides timeframe information of i -th customer: start and end time of breakfast timzeframe ( ast_i , aet_i ), those of lunch timeframe ( hst_i , het_i ) and those of supper timeframe ( bst_i , bet_i ). The end time must be later in time as the start time. Timeframes do not overlap, i.e. hst_i is later in time than aet_i , and so on. Each time settings must not cross 0:00 midnight. h m Each time information is given by hour h (0≤ h ≤23) and minute m (0≤ m ≤59). Output Output the maximum number of customers that can be served all three meals (breakfast, lunch, and supper). Sample Input 1 5 1 0 2 0 3 30 4 30 6 0 7 0 2 30 3 0 4 0 5 0 5 30 6 30 1 30 2 30 4 30 5 0 6 30 7 0 2 30 3 0 5 0 6 0 6 30 7 0 1 0 2 0 3 0 3 30 4 0 5 0 Sample Output 1 3
[ { "submission_id": "aoj_0372_11026034", "code_snippet": "#pragma GCC target(\"avx2\")\n#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\nint N;\nint A[50], B[50], C[50], D[50], E[50], F[50];\nsigned main(){\n set<int> st;\n cin>>N;\n for(int i=0;i<N;i++){\n int a,b;\n ...
aoj_0386_cpp
Transporter In the year 30XX, an expedition team reached a planet and found a warp machine suggesting the existence of a mysterious supercivilization. When you go through one of its entrance gates, you can instantaneously move to the exit irrespective of how far away it is. You can move even to the end of the universe at will with this technology! The scientist team started examining the machine and successfully identified all the planets on which the entrances to the machine were located. Each of these N planets (identified by an index from $1$ to $N$) has an entrance to, and an exit from the warp machine. Each of the entrances and exits has a letter inscribed on it. The mechanism of spatial mobility through the warp machine is as follows: If you go into an entrance gate labeled with c, then you can exit from any gate with label c. If you go into an entrance located on the $i$-th planet, then you can exit from any gate located on the $j$-th planet where $i < j$. Once you have reached an exit of the warp machine on a planet, you can continue your journey by entering into the warp machine on the same planet. In this way, you can reach a faraway planet. Our human race has decided to dispatch an expedition to the star $N$, starting from Star $1$ and using the warp machine until it reaches Star $N$. To evaluate the possibility of successfully reaching the destination. it is highly desirable for us to know how many different routes are available for the expedition team to track. Given information regarding the stars, make a program to enumerate the passages from Star $1$ to Star $N$. Input The input is given in the following format. $N$ $s$ $t$ The first line provides the number of the stars on which the warp machine is located $N$ ($2 \leq N \leq 100,000$). The second line provides a string $s$ of length $N$, each component of which represents the letter inscribed on the entrance of the machine on the star. By the same token, the third line provides a string $t$ of length $N$ consisting of the letters inscribed on the exit of the machine. Two strings $s$ and $t$ consist all of lower-case alphabetical letters, and the $i$-th letter of these strings corresponds respectively to the entrance and exit of Star $i$ machine. Output Divide the number of possible routes from Star $1$ to Star $N$ obtained above by 1,000,000,007, and output the remainder. Sample Input 1 6 abbaba baabab Sample Output 1 5 Sample Input 2 25 neihsokcpuziafoytisrevinu universityofaizupckoshien Sample Output 2 4
[ { "submission_id": "aoj_0386_9202923", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n//#include<atcoder/all>\n//using namespace atcoder;\n#define rep(i,n) for(int i=0;i<(int)n;i++)\n#define rep1(i,n) for(int i=1;i<=(int)n;i++)\nusing ll = long long;\nusing ull = unsigned long long;\nusing ...
aoj_0387_cpp
Taxi PCK Taxi in Aizu city, owned by PCK company, has adopted a unique billing system: the user can decide the taxi fare. Today as usual, many people are waiting in a queue at the taxi stand in front of the station. In front of the station, there are $N$ parking spaces in row for PCK taxis, each with an index running from $1$ to $N$. Each of the parking areas is occupied by a taxi, and a queue of potential passengers is waiting for the ride. Each one in the queue has his/her own plan for how much to pay for the ride. To increase the company’s gain, the taxi driver is given the right to select the passenger who offers the highest taxi fare, rejecting others. The driver in the $i$-th parking space can perform the following actions any number of times in any sequence before he finally selects a passenger and starts driving. Offer a ride to the passenger who is at the head of the $i$-th parking space’s queue. Reject to offer a ride to the passenger who is at the head of the $i$-th parking space’s queue. The passenger is removed from the queue. Move to the $i + 1$-th parking area if it is empty. If he is in the $N$-th parking area, he leaves the taxi stand to cruise the open road. A preliminary listening is made as to the fare the users offer. Your task is to maximize the sales volume of PCK Taxi in reference to the table of offered fares. A taxi cannot accommodate more than one passenger. Given the number of taxi parking spaces and information regarding the persons waiting in the parking areas, calculate the maximum possible volume of sales. Input The input is given in the following format. $N$ $s_1$ $s_2$ $...$ $s_N$ The first line provides the number of taxi parking areas $N$ ($1 \leq N \leq 300,000$). Each of the subsequent $N$ lines provides information on the customers queueing in the $i$-th taxi parking area in the following format: $M$ $c_1$ $c_2$ ... $c_M$ The first integer $M$ ($1 \leq M \leq 300,000$) indicates the number of customers in the queue, and the subsequent array of integers $c_j$ ($1 \leq c_j \leq 10,000$) indicates the fare the $j$-th customer in the queue is willing to pay. The total number of customers in the taxi stand is equal to or less than $300,000$. Output Output the maximum volume of sales. Sample Input 3 3 8 10 1 4 7 1 2 15 3 11 8 19 Sample Output 45
[ { "submission_id": "aoj_0387_10876661", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint main(void){\n\tlong long n,m,c,i,j,sum=0;\n\tpriority_queue<int,vector<int>,greater<int>> que;\n\tcin>>n;\n\tfor(i=0;i<n;i++){\n\t\tcin>>m;\n\t\tfor(j=0;j<m;j++){\n\t\t\tcin>>c;\n\t\t\tque.push(c);\n...
aoj_0388_cpp
Points on a Straight Line The university of A stages a programming contest this year as has been the case in the past. As a member of the team in charge of devising the problems, you have worked out a set of input data for a problem, which is an arrangement of points on a 2D plane in the coordinate system. The problem requires that any combination of these points greater than or equal to $K$ in number must not align on a line. You want to confirm that your data satisfies this requirement. Given the number of points $K$ and their respective 2D coordinates, make a program to check if any combination of points greater or equal to $K$ align on a line. Input The input is given in the following format. $N$ $K$ $x_1$ $y_1$ $x_2$ $y_2$ $...$ $x_N$ $y_N$ The first line provides the number of points $N$ ($3 \leq N \leq 3000$) on the 2D coordinate system and an integer $K$ ($3 \leq K \leq N$). Each of the subsequent lines provides the $i$-th coordinate $x_i,y_i$ ($0 \leq x_i,y_i \leq 10000$) as integers. None of these coordinates coincide, i.e., if $i \ne j$, then $x_i \ne x_j$ or $y_i \ne y_j$. Output Output 1 if any combination of points greater or equal to $K$ aligns on a line, or 0 otherwise. Sample Input 1 5 4 0 0 1 0 1 1 0 1 2 2 Sample Output 1 0 Sample Input 2 7 5 0 0 1 0 1 1 0 1 2 0 3 0 4 0 Sample Output 2 1
[ { "submission_id": "aoj_0388_10874948", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\nint f(int a, int b){\n if(a < b) swap(a,b);\n int x,y;\n if(b == 0){\n return a;\n }\n else{\n return f(b,a%b);\n }\n}\nsigned main(){\n int N,K;...
aoj_0390_cpp
Disc A mysterious device was discovered in an ancient ruin. The device consists of a disc with integers inscribed on both sides, a bundle of cards with an integer written on each of them and a pedestal within which the cards are placed. The disc rotates when the bundle of cards is put into the pedestal. Only one side of the disc is exposed to view at any one time. The disc is radially segmented in equal angles into $K$ sections, and an integer, from $1$ to $K$ in increasing order, is inscribed clockwise on each section on the front side. The sections on the back side share the same boundaries with the front side, and a negative counterpart of that in the section on the front side is inscribed, i.e., if $X$ is inscribed in the front side section, $-X$ is inscribed on the counterpart section on the back side. There is a needle on the periphery of the disc, and it always points to the middle point of the arc that belongs to a section. Hereafter, we say "the disc is set to $X$" if the front side section just below the needle contains an integer $X$. Fig.1 Disc with 5 sections (i.e., $K = 5$): When the disc is set to $1$ on the front side, it is set to $-1$ on the back side. Investigation of the behavior of the device revealed the following: When the bundle of cards is placed in the pedestal, the device sets the disc to 1. Then, the device reads the cards slice by slice from top downward and performs one of the following actions according to the integer $A$ on the card If $A$ is a positive number, it rotates the disc clockwise by $|A|$ sections. If $A$ is zero, it turns the disc back around the vertical line defined by the needle and the center of the disc. If $A$ is a negative number, it rotates the disc counterclockwise by $|A|$ sections. It is provided that $|A|$ is the absolute value of $A$. The final state of the device (i.e., to what section the needle is pointing) varies depending on the initial stacking sequence of the cards. To further investigate the behavior of the device, we swapped two arbitrary cards in the stack and placed the bundle in the pedestal to check what number the disc is set to. We performed this procedure over again and again. Given the information on the device and several commands to swap two cards, make a program to determine the number to which the device is set at the completion of all actions. Note that the stacking sequence of the cards continues to change as a new trial is made. Input The input is given in the following format. $K$ $N$ $Q$ $A_1$ $A_2$ ... $A_N$ $L_1$ $R_1$ $L_2$ $R_2$ $...$ $L_Q$ $R_Q$ The first line provides the number of sections $K$ ($2 \leq K \leq 10^9$), the slices of cards $N$ ($2 \leq N \leq 100,000$), and the number of commands $Q$ ($1 \leq Q \leq 100,000$). The second line provides an array of $N$ integers inscribed on the sections of the disc. $A_i$ ($-10^9 \leq A_i \leq 10^9$) represents the number written on the $i$-th card from the top. Each of the subsequent $Q$ lines defines the $i$-th ...(truncated)
[ { "submission_id": "aoj_0390_10875152", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\nint K;\ntemplate < typename NodeT, typename LazyT>\nstruct SEG{\n using F = function<NodeT(const NodeT&, const NodeT&)>;\n using G = function<NodeT(const NodeT&, const LazyT&...
aoj_0384_cpp
Dudeney Number A Dudeney number is a positive integer for which the sum of its decimal digits is equal to the cube root of the number. For example, $512$ is a Dudeney number because it is the cube of $8$, which is the sum of its decimal digits ($5 + 1 + 2$). In this problem, we think of a type similar to Dudeney numbers and try to enumerate them. Given a non-negative integer $a$, an integer $n$ greater than or equal to 2 and an upper limit $m$, make a program to enumerate all $x$’s such that the sum of its decimal digits $y$ satisfies the relation $x = (y + a)^n$, and $x \leq m$. Input The input is given in the following format. $a$ $n$ $m$ The input line provides three integers: $a$ ($0 \leq a \leq 50$), $n$ ($2 \leq n \leq 10$) and the upper limit $m$ ($1000 \leq m \leq 10^8$). Output Output the number of integers that meet the above criteria. Sample Input 1 16 2 1000 Sample Output 1 2 Two: $400 = (4 + 0 + 0 + 16)^2$ and $841 = (8 + 4 + 1 + 16)^2$ Sample Input 2 0 3 5000 Sample Output 2 3 Three: $1=1^3$, $512 = (5 + 1 + 2)^3$ and $4913 = (4 + 9 + 1 + 3)^3$. Sample Input 3 2 3 100000 Sample Output 3 0 There is no such number $x$ in the range below $100,000$ such that its sum of decimal digits $y$ satisfies the relation $(y+2)^3 = x$.
[ { "submission_id": "aoj_0384_10872265", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll=long long;\nusing dl=double;\nusing vi=vector<int>;\nusing vpi=vector<pair<int,int>>;\n\nint main(){\n int a,n,m; cin>>a>>n>>m;\n int count=0;\n for(int i=1; i<m; i++){\n int d=i;\n int...
aoj_0394_cpp
Pilling Blocks We make a tower by stacking up blocks. The tower consists of several stages and each stage is constructed by connecting blocks horizontally. Each block is of the same weight and is tough enough to withstand the weight equivalent to up to $K$ blocks without crushing. We have to build the tower abiding by the following conditions: Every stage of the tower has one or more blocks on it. Each block is loaded with weight that falls within the withstanding range of the block. The weight loaded on a block in a stage is evaluated by: total weight of all blocks above the stage divided by the number of blocks within the stage. Given the number of blocks and the strength, make a program to evaluate the maximum height (i.e., stages) of the tower than can be constructed. Input The input is given in the following format. $N$ $K$ The input line provides the number of blocks available $N$ ($1 \leq N \leq 10^5$) and the strength of the block $K$ ($1 \leq K \leq 10^5$). Output Output the maximum possible number of stages. Sample Input 1 4 2 Sample Output 1 3 Sample Input 2 5 2 Sample Output 2 4
[ { "submission_id": "aoj_0394_10934103", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)\n\nint main() {\n\tcin.tie(nullptr);\n\tios_base::sync_with_stdio(false);\n\t\n\tll N, K;\n\tcin >> N >> K;\n\t\n\tll...
aoj_0389_cpp
Dungeon 2 Bob is playing a game called "Dungeon 2" which is the sequel to the popular "Dungeon" released last year. The game is played on a map consisting of $N$ rooms and $N-1$ roads connecting them. The roads allow bidirectional traffic and the player can start his tour from any room and reach any other room by way of multiple of roads. A point is printed in each of the rooms. Bob tries to accumulate the highest score by visiting the rooms by cleverly routing his character "Tora-Tora." He can add the point printed in a room to his score only when he reaches the room for the first time. He can also add the points on the starting and ending rooms of Tora-Tora’s journey. The score is reduced by one each time Tora-Tore passes a road. Tora-Tora can start from any room and end his journey in any room. Given the map information, make a program to work out the maximum possible score Bob can gain. Input The input is given in the following format. $N$ $p_1$ $p_2$ $...$ $p_N$ $s_1$ $t_1$ $s_2$ $t_2$ $...$ $s_{N-1}$ $t_{N-1}$ The first line provides the number of rooms $N$ ($1 \leq N \leq 100,000$). Each of the subsequent $N$ lines provides the point of the $i$-th room $p_i$ ($-100 \leq p_i \leq 100$) in integers. Each of the subsequent lines following these provides the information on the road directly connecting two rooms, where $s_i$ and $t_i$ ($1 \leq s_i < t_i \leq N$) represent the numbers of the two rooms connected by it. Not a single pair of rooms are connected by more than one road. Output Output the maximum possible score Bob can gain. Sample Input 1 7 6 1 -1 4 3 3 1 1 2 2 3 3 4 3 5 5 6 5 7 Sample Output 1 10 For example, the score is maximized by routing the rooms in the following sequence: $6 \rightarrow 5 \rightarrow 3 \rightarrow 4 \rightarrow 3 \rightarrow 2 \rightarrow 1$. Sample Input 2 4 5 0 1 1 1 2 2 3 2 4 Sample Output 2 5 The score is maximized if Tora-Tora stats his journey from room 1 and ends in the same room.
[ { "submission_id": "aoj_0389_10875058", "code_snippet": "#include <bits/stdc++.h>\n\n#define rep(i, n) for (int i = 0; i < (int)(n); ++i)\n#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)\n#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)\n#define rrep2(i, a, b) for (int i = (int)(b)...
aoj_0398_cpp
Beautiful Sequence Alice is spending his time on an independent study to apply to the Nationwide Mathematics Contest. This year’s theme is "Beautiful Sequence." As Alice is interested in the working of computers, she wants to create a beautiful sequence using only 0 and 1. She defines a "Beautiful" sequence of length $N$ that consists only of 0 and 1 if it includes $M$ successive array of 1s as its sub-sequence. Using his skills in programming, Alice decided to calculate how many "Beautiful sequences" she can generate and compile a report on it. Make a program to evaluate the possible number of "Beautiful sequences" given the sequence length $N$ and sub-sequence length $M$ that consists solely of 1. As the answer can be extremely large, divide it by $1,000,000,007 (= 10^9 + 7)$ and output the remainder. Input The input is given in the following format. $N$ $M$ The input line provides the length of sequence $N$ ($1 \leq N \leq 10^5$) and the length $M$ ($1 \leq M \leq N$) of the array that solely consists of 1s. Output Output the number of Beautiful sequences in a line. Sample Input 1 4 3 Sample Output 1 3 The sequences with length 4 that include 1s in successive array of length 3 are: 0111, 1110 and 1111. Sample Input 2 4 2 Sample Output 2 8 The sequences with length 4 that include 1s in successive array of length 2 are: 0011, 0110, 0111, 1011, 1100, 1101, 1110 and 1111.
[ { "submission_id": "aoj_0398_9703022", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\nconst int mod = 1e9 + 7;\nll fast_power(ll a , ll p) {\n if(p == 0)return 1;\n ll res = fast_power(a , p / 2);\n res = res * res % mod;\n if(p & 1)res = a % mod * res % m...
aoj_0391_cpp
Meeting in a City You are a teacher at Iazu High School is the Zuia Kingdom. There are $N$ cities and $N-1$ roads connecting them that allow you to move from one city to another by way of more than one road. Each of the roads allows bidirectional traffic and has a known length. As a part of class activities, you are planning the following action assignment for your students. First, you come up with several themes commonly applicable to three different cities. Second, you assign each of the themes to a group of three students. Then, each student of a group is assigned to one of the three cities and conducts a survey on it. Finally, all students of the group get together in one of the $N$ cities and compile their results. After a theme group has completed its survey, the three members move from the city on which they studied to the city for getting together. The longest distance they have to travel for getting together is defined as the cost of the theme. You want to select the meeting city so that the cost for each theme becomes minimum. Given the number of cities, road information and $Q$ sets of three cities for each theme, make a program to work out the minimum cost for each theme. Input The input is given in the following format. $N$ $Q$ $u_1$ $v_1$ $w_1$ $u_2$ $v_2$ $w_2$ $...$ $u_{N-1}$ $v_{N-1}$ $w_{N-1}$ $a_1$ $b_1$ $c_1$ $a_2$ $b_2$ $c_2$ $...$ $a_Q$ $b_Q$ $c_Q$ The first line provides the number of cities in the Zuia Kingdom $N$ ($3 \leq N \leq 100,000$) and the number of themes $Q$ ($1 \leq Q \leq 100,000$). Each of the subsequent $N-1$ lines provides the information regarding the $i$-th road $u_i,v_i,w_i$ ($ 1 \leq u_i < v_i \leq N, 1 \leq w_i \leq 10,000$), indicating that the road connects cities $u_i$ and $v_i$, and the road distance between the two is $w_i$. Each of the $Q$ lines that follows the above provides the three cities assigned to the $i$-th theme: $a_i,b_i,c_i$ ($1 \leq a_i < b_i < c_i \leq N$). Output For each theme, output the cost in one line. Sample Input 1 5 4 1 2 3 2 3 4 2 4 2 4 5 3 1 3 4 1 4 5 1 2 3 2 4 5 Sample Output 1 4 5 4 3 In the first theme, traveling distance from City 3 (the student conducts survey) to City 2 (meeting venue) determines the cost 4. As no other meeting city can provide smaller cost, you should output 4. In the second theme, you can minimize the cost down to 5 by selecting City 2 or City 4 as the meeting venue. Sample Input 2 5 3 1 2 1 2 3 1 3 4 1 4 5 1 1 2 3 1 3 5 1 2 4 Sample Output 2 1 2 2 Sample Input 3 15 15 1 2 45 2 3 81 1 4 29 1 5 2 5 6 25 4 7 84 7 8 56 4 9 2 4 10 37 7 11 39 1 12 11 11 13 6 3 14 68 2 15 16 10 13 14 13 14 15 2 14 15 7 12 15 10 14 15 9 10 15 9 14 15 8 13 15 5 6 13 11 13 15 12 13 14 2 3 10 5 13 15 10 11 14 6 8 11 Sample Output 3 194 194 97 90 149 66 149 140 129 129 194 111 129 194 140
[ { "submission_id": "aoj_0391_10875318", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\nstruct SEG{\nprivate:\n int n;\n vector<int> node;\npublic:\n SEG(int N){\n n = 1;\n while(n < N) n *= 2;\n node.resize(2*n-1);\n }\n void updat...
aoj_0399_cpp
Payroll PCK company has $N$ salaried staff and they have specific ID numbers that run from $0$ to $N-1$. The company provides $N$ types of work that are also identified by ID numbers from $0$ to $N-1$. The time required to complete work $j$ is $t_j$. To get each of the working staff to learn as many types of work as possible, the company shifts work assignment in the following fashion. On the $d$-th day of a shift (the first day is counted as $0$th day), work $j$ is assigned to the staff $(j + d) \% N$, where the notation $p \% q$ indicates the remainder when $p$ is divided by $q$. Salaries in the PCK company are paid on a daily basis based on the hourly rate. When a staff whose hourly rate is $a$ carries out work $j$, his/her salary will amount to $a \times t_j$. The initial hourly rate for a staff $i$ is $s_i$ yen. To boost staff’s motivation, the company employs a unique scheme for raising hourly rates. The promotion scheme table of the company contains a list of factors $f_k$ ($0 \leq k \leq M-1$), and the hourly rate of a staff is raised based on the following rule. At the completion of the $d$-th day’s work (the first day is counted as the $0$th day), the hourly rate of the staff with ID number $d \% N$ is raised by $f_{(d \% M)}$ yen. The salary raise takes effect after the day’s pay has been made. As the person in charge of accounting in PCK company, you have to calculate the total amount of pay for $D$ days from the $0$-th day. Make a program to work out the total amount of salary given the following information: initial hourly rate of each staff, hours required to complete a job, a table of promotion coefficients, and the number of days. Because the total may become excessively large, report the remainder when the total salary amount is divided by $1,000,000,007(= 10^9 + 7)$. Input The input is given in the following format. $N$ $M$ $D$ $s_0$ $s_1$ $...$ $s_{N-1}$ $t_0$ $t_1$ $...$ $t_{N-1}$ $f_0$ $f_1$ $...$ $f_{M-1}$ The first line provides the number of working staff and types of work $N$ ($1 \leq N \leq 100$), the number of factors $M$ ($1 \leq M \leq 100$)and the number of days $D$ ($1 \leq D \leq 10^{15}$). Note that $M + N$ is equal to or less than 100. The second line provides the initial hourly rate for each staff $s_i$ ($1 \leq s_i \leq 10^8$) as integers. The third line provides the hours $t_j$ ($1 \leq t_j \leq 10^8$) required to complete each type of job as integers. The fourth line lists the factors $f_k$ ($1 \leq f_k \leq 10^8$) in the promotion scheme table. Output Output the total amount of the salary. Sample Input 1 3 2 2 3 2 1 1 2 3 1 2 Sample Output 1 26 Sample Input 2 3 2 5 3 2 1 1 2 3 1 2 Sample Output 2 91
[ { "submission_id": "aoj_0399_6039851", "code_snippet": "#ifdef LOGX\n#define _GLIBCXX_DEBUG\n#endif\n#include<bits/stdc++.h>\nusing namespace std;\n\nusing ll=long long;\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define rep2(i,s,n) for(int i=int(s);i<(int)(n);i++)\n#define all(a) (a).begin(),(a).end()\...
aoj_0396_cpp
Treasure Map Mr. Kobou found a bundle of old paper when he was cleaning his family home. On each paper, two series of numbers are written. Strange as it appeared to him, Mr. Kobou further went through the storehouse and found out a note his ancestor left. According to it, the bundle of paper is a treasure map, in which the two sequences of numbers seem to give a clue to the whereabouts of the treasure the ancestor buried. Mr. Kobou’s ancestor divided the area where he buried his treasure in a reticular pattern and used only some of the grid sections. The two series of numbers indicate the locations: the $i$-th member of the first series indicates the number of locations in the $i$-th column (form left) of the grid sections where a part of the treasure is buried, and the $j$-th member of the second indicates the same information regarding the $j$-th row from the top. No more than one piece of treasure is buried in one grid section. An example of a 5 × 4 case is shown below. If the pieces of treasure are buried in the grid sections noted as " # " the two series of numbers become "0,2,2,1,1" and "1,1,1,3". 0 2 2 1 1 1 # 1 # 1 # 3 # # # Mr. Kobou’s ancestor seems to be a very careful person. He slipped some pieces of paper with completely irrelevant information into the bundle. For example, a set of number series "3,2,3,0,0" and "4,2,0,0,2" does not match any combination of 5 × 5 matrixes. So, Mr. Kobou has first to exclude these pieces of garbage information. Given the set of information written on the pieces of paper, make a program to judge if the information is relevant. Input The input is given in the following format. $W$ $H$ $a_1$ $a_2$ $...$ $a_W$ $b_1$ $b_2$ $...$ $b_H$ The first line provides the number of horizontal partitions $W$ ($1 \leq W \leq 1000$) and vertical partitions $H$ ($1 \leq H \leq 1000$). The second line provides the $i$-th member of the first number series $a_i$ ($0 \leq a_i \leq H$) written on the paper, and the third line the $j$-th member of the second series $b_j$ ($0 \leq b_j \leq W$). Output Output " 1 " if the information written on the paper is relevant, or " 0 " otherwise. Sample Input 1 5 4 0 2 2 1 1 1 1 1 3 Sample Output 1 1 Sample Input 2 5 5 3 2 3 0 0 4 2 0 0 2 Sample Output 2 0
[ { "submission_id": "aoj_0396_10934164", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)\n\nint main() {\n\tcin.tie(nullptr);\n\tios_base::sync_with_stdio(false);\n\t\n\tint W, H;\n\tcin >> W >> H;\n\tvecto...
aoj_0395_cpp
Round Table of Sages $N$ sages are sitting around a round table with $N$ seats. Each sage holds chopsticks with his dominant hand to eat his dinner. The following happens in this situation. If sage $i$ is right-handed and a left-handed sage sits on his right, a level of frustration $w_i$ occurs to him. A right-handed sage on his right does not cause such frustration at all. If sage $i$ is left-handed and a right-handed sage sits on his left, a level of frustration $w_i$ occurs to him. A left-handed sage on his left does not cause such frustration at all. You wish you could minimize the total amount of frustration by clever sitting order arrangement. Given the number of sages with his dominant hand information, make a program to evaluate the minimum frustration achievable. Input The input is given in the following format. $N$ $a_1$ $a_2$ $...$ $a_N$ $w_1$ $w_2$ $...$ $w_N$ The first line provides the number of sages $N$ ($3 \leq N \leq 10$). The second line provides an array of integers $a_i$ (0 or 1) which indicate if the $i$-th sage is right-handed (0) or left-handed (1). The third line provides an array of integers $w_i$ ($1 \leq w_i \leq 1000$) which indicate the level of frustration the $i$-th sage bears. Output Output the minimum total frustration the sages bear. Sample Input 1 5 1 0 0 1 0 2 3 5 1 2 Sample Output 1 3 Sample Input 2 3 0 0 0 1 2 3 Sample Output 2 0
[ { "submission_id": "aoj_0395_11026284", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nint main(){\n int N;\n std::cin >> N;\n \n std::vector<int> a(N), w(N);\n for (int i = 0; i < N; i++) {\n std::cin >> a[i];\n }\n for (int i = 0; i < N; i++) {\n...
aoj_0397_cpp
Common-Prime Sort You are now examining a unique method to sort a sequence of numbers in increasing order. The method only allows swapping of two numbers that have a common prime factor. For example, a sequence [6, 4, 2, 3, 7] can be sorted using the following steps. Step 0: 6 4 2 3 7 (given sequence) Step 1: 2 4 6 3 7 (elements 6 and 2 swapped) Step 2: 2 6 4 3 7 (elements 4 and 6 swapped) Step 3: 2 3 4 6 7 (elements 6 and 3 swapped) Depending on the nature of the sequence, however, this approach may fail to complete the sorting. You have given a name "Coprime sort" to this approach and are now examining if a given sequence is coprime-sortable. Make a program to determine if a given sequence can be sorted in increasing order by iterating an arbitrary number of swapping operations of two elements that have a common prime number. Input The input is given in the following format. $N$ $a_1$ $a_2$ $...$ $a_N$ The first line provides the number of elements included in the sequence $N$ ($2 \leq N \leq 10^5$). The second line provides an array of integers $a_i$ ($2 \leq a_i \leq 10^5$) that constitute the sequence. Output Output " 1 " if the sequence is coprime-sortable in increasing order, or " 0 " otherwise. Sample Input 1 5 6 4 2 3 7 Sample Output 1 1 Sample Input 2 7 2 9 6 5 6 7 3 Sample Output 2 0
[ { "submission_id": "aoj_0397_10994353", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std; \n/*#include <boost/multiprecision/cpp_dec_float.hpp>\n#include <boost/multiprecision/cpp_int.hpp>\n#include <boost/rational.hpp>\nusing namespace boost::multiprecision;*/\n/*#include <atcoder/all>\nusing...