group_id
stringlengths
12
18
problem_description
stringlengths
85
3.02k
candidates
listlengths
3
20
aoj_1465_cpp
Scheduling Two Meetings You are the chief judge of the International Collegiate Quiz Contest (ICQC) this year. You want to hold judge meetings twice for preparing the problem set of the contest. You proposed candidate schedules for the meetings, and all the judges answered for each of the time slots whether they would attend the meeting on-site or remotely via a video conference tool. You have to choose a pair of two distinct time slots, so that every judge attends at least one of the two meetings on-site. When there are multiple such pairs, you’d like to choose one with the largest number of judges attending both meetings on-site. If multiple pairs are still equally desirable with these criteria, one with the earlier first meeting is preferred. If there still remain multiple pairs with the same time slot for the first meeting, the one with the earliest second meeting should be chosen. Input The input consists of a single test case of the following format. $n$ $m$ $a_{1,1}$ ... $a_{1,m}$ $\vdots$ $a_{n,1}$ ... $a_{n,m}$ Two integers $n$ and $m$ are given in the first line. The first integer $n$ ($2 \leq n \leq 2 \times 10^5$) is the number of candidate time slots. Here, the candidate time slots are numbered 1 through $n$, and smaller numbers mean earlier time slots. The second integer $m$ ($2 \leq m \leq 20$) is the number of judges. In the following $n$ lines, $a_{i,j}$ is either a character Y indicating that the $j$-th judge attends a meeting at the $i$-th candidate time slot on-site, or a character N indicating remote attendance. Output Output a line containing the two time slot numbers of the most preferable choice, separated by a space, with the earlier time slot first. If there are no pairs of time slots satisfying the condition, output No . Sample Input 1 4 3 NNY YYN YNY NYY Sample Output 1 2 3 Sample Input 2 3 6 NNNYYY YYNYYN YYYNNN Sample Output 2 1 3 Sample Input 3 6 5 NNNNN YNNNY YYNNN YYNNN NYYNY NNYYY Sample Output 3 3 6 Sample Input 4 3 3 YNN NYN NNY Sample Output 4 No Sample Input 5 4 4 NYNN YNYY YNYN NNYY Sample Output 5 1 2
[ { "submission_id": "aoj_1465_11055654", "code_snippet": "#include <bits/stdc++.h>\n#include <numeric>\nusing namespace std;\n\nusing i64 = int64_t;\n\n#define rep(i, n) for(i64 i = 0; i < (n); i++)\n#define each(x, a) for(auto& x: a)\n\ntemplate<class T, T id, T (*op)(T,T)>\nstruct SegTree{\n\ti64 n=1;\n\tv...
aoj_1463_cpp
Greatest of the Greatest Common Divisors You are given a sequence of integers and a number of intervals in the sequence. The intervals are specified by their leftmost and rightmost positions. An interval consisting of $k$ integers has $k(k − 1)/2$ pairs of integers at different positions, which have their greatest common divisors. For each given interval, find the greatest one among such greatest common divisors. For example, when the sequence is ($a_1, . . . , a_6) = (10, 20, 30, 40, 50, 60)$, and the whole sequence is specified as the interval, the following 15 pairs of two integers at different positions $x$ and $y$, and their greatest common divisors should be considered. x 1 1 1 1 1 2 2 2 2 3 3 3 4 4 5 y 2 3 4 5 6 3 4 5 6 4 5 6 5 6 6 ax 10 10 10 10 10 20 20 20 20 30 30 30 40 40 50 ay 20 30 40 50 60 30 40 50 60 40 50 60 50 60 60 gcd(ax, ay) 10 10 10 10 10 10 20 10 20 10 10 30 10 20 10 The greatest of the greatest common divisors of the 15 pairs is $gcd(30, 60) = 30$, in this case. Input The input consists of a single test case of the following format. $n$ $a_1$ ... $a_n$ $q$ $l_1$ $r_1$ $\vdots$ $l_q$ $r_q$ The first line contains an integer n, which is the number of integers in the given sequence, satisfying $2 \leq n \leq 10^5$. The second line contains n positive integers a1 through an, specifying the sequence. None of them exceeds $10^5$. The third line contains a positive integer $q$, specifying the number of intervals in the sequence to be considered, which does not exceed $10^5$. It is followed by $q$ lines, each specifying an interval in the sequence to be considered. The $i$-th line of them has two integers, $l_i$ and $r_i$ ($1 \leq l_i < r_i \leq n$), specifying the interval $a_{l_i}$ through $a_{r_i}$ in the sequence. Output Output $q$ lines, the $i$-th line of which should have the greatest of the greatest common divisors of all pairs in the interval specified by $l_i$ and $r_i$. Sample Input 1 6 10 20 30 40 50 60 3 1 6 2 5 4 5 Sample Output 1 30 20 10 Sample Input 2 10 13 2 35 4 13 2 5 1 7 4 5 1 4 4 10 3 8 3 9 1 10 Sample Output 2 2 4 5 7 13
[ { "submission_id": "aoj_1463_11052822", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll INF = 1LL << 60;\n#define rep(i, a) for(ll i = 0; i < (a); i++)\n#define all(a) begin(a), end(a)\n#define sz(a) (a).size()\nbool chmin(auto &a, auto b) { return a > b ? a ...
aoj_1506_cpp
Problem G: Dice サイコロ3兄弟は三つ子であり一心同体、3つでひとつとも言ってもよい。三つ子であるため見分けがつかないほど似ている。そんなサイコロ3兄弟はA君のお気に入りのおもちゃだ。A君はいつも3つ1セットでサイコロを転がして遊んでいる。このようにA君が楽しく遊ぶおもちゃだが、実は大きな秘密があった。彼らは実は生きていて、話したり自由に動いたりできるのだ。子ども部屋は r × c の大きさのグリッドで表すことができ、とても大きいものが散乱している。 サイコロ3兄弟は室内を東西南北の4方向へ進行方向に向かって転がりながら移動する。その形ゆえに斜め移動ができないのが、少し気の毒である。進行方向にとても大きいものがある場合、サイコロは進行方向に進むことができない。 サイコロ3兄弟は3つで1セットであるため、一度にひとつのサイコロしか移動することができない。また、サイコロたちはその場で回転するなどという器用な能力は持っていない。 彼らが話したり動いたりしている事実は人間には知られてはいけないというのが「おもちゃのルール」である。このルールを破ったおもちゃはこの世から消滅してしまう。A君が出かけているときに動いたり話したりしているおもちゃたちだが、A君が帰宅して子ども部屋に入ってきたときにもとの位置に戻っていなければならない。A君が帰宅した直後は緊急事態であり、A君が子ども部屋にたどり着く前にあらゆるおもちゃは即座にもとの場所へ戻らなければならない。 A君は床を見下ろすかたちになるため、サイコロの場所と上を向いている数字が正しければ、移動したことに気づくことはない。また、サイコロたちは見分けがつかないほど似ているので、どの収納場所にどのサイコロが向かっても支障はない。 そんなある日、A君のおもちゃ箱の中で最も優秀なプログラマーであるあなたに、与えられたサイコロの位置情報と収納場所から最短何手でサイコロ達が収納場所に戻れるかを計算する任務が与えられた。 サイコロ3兄弟を溺愛しているA君の手からサイコロ3兄弟が消えてしまったら、A君は泣いてしまうので、とても重要な任務である。 Input 入力は以下のフォーマットで与えられる。 r c grid 1 . . . grid r grid i は長さ c の文字列で、1から6までの数字か"."か”x”か”o”からなる。 数字は収納場所かつ、到着時のサイコロの上の値を示している。 “.”は普通の床を示している。 “x”はとてもおおきなものを示している。 “o”はサイコロを示している。 入力は以下の制約を満たす 1 ≤ r,c ≤ 20 初期状態でのサイコロの向きは以下の図に従う。 Output それぞれの入力に対する、各サイコロがすべて収納できるまでの最短手数を求めよ。 Sample Input 1 3 3 4o. 4o. 4o. Sample Output 1 3 各サイコロが西方向に移動すればよい。 Sample Input 2 4 4 4o.. .o3. .o.. .5.. Sample Output2 3 1行目のにあるサイコロが西方向 2行目のにあるサイコロが東方向 3行目のにあるサイコロが南方向に移動すればよい。 Sample Input 3 6 10 1xxxxxxxxx o.......o. xxxx.xxxx4 xoxx.xxxx. x....3xx.. x..xxxxx.. Sample Output 3 34
[ { "submission_id": "aoj_1506_10349794", "code_snippet": "// AOJ #1506 Dice\n// 2025.4.5\n\n#include <bits/stdc++.h>\nusing namespace std;\nconst int INF = 10000000;\n\nint R, C;\nchar g[25][25];\nint goalX[3], goalY[3], goalTop[3];\n\nint tableArr[6][4] = {\n {1, 3, 4, 2},\n {0, 2, 5, 3},\n {0, 4, ...
aoj_1502_cpp
Problem C : War A国とB国という2つの国が戦争をしている。A国の軍人であるあなたは n 人の兵士を率いて、B国の領土を占領する事になった。 B国の領土は2次元グリッドで表されている。あなたが最初に占領する場所は二次元グリッド上のある1マスである。 あなたが率いている兵士たちは、それぞれ h i の体力を持っている。 それぞれの兵士は体力を1を消費して移動することができる。 現在いるマスを(a,b)とすると、(a+1,b),(a-1,b),(a,b+1),(a,b-1)の4方向を移動先として選ぶことが可能である。 兵士は体力が0になったらそこから動くことができなくなる。 一人以上の兵士が通過したマスはすべて占領することができる。 あなたの仕事は、最大でいくつのマスを占領することができるかを求めることである。 ただし、この2次元グリッドのサイズは無限に広いとすること。 Input 入力は以下のフォーマットで与えられる。 n h 1 . . . h n 入力は以下の制約を満たす 1 ≤ n ≤ 500 1 ≤ h i ≤ 10,000,000 Output 答えの値を1行に出力せよ Sample Input 1 2 5 5 Sample Output 1 11 Sample Input 2 10 10 10 10 10 10 10 10 10 10 10 Sample Output 2 93 Sample Input 3 5 1 2 3 4 5 Sample Output 3 15
[ { "submission_id": "aoj_1502_7916695", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)\n\nusing namespace std;\nusing ll = long long;\n\nconst int MAX_N = 1e7+10;\nint main(){\n\tint N; cin >> N;\n\tvector<int> H(N);\n\trep(i, N) cin >> H[i];\n\n\tvector<ll> num(M...
aoj_1466_cpp
Peculiar Protocol The Kingdom of Icpca has a peculiar protocol in wedding ceremonies. That is, amounts of monetary gifts should be a multiple of a fixed quantity plus a fixed extra. When the fixed quantity is $d$ and the fixed extra is $r$, courteous amounts of wedding gifts are $k \times d + r$ for any non-negative integer multipliers $k$. Initially, you have a pile of $n$ banknotes. Every time you attend a wedding ceremony, you draw out a contiguous portion from your current banknote pile as your gift that sums up to a courteous amount, that is, a multiple of $d$ plus additional $r$. If no contiguous portion sums up to such an amount, you cannot attend wedding ceremonies any more. After drawing out, the remaining banknotes are squeezed to form a single pile, keeping their relative order. The resultant pile of banknotes may have portions with such an amount, which allows you to attend more ceremonies. Your monetary gifts are expected to raise your social reputation. As the additional amount r is considered mandatory, the multiplier $k$ is considered significant. Your reputation is raised in proportion to $k$ at each of the ceremonies you attend. For example, assume $d = 5$ and $r = 1$, and you have banknotes whose values are 2, 2, 2, 4, and 4, piled up in this order. When you attend a wedding ceremony, there are following two possible ways to give courteous monetary gifts. Give a monetary gift consisting of the first three banknotes from the top, totaling $2+2+2 = 6 = 1 \times d+r$. After drawing them out, you have banknotes with values 4 and 4. No contiguous portion of your remaining banknote pile sums up to a courteous amount. Thus, you cannot attend wedding ceremonies anymore. Give a monetary gift consisting of the third and the fourth banknotes, totaling $2+4 = 6 = 1 \times d+r$. After drawing them out, you have banknotes with values 2, 2, and 4, in this order. You can attend another wedding ceremony because the second and the third banknotes total $2+4 = 6 = 1 \times d+r$, which is courteous. In this example, the second way can maximize your social reputation by attending two ceremonies, because the total of the multipliers is $1 + 1 = 2$, which achieves the maximum possible. In contrast, if the value of the first banknote is 12, giving the first three banknotes at a ceremony prevents you from attending more ceremonies. That, however, maximizes your social reputation because the total of the multipliers is 3, which achieves the maximum possible. Compute the maximum possible total of the multipliers with your monetary gifts at wedding ceremonies. You may assume that you have so many unmarried relatives and friends that you can attend any number of wedding ceremonies as long as you can give courteous monetary gifts. Input The input consists of a single test case in the following format. $n$ $d$ $r$ $a_1$ ... $a_n$ The first line has three integers $n$, $d$, and $r$. The integer $n$ ($1 \leq n \leq 500$) is the number of banknotes you have. Th ...(truncated)
[ { "submission_id": "aoj_1466_10437156", "code_snippet": "// AOJ #1486 Peculiar Protocol\n// 2025.5.1\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll INF = (ll)1e18;\n\n#define gc() getchar_unlocked()\n\nint Cin() {\n\tint n = 0, c = gc();\n\tdo n = 10*n + (c & 0xf), c = gc...
aoj_1501_cpp
Problem B : Grid r × c の2次元グリッド上の2つの座標 ( a 1 , a 2 ) と ( b 1 , b 2 ) が与えられる。 あるマス (e,f) から (e+1,f)、(e-1,f)、(e,f+1)、(e,f-1) のどれかのマスに移動するコストを1とする。 また、(e,c-1) と (e,0)、(r-1,f) と (0,f) の間もコスト1で移動することができる。 この時に1つ目の座標から2つ目の座標へ最短コストで移動できる経路の数を求めよ。 Input 入力は以下のフォーマットで与えられる。 r c a 1 a 2 b 1 b 2 入力は以下の制約を満たす 1 ≤ r , c ≤ 1,000 0 ≤ a 1 , b 1 < r 0 ≤ a 2 , b 2 < c Output 答えの値を100,000,007で割った余りを出力せよ。 Sample Input 1 4 4 0 0 3 3 Sample Output 1 2 Sample Input 2 4 4 0 0 1 1 Sample Output 2 2 Sample Input 3 2 3 0 0 1 2 Sample Output 3 4 Sample Input 4 500 500 0 0 200 200 Sample Output 4 34807775
[ { "submission_id": "aoj_1501_7916754", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n) for(int i =0; i<(n); i++)\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\nconst int mod = 100000007;\nconst int inf ( 1<<30);\n\nint main(){\n\tint r,c; cin>>r>>c;\n\tvector<vector<ll>> ...
aoj_1504_cpp
Problem E : Connect r × c のグリッドが与えられる。 グリッドのいくつかのマスには1から8までの数字が書かれている。 数字が書かれているマスと他の数字が書かれているマスを線で結ぶ必要がある。 数字が書かれているマスについて、そのマスに書かれている本数だけ、他のマスとの間に線を結ぶ必要がある。 ただし、線を結べるのは上下左右方向だけで、一つの方向について、最大2本までである。 他の数字を跨いで線で結ぶことはできない。 また、次のように交差してしまう結び方をすることはできない。 グリッドが入力として与えられるので、数字が書かれているマス全てを正しく結ぶ方法が、何通りあるのかを数えて欲しい。 Input 入力は以下のフォーマットで与えられる。 r c grid 1 . . . grid r-1 grid i は長さcの文字列で、1から8までの数字か"."からなる。 入力は以下の制約を満たす 1 ≤ r,c ≤ 10 Output 答えの値を100,000,007で割った余りを1行に出力せよ Sample Input 1 3 3 .1. 1.1 .1. Sample Output 1 0 Sample Input 2 1 3 1.1 Sample Output 2 1 Sample Input 3 3 3 4.2 ... 2.. Sample Output 3 1 Sample Input 4 7 7 2.3.1.1 ....... 2...... ....... ..3.3.. ....... 2.2.4.3 Sample Output 4 10
[ { "submission_id": "aoj_1504_10344187", "code_snippet": "// AOJ #1504 Connect\n// 2025.4.2\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst int MOD = 100000007;\n\nint dp[2][3][60005];\nint d[11];\n\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n int H, W;\n...
aoj_1500_cpp
Problem A : ID A大学ではIDの入力ミスが多発していた。 そこで、A大学は入力ミス防止のため新しいIDを発行することにした。 新しいIDには入力ミス防止のためにIDが正しいかどうかチェックする方法がある。 ・全ての桁の数字の総和を求める。 ・ただし、右端の桁を1番目として、偶数番目の桁の数字を2倍にする。 ・2倍することによって数字が10以上になった時、1の位の数字と10の位の数字を加算した数字をその桁の数字とする。 ・総和が10で割り切れれば正しいID、そうでなければ間違いとする。 例として、53579というIDをチェックする。 全ての桁の数字の総和を求めるので、 5 + 3 + 5 + 7 + 9 ただし、偶数番目の桁の数字を2倍にするので、 5 + 6 + 5 + 14 + 9 2倍することによって数字が10以上になった時、1の位の数字と10の位の数字を加算した数字をその桁の数字とするので、 5 + 6 + 5 + (1 + 4) + 9 以上より、総和は30となる。30は10で割り切れるので、53579は正しいIDである。 B君はA大学の大学生であり、新しいIDを発行してもらったが、IDの一部の桁を忘れてしまった。 しかし、忘れてしまった部分にどんな数字が入るか、いくつか候補を絞ることに成功した。 あなたの仕事は、B君のIDの正しい組み合わせが何通りあるかを求めることである。 Input 入力は以下のフォーマットで与えられる。 n ID m a 0 a 1 ... a m-1 n は ID の桁の数である。 ID の各桁には0~9の数字または忘れた桁であるということを示す'*'という文字が入る。 m は'*'に入る数字の候補の数である。 a i は'*'に入る数字の候補である。 入力は以下の制約を満たす 1 ≤ n ≤ 100,000 1 ≤ m ≤ 10 0 ≤ a i ≤ 9 1 ≤ '*'の数 ≤ 7 Output 答えの値を1行に出力せよ Sample Input 1 5 5*57* 2 3 9 Sample Output 1 1 Sample Input 2 15 2***9*2*6*1199* 9 0 1 2 3 4 6 7 8 9 Sample Output 2 478297
[ { "submission_id": "aoj_1500_6006661", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <stdio.h>\n#include <vector>\nusing namespace std;\nconst int maxn=100010;\n\nint a[10]={0,2,4,6,8,1,3,5,7,9};\nint choose[10];\nint sum=0,sum2=0,sum3=0;\nint ans=0;\nint n,m;\nvoid dfs2(int x,int ss)...
aoj_1505_cpp
Problem F : Dungeon (I) あなたはとあるゲームの開発に携わっている。 そのゲームはランダムに生成されたダンジョンをプレイヤーが探索するというものである。 このゲームで生成されるダンジョンには n 個の部屋が存在しており、0から n-1 までの番号が割り振られている。 部屋と部屋は通路で結ばれている。部屋と部屋を結ぶ通路は、 m 本ある。 通路はどちらの方向へも進むことができる。 また、部屋と部屋の間には距離が設定されている。 生成されたダンジョンではいくつかの通路を経由して、ある部屋から他のすべての部屋へ行くことが可能である。 そして、プレイヤーがゲームを行う際に、0番の部屋がスタート地点、 n-1 番の部屋がゴール地点として選ばれる。 ユーザーがダンジョンを探索する際に、手助けになるようなアイテムが入った宝箱を、どこかの部屋に設置したい。 その時に、スタート地点から遠すぎたり、ゴール地点から近すぎると意味がない。 そこで、スタートから距離 fs 以内で辿りつけて、ゴールに辿り着くために最短経路を通った場合に、少なくとも fg かかる部屋を宝箱を設置する候補としたい。 生成されたダンジョンと、 q が与えられる。 q 個のクエリーについて、宝箱を設置する場所の候補の数を数えて欲しい。 Input 入力は以下のフォーマットで与えられる。 n m a 1 b 1 c 1 . . . a m b m c m q fs 1 fg 1 . . . fs q fg q a i b i c i は 部屋 a i と b i を結ぶ通路の距離が c i であることを表す。 入力は以下の制約を満たす 2 ≤ n ≤ 100,000 0 ≤ a i , b i < n 0 ≤ c i ≤ 100,000 n-1 ≤ m ≤ 100,000 1 ≤ q ≤ 100,000 0 ≤ fs i , fg i ≤ 2 60 Output 各クエリーについて、答えの値を1行に出力せよ Sample Input 1 4 4 0 1 3 1 3 4 0 2 2 2 3 1 4 0 0 2 2 2 1 3 4 Sample Output 1 1 1 2 1
[ { "submission_id": "aoj_1505_10000615", "code_snippet": "#include <cstdio>\n#include <algorithm>\n#include <vector>\n#include <queue>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<ll,int> P;\n#define pu push\n#define pb push_back\n#define mp make_pair\n#define INF 10000000000000000\nint n,m;\nl...
aoj_1503_cpp
Problem D : Numbers n が与えられるので、 n 個の連続した正の整数を求めよ。 ただしすべての数が、1とその数自身以外の約数をもたなくてはならない。 Input 入力は以下のフォーマットで与えられる。 n 入力は以下の制約を満たす。 1 ≤ n ≤ 1,500 Output 最初の行に、あなたが選んだ連続した n 個の正の整数の中で一番小さいものを出力せよ。 2行目から n+1 行目に、それぞれの値に対する約数を出力せよ。 約数は1かその数自身でなければどの値を出力しても良い。 1行目に出力した数を x として、i行目には x+i-2 の約数を出力せよ。 出力する値は5,000桁を超えてはいけない。 Sample Input 1 2 Sample Output 1 8 2 3 Sample Input 2 3 Sample Output 2 8 2 3 5 Hint Sample Output 2では、8,9,10を3個の連続した整数として選んでいる。 2行目に、8の約数として2,3行目は9の約数として3,4行目には10の約数として5,を出力している。
[ { "submission_id": "aoj_1503_3430839", "code_snippet": "#include<bits/stdc++.h>\n#include <boost/multiprecision/cpp_dec_float.hpp>\n#include <boost/multiprecision/cpp_int.hpp>\n#include <boost/rational.hpp>\nusing namespace std;\n\n#define lint long long\n#define P pair<int, int>\n#define LLP pair<long long...
aoj_1507_cpp
Problem H : Dungeon (II) あなたはとあるゲームの開発に携わっている。 そのゲームはランダムに生成されたダンジョンをプレイヤーが探索するというものである。 ゲームの仕様として、プレイヤーに予めダンジョンの危険度を提示し、生成されたダンジョンを探索するのか、それとも新しくダンジョンを生成しなおすかを、選択できるようにしたい。 このゲームで生成されるダンジョンには n 個の部屋が存在しており、0から n-1 までの番号が割り振られている。 部屋と部屋は通路で結ばれている。部屋と部屋を結ぶ通路は、合計で n-1 本存在している。 通路はどちらの方向へも進むことができる。 また、部屋と部屋の間には距離が設定されている。 生成されたダンジョンではいくつかの通路を経由して、ある部屋から他のすべての部屋へ行くことが可能である。 そして、プレイヤーがゲームを行う際に、2つの異なる部屋がスタート地点とゴール地点として選ばれる。 あなたはダンジョンの評価を行うために、危険度の評価方法を決めることにした。 まず、ある部屋から別の部屋までに移動する際の危険度を、部屋間を最短で移動するために使う通路の中で、最もコスト高い通路の値とする。 そして、ダンジョンの危険度を、 i < j となる部屋のペアの間を移動する際の危険度の総和とすることにした。 ランダムに生成されたダンジョンのが入力として与えられる。 まず、 i < j となるすべての部屋のペアについて、移動する際の危険度を計算して欲しい。 そして、その総和を問題の答えとして出力せよ。 Input 入力は以下のフォーマットで与えられる。 n a 1 b 1 c 1 . . . a n-1 b n-1 c n-1 a i b i c i は 部屋 a i と b i を結ぶ通路の距離が c i であることを表す。 入力は以下の制約を満たす 2 ≤ n ≤ 200,000 0 ≤ a i , b i < n 0 ≤ c i ≤ 100,000 Output 答えの値を1行に出力せよ Sample Input 1 4 0 1 3 1 2 5 1 3 2 Sample Output 1 23 Sample Input 2 6 0 2 5 2 1 1 2 3 10 3 5 4 3 4 2 Sample Output 2 111
[ { "submission_id": "aoj_1507_10772260", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define lint long long\n#define P pair<int, int>\n#define LLP pair<long long, long long>\n#define REP(i, x, n) for(int i = (x), i##_len = (int)(n) ; i < i##_len ; ++i)\n#define rep(i, n) for(int i = 0, ...
aoj_1509_cpp
Problem A: Rental DVD Shop NEO Problem 僕はレンタルDVDショップ、「NEO」でアルバイトを始めた。まずはこの店の料金システムを勉強することになった。 レンタルDVDは旧作、準新作、新作の3種類存在し、1本のレンタル料金がそれぞれ a 円、 b 円、 c 円である。 会計の際に以下に示すセットレンタルを複数回適用することができる。 まだセットレンタルを適用していないDVDを数本選ぶ。 選んだDVDの本数が d 本以上の場合、選んだDVDの合計金額が、 (選んだDVDの本数) * e 円を超える場合は、それらを (選んだDVDの本数) * e 円でレンタル出来る。 選んだDVDの本数が d 本未満の場合、選んだDVDの合計金額が d * e 円を超える場合は、それらを d * e 円でレンタル出来る。 上記に当てはまらない場合、選んだDVDは通常料金でレンタルとなる。 ここで僕はある問題に気づいた。セットレンタルはレジに通した時に自動的に適用されるのではなく、手動で適用しているのだ。これでは最適ではない(もっと安くなる余地がある)セットレンタルの適用をしてしまう可能性がある。これではクレームが発生しかねない。僕はクレーム対応が嫌いなので、セットレンタルを最適に適用した時の料金を計算するプログラムを作成することにした。 Input 入力は複数のデータセットからなる。 各データセットは以下で表される。 1行目は5つの整数 a , b , c , d , e がスペース区切りで与えられる。 2行目にはレンタル本数が与えられる。3つの整数 na , nb , nc がスペース区切りで与えられる。それぞれ旧作、準新作、新作のDVDの本数を表す。 入力の終わりは5つのゼロからなる。 a b c d e na nb nc Constraints 入力は以下の条件を満たす。 入力に含まれる値はすべて整数 0 < a < b < e < c ≤ 1000 0 < d ≤ 100000 0 ≤ na , nb , nc ≤ 100000 0 < na + nb + nc データセットの数は100個以下 Output 各データセットにつき、セットレンタルを最適に適用した時の料金を1行に出力せよ。 Sample Input 70 100 340 4 200 1 1 4 70 100 340 4 200 0 1 3 70 100 340 4 200 1 1 2 0 0 0 0 0 Sample Output 970 800 800
[ { "submission_id": "aoj_1509_8293237", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nlong long A, B, C, D, E;\nlong long NA, NB, NC;\n\nint main() {\n\twhile (true) {\n\t\t// Step 1. Input\n\t\tcin >> A >> B >> C >> D >> E; if (A + B + C + D + E == 0)...
aoj_1510_cpp
Problem B: Independent Research Problem 僕は夏休みの自由研究のテーマとして、生き物観察を選択し、生き物観察キットを購入した。 この生き物は3次元グリッド状の空間を好んで生息する。 各セルには最大1匹までしか入れない。 周囲の環境に合わせ1日が経過するごとに誕生と死滅を繰り返す。 誕生と死滅の条件は、セルに隣接する生き物の数に依存する。 ここで、あるセルに生き物が隣接するとは、あるセルと生き物が生息しているもう一つのセルが面、辺、または点を共有していることを言う。 誕生と死滅のルールは次のようになる。 生き物が生息していないセルにおいて、隣接する生き物の数が a i (1 ≤ i ≤ M 1 ) 匹であるような i がある場合、そのセルに生き物が誕生する。 生き物が生息しているセルにおいて、隣接する生き物の数が b j (1 ≤ j ≤ M 2 ) 匹であるような j がない場合、そのセルの生き物は死滅する。 今回購入した飼育箱はセルの数が5*5*5である立方体の飼育箱だ。 この飼育箱ではこの生き物はどのような振る舞いをするのだろうか…? とても楽しみである。 〜数日後〜 とりあえず、飼育してみたが…  毎日観察するなんて時間がかかるし、僕にはめんどくさくてやる気がおきない。 そうだ、コンピューターとプログラムを使ってシミュレートしよう。 Input 入力は複数のデータセットからなる。 各データセットは以下のフォーマットで与えられる。 N ( z = 0 の飼育箱の状態) (空行) ( z = 1 の飼育箱の状態) (空行) ( z = 2 の飼育箱の状態) (空行) ( z = 3 の飼育箱の状態) (空行) ( z = 4 の飼育箱の状態) (空行) M 1 a 1 a 2 … a M 1 M 2 b 1 b 2 … b M 2 最初にシミュレートする日数 N が与えられる。 次に飼育箱の初期状態の情報が与えられる。これは5つの5*5の2次元グリッドで与えられる。 各2次元グリッドは0と1から成り立ち、1は生き物がいることを示し、0は何もいないことを示す。 例えば、 z = 0のセルの状態の2次元グリッドの4行2列目の値が1だった場合、飼育箱の座標(1, 3, 0)の位置に生き物がいることを表す。 次に、整数 M 1 が与えられ、そのあとに M 1 個の数字 a i が与えられる。 次に、整数 M 2 が与えられ、そのあとに M 2 個の数字 b j が与えられる。 入力の終わりは N = 0で表される。 Constraints 入力は以下の条件を満たす。 1 ≤ N ≤ 100 0 ≤ M 1 , M 2 ≤ 27 0 ≤ a i , b j ≤ 26 (1 ≤ i ≤ M 1 , 1 ≤ j ≤ M 2 ) 任意の i , j (1 ≤ i < j ≤ M 1 ) において、 a i ≠ a j 任意の i , j (1 ≤ i < j ≤ M 2 ) において、 b i ≠ b j Output 各データセットに対し、 N 日経過後の状態を出力せよ。 出力は以下のフォーマットに従う。 Case (テストケースの番号): ( z = 0 の飼育箱の状態) (空行) ( z = 1 の飼育箱の状態) (空行) ( z = 2 の飼育箱の状態) (空行) ( z = 3 の飼育箱の状態) (空行) ( z = 4 の飼育箱の状態) 1行目にテストケース番号を出力し、次の行から N 日経過後の状態として、5*5の2次元グリッドを5つ出力せよ。 各テストケースの出力間には空行を出力しなさい。 Sample Input 5 00000 01010 00000 00100 00000 00000 01010 00000 01010 00000 00000 00100 00000 01010 00000 00000 01010 00000 00100 00000 00000 00000 00100 00000 00000 1 2 2 3 4 4 01110 00100 00100 00100 01110 01110 10001 10000 10001 01110 11111 10001 11111 10000 10000 01110 10001 10000 10001 01110 00000 00000 00000 00000 00000 2 3 4 1 2 100 00100 01010 01110 10001 10001 01110 00100 00100 00100 01110 00000 00000 00000 00000 00000 11111 00010 00100 01000 11111 10001 10001 10001 10001 01110 5 1 3 5 7 9 5 0 2 4 6 8 0 Sample Output Case 1: 00000 00000 01010 01110 10101 00000 10001 00000 00000 00000 00000 00000 11111 10001 00000 00000 00000 00000 00000 01010 00000 00000 01110 00100 11111 Case 2: 00010 10110 00000 11000 10000 00001 00000 10101 00010 00100 10001 00000 00000 00000 00010 01110 10001 00000 00001 10000 00000 00000 00111 01001 01000 Case 3: 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000
[ { "submission_id": "aoj_1510_2169979", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,n)for(int i=0;i<(n);i++)\nusing namespace std;\n\nint f[2][5][5][5], m1, a[27], m2, b[27];\nint main() {\n\tint n, cnt = 1;\n\twhile (scanf(\"%d\", &n), n) {\n\t\tif (cnt != 1)printf(\"\\n\");\n\t\trep(i, 5)rep(j,...
aoj_1511_cpp
Problem C: General of Taiko Problem とあるゲームセンターには、曲に合わせて流れてくる譜面通りに和太鼓を叩くゲームがあります。 譜面は長さ L のセルからなり、各セルには何もない、またはノートと呼ばれるプレイヤーがとるべき行動を表した記号があります。 ノートは2種類ありそれぞれ、和太鼓の面を叩く「トン」、和太鼓の縁を叩く「コツ」があります。 これらのノートに合わせて和太鼓を叩くと得点を得ることができます。 この得点の合計が10000点以上であればクリアとなります。 このとき、プレイヤーが曲をクリアできる確率を求めなさい。ただし、プレイヤーは常に最適な行動をとることにします。 プレイヤーが譜面通りに和太鼓を叩く精度は11段階あり、それぞれ、 0%, 10%, 20%, ..., 90%, 100%の確率で譜面通りに和太鼓を叩くことができます。 上記の二種類の動作は右腕と左腕どちらででも行うことができます。 プレイヤーの情報として、それぞれの腕で行った場合の精度の安定率を表す値が16種類、下記のように与えられます。 lt _ lt lt _ rt lt _ lk lt _ rk rt _ lt rt _ rt rt _ lk rt _ rk lk _ lt lk _ rt lk _ lk lk _ rk rk _ lt rk _ rt rk _ lk rk _ rk ただし、 lt は左腕でトン, rt は右腕でトン, lk は左腕でコツ, rk は右腕でコツの動作を表します。 例えば、 lt _ rk は左腕でトンのあとに右腕でコツを行った時の精度の安定の度合いを表し、この値によって左腕でトンのあとに右腕でコツを行うときの精度が、下記のように変化します。 プレイヤーの精度 = max(0, (精度の安定率 - 10) * 10 + 一つ前の精度) (%) 曲の情報は以下のとおりです。 曲の長さを表す L 、譜面を表す L 個の数字 s i (0 ≤ i < L )で示される。譜面の先頭は s 0 である。 s i の値は以下の3つです。 0 ... ノートなし 1 ... トン 2 ... コツ プレイヤーが最初に和太鼓を叩くときの精度は100%です。 また、プレイヤーは譜面を無視することができます。 ノートがなかったり、ノートを無視した場合、プレイヤーの精度は100%になります。 各ノートに合わせて和太鼓を叩いたときの得点は下記のようになります。 得点 = A + B * min(コンボ数, 10) この問題におけるコンボ数とは、連続してノートに合わせて和太鼓を叩けた数です。 プレイヤーがノートに合わせて叩いた場合、上記の式を元に得点が入り、その後にコンボ数が1増えます。 ノートに合わせて和太鼓を叩けなかった場合、コンボが途切れ、コンボ数が0になります。 Input 入力は複数のデータセットからなります。 各データセットは以下のとおりです。 lt _ lt lt _ rt lt _ lk lt _ rk rt _ lt rt _ rt rt _ lk rt _ rk lk _ lt lk _ rt lk _ lk lk _ rk rk _ lt rk _ rt rk _ lk rk _ rk L s 0 s 1 s 2 … s L - 1 A B 入力の終わりは負の整数4つからなります。 Constraints 入力は以下の条件を満たします。 0 < L ≤ 100 0 ≤ 精度の安定率 ≤ 10 精度の安定率は整数 0 < A , B ≤ 10000 A と B はともに100の倍数 データセットの数は100個以下 Output 各入力に対して、クリアできる確率を1行で出力しなさい。 ただし、出力は0.001以下の誤差を含んでも良いです。 Sample Input 9 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 5 1 1 1 1 1 1000 500 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 5 1 0 2 0 1 1000 2000 3 8 6 10 0 1 6 8 10 2 4 7 8 6 6 8 19 2 2 0 2 2 0 2 1 0 1 2 0 1 2 0 1 0 2 2 200 100 -1 -1 -1 -1 Sample Output 0.3024000000 0.0000000000 0.5120000000
[ { "submission_id": "aoj_1511_9613108", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <stack>\n#include <queue>\n#include <set>\n#include <map>\n#include <cassert>\n#include <random>\n#include <iomanip>\n#pragma GCC optimize(\"O3\...
aoj_1512_cpp
Problem D: Smartphone Game Problem 人気のスマフォゲームの一機能を実装してみよう。ゲームは以下の仕様に基づく。 5*5のマスに、5種類のブロックが設置されている。 それぞれの種類のブロックには得点が設定されている。 得点には、ブロックの得点の他に、ボーナス得点が設定されている。最初のボーナス得点は1である。 ブロックを任意に1つだけ決めて、最大で n 回まで上下左右に移動できる。移動先のブロックは移動元のブロックのあった場所に移動する。つまり、隣接したブロックを交換する事になる。 移動した後、同じ種類のブロックが縦3個以上又は横3個以上に並んだ場合、並んだブロックは全て消える。 消えるべきブロックが全て消えた後、あるブロックの下に別のブロックが存在しない場合、そのブロックは落下する。落下とは、あるブロックの1つ上に辿り着くまで下に移動する事をいう。ブロックが下に存在しない場合は一番下に移動する。 全てのブロックの落下後にボーナス得点が1だけ加算される。 その後にブロックが並んだ場合、さらに消え、落下する。 ブロックが消える時に、ブロック1つにつき「ブロックの得点*ボーナス得点」が自分の得点に加算される。 1回のプレイで得られる最大の点数を求めよ。 Input 入力は複数のデータセットからなる。 各データセットは以下で表される。 n a 11 .. a 15 . . a 51 .. a 55 score 1 .. score 5 a ij は1から5までの数字で、ブロックの種類を表す。 score i は i 種類目のブロック1つ分の得点を表す。 n = -1 の時、入力が終了する。 Constraints 入力は以下の条件を満たす。 0 ≤ n ≤ 5 1 ≤ a ij ≤ 5 0 ≤ score i ≤ 100 テストケースの数は 10 を超えない。 入力に含まれる値は全て整数である。 Output 各データセット毎に、答えを一行に出力しなさい。 Sample Input 0 1 1 1 5 5 5 5 5 5 5 5 5 1 5 5 5 1 1 1 5 5 5 5 5 5 0 0 0 0 1 2 1 2 3 4 5 2 3 4 5 5 2 3 4 5 5 3 4 5 5 1 5 4 3 2 1 100 99 98 97 96 5 1 2 3 4 5 2 3 4 5 1 1 2 3 4 5 5 4 3 2 1 1 2 3 4 5 99 79 31 23 56 -1 Sample Output 17 2040 926
[ { "submission_id": "aoj_1512_10235110", "code_snippet": "// AOJ #1512 Smartphone Game\n// 2025.2.20\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nconst int NROWS = 5, NCOLS = 5;\nint dr[4] = {-1,1,0,0};\nint dc[4] = {0,0,-1,1};\n\nvector<int> blockScore(6);\n\nint simulateChain(vector<vector<int>> bo...
aoj_1514_cpp
Problem F: Ballon Contest Problem 空の様子がいつもと違う。色とりどりの多彩な熱気球が空を覆っていた。今日は熱気球の大会だ。 熱気球から落とされた得点付きボールを参加者全員で取り合うらしい。 せっかくなので優勝者を予想してみることにした。 レクリエーションにはN人参加する。 N 人の参加者はそれぞれ自分の位置が与えられる。複数の参加者に同じ位置が与えられる事はない。 上空の熱気球からM個のボールが1個ずつ落下する。 参加者は全員同じタイミングで走り始め、ボールに向かって同じ速度で一直線に走る。 ボールの落下位置に一番早く辿り着けた人がボールを取得できる。同時に複数人辿り着いた場合は、一様な確率で取得できる人が決まる。 参加者がボールを取得すると参加者全員が元の位置に戻る。 参加者が走り始めてから参加者の全員が元の位置に戻るまでに別のボールが落下する事はない。 各ボールには得点と落下する位置が与えられ、ボールを取得すると得点を得られる。 ボールは落下中に空気抵抗を受けるため、実際に落下する地点にはズレが発生する。落下予定の位置より最大でX軸方向に± dx 、Y軸方向に± dy だけ一様な確率でズレる。 得られる得点の期待値を求め、期待値の最も大きい参加者の期待値を出力せよ。 Input 入力は複数のデータセットからなる。 各データセットは以下で表される。 N M x 1 y 1 . . x N y N bx 1 by 1 dx 1 dy 1 score 1 . . bx M by M dx M dy M score M 1行目には、参加者の人数 N 、ボールの数 M が与えられる。 2行目から N +1行目までには、参加者の情報が与えられる。 x i , y i は、それぞれ参加者の位置のX座標・Y座標である。 N +2行目から N + M +1行目までには、ボールの情報が与えられる。各ボールが実際に落下する地点のX座標・Y座標はそれぞれ bx j - dx j から bx j + dx j まで、 by j - dy j から by j + dy j までの範囲のどこかである。 score j はボールの得点である。 入力の終わりは2つのゼロからなる。 Constraints 入力は以下の条件を満たす。 1 ≤ N ≤ 100 1 ≤ M ≤ 10 0 ≤ x i , y i , bx j , by j ≤ 10000 1 ≤ dx j , dy j ≤ 10000 1 ≤ score j ≤ 100 テストケースの数は 10 を超えない。 入力に含まれる値は全て整数である。 Output 各データセット毎に、答えを一行に出力しなさい。 出力は0.0001以下の誤差を含んでもよい。 Sample Input 3 4 10 75 50 5 90 75 50 50 10 10 2 40 90 1 1 3 10 20 10 15 1 50 70 50 50 4 4 2 25 25 25 75 75 75 75 25 50 50 10 10 1 50 50 15 15 2 1 1 5 5 1 1 1 1 1 0 0 Sample Output 5.442857 0.750000 1.000000
[ { "submission_id": "aoj_1514_9729453", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cmath>\nusing namespace std;\n\n#define EPS 1e-8\n#define INF 1e100\n\ntypedef complex<double> P;\ntypedef vector<P> vP;\n\nconst P iu(0.0, 1...
aoj_1522_cpp
Planarian Regeneration Problem 皆さん、「プラナリア」って知っていますか? プラナリアとは、日本では川の上流の石や枯葉などの裏に張り付いて生息している水生生物です。 プラナリアの最も優れた能力はその「再生能力」です。プラナリアの再生能力は著しく、例えば、三等分するとそれぞれが再生し、数週間後には元の完全な状態のプラナリアが3匹できあがります。 この度、会津の山奥の上流にて発見された新種のプラナリアの姿は長方形をしていて、プラナリアはある法則により再生します。 このとき、プラナリアは下の図のように配置し、2つの手段でプラナリアの切断実験を行います。 まず1つ目の手段として、垂直方向に切ることを考えます。一度の切断では、すべての断片を水平方向の長さが(断片の頭に近い部分):(断片の頭に遠い部分)= m : n になるように切断します。この動作を x 回繰り返します。 次に2つ目の手段として、水平方向に切ることを考えます。一度の切断では、すべての断片を垂直方向の長さが(断片の右端に近い部分):(断片の左端に近い部分)= k : l に切断します。この動作を y 回繰り返します。これらの切断によって断片が移動することはありません。 数週間後に、各断片が元の完全なプラナリアの状態に再生している確率は以下の式により求まります。 上の図は、垂直方向に1:2で2回、水平方向に1:1で1回切断した状態です。 以上の操作で切断した時の、数週間後に元の姿に再生しているプラナリアの数の期待値を求めてください。元のプラナリアの垂直方向の長さと水平方向の長さはともに1とします。 Input m n x k l y 入力では、6つの整数 m , n , x , k , l , y が上記の入力フォーマットで与えられます。 これら6つの整数は問題文中のものと対応しており、水平方向に m : n で切断する動作を x 回、垂直方向に k : l で切断する動作を y 回繰り返します。 1 ≤ m , n , k , l ≤ 100、 0 ≤ x , y ≤ 40 であり、 m , n と l , k はそれぞれ互いに素です。 Output 一行に数週間後に元の姿に再生しているプラナリアの数の期待値を出力してください。 出力は 10 -6 以下の誤差ならば許容されます。 Sample Input 1 1 1 1 1 1 1 Sample Output 1 0.562500 Sample Input 2 1 2 2 1 1 1 Sample Output 2 0.490741 Sample Input 3 1 2 0 3 4 0 Sample Output 3 1.000000 Notes この問題では、doubleよりも精度の高い浮動小数を使用することを推奨します。
[ { "submission_id": "aoj_1522_2102518", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\nusing ld = long double;\nconst ld eps = 1e-7;\n\nld getans(ld l,ld r, int b,ld c) {\n\tif (b == 0||abs(r-l)<eps) {\n\t\treturn (1-l)*(r - l);\n\t}\n\tld aa = getans(l,(1-c)*l+c*r, b - 1, c);\n\tld ab = ...
aoj_1515_cpp
Problem G: The Humans Braving the Invaders Problem 現在、地球は宇宙からの侵略者であるインベーダーの攻撃を受け、人類で生き残った者は基地にいる我々だけとなった。 奴らに対抗できる戦力もほとんど残されていない。 だが、ここで我々は諦めるということはしない。 インベーダを全滅させることが、我々人類が生き残る最後の手段である。 今から最後になるであろう作戦の内容を説明する。 まず、インベーダーの戦力に比べ我々の戦力はあまりにも少ないので、この基地に立てこもり、籠城戦を行う。 この基地は高い山に囲まれていて、インベーダーが侵攻するには基地正面にある真っ直ぐな道を通る他無い。 この道をフィールドと呼ぶことにする。この特徴により、前方に集中してインベーダーを攻撃することが可能である。 基地にいる我々はインベーダーに対し2種類の武器で攻撃を行う。 1つはインベーダー1体を狙撃するスナイパーライフルである。もう1つは広範囲に攻撃ができるグレネードランチャーである。 人類でたった1人のプログラマーである君の仕事は、インベーダーと我々の行動記録を元に戦闘をシミュレーションすることだ。 行動記録はクエリー形式で与えられる。各クエリーは1つまたは複数の整数からなり、次のように与えられる。 0 インベーダーがフィールド上の、基地からの距離 L の位置に出現する。 1 d 現在フィールド上にいるすべてのインベーダーが d だけ基地に近づく。 この動作の後、基地に到達したインベーダーからダメージを受け、また、それらの敵はフィールド上から消滅する。 ダメージを受けた場合は"damage (この時基地に到達したインベーダーの数)"を1行に出力する。 2 k 現在フィールド上にいるインベーダーの数が k 体以上の場合、基地に近い方から k 番目のインベーダーをスナイパーライフルで攻撃する。 そのインベーダーはフィールド上から消滅する。そして"hit"を1行に出力する。 フィールド上のインベーダーの数が k 体未満の場合、"miss"を1行に出力する。 3 x r 基地からの距離が x の位置に範囲 r のグレネードランチャーで攻撃する。基地からの距離が x の位置に着弾し、そこからの距離が r 以下の位置にいるすべてのインベーダーはフィールド上から消滅する。 そして"bomb (倒したインベーダーの数)"を1行に出力する。補足として、基地はグレネードランチャーのダメージを受けることはない。 4 k フィールド上のインベーダーの数が k 体以上の場合、"distance (基地に近い方からk番目のインベーダーと基地との距離)"を1行に出力する。 フィールド上のインベーダーの数が k 体未満の場合、"distance -1"を1行に出力する。 作戦の説明は以上だ。総員、作戦開始!・・・幸運を祈る。 Input 入力は複数のデータセットからなる。 各データセットは以下で表される。 1行目は2つの整数 Q , L がスペース区切りで与えられる。 続く Q 行には、上記で説明したクエリーが Q 個与えられる。 入力の終わりは2つのゼロからなる。 Constraints 入力は以下の条件を満たす。 入力に含まれる値はすべて整数 1 ≤ Q ≤ 100000 1 ≤ L ≤ 10 9 1 ≤ d , k ≤ 10 9 0 ≤ x ≤ L 0 ≤ r ≤ 10 9 同じ位置にインベーダーが2体以上存在することはない データセットの数は3個以下 Output 各データセットにつき、出力の指示があるクエリーに対して出力せよ。各データセットの最後には"end"を出力せよ。 Sample Input 18 10 0 4 1 1 1 4 1 0 1 1 0 2 2 1 10 0 1 1 0 1 1 0 1 5 3 4 0 3 4 1 0 9 10 4 1 2 2 3 5 5 0 4 1 2 2 3 5 5 0 2 1 0 0 Sample Output distance 10 distance 9 hit damage 2 bomb 1 bomb 2 end distance -1 miss bomb 0 distance 10 miss bomb 1 hit end
[ { "submission_id": "aoj_1515_8293599", "code_snippet": "#include <iostream>\n#include <vector>\n#include <set>\n#include <algorithm>\nusing namespace std;\n\nclass BIT {\npublic:\n\tint size_ = 1;\n\tvector<int> dat;\n\n\tvoid init(int sz) {\n\t\tsize_ = sz + 2;\n\t\tdat.resize(size_ + 2, 0);\n\t}\n\n\tvoid...
aoj_1508_cpp
Problem I : RMQ n 個の数字 a 0 , a 1 , ... , a n-1 と q が与えられる。 q 個のクエリーに対して適切な処理を行なって欲しい。 クエリーは以下の3種類の操作が存在する。 値のシフトを行う l と r のペアが与えられる。( l < r ) a l から a r までの値を Circular Shiftさせる。 0 1 2 3 4 5 6 7 8 9 に対してl=2,r=5というクエリーが与えられたとする。 シフトを行った数字列は 0 1 5 2 3 4 6 7 8 9 となる。 最小値を求める l と r のペアが与えられる。( l ≤ r ) a l から a r までの値の中で最小の値を求める。 値の更新 pos と val のペアが与えられる。 a pos の値を val に更新する。 Input 入力は以下のフォーマットで与えられる。 n q a 0 a 1 . . . a n-1 x 1 y 1 z 1 x 2 y 2 z 2 . . . x q y q z q x i = 0 なら、シフトのクエリーを表す。このとき、 l = y i , r = z i とする。 x i = 1 なら、最小値を求めるクエリーを表す。このとき、 l = y i , r = z i とする。 x i = 2 なら、値を更新するクエリーを表す。このとき、 pos = y i , val = z i とする。 入力は以下の制約を満たす 1 ≤ n , q ≤ 200,000 0 ≤ a i ≤ 10,000,000 値を更新するクエリーについて、0 ≤ val ≤ 10,000,000 Output クエリーとして x i = 1 が与えられたときに、答えの値を1行に出力せよ。 Sample Input 1 10 3 0 1 2 3 4 5 6 7 8 9 1 5 7 0 2 5 1 5 7 Sample Output 1 5 4 Sample Input 2 10 10 289383 930886 692777 636915 747793 238335 885386 760492 516649 641421 2 7 368690 1 3 6 0 2 6 1 1 8 0 2 9 2 2 665123 1 5 9 0 2 8 2 7 961393 1 1 2 Sample Output 2 238335 238335 238335 368690
[ { "submission_id": "aoj_1508_11059198", "code_snippet": "// Begin include: \"../../template/template.hpp\"\nusing namespace std;\n\n// intrinstic\n#include <immintrin.h>\n\n#include <algorithm>\n#include <array>\n#include <bitset>\n#include <cassert>\n#include <cctype>\n#include <cfenv>\n#include <cfloat>\n...
aoj_1513_cpp
Problem E: Doragoso Ball Problem L 個集めるとドラゴソが現れどんな願いでも一つだけ叶えてくれるというドラゴソボールがとある迷宮に散らばっている。入口からスタートし、途中で立ち止まることなくすべてのボールを回収し、それらを迷宮内にある祭壇に奉納し、そのまま呪文を唱えるとドラゴソが現れ願いを叶えてくれるという。ただし、呪文を唱え始める時間は入口からボールを奉納するまでのルートのうち、移動時間が K 番目に短いルートで移動したときの時間でなければならない。 この迷宮は部屋とわたり通路からなり、部屋と部屋はわたり通路を通る事によって行き来できる。ドラゴソボールはそれらの数ある部屋のどこかに落ちている。 なお、迷宮は入り口から祭壇へたどりつく道が存在しない可能性もあるようだ。また、移動時間はわたり通路の移動だけを考慮し、部屋の中を移動する時間、ボールを拾うときにかかる時間、祭壇に奉納する時間は考慮しなくてよい。 また、同じ通路や同じ部屋に何度も訪れてもよいが、ドラゴソボールがある部屋に初めて入った場合は、そのボールを拾うことにする。祭壇に到達したとき、必ずしもボールを納めなければならないわけではない。 迷宮の内部構造の情報と迷宮の入り口、祭壇の場所とボールの数 L とボールの配置場所と求められている最短な経路の順位 K が与えられるので、呪文を唱え始めるべき時間を求めよ。 また、異なる移動ルートでも、移動時間が同じである場合はそれらのルートは同じ順位として扱う。 例えば、 ルートA 時間 2 ルートB 時間 5 ルートC 時間 5 ルートD 時間 5 ルートE 時間 5 ルートF 時間 7 このようなとき、ルートB,C,D,Eは2位かつ3位かつ4位かつ5位として扱う。ルートFは6位として扱う。 Input 入力は複数のデータセットからなる。 各データセットは以下のフォーマットで表される。 N M L K u 1 v 1 c 1 … u M v M c M S G B 1 B 2 … B L 最初に N , M , L , K が与えられる。 それぞれ、部屋の数、わたり通路の数、ボールの数、求められている最短な経路の順位を表す。それぞれの部屋は1〜 N で番号付けされる。 次に M 行にわたり通路の情報、 u i , v i , c i が与えられる。 部屋 u i , v i 間にわたり通路が存在し、通過するのに時間 c i がかかることを表す。 次に S , G が与えられる。 それぞれ、迷宮の入り口と祭壇がある部屋番号を表す。 次に L 行にわたってドラゴソボールが落ちている部屋の番号 B i ( 1 ≤ i ≤ L )が与えられる。 入力の終わりは4つのゼロからなる。 Constraints 入力は以下の条件を満たす。 入力に含まれる値はすべて整数 2 ≤ N ≤ 50 0 ≤ M ≤ 1225 1 ≤ L ≤ 7 1 ≤ K ≤ 10 1 ≤ S , G ≤ N 1 ≤ B i ≤ N ( 1 ≤ i ≤ L ) B i ≠ B j ( 1 ≤ i < j ≤ L ) 1 ≤ u i , v i ≤ N ( 1 ≤ i ≤ M ) 1 ≤ c i ≤ 1000 ( 1 ≤ i ≤ M ) データセットは最大10個である Output 各データセットに対し、呪文を唱え始めるべき時間を1行に出力せよ。 K 番目に短い経路が存在しない場合は"NA"を1行に出力せよ。 Sample Input 9 14 5 10 1 2 1 1 4 5 2 4 4 2 3 3 2 6 2 3 6 3 3 7 2 4 6 5 4 5 6 5 8 8 6 7 3 7 8 1 7 9 9 8 9 2 1 9 1 2 5 7 9 4 5 1 3 1 2 1 1 3 2 2 3 2 2 4 9 3 4 1 1 4 2 4 5 1 10 1 2 1 1 3 2 2 3 2 2 4 9 3 4 1 1 4 2 4 3 1 1 1 2 1 2 3 1 3 4 1 1 4 1 4 3 1 2 1 2 1 2 3 1 3 4 1 1 4 1 4 3 1 10 1 2 1 2 3 1 3 4 1 1 4 1 2 1 1 1 1 2 1 1 2 1 2 1 1 10 1 2 1 1 2 1 4 2 1 4 1 2 1 3 4 1 1 4 2 0 0 0 0 Sample Output 27 6 8 3 5 7 1 19 NA
[ { "submission_id": "aoj_1513_10235149", "code_snippet": "// AOJ #1513 Doragoso Ball\n// 2025.2.20\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n \nstruct Edge { int to, cost; };\n \nstruct State { ll cost; int room, mask; };\n \nstruct StateCompare {\n bool operator()(const St...
aoj_1520_cpp
Problem E: Light Source Problem 二次元平面上に n 個の円状の光源が配置されている。ある光源が光を受けると、下図のようにその光源の中心点から扇状に光が放出される。 光源の中心点は( x , y )、半径は r で表される。 光となる扇形は角 β を中心に- θ /2, + θ /2の方向へ対称に広がり、その半径は α である。 光源の円が扇型の光に完全に覆われるとき、光源はその光を受けたとみなされる。光源から放出される光の強さは、受けた光の強さの合計となる。ただし、放出される光の強さには上限があり、上限を超えた場合、光の強さは上限値と同じになる。なお、ある光や光源によって光が遮られることはない。 ある地点に存在する光源を目的光源とする。目的光源では光が放出されず、受ける光の強さに制限はない。 あなたは座標(0,0)から、ある強さの扇形の光を、任意の方向に1度だけ放出することができる。目的光源が受ける光の強さの合計の最大値を求めるプログラムを作成せよ。ただし、全ての光源において、放出された光が元の光源に戻るような入力データは与えられないと仮定してよい。また、この問題では、領域の境界からの距離が0.000001以内の場合、領域上とみなすことにする。 Input n θ 0 α 0 power x 1 y 1 r 1 θ 1 α 1 β 1 maxpower 1 . . x n y n r n θ n α n β n maxpower n x n+1 y n+1 r n+1 1行目には目的光源を除く光源の数 n が与えられる。 2行目に最初に光を放出する座標(0,0)の情報、3行目からn+2行目に目的でない光源の情報、n+3行目に目的光源の情報がそれぞれ空白区切りで与えられる。 各情報に対して、 x i , y i は光源の中心点の座標、 r i は光源の半径、 θ i は光が広がる角度、 α i は光の半径、 β i は光が向かう方向を表す。2行目の power は最初に放出される光の強さ、3行目からn+2行目の maxpower i は放出される光の上限を表す。 光の方向 β i はx軸の正の向きから反時計回りに回転し、各角度の単位は度とする。 Constraints 入力は以下の条件を満たす。 入力は全て整数からなる 1 ≤ n ≤ 100 -1000 ≤ x i , y i ≤ 1000 1 ≤ r i ≤ 1000 1 ≤ θ i ≤ 180 1 ≤ α i ≤ 1000 0 ≤ β i < 360 0 ≤ power , maxpower i ≤ 1000 Output 目的光源を全て囲うような光の強さの合計の最大値を1行に出力せよ。 Sample Input 1 1 90 10 20 3 3 1 90 10 315 10 6 0 1 Sample Output 1 30 Sample Input 2 2 90 10 10 -3 0 1 45 10 90 10 -6 0 2 30 3 180 10 -9 0 1 Sample Output 2 10
[ { "submission_id": "aoj_1520_2964704", "code_snippet": "#include <bits/stdc++.h>\n#define PI M_PI\n#define EPS 1e-10\nusing namespace std;\ntypedef long long ll;\ntypedef complex<double> P;\n\nstruct light {\n P pos;\n double r, t, a, b;\n ll p;\n};\n\ndouble cross(P a, P b) { return a.real() * b.imag() ...
aoj_1518_cpp
Problem C: Last One Problem あなたは、集合好きな友人Aと一風変わったゲームをしてみる事にした。 n個の要素からなる重複が許される非負の整数の集合Sが与えられる。集合Sに含まれる各要素は非負の p i 進数である。集合Sに対して 以下のStep1~3を1つのターンとして、あなたと相手の二人が交互にターンを繰り返す。各ターンではこの番号の順に操作を行う。 Step 1: 集合Sに含まれる全ての要素を一度取り除き、その取り除いた各要素を2進数に変換して、0を消して1が1つ以上連続する部分に分割し、その分割された部分を全て集合Sに加える。 Step 2: このとき集合Sが空になっていたならば、現在このターンをプレイしているプレイヤーは負ける。 Step 3: 集合Sに含まれる要素aを1つ選び、そのaから 1 ≤ x ≤ a を満たす任意の整数xを引く。 下の図は次の入力が与えられたときの最初の1ターンの進行の仕方のうちの1つの例である。 4 10 3 2 11 16 5 16 AE 最初はあなたのターンだ。与えられた入力に対して両者が最善を尽くすとき、果たしてあなたに勝ち目はあるのだろうか。 Input 入力は次の形式で与えられる。 n p 1 m 1 p 2 m 2 ... p i m i ... p n m n 集合の要素である p i 進数 m i が n 個与えられる。集合の要素の数 n は10 5 を超えない。また、2≦ p i ≦62を満たす。 p i 進数 m i は通常の数字である0~9に加え、アルファベット52文字を使用する。 その順番は012...789ABC...XYZabc...xyzを昇順とし、 i 番目までの文字を使うものとする。また、 p i 進数 m i は10進数で2 18 -1を超えない. Output 与えられた入力に対しあなたが勝てるならwin、負けるならloseを出力せよ。 Sample Input 1 1 10 1 Sample Output 1 win Sample Input 2 2 10 1 10 1 Sample Output 2 lose Sample Input 3 3 25 53AI 43 3BI0 62 pn6 Sample Output 3 win
[ { "submission_id": "aoj_1518_5003973", "code_snippet": "#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\nusing namespace std;\n\nint ctoi(char c){\n if('A' <= c && c <= 'Z'){\n return c - 'A' + 10;\n }else if('a' <= c && c <= 'z'){\n return c - 'a' + 36;\n ...
aoj_1525_cpp
Problem J: J's Final Problem Problem 不思議なダンジョンとは構造の変化を伴うダンジョンである。不思議なダンジョンは階層が深いものから浅いものまで様々なものがあり、深い階層には凶悪なモンスターが生息していたり、財宝が眠っていたりする。ジェイは不思議なダンジョンを研究している研究者である。ある日、新しいダンジョンを掘っていたところ、とても大きく深いダンジョンに繋がってしまった。ジェイはこのダンジョンを「ジェイの最終問題」と名付けた。このダンジョンの調査が、優秀な冒険者であるあなたに依頼されることになった。冒険者の目的はできるだけ深い層のフロアに到達することである。このダンジョンの特徴を以下に述べる。 地上からダンジョンの地下1階の部屋への下りの階段は1つしか存在しない。 各部屋は1つの上りの階段と2つの下りの階段が存在する。 2つの下りの階段のうち片方を右の階段、もう片方を左の階段とする。 冒険者が階段を降りた際に、ダンジョンの構造が変化することがある。 階段を降りるとは、地下 i 階のある部屋から地下 i +1階のある部屋へ降りることを示す。 階段を上った場合はダンジョンに対して何も変化を及ぼさない。 地下 n 階目には階段は存在しない。 最近、ジェイの最終問題に関する石碑が発見された。 どうやら、各階の階段を降りたときの変化がメモとして記されているようだ。 メモの形式は、 num1 direction1 : num2 direction2 という形である。 地下 num1 階のすべての部屋について、direction1の階段を降りている途中で、地下num2階のすべての部屋について、 direction2の階段を降りた先の部屋が消滅し、さらにそこから階段を降りることによって到達できる部屋や階段がすべて消滅する。部屋や階段の消滅に冒険者が巻き込まれた場合、冒険は失敗する。地下 i 階から地下 i +1階へ向かう階段の途中で冒険者が失敗した場合は地下 i 階まで到達したものとして扱う。 石碑において言及されていない階段は、その階段を降りてもダンジョンの構造に変化を及ぼさないことを示す。 冒険者はこの石碑をヒントにして探索を行うことにした。 ダンジョンの深さ n とメモの情報が m 個与えられるので、最大地下何階まで到達できるかを答えなさい。 Input 入力は複数のデータセットからなる。 n m memo 1 memo 2 … memo m 最初に n , m が与えられる。 それぞれ、ダンジョンの変化前の最下層の階数、石碑のメモの数を表す。 次に m 行にわたりメモの情報 num1 direction1 : num2 direction2 が与えられる。 Constraints 入力は以下の条件を満たす。 入力に含まれる値はすべて整数 1 ≤ n ≤ 10000 0 ≤ m ≤ ( n -1)×4 1 ≤ num1 , num2 ≤ n -1 direction1,direction2は"left"と"right"のいずれかである。 Output 最大地下何階まで降りることができるか1行に出力して答えよ。 Sample Input1 10 2 9 left : 9 left 9 right : 9 right Sample Output1 9 Sample Input2 4 5 1 left : 2 right 2 left : 3 right 2 left : 3 left 1 right : 3 left 2 right : 3 right Sample Output2 3
[ { "submission_id": "aoj_1525_3086993", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\nstruct SCC{\n int n;\n vector<vector<int> > G,rG,T,C;\n vector<int> vs,used,belong;\n SCC(){}\n SCC(int sz):n(sz),G(sz),rG(sz),used(sz),belong(sz){}\n \n void add_edge(int f...
aoj_1529_cpp
Problem D: Cheat Case Problem 算数のテストを受けることになった俺達は、今から勉強しても赤点を取ってしまうことを確信し、カンニングで乗り切ることを決意した。 算数のテスト問題を作成するA先生はテスト用紙を手書きで作ることで知られている。彼がテスト問題を作成している時の筆跡情報を、近所の博士に作ってもらった筆跡盗聴鉛筆で盗み取る作戦だ。だが肝心の筆跡情報から問題を復元するプログラムは博士の技術では作ることができなかった。しかも、たとえ問題が復元できたとしても、俺達の頭脳では問題を解くことができない。どうすることもできなくなった俺達は、プログラマーであるあなたに、筆跡情報から問題を復元し、それを解くプログラムの作成を依頼することにした。 今回のテストでは、数式を解く問題が出題される。数式中の演算記号は+(加算),-(減算),・(乗算)の3つのみで、乗算は加算、減算よりも優先して計算される。数式は必ず構文が正しいものが与えられる。また、0以外の数で、先頭が0であるものは与えられない。 問題が書かれる紙はグリッドで表される。筆跡情報は線分の集合で与えられ、各線分はX軸に水平または垂直、もしくは始点、終点が同じ(点)である。この線分は、始点から終点までにあるマスを黒く塗りつぶすことを表す。座標(0, 0)が紙の左上である。数式の各文字(0,1,2,3,4,5,6,7,8,9,+,-,・)は以下で表される。 0 1 2 3 4 5 6 7 8 9 + - ・ A先生は印刷したかのような字を書くことで知られており、各文字は上記以外の書かれ方はしない。また、上記以外の文字が書かれることはない。2つ以上の文字がマスの辺を共有することはない。各文字の位置は、文字を構成するマス(黒いマス)の中で、最も左側にあるもののX座標で表される。数式の文字は、位置が小さい順に(左から右へ)解釈される。2つ以上の文字の位置が等しくなることはない。 Input N X 11 Y 11 X 12 Y 12 X 21 Y 21 X 22 Y 22 : X N1 Y N1 X N2 Y N2 1行目に線分の数を表す1つの整数 N が与えられる。次に線分の情報が N 行で与えられる。線分の情報の i 行目には4つの整数 X i1 , Y i1 , X i2 , Y i2 が空白区切りで与えられる。それぞれ、線分の1つ目の端点のX座標、Y座標、2つ目の端点のX座標、Y座標を表す。 Constraints 入力は以下の条件を満たす。 1 ≤ N ≤ 150 0 ≤ X i1 , Y i1 , X i2 , Y i2 ≤ 200 (1 ≤ i ≤ N ) X i1 = X i2 または Y i1 = Y i2 (1 ≤ i ≤ N ) 計算の途中と結果に現れる数は符号付き32ビット整数に収まる Output 各ケースの計算結果を一行に出力せよ。 Sample Input 1 4 1 1 1 5 3 3 5 3 4 2 4 4 7 1 7 5 Sample Output 1 2 Sample Input 2 23 1 1 3 1 3 1 3 3 3 3 1 3 1 3 1 5 1 5 3 5 5 2 7 2 7 2 7 4 7 4 5 4 5 4 5 6 5 6 7 6 11 4 13 4 13 0 15 0 15 0 15 4 18 2 18 2 21 5 23 5 21 5 21 7 21 7 23 7 23 7 23 9 23 9 21 9 24 0 26 0 24 0 24 4 24 4 26 4 26 0 26 4 Sample Output 2 -328 Hint 1つのグリッドを2度以上塗りつぶすことがある '1'など、1つの線分で表すことが可能でも、2つ以上の線分で表す場合がある Sample Input 1の数式。 Sample Input 2の数式。"-7"の部分のように、ある文字を覆う最小の長方形の内部に別の文字が入り込む場合がある。
[ { "submission_id": "aoj_1529_894238", "code_snippet": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <cassert>\nusing namespace std;\ntypedef pair<int,int> P;\n\nnamespace NS {\n string s;\n int p;\n int exp();\n int term();\n int fact();\n \n int exp() {\n int x = term();...
aoj_1533_cpp
Problem H: Caterpillar とある天空都市に住む大学生のGは、芋虫のいも太郎を飼っている。 彼は、いも太郎に最短歩数で全てのえさを順番に食べるように躾けをした。彼の友人であるあなたは、彼からいも太郎が本当に躾けられているかどうかを調べて欲しいと依頼されたので、プログラムを書くことにした。 Problem 複数の障害物とエサがグリッド状のエリア内に存在する。頭と 5 つの胴体を持つ芋虫がこのエリアを探索する。芋虫が順番に最後までエサを食べたときの最小の歩数を求めよ。 ただし全てのエサを食べることが不可能な場合は -1 を出力すること。なお、エサの上に芋虫の頭が重なったとき、エサを食べることができる。 エリアは以下の文字で構成される。 何もないマス -> ‘.’ 障害物 -> ‘#’ エサ -> ‘1’, ‘2’, ..., ‘9’ 芋虫の初期位置 -> 頭 ‘S’, 胴体 ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ 芋虫は頭 ’S’ から 5 つの胴体 ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ と 順に上下左右の 4 方向に隣接して、それらが連なって構成される。 例えば、芋虫の構成を表したとき、以下の (1) は正しい芋虫の構成であるが、 (2) や (3) のような構成はありえない。 (1) 正しい .bc Sad ..e (2) eが存在しない Sab .dc (3) "Sab" と "cde" とで芋虫が切断されている Sab cde (1) のような正しい芋虫の構成は、入力で与えられた状態から任意の回数の芋虫の移動の間、一貫して保たれる。 つまり、芋虫の頭が、隣接している 4 つのマスのうちのいずれかの空いているマスに一歩進むと、その後に続いて 1 番目の胴体は頭が元あったマスへ動き、2 番目の胴体は 1 番目の胴体が元あったマスへ、 3 番目は 2 番目の元へ‥… というように頭の軌跡をたどるように胴体が動く。以下は、1マス左へ動くときの例である。 移動前 移動後 ...... ...... ...bc. ...cd. ..Sad. .Sabe. ....e. ...... 芋虫の移動できる場所は以下のように定められている。 芋虫は自分の胴体、障害物、エリア外を移動することは出来ない。ただしエサの上を、そのエサを食べずに移動することは出来る。 移動前に胴体が進む先にあった場合でも、移動後に頭と胴体が重ならないならば移動できる。 Input H W N area 入力は H + 1 行で与えられる。1行目には 3つの整数 H , W , N ( 2 ≤ H ≤ 10, 2 ≤ W ≤ 10, 1 ≤ N ≤ 9 ) が書かれている。 H はエリアの高さ、 W は幅、 N がエサの個数を表す ( このとき 6 + N ≤ H × W が必ず成立する ) 。 2 行目から H + 1 行目までの各行には ´S´,´1´, 2´ , … ´9´,´a´,´b´,´c´,´d´,´e´,´#´,´.´ からなる W 文字の文字列が書かれており,各々がエリアの各区画の状態を表している。また ´S´,´a´,´b´,´c´,´d´,´e´ は芋虫の初期状態を表している。初期状態の芋虫に覆われるようにしてエサや障害物は存在しない。また、芋虫の初期位置およびエサの位置は正しく入力されることが保証されている。 Output 芋虫がエサを順番通りに食べたときの最小の歩数、またそれが不可能なら -1 を出力せよ。 Sample Input1 5 8 3 #....... #.####2# #.#.3..# #.###### .1Sabcde Sample Output1 14 Sample Input2 2 6 2 .1.baS .2.cde Sample Output2 7 Sample Input3 2 6 2 .1#baS .2.cde Sample Output3 -1
[ { "submission_id": "aoj_1533_893191", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef pair<int,int> pii;\n\nint H,W,N;\nchar field[51][51];\nconst int dy[]={-1,0,0,1};\nconst int dx[]={0,-1,1,0};\npii esas[11];\nint d[11][11][11][4][4][4][4][4];\n\nint getAng(int cy,int cx,int n...
aoj_1528_cpp
Problem C: Warping Girl Problem 会津魔法学校は, 魔法を使える者が集まる学校である. その学校の生徒の一人であるハルカは, 魔法陣の上でワープの魔法を使う事ができる. 彼女の家から学校には, 長さ L の一直線状の道がある. また, この道には所々魔法陣が描かれている. 彼女は毎日この道を使って学校に通っているため, できるだけ短い時間で学校に辿り着きたいと思っている. そこでプログラマーであるあなたは, 彼女に学校まで行くときにかかる最小の時間を教えてあげることにした. ハルカは, 1 分で距離 1 だけ歩いて先に進むことができる ( *戻ることはできない).また, ワープの魔法陣が書いてある位置 P i で魔法を唱えることで, P i からちょうど D i だけ進んだ場所に T i 分で移動することができる. 移動先に魔法陣がある場合も, 連続してワープ魔法を使用することができる. 彼女の家の位置は 0 、学校の位置は L である. Input L n P 1 D 1 T 1 P 2 D 2 T 2 . . P n D n T n 1行目に道の長さ L , 魔法陣の数 n が与えられる. 次に n 個の魔法陣の状態が与えられる. P i は i 番目の魔法陣がある位置, D i は i 番目の魔法陣からワープする距離, T i は i 番目の魔法陣を使ってワープしたときにかかる時間を表す. Constraints 入力は以下の条件を満たす. 与えられる数は全て整数である 1 ≤ L ≤ 10 9 0 ≤ n ≤ min(10 3 , L) ( min は A, B の小さい方を表す) 0 ≤ P i ≤ L - 1 0 ≤ D i ≤ L - P i 0 ≤ T i ≤ 10 3 P i ≠ P j Output 学校に辿り着くための最小の時間を一行に出力せよ. Sample Input 1 10 3 2 2 1 4 2 1 8 1 1 Sample Output 1 8 Sample Input 2 10 2 2 3 1 3 5 1 Sample Output 2 6 Sample Input 3 1 0 Sample Output 3 1
[ { "submission_id": "aoj_1528_5851546", "code_snippet": "#include<bits/stdc++.h>\n\nusing Int = long long;\nusing namespace std;\n\nconstexpr Int inf = 1e10;\n\nint main(){\n Int L,N;\n cin >> L >> N;\n vector<tuple<Int,Int,Int>>edge;\n for(int i = 0; i < N; ++i){\n Int p,d,t;\n cin...
aoj_1519_cpp
Problem D: Room of Time and Spirit Problem 20XX年、とある科学者がバイオテクノロジーによって強力な人造人間を開発してしまった。この人造人間はコンピュータにより、戦闘の達人たちの細胞を組み合わせて作られているため非常に強力である。 このままでは地球は人造人間に支配されてしまうので、 N 人の戦士たちは人造人間と戦うことにした。しかし今の戦士たちでは到底人造人間に敵わないため、修行をしなければならない。また、 N 人の戦士たちはそれぞれ 1 ~ N の番号が付いている。 この時代、天空都市AIZUには SRLU室と呼ばれる修行のための特殊な部屋が存在した。この部屋での1年は外界の時間の1日と同じであるため、短期間で戦闘力を大幅に上昇することが出来る。同時に部屋に入れる人数の上限は2人である。また、その2人が部屋に入った時、それぞれ戦闘力が上昇する。 戦士たちは残された時間でどのように部屋に入るか検討するため、あなたに以下のクエリを処理するプログラムの作成を依頼した。 与えられるクエリは以下の通りである。 また以下の説明で出てくる A , B はそれぞれ戦士の番号を表す。 A と B が等しい事は無い。 IN A B C A と B が部屋に入り、それぞれ C だけ戦闘力が上昇する。この時、(部屋に入る直前の B の戦闘力) - (部屋に入る直前の A の戦闘力) = C が成り立つ。また、この時 B の戦闘力は A の戦闘力より高い事が保証される。 COMPARE A B A と B の戦闘力の差 (現在の B の戦闘力) - (現在の A の戦闘力)を出力する。もしその差が特定出来なければ、WARNING と出力せよ。 Input N Q query 1 . . . query Q 最初に戦士の人数 N , クエリの数 Q が与えられる。 次に Q 回だけクエリが与えられる。 Constraints 入力は以下の条件を満たす。 2 ≤ N ≤ 100000 1 ≤ Q ≤ 100000 1 ≤ A , B ≤ N 1 ≤ C ≤ 5000 Output 上記のように与えられたクエリを順番に処理していき、 COMPAREの入力が与えられた際に出力を行う。 Sample Input1 3 5 COMPARE 1 2 IN 1 2 5 IN 2 3 3 COMPARE 2 3 COMPARE 1 3 Sample Output1 WARNING 3 11 Sample Input2 4 3 IN 1 4 10 IN 2 3 20 COMPARE 1 2 Sample Output2 WARNING Sample Input3 3 4 IN 2 1 2 IN 3 1 2 COMPARE 1 3 COMPARE 2 3 Sample Output3 -2 2 Sample Input4 10 4 IN 10 8 2328 IN 8 4 3765 IN 3 8 574 COMPARE 4 8 Sample Output4 -3191 Sample Input5 3 5 IN 1 2 5 IN 1 2 5 IN 2 3 10 COMPARE 1 2 COMPARE 2 3 Sample Output5 15 10
[ { "submission_id": "aoj_1519_9725871", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\nint par[100001], D[100001], sum[100001];\n\nint find(int x) {\n if (par[x] != x) {\n int px = par[x];\n par[x] = find(par[x]);\n D[x] += D[px];\n return par[x];\n }\n ...
aoj_1537_cpp
Problem L: WW Problem 19XX年、nつの国で連合国軍が結成された。連合国軍に属する各国では、敵軍の侵略から自国を守るために国内に1つ以上の拠点を作り、周囲を警戒している。図1は連合国の例を示し、長方形が各国を、その中にある円がその国の拠点を表す。 図1 敵軍が侵略してきた場合、それを一番最初に発見した拠点からその情報を連合国軍に属する他国または自国の拠点に発信することになっている。 情報を他の拠点に伝えるには正の整数のコストがかかる。 また、各国では自国の拠点のうち、連合国軍翻訳通信部(ATIS : Allied Translator and Interpreter Section)に属する兵士が配属されている拠点をちょうど1つだけ持っている。 この兵士だけが連合国軍に属する全ての国の言語を話すことができ、その他の兵士は自分の国の言語しか話せない。 その為、情報を受け取った拠点が他国に情報を伝えようとした際にその拠点がATISに属する兵士をもたない場合、そこから自国のATISに属する兵士をもつ拠点に情報を伝え他国に情報を発信してもらう必要がある。 国毎で各拠点に0から順に[その国がもつ拠点の数-1]までの番号が割り振られており、0番目の拠点にATISに属する兵士がいる。 つまり、他国の拠点に情報を伝達できるのは0番目の拠点だけであり、その他の拠点から他国へは通信できない。 また、その国の0番目の拠点に情報が伝わればその国全体に情報が伝わったものとする。 様々な理由から、全ての拠点の間で通信ができるとは限らない。 ある拠点から別の拠点に情報を伝える事ができたとしても、その逆が成り立つとは限らない。 また、ある拠点から別の拠点に情報を伝えた際のコストとその逆でかかるコストが同じとも限らない。 これらのことを踏まえて、ある拠点が敵軍を発見した際に、連合国軍全体に情報が行き渡るコストの総和が最も少ない伝達方法を知りたい。 連合国軍に属する全ての国の0番目の拠点に対して最初に敵軍を見つけた拠点から情報が伝達された時、連合国軍全体に情報が行き渡ったものとする。 敵軍を発見した拠点が質問として与えられるので、最も少ないコストでその情報が連合国軍全体に行き渡る伝達方法とそのコストを出力せよ。 例えば、以下のケースについて考える ( Sample Input 4、1つめの質問 ) 国neepalの1番目の拠点が敵軍を発見したとする。 この場合、1番目の拠点から他国へは通信できないため、まず自国の0番目の拠点へ情報を伝える。 次に、0番目の拠点から国luxenbourg,nowayに情報を伝える。 neepalからluxenbourgの0番目の拠点へ直接通信することはできるが、3番目の拠点に情報を伝え、そこから0番目の拠点へ伝えたほうがコストが少なく済む。 この場合、全ての国の0番目の国へ情報を伝えるためにかかる最小のコストは12となる。 コストが12で済む伝達方法は複数存在するが、そのうちのどれか一つを答えとして出力すれば良い。 以下の図2の赤い矢印はその答えのうちの1つである。 図2 Input n countryname 0 m 0 countryname 1 m 1 … countryname n-1 m n-1 e c1 0 v1 0 c2 0 v2 0 cost 0 c1 1 v1 1 c2 1 v2 1 cost 1 … c1 e-1 v1 e-1 c2 e-1 v2 e-1 cost e-1 q c3 0 v3 0 c3 1 v3 1 … c3 q-1 v3 q-1 n は連合国軍に属する国の数を表す countryname i は連合国軍に属する i 番目の国名を, m i は i 番目の国の拠点の数を表す e は通信可能な拠点のペアの数を表す 続く e 行には国 c1 i の拠点 v1 i から国 c2 i の拠点 v2 i に情報を伝えるのにかかるコスト cost i が与えられる q は質問の数を表す 続く q 行には敵軍を発見した国の名前 c3 i とその拠点 v3 i が与えられる c1 i , c2 i , c3 i は連合国軍に属する国のいずれかである v1 i , v2 i , v3 i はその国の拠点の数以上である事は無い Constraints 1 ≤ n ≤ 26 1 ≤ m i ≤ 100 0 ≤ e ≤ 322400 0 ≤ cost i ≤ 100 1 ≤ q ≤ 10 countryname は小文字のアルファベット ‘a’ から’z’までで構成される長さ1以上20以下の文字列である 同じ名前の国が2つ以上与えられる事はない 通信可能な拠点のペアの c1 i と c2 i が同じ時、 v1 i と v2 i は異なる 通信可能な拠点のペアの c1 i と c2 i が異なる時、 v1 i は必ず0である Output 各質問に対して以下の形式で答えを出力する。 各質問に対する答えの最後の行には、5つの '-’ からなる"-----” ( “ は除く ) という1行を出力すること。 答えが存在する場合 1行目に質問で与えられた国の拠点から連合国軍全体に情報が行き渡るために必要なコストの最小値を出力する。 2行目以降にはその時の伝達方法を出力する。 伝達方法は以下の形式で表現される。 c4 0 v4 0 c5 0 v5 0 c4 1 v4 1 c5 1 v5 1 ... 国 c4 i の拠点 v4 i から国 c5 i の拠点 v5 i に情報を伝えたことを表す。 以下の事に気をつける事。 通信が行われた順番通りに出力する必要はない コストの総和が最小となる伝達方法が1つ以上存在する場合、そのうちのいずれか1つを出力すれば良い 同じ通信可能なペアを2回以上出力してはいけない c4 i , c5 i は入力で与えられた国のいずれかでなければならない v4 i , v5 i はその国の拠点の数未満でなければならない 答えが存在しない場合 1行に ”Impossible” ( “ は除く ) と出力すること。 Sample Input 1 3 frence 1 usi 1 powland 1 2 usi 0 frence 0 10 frence 0 powland 0 10 2 usi 0 powland 0 Sample Output 1 20 usi 0 frence 0 frence 0 powland 0 ----- Impossible ----- Sample Input 2 2 usso 2 caneda 2 4 usso 1 usso 0 1 usso 0 caneda 0 10 usso 0 caneda 1 2 caneda 1 caneda 0 2 1 usso 1 Sample Output 2 5 usso 1 usso 0 usso 0 caneda 1 caneda 1 caneda 0 ----- Sample Input 3 3 chinax 1 ok 1 austraria 2 6 chinax 0 austra ...(truncated)
[ { "submission_id": "aoj_1537_893213", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <vector>\n#include <algorithm>\n#include <complex>\n#include <cstring>\n#include <cstdlib>\n#include <string>\n#include <cmath>\n#include <cassert>\n#include <queue>\n#include <set>\n#...
aoj_1535_cpp
Problem J: Hanimon ハニカムモンスターはハニカム模様の六角形状の不思議な生き物である。 ハニカムモンスターには様々なサイズのものがあり、一辺が N 個の正六角形のマスから成るハニカムモンスターをサイズ N のハニカムモンスターとする。 一方、聖なる模様はハニカムモンスターと同じく、六角形として定義され1辺が P 個の正六角形のマスから成る聖なる模様をサイズ P の聖なる模様とする。 ハニカムモンスターと聖なる模様の形の例を以下に示す。サイズは1辺の正六角形のマスの数に対応する。 図1: サイズの例 図2: ハニカムモンスターと聖なる模様の例 (Sample Input 1) ハニカムモンスターと聖なる模様の各マスには色が着いており0と1によって表される。 サイズ P の Q 個の聖なる模様のすべてを含むハニカムモンスターは伝説のハニモンと呼ばれている。 サイズ N のハニカムモンスターとその模様、 Q 個のサイズ P の聖なる模様が与えられるので、 そのハニカムモンスターが伝説のハニモンであるかどうかを判定するプログラムを作成せよ。 Input 入力は次の形式で表される。 N P Q (空行) ハニカムモンスターの模様の情報 (空行) 1つ目の聖なる模様の情報 (空行) 2つ目の聖なる模様の情報 (空行) ... Q つ目の聖なる模様の情報 N , P , Q はそれぞれハニカムモンスターのサイズ,聖なる模様のサイズ,聖なる模様の個数を表す。 サイズ N のハニカムモンスターの模様の情報は以下の様に2× N -1行で与えられる。 N 個のマス N +1個のマス N +2個のマス . . 2× N -2個のマス 2× N -1個のマス 2× N -2個のマス . . N +2個のマス N +1個のマス N 個のマス サイズ P の聖なる模様の情報は以下の様に2× P -1行で与えられる。 P 個のマス P +1個のマス P +2個のマス . . 2× P -2個のマス 2× P -1個のマス 2× P -2個のマス . . P +2個のマス P +1個のマス P 個のマス それぞれのマスの間には1つの空白が入り、マスには0か1が入る。 Constraints 1 ≤ N ≤ 1000 1 ≤ P ≤ 50 1 ≤ Q ≤ 100 同じ模様の聖なる模様が複数個与えられることは無い。 ハニカムモンスターのあるマスについて、2つ以上の聖なる模様の一部分であることがある。 ハニカムモンスターと聖なる模様は回転できない。 Output 伝説のハニモンなら YES を、そうでなければ NO を一行に出力せよ。 Sample Input 1 6 2 2 1 1 1 1 1 1 1 0 0 0 0 1 0 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 0 0 0 0 1 0 0 0 1 0 1 1 1 1 1 0 1 0 0 1 1 0 1 0 1 0 1 0 0 1 1 1 0 0 1 1 1 1 0 0 1 1 0 0 0 0 1 0 1 0 1 1 1 0 1 1 0 0 1 1 0 0 1 0 1 1 1 1 1 0 0 0 0 1 0 1 1 Sample Output 1 YES Sample Input 2 6 2 2 0 0 0 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 1 0 0 1 0 1 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 0 0 0 1 0 0 1 1 0 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1 1 1 1 1 1 0 1 0 0 1 0 0 0 0 0 1 0 1 0 0 0 1 1 0 1 0 1 0 1 0 0 1 1 0 1 0 0 1 0 1 Sample Output 2 NO
[ { "submission_id": "aoj_1535_890620", "code_snippet": "#include<bits/stdc++.h>\n\n#define rep(i,n) for(int i=0;i<(int)n;i++)\n#define all(c) (c).begin(),(c).end()\n#define mp make_pair\n#define pb push_back\n#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)\n#define dbg(x) cerr<<__...
aoj_1541_cpp
Problem A: Yu-kun Likes an Integer Background 会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。園児の一人であるゆう君は、プログラミングと同じくらい数字が大好きだ。そんなゆう君は最近、表に0から9までのいずれかの数字の書かかれたプレートを n 枚購入して n 桁の数を作る遊びに熱中している。 今月もゆう君は貰ったお小遣いで n 枚のプレートを買いに行こうと考えていた。 Problem プレートは表に書かれている数字によって値段が異なる。 それらを使って作れる n 桁の数をできる限り小さくしたい。 ゆう君の所持金額と各プレートの値段、購入するプレートの枚数が与えられるので、そこから作ることのできる数の最小値を求めよ。 購入するプレートの値段の総和が所持金額を超えなければ自由にプレートを購入できる。 同じ数字の書かれたプレートを複数枚購入しても良い。 プレートは必ず n 枚購入しなければならない。 n 枚購入できない場合は "NA" と出力すること。 購入後は n 枚のプレートを任意の順番で一直線上に並べる。 そうしてできる数字の列を10進数の数とし、その値が最小になるようにしたい。 先頭に1つ以上の0が連続していても良い。 ( 例えば 0019 なら 19 となり, 0000 なら 0 として考える ) 図1は0、1、9のプレートを購入した場合についての説明である。 図1 : 0,1,9のプレートを購入した場合 Hint この問題を解くに当たって、以下のことを参考にしても良い。 整数値を文字列に変換する方法を示す。 value を文字列として str に代入する。 Cの場合 #include<stdio.h> int main(){ int value = 123; // この値を文字列に変換する char str[6]; // この変数に value を文字列にしたものが入る sprintf(str,"%d",value); return 0; } C++の場合 #include<sstream> using namespace std; int main(){ int value = 123; // この値を文字列に変換する string str; // この変数に value を文字列にしたものが入る stringstream ss; ss << value; ss >> str; return 0; } JAVAの場合 class Main { public static void main(String args[]){ int value = 123; // この値を文字列に変換する String str = new Integer(value).toString(); // この変数に value を文字列にしたものが入る } } Input n m c 0 c 1 c 2 ... c 9 1行目に2つの整数 n , m が空白区切りで与えられる。 n は購入するプレートの枚数, m はゆう君の所持金額を表す。 2行目には10個の整数が空白区切りで与えられる。 c i ( i は0以上9以下 ) は表に i が書かれたプレートの値段を表す。 Constraints 入力は以下の条件を満たす。 1 ≤ n ≤ 5 0 ≤ m ≤ 500 1 ≤ c i ≤ 1000 ( 0 ≤ i ≤ 9 ) Output n 枚のプレートを購入し、それらを任意の順番で並べることでできる数の値の最小値を出力せよ。 先頭にいくつかの0を含む場合はそのまま出力すること。 ( 例えば答えが 0019 の場合は先頭の0を取り除いて 19 とするのではなく、そのまま 0019 と出力すること ) 所持金額では n 枚プレートを購入できない場合は "NA" と出力すること。 Sample Input 1 1 10 1 2 3 4 5 6 7 8 9 10 Sample Output 1 0 Sample Input 2 3 10 8 4 5 3 5 6 9 10 11 2 Sample Output 2 119 Sample Input 3 5 30 25 51 32 9 2 1 10 2 5 10 Sample Output 3 04555 Sample Input 4 5 100 101 101 101 101 101 101 101 101 101 101 Sample Output 4 NA
[ { "submission_id": "aoj_1541_9659771", "code_snippet": "#include<bits/stdc++.h>\n\nusing namespace std;\n\nvector<vector<int> > combinations;\n\nvoid gen(int idx, vector<int> &comb) {\n if (idx == 0) {\n combinations.push_back(comb);\n return;\n }\n for (int i = 0; i < 10; i++) {\n ...
aoj_1531_cpp
Problem F: Remainder Zero Problem 整数の集合A,Bについて,次の条件を満たす整数 x がいくつ存在するか答えよ。 A i mod x = 0 かつ x mod B j = 0 がすべての i (1 ≤ i ≤ N), j (1 ≤ j ≤ M) について成り立つ。 ( a mod b は a を b で割ったときの余りを意味する) Input N M A 1 A 2 ... A N B 1 B 2 ... B M Constraints 入力は以下の条件を満たす。 1 ≤ N , M ≤ 5 1 ≤ A i , B j ≤ 10 14 (1 ≤ i ≤ N , 1 ≤ j ≤ M ) Output 条件を満たす整数 x がいくつ存在するか一行で出力せよ。 Sample Input 1 1 2 18 6 9 Sample Output 1 1 Sample Input 2 1 2 256 2 4 Sample Output 2 7
[ { "submission_id": "aoj_1531_10314507", "code_snippet": "#include<bits/stdc++.h> \nusing namespace std;\n\nusing ll=long long;\n\nll modpow(ll a,ll n,ll p){\n\tll r=1;\n\twhile(n>0){\n\t\tif(n%2)r=(r*a)%p;\n\t\tn/=2;\n\t\ta=(a*a)%p;\n\t}\n\treturn r;\n}\n\nint main(){\n\t\n\tcin.tie(nullptr);\n\tios::sync_w...
aoj_1543_cpp
Problem C: Changing Grids Background A君とB君は、『Changing Grids』というゲームに熱中している。このゲームは2人用で、プレイヤー1がステージを構成し、プレイヤー2がそのステージに挑戦しゴールを目指すというものである。 今、A君とB君はこのゲームを何度かプレイしているが、A君の連勝でB君は1度も勝つことができていない。そこであなたは、B君にこのゲームを攻略するためのヒントを教えてあげることにした。 Problem 時刻 T 0 = 0における縦 H ×横 W の大きさの二次元グリッドの状態が Area 0 として与えられる。次に、このグリッドの状態は時刻 T i において、状態 Area i に切り替わる。この切り替わる過程は N 回繰り返される。初期状態のグリッドにはスタートの位置'S'とゴールの位置'G'が与えられる。いずれかのグリッドにおいてゴールへ辿り着ける場合は、そのときの最小歩数を出力し、ゴールへ辿り着けない場合は、'-1'を出力せよ。なお、以下の条件も満たす必要がある。 Area i は以下の要素で構成されている。 ‘.’は何もなく移動可能なマス ’#’は障害物であり、移動不可能なマス 'S'はスタート位置を表すマス 'G'はゴール位置を表すマス プレイヤーは現在いるマスの隣接している上下左右のいずれか1マスに移動する、または現在いるマスに留まるのに1秒かかる。ただし、障害物のマスやグリッドの範囲外には移動できない。 歩数は、現在プレイヤーがいるマスから上下左右のマスへ1マス進むと1増加する。その場に留まる場合には増加しない。 全てのグリッドの大きさは縦 H ×横 W である。 1マス移動、またはその場に留まった後にグリッドが切り替わる際、次のグリッドにおいて障害物が存在しないマスであれば今のグリッドの状態に関わらず移動が可能である。 全てのグリッドにおいて、初期のグリッドに与えられたゴール位置に到達した場合ゴールとみなす。 Input 入力は以下の形式で与えられる。 H W Area 0 N T 1 Area 1 T 2 Area 2 . . T N Area N 1行目に2つの整数 H,W が空白区切りで与えられる。これは、それぞれ二次元グリッドの縦と横の大きさを表す。2行目から H +1行目までの各行に初期状態の二次元グリッドの状態が与えられる。 H +2行目に整数 N が与えられる。これは、二次元グリッドの変化する回数を表す。 H +3行目以降に N 個の二次元グリッドの切り替わる時刻 T i とその状態が与えられる。ただし、 T i は全て整数である。 Constraints 入力は以下の条件を満たす。 2 ≤ H,W ≤ 20 1 ≤ N ≤ 15 1 ≤ T i ≤ 200 ( T 1 < T 2 < ... < T N ) スタート位置'S'とゴール位置'G'はそれぞれ初期の二次元グリッドに1つだけ存在する。 Output スタートからゴールへ到達するための最小の歩数を出力せよ。ただし、ゴールに到達できない場合は'-1'を出力せよ。 Sample Input 1 2 2 S. .G 1 3 ## ## Sample Output 1 2 1番目のグリッドに切り替わる時刻 T 1 は3であり、時間内にプレイヤーはスタートからゴールまでの最短歩数が2歩で辿り着くことが可能なので2を出力する。 Sample Input 2 2 2 S. .G 1 2 ## ## Sample Output 2 -1 Sample Input 3 2 3 S## ##G 4 2 ### .## 3 ### #.# 5 ### ##. 7 ### ### Sample Output 3 3 Sample Input 4 4 3 S.. ... .G. ... 4 2 ### #.# ### #.# 4 ### #.. #.. ### 6 ### #.# ### #.. 8 ### #.. #.. ### Sample Output 4 3 Sample Input 5 3 3 S## ### ##G 1 1 ... ... ... Sample Output 5 4
[ { "submission_id": "aoj_1543_3577848", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define dbg(x) cout<<#x<<\"=\"<<x<<endl\n\n#define INF INT_MAX/3\n\nstruct state{\n int i,j,t,d;\n};\nbool operator...
aoj_1545_cpp
Problem E: Rooted Tree Game Problem 初期状態として複数の根付き木が与えられる。これに対しAliceとBobはゲームを行う。ゲームは2人交互に行い、先手がAliceで後手がBobである。ターンが回ってきたプレイヤーは以下の行動を取る。 根(親を持たない頂点)を1つ選択する。この頂点を S とする。 S を根とする根付き木に含まれる頂点を選択する(ここでは S も選択可能)。この頂点を T とする。 S から T への経路上にある頂点を、 S と T も含めすべて削除する。また、削除された頂点が端点であるような辺もすべて削除する。 ターンが回ってきた時点で頂点がすべて削除されていた場合、そのプレイヤーの負けとなる。 AliceとBobが常に最適な行動を取る時、与えられた初期状態に対し、勝利するプレイヤーを判定せよ。 以下の図は、プレイヤーの行動の例を示す。 Input 入力は以下の形式で与えられる。 N M p 1 p 2 : p M 1行目に、初期状態の頂点数 N と辺の数 M が空白区切りで与えられる。この時、各頂点を表す番号は1~ N である。次の M 行では、辺の情報が与えられる。このうち i 行目では1つの整数 p i が与えられる。これは、頂点 p i から頂点 i への辺があることを表す。言い換えると、頂点 i の親が頂点 p i であることを表す。 Constraints 入力は以下の制約を満たす。 1 ≤ N ≤ 1000 0 ≤ M < N i < p i ≤ N ( 1 ≤ i ≤ M ) Output 勝利するプレイヤーの名前(AliceまたはBob)を1行に出力せよ。 Sample Input1 6 3 4 4 5 Sample Output1 Alice Sample Input2 6 4 2 5 4 5 Sample Output2 Bob
[ { "submission_id": "aoj_1545_8295647", "code_snippet": "#include <iostream>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nint N, P[1 << 18];\nint M;\nint Answer[1 << 18];\nvector<int> Cand;\nvector<int> G[1 << 18];\n\nint FindMex(vector<int> vec) {\n\tvec.push_back(-1);\n\tvec.push_back(...
aoj_1546_cpp
Problem F: Coupling Problem 愛津大学では毎年大規模な合コンが行われています。 今年は N 人の男性と M 人の女性が参加します。 それぞれ男性は0から順に N −1までIDが割り振られおり、女性は0から順に M −1までIDが割り振られています。 この合コンでは自分の「大好きな人」と「そこそこ好きな人」のIDを提示します。 男性はそれぞれ女性のIDを、女性はそれぞれ男性のIDを提示します。 その後、以下のルールに従ってカップルが成立します。 男性は複数の女性、女性は複数の男性とカップルになってはいけない。 互いに「大好きな人」と提示した男性と女性でペアができる。 そこから、任意のペアを選びカップルを作ることができる。 片方が「大好きな人」、もう片方が「そこそこ好きな人」と提示した男性と女性でペアができる。 そこから、任意のペアを選びカップルを作ることができる。 互いに「そこそこ好きな人」と提示した男性と女性でペアができる。 そこから、任意のペアを選びカップルを作ることができる。 ルール2で出来たカップル、ルール3で出来たカップル、ルール4で出来たカップルの順にカップルの数が最大になるようにカップルが成立する。 サジ君はこの合コンの主催者です。 近年では参加者の数が多くなり手動でカップルの数を把握するのが大変になってきました。そこでプログラマーであるあなたは、サジ君の手伝いをすることにしました。上記のルールに従ってカップルを作った時の「大好きな人」同士のカップルの数と「大好きな人」と「そこそこ好きな人」によるカップルの数と「そこそこ好きな人」同士のカップルの数を出力するプログラムを作成してください。 Input 入力は以下の形式で与えられる。 N M L1 a 1 b 1 a 2 b 2 : a L1 b L1 L2 c 1 d 1 c 2 d 2 : c L2 d L2 L3 e 1 f 1 e 2 f 2 : e L3 f L3 L4 g 1 h 1 g 2 h 2 : g L4 h L4 1行目にそれぞれ男性の参加者の数と女性の参加者の数を表す2つの整数 N , M が与えられる。次に男性側の大好きな人を表すデータの数 L1 が与えられる。続くL1行に a i と b i が空白区切りで与えられる。それぞれ、ID a i の男性がID b i の女性を「大好き」であることを示す。 続く行に男性側のそこそこ好きな人を表すデータの数 L2 が与えられる。続くL2行に c i と d i が空白区切りで与えられる。それぞれ、ID c i の男性がID d i の女性を「そこそこ好き」であることを示す。次に女性側の大好きな人を表すデータの数 L3 が与えられる。続くL3行に e i と f i が空白区切りで与えられる。それぞれ、ID e i の女性がIDの f i の男性を「大好き」であることを示す。 続く行に女性側のそこそこ好きな人を表すデータの数 L4 が与えられる。続くL4行に g i と h i が空白区切りで与えられる。それぞれ、ID g i の女性がID h i の男性を「そこそこ好き」であることを示す。 Constraints 入力は以下の条件を満たす。 1 ≤ N , M ≤ 100 0 ≤ a i , c i , f i , h i ≤ N −1 0 ≤ b i , d i , e i , g i ≤ M −1 0 ≤ L1 , L2 , L3 , L4 ≤ 2000 L1 > 0, L2 > 0 において ( a i , b i ) ≠ ( c j , d j ) ( 0 ≤ i < L1 , 0 ≤ j < L2 ) L3 > 0, L4 > 0 において ( e i , f i ) ≠ ( g j , h j ) ( 0 ≤ i < L3 , 0 ≤ j < L4 ) ( a i , b i ) ≠ ( a j , b j ) ( i ≠ j ) ( 0 ≤ i < L1 , 0 ≤ j < L1 ) ( c i , d i ) ≠ ( c j , d j ) ( i ≠ j ) ( 0 ≤ i < L2 , 0 ≤ j < L2 ) ( e i , f i ) ≠ ( e j , f j ) ( i ≠ j ) ( 0 ≤ i < L3 , 0 ≤ j < L3 ) ( g i , h i ) ≠ ( g j , h j ) ( i ≠ j ) ( 0 ≤ i < L4 , 0 ≤ j < L4 ) Output 問題文のルールに従ってカップルが成立した時の、「大好きな人」同士のペアの数と「大好きな人」と「そこそこ好きな人」のペアの数と「そこそこ好き」同士のペアの数を空白区切りで1行に出力せよ。 Sample Input 1 3 3 3 0 0 1 1 2 2 2 1 0 2 1 3 1 1 2 2 0 1 2 1 0 2 0 Sample Output 1 2 0 0 この場合男性1と女性1、男性2と女性2が大好き同士でカップルとなり「大好き同士」のペア数が2になる。 すると、男性0のことが「大好き」な女性も「そこそこ好き」な女性もいなくなってしまいます。 また、女性0のことが「大好き」な男性も「そこそこ好き」な男性もいなくなってしまうため以降カップルは成立しなくなります。 Sample input 2 5 5 5 4 1 2 2 1 4 1 3 4 2 5 1 1 1 2 3 3 3 0 3 4 5 2 3 2 2 0 3 0 2 4 1 5 2 0 4 0 1 0 4 3 2 1 Sample Output2 2 1 0
[ { "submission_id": "aoj_1546_3578210", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define dbg(x) cout<<#x<<\"=\"<<x<<endl\n#define all(x) (x).begin(),(x).end()\n\ntypedef pair<int,int> P;\ntypedef p...
aoj_1550_cpp
Problem J: Yu-kun Likes To Play Darts Background 会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。園児の一人であるゆう君は、プログラミングと同じくらいダーツが大好きだ。 そんなゆう君は最近ダーツにはまっていたが、普通のダーツに飽きてしまったため独自のダーツボードを作ることにした。 ダーツについては ダーツ を参照。 Problem ゆう君が考えたダーツの内容は以下の通りだ。 無限に広いダーツボードには得点の書かれた多角形が幾つかある。 プレイヤーはダーツの矢を1本だけ持っている。 プレイヤーは矢を投げ、いずれかの多角形に矢を刺すことでそこに書かれている得点を得る。 それ以外に刺さった場合は得点は得られない。 ゆう君は矢を投げる位置を決めたのだが、正確に刺さるとは限らない。 ダーツボードを2次元平面とし、ゆう君が狙う位置を点( cx , cy )とする。 ゆう君の投げた矢が刺さる場所は、点( cx , cy )を中心とする半径 r の円に含まれる任意の点から一様な確率で選ばれる。 ばれる点の座標は整数である必要はない。 ダーツボードの情報とゆう君が狙う位置( cx , cy )、半径 r が与えれるので、ゆう君が獲得することの出来る得点の期待値を答えよ。 Input n cx cy r 0番目の多角形の情報 1番目の多角形の情報 ... (n-1) 番目の多角形の情報 n はダーツボード上にある多角形の数である。 多角形の情報は以下の形式で与えられる。 p score x 0 y 0 x 1 y 1 ... x (p-1) y (p-1) p は多角形の頂点数を表し、 score は多角形に書かれている得点を表す。 多角形の各線分は( x i , y i )と( x i+1 , y i+1 ) ( i < p -1 )の頂点を繋いだ線分と、( x p-1 , y p-1 )と( x 0 , y 0 )の頂点を繋いだ線分である。 Constraints 入力は以下の条件を満たす。 入力は全て整数で与えられる 1 ≤ n ≤ 50 0 ≤ cx , cy , x , y ≤ 1000 1 ≤ r ≤ 100 3 ≤ p ≤ 10 1 ≤ score ≤ 100 多角形の頂点は、隣り合った頂点を時計回り、または反時計回りに訪問するような順番で与えられる 多角形の辺が別の多角形の辺と共通な点をもつことはない 多角形が別の多角形を内包することはない 矢が多角形の辺上に刺さった場合、得点は得られないものとする Output ゆう君が獲得することの出来る得点の期待値を1行で出力せよ。小数点以下は何桁数字を出力しても構わない。ただし、解答の誤差は0.000001(10 -6 )を超えてはならない。 Sample Input 1 1 2 2 1 4 1 0 0 2 0 2 2 0 2 Sample Output 1 0.2500000000 Sample Input 2 1 2 2 1 4 1 0 0 5 0 5 5 0 5 Sample Output 2 1.0000000000 Sample Input 3 4 3 3 2 3 1 1 1 3 3 1 5 4 2 2 0 5 0 4 2 3 2 3 3 4 3 6 1 6 5 4 4 3 4 4 4 5 6 2 6 Sample Output 3 1.0574955319 Sample Input 4 1 10 10 1 4 10 0 0 1 0 1 1 0 1 Sample Output 4 0.0000000000
[ { "submission_id": "aoj_1550_8296263", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\n// ==================================================================== Geometry Library ===========================================================...
aoj_1547_cpp
Problem G: Yu-kun Likes People Watching Background 会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。園児の一人であるゆう君は、プログラミングと同じくらい人を観察することが大好きだ。 そんなゆう君は、去年の夏休みに作成した人の移動経路のデータと建物の位置情報を観察してあることを疑問に思った。 全ての人が建物内にいる時間の長さのうち、最長のものはどのくらいなのだろうか? Problem 建物と人の移動経路の情報が与えられる。 全ての人が連続して建物内にいるような時間のうち、最も長いものの長さを出力せよ。 そのような時間が存在しない場合は長さは0とする。 建物と人はそれぞれ2次元平面上の多角形と点として表される。 人は単位時間あたり x 軸方向と y 軸方向にそれぞれ速度 vx と vy で移動する。 人を表す点が建物を表す多角形内にある場合、その人は建物内にいるものとする。 ( 多角形の辺上に人がいる場合も建物内にいるものとする ) 図1は、Sample Input 2 における建物と人の移動経路を表している。多角形は建物、黒い丸は人の初期位置、矢印はその人の移動経路を表す。 時刻2から時刻4、時刻9から時刻12の間に人は建物内にいるため、建物内に全ての人がいる時間の最長は3となる。 図1: Sample Input 2 Input n m T 建物0の情報 建物1の情報 ... 建物 n-1 の情報 人0の移動経路 人1の移動経路 ... 人 m-1 の移動経路 n , m , T はそれぞれ建物を表す多角形の数、人の数、人が移動する時間の上限を表す。 建物の情報は以下のような形式で与えられる。 ( x i , y i )と( x i+1 , y i+1 )の頂点を繋いだ線分が多角形の一辺である。ただし、 p-1 番目の頂点とは、0番目の頂点が繋がる。 p x 0 y 0 x 1 y 1 ... x p-1 y p-1 人の経路情報は以下のような形式で与えられる。 t 0 sx 0 sy 0 ( t 0 = 0 ) t 1 vx 1 vy 1 t 2 vx 2 vy 2 ... t k vx k vy k ( t k = T ) 人は2次元平面上を移動する。 ( sx 0 , sy 0 ) は人が時刻0にいる位置である。 時刻 t i-1 から t i の間( 0 < i ≤ k ), x 軸方向と y 軸方向にそれぞれ速度 vx i と vy i で移動する。 Constraints 入力は以下の条件を満たす。 入力は全て整数として与えられる 1 ≤ n , m ≤ 10 3 ≤ p ≤ 10 1 ≤ T ≤ 3600 0 ≤ x , y , sx , sy ≤ 1000 0 = t 0 < t 1 < ... < t k = T -10 ≤ vx 1 , vy 1 ,..., vx k , vy k ≤ 10 建物を表す多角形は時計回りまたは反時計周りで与えられる 多角形の連続する3つの頂点は、一直線上に並ばないものとする 人の位置は常に0以上1000以下である 同じ時刻に2人以上の人が同じ座標にいても良い 建物が別の建物を内包するようなことはない 建物を表す多角形の辺が別の建物を表す多角形の辺と共通な部分をもつことはない Output 全ての人が建物内にいるような時間のうち、その長さが最長のものの長さを1行で出力せよ。 そのような時間がない場合、長さは0とする。 小数点以下は何桁数字を出力しても構わない。ただし、解答の誤差は0.000001(10 -6 )を超えてはならない。 Sample Input 1 1 1 10 4 3 0 7 0 7 10 3 10 0 0 5 10 1 0 Sample Output 1 4.0000000000 Sample Input 2 1 1 14 4 2 3 6 3 6 6 2 6 0 0 4 3 1 0 6 0 -1 7 1 0 10 0 1 14 1 0 Sample Output 2 3.0000000000 Sample Input 3 2 2 7 4 2 2 5 2 5 5 2 5 4 7 2 10 2 10 5 7 5 0 0 4 3 1 0 7 0 -1 0 7 7 2 1 0 5 0 -1 7 1 0 Sample Output 3 1.0000000000 Sample Input 4 1 3 10 9 2 3 3 6 4 3 5 6 6 3 7 6 8 3 8 7 2 7 0 1 5 10 1 0 0 11 4 10 -1 0 0 4 8 6 0 -1 8 1 0 10 0 1 Sample Output 4 0.3333333333 Sample Input 5 1 2 7 4 2 1 6 1 6 5 2 5 0 1 4 7 1 0 0 9 3 7 0 1 Sample Output 5 0.0000000000
[ { "submission_id": "aoj_1547_10334747", "code_snippet": "// AOJ #1547 Yu-kun Likes People Watching\n// 2025.3.29\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nconst double EPS = 1e-9;\n\nbool inPoly(double x, double y, const vector<pair<double,double>> &poly) {\n int cnt = 0...
aoj_1544_cpp
Problem D: Smell Searcher Problem 廃津大学では強烈な香りを放つ N 個の廃材が一列に並んでいます。 廃材には1から N の番号が順番にふられていて、 i 番目の廃材は強さ a i の香りを放っています。 リヒト君は仕事で、すべての廃材の放つ香りの総和を求めるよう依頼されました。 香りの総和が M 以上になると「大変きつい仕事」とみなされ特別支給金がもらえます。 この仕事を行うために、リヒト君は精度 R の香り検出器を使います。 精度 R の香り検出器を使うと i 番目の廃材の香りを測ろうとすると同時に i − R , i − R +1,..., i −1, i , i +1,..., i + R −1, i + R 番目の廃材の香りも検出されます。言い換えると、閉区間[ max( i − R ,1), min( i + R , N ) ] の廃材の香りを検出します。ここで、max( a , b )は a と b の最大値、min( a , b )は a と b の最小値を表します。 ただし、測った廃材の1つ隣の廃材の香りの強さは本来の香りの強さより C 減らされ、2つ隣の廃材の強さ香りは本来の香りの強さより2* C 減らされて検出されます。つまり、 j (0≤ j ≤ R )個隣にある廃材の香りの強さは a i − j * C として検出されます。結果的に、 i 番目の廃材に精度 R の検出器を使うことで検出された香りの強さの最大値が i 番目の廃材の香りの強さとして認識されます。 精度の高い検出器を使うとその分費用がかかるため、リヒト君は検出器が認識した1から N 番目の廃材の香りの総和が M 以上になる最低の精度 R の値を知りたいと思っています。 Input 入力は以下の形式で与えられる。 N M C a 1 a 2 ... a N 1行目に、1つの整数 N , M , C が空白区切りで与えられる。2行目に N つの整数が空白区切りで与えられる。 a i は i 番目の廃材の香りの強さを表す。 Constraints 入力は以下の制約を満たす。 1 ≤ N ≤ 10 5 1 ≤ M ≤ 10 14 1 ≤ C ≤ 10 9 0 ≤ a i ≤ 10 9 (1 ≤ i ≤ N ) Output 精度 R の検出器を使って認識される廃材の香りの総和を M 以上にしたときの R の最小値を出力せよ。 それが不可能な場合は−1を出力せよ。 R は必ず0以上であり、負の値の精度は存在しない。 Sample Input1 6 25 3 8 5 1 1 2 6 Sample Output1 1 このとき、精度0の検出器を使うと 8 + 5 + 1 + 1 + 2 + 6 = 23 で25以上になりません。 精度1の検出器を使うと max(8,5-3) + max(8-3,5,1-3) + max(5-3,1,1-3) + max(1-3,1,2-3) + max(1-3,2,6-3) + max(2-3,6) = 8 + 5 + 2 + 1 + 3 + 6 = 25 で25以上になるので1が正解です。 Sample Input2 4 10 1 1 2 3 4 Sample Output2 0 Sample Input3 4 11 1 1 2 3 4 Sample Output3 -1
[ { "submission_id": "aoj_1544_10071527", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nlong long computeSum(const vector<long long>& a, long long C, int R, long long M) {\n int N = (int)a.size();\n vector<long long> L(N), Rv(N);\n for(int j = 0; j < N; j++){\n L[j] = a[j...
aoj_1536_cpp
Problem K: Witch Craft Moves Problem 会津魔法学校は、魔法を使える者が集る学校である。 会津魔法学校には1~ N までの番号がついた教室と教室同士をつなぐ廊下がある。教室や廊下は何度でも通ることができる。また、会津魔法学校には任意の教室 u , v 間を0個以上の教室と廊下を一度ずつ通って移動することができるような経路がただ1つある。 各教室には魔法陣が設置してある。この魔方陣によって、教室に入る度に入った者の魔力が増減させられてしまうのだ。生徒の移動を助けるために以下の二つの質問に応えてほしい。 質問1 0 A B 教室 A から B へ移動したときの魔力の残量を出力してほしい。教室 A と B での増減も含む。また、この質問開始時点での魔力の量は毎回0である。 質問2 1 A C 教室 A の魔力の増減量が C だけ変動させる。 Input N cost 1 : cost N a 1 b 1 : a N-1 b N-1 Q query 1 : query Q 入力の最初に教室の数 N が与えられる。続く N 行に各教室に入ったときの魔力が増減する量が与えられる。 i 行目の値が i 番の教室の情報に対応する。続く N-1 行に廊下の情報が与えられる。 j 行目の廊下は a j 番目と b j 番目の教室をつなぐことを表す。次に質問の数 Q が与えられる。続く Q 行に上記の形式で質問が与えられる。 Output 各質問1について、答えを1行に出力せよ。 Constraints 入力は以下の条件を満たす。 1 ≤ N ≤ 2000 1 ≤ A i ≤ 2000 1 ≤ B i ≤ 2000 1 ≤ a j , b j ≤ N 1 ≤ Q ≤ 1000000 -10000 ≤ C i ≤ 10000 Sample Input 1 7 1 2 3 4 5 6 7 1 2 1 3 3 4 3 5 5 6 5 7 10 0 1 7 0 2 7 1 1 15 0 1 7 0 2 7 0 1 1 1 1 -15 0 1 7 0 2 7 0 1 1 Sample Output 1 16 18 31 33 16 16 18 1
[ { "submission_id": "aoj_1536_10238012", "code_snippet": "// AOJ #1536 Witch Craft Moves\n// 2025.2.21\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\t\tdo...
aoj_1542_cpp
Problem B: Yu-kun Likes Letters in the English Alphabet Background 会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。園児の一人であるゆう君は、プログラミングと同じくらい英小文字が大好きだ。 そんなゆう君は紙と丸と線、そして英小文字を使う新しい遊びを考え、その採点用プログラムを書くことにした。 Problem 初期状態として、紙に V 個の丸と E 本の線が書かれている。丸には0から昇順に番号がつけられており、各丸の中には英小文字が1つ書かれているか、何も書かれていない。 各線は異なる2つの丸を結んでいる。1つの丸が26本以上の線で結ばれることはない。 遊びの手順は以下の通りである。 何も書かれていない丸を1つ選ぶ。 そのような丸が存在しない場合はこの処理を終了する。 丸の中に英小文字を1つ書く。 ただし丸が既に英小文字の書かれている丸と線で結ばれている場合、その英小文字と同じ英小文字は書くことはできない。 1. に戻る。 上記の手順に従って全ての丸に英小文字を書いた後、丸の番号が小さい順に丸の中の英小文字を並べ文字列を作る。 こうしてできる文字列を辞書順で最小となるようにしたい。 2つの同じ長さの文字列 s , t があり、 s が t より辞書順で小さいとは次のような場合を言う。 s i は文字列 s の i 番目の英小文字を、 t i は文字列 t の i 番目の英小文字を表す。 s i が t i と異なるような最小の i について、 s i が t i より小さい。 紙の初期状態が与えられるので、そこから上記の手順にしたがって作成できる文字列のうち辞書順最小のものを出力せよ。 Input V E a 0 a 1 ... a (V-1) s 0 t 0 s 1 t 1 ... s (E-1) t (E-1) 1行目に丸の数 V と線の数 E が空白区切りで与えられる。 2行目に丸の初期状態が空白区切りで与えられる。 a i が英小文字の場合は i 番目の丸にその英小文字が書かれており、'?'の場合は i 番目の丸には何も書かれていないことを表す。 続く E 行に線の情報が s i t i として与えられ、これは s i 番の丸と t i 番の丸が線で結ばれていることを表す。 Constraints 入力は以下の条件を満たす。 1 ≤ V ≤ 100,000 0 ≤ E ≤ 200,000 a i は英小文字, または '?' のいずれか ( 0 ≤ i ≤ V-1 ) 0 ≤ s i , t i ≤ V-1 ( s i < t i , 0 ≤ i ≤ E-1 ) 1つの丸が26本以上の線で結ばれることはない Output 問題文中の手順に従って作成可能な文字列のうち、辞書順で最も小さいものを1行に出力せよ Sample Input 1 3 3 c ? ? 0 1 0 2 1 2 Sample Output 1 cab Sample Input 2 3 2 c ? ? 0 1 0 2 Sample Output 2 caa Sample Input 3 7 6 ? a ? ? z a ? 0 1 0 2 3 4 4 5 4 6 5 6 Sample Output 3 baaazab Sample Input 4 5 0 ? ? ? ? ? Sample Output 4 aaaaa
[ { "submission_id": "aoj_1542_8295587", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\n\nint N;\nint M, A[1 << 18], B[1 << 18];\nchar C[1 << 18];\nvector<int> G[1 << 18];\n\nint main() {\n\t// Step 1. Input\n\tcin >> N >> M;\n\tfor (int i = 1; i <= N; i++) cin >> C[i];\n\tfor ...
aoj_1554_cpp
Problem D: Friday the 13th Problem 今年も「立命館大学競技プログラミング合宿」が開催されることとなった。僕は毎年開催されるこの合宿をとても楽しみにしている。しかし、欲望に耐え切れず合宿前に散財してしまい、お金に余裕が無くなってしまった。そこで僕は、立命館大学の最寄り駅である南草津まで移動するのに最も安く済む青春18きっぷを使うことにした。この切符は安く済む代わりに乗り換えが多く発生し、移動に丸1日使わなければならないのでとても疲れる。そんな移動をする日が13日の金曜日であることに気づいたのは会津若松駅を出発した後だった。僕は12時間、不安を感じながら電車に揺られるはめになった。 今日は合宿2日目、2015年3月15日、日曜日である。13日は特に何事もなく南草津に到着したが、僕はもうこのような不安を味わいたくないと思った。そこで2日目のジャッジである僕は、指定された期間内に存在する13日の金曜日の数を求める問題を出題し、プログラムを作成してもらうことにした。 閏(うるう)年の定義は以下の通りである。 西暦年が4で割り切れる年は閏年である。 ただし、100で割り切れる年は閏年でない。 ただし、400で割り切れる年は閏年である。 Input 空白で区切られた6つの整数 Y 1 , M 1 , D 1 , Y 2 , M 2 , D 2 が1行で与えられる。 Constraints 入力は以下の制約を満たす。 1 ≤ Y 1 ≤ Y 2 ≤ 10 18 1 ≤ M i ≤ 12 1 ≤ D i ≤ 31 ( M i = 1, 3, 5, 7, 8, 10, 12) 1 ≤ D i ≤ 30 ( M i = 4, 6, 9, 11) 1 ≤ D i ≤ 28 ( M i = 2 かつ Y i 年は閏年でない) 1 ≤ D i ≤ 29 ( M i = 2 かつ Y i 年は閏年である) Y 1 年 M 1 月 D 1 日は Y 2 年 M 2 月 D 2 日より0日以上前の日付である Output Y 1 年 M 1 月 D 1 日から Y 2 年 M 2 月 D 2 日までの間に存在する13日の金曜日の数を1行に出力せよ。 Sample Input1 2015 3 13 2015 3 13 Sample Output1 1 Sample Input2 2015 2 14 2015 3 15 Sample Output2 1 Sample Input3 1234 5 6 789012345678901234 5 6 Sample Output3 1357101234567708000
[ { "submission_id": "aoj_1554_5550660", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)\n#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);-...
aoj_1557_cpp
Problem G: Yu-kun Likes a Treasure Background 会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。園児の一人であるゆう君は、プログラミングと同じくらい財宝が大好きだ。ゆう君は今日も財宝を探すため洞窟を探索する予定だ。そこでゆう君は洞窟の地図から、洞窟を全て見て回る際に歩く距離を求めるプログラムを書くことにした。 Problem 洞窟の情報とゆう君の初期位置が与えられる。Fig.1のように、 洞窟は2次元平面上の多角形で表される部屋が、1つ以上繋がった集合で構成される。部屋が別の部屋と接する場合は必ず点で接し、その接点が洞窟の道となり、接点に訪れるとその道を通ったことになる。 道は扉で閉ざされており、扉が閉まっている状態でその道を通ることはできない。扉を開くためには部屋の中にちょうど1つだけ存在するボタンを押す必要がある。ボタンは2次元平面上の点として表される。 ゆう君はボタンが存在する点を訪れることでボタンを押すことができる。ボタンを押すと部屋にある全ての扉が開く。Fig.1では部屋の中にある丸がボタンを表し、顔のマークがゆう君を、そして矢印が指しているボタンがゆう君のいる場所を表す。ボタンを押して扉を開いた後、道を通るとすぐに全ての扉は閉まる。そのため、そこから別の部屋へと移動するためには再度その部屋の中にあるボタンを押す必要がある。ゆう君は最初、いずれかの部屋の中にあるボタンと同じ場所にいる。 Fig. 1 Fig. 2 に移動の例を示す。番号の付いている矢印はゆう君の移動経路を表す。 次の順番で移動することは可能である。 1 -> 2 -> 3 -> 4 次の順番で移動することは不可能である。 1 -> 4 これは、1の経路で移動し部屋同士の接点を訪れるとその瞬間に扉が閉まるからである。 Fig. 2 ゆう君が洞窟内の全ての道を通り、再度初期位置に戻ってくるまでにかかる移動距離の最小値を出力せよ。ただし、同じ道を何度通っても良い。 Input 入力は以下の形式で与えられる。 n m 部屋1の情報 部屋2の情報 … 部屋 n の情報 n , m はそれぞれ洞窟を構成する部屋を表す多角形の数、ゆう君が最初にいる部屋の番号を表す。 部屋の情報は次の形式で与えられる。 p x 1 y 1 x 2 y 2 … x p y p bx by p は多角形の頂点数を表す。( x i , y i ) と ( x i+1 , y i+1 ) の頂点を繋いだ線分が多角形の一辺である。ただし、 p 番目の頂点は1番目の頂点と繋がる。 ( bx , by ) は多角形の内部にあるボタンの位置を表す。多角形は反時計回りで与えられる。 Constraints 入力は以下の制約を満たす。 1 ≤ n ≤ 15 1 ≤ m ≤ n 3 ≤ p ≤ 20 0 ≤ x , y , bx , by ≤ 1000 多角形は反時計回りで与えられる 多角形が別の多角形を内包するようなことはない 多角形はその内部 ( 多角形の辺は含まない ) にちょうど1つだけボタンを持つ 任意の異なる2つの多角形は共通な面積を持たない 任意の異なる2つの多角形 a , b において、 a と b の接点は高々1つしかない 任意の異なる2つの多角形 a , b において、直接または1つ以上の多角形を経由することで a から b へ移動することができる Output ゆう君が初期位置から全ての道を通って再度初期位置に戻ってくるまでにかかる移動の距離の総和の最小値を出力せよ。小数点以下は何桁数字を出力しても構わない。ただし、解答の誤差は0.00001(10 -5 )を超えてはならない。 Sample Input1 3 1 4 0 4 2 4 2 7 0 7 1 6 5 1 0 6 0 4 5 2 2 1 4 4 2 3 2 6 6 4 6 7 5 6 Sample Output1 14.6502815399 Sample Input2 3 2 3 0 2 3 5 0 5 1 4 3 1 0 4 0 1 3 2 1 3 2 2 4 3 2 4 3 3 Sample Output2 8.0644951022 Sample Input2 4 4 3 0 5 3 8 0 8 1 7 3 1 3 4 3 1 6 2 4 5 2 0 7 0 7 2 3 2 2 3 6 1 3 6 2 7 7 2 7 6 6 Sample Output2 18.9356648257
[ { "submission_id": "aoj_1557_3561159", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<...
aoj_1549_cpp
Problem I: Hard Beans Problem 大津大学では豆が盛んです。 N 個の豆が一直線上に並んでいます。 それぞれ0から順に N −1まで番号がふられており、 i 番目の豆の硬さを a i とします。 シアン君は理想の豆の硬さを D だと考えています。しかし、シアン君は面倒くさがりなのであまり遠くにある豆を取りに行きたくありません。したがって、シアン君は l 番目の豆から r 番目の豆の中で硬さが D に最も近い豆を知りたいと思っています。 シアン君は Q 個の質問をしてくるので、それぞれの質問に対し閉区間[ l , r ]番目にある | 豆の硬さ − D | の最小値を求めるプログラムを作成してください。(ただし、| a | は a の絶対値を表します。) Input 入力は以下の形式で与えられる。 N a 0 a 1 ... a N−1 Q l 0 r 0 D 0 l 1 r 1 D 1 . . . l Q−1 r Q−1 D Q−1 1行目に、1つの整数 N が与えられる。2行目に、 N つの整数が空白区切りで与えられる。3行目に、クエリの数が1つの整数 Q として与えられる。続く4行から3+ Q 行までにクエリの値 l , r , D が与えられる。 Constraints 入力は以下の制約を満たす。 1 ≤ N ≤ 10 5 0 ≤ |a i | ≤ 10 6 (0 ≤ i ≤ N −1) 1 ≤ Q ≤ 10 5 0 ≤ D i ≤ 10 6 0 ≤ l i ≤ r i ≤ N −1 Output 各クエリに対し、 D と[ l , r ]番目の豆の中で硬さ D に最も近い豆の硬さとの差の絶対値を1行に出力せよ。 Sample Input1 3 1 2 3 3 0 2 2 0 2 4 0 0 2 Sample Output1 0 1 1 Sample Input2 10 4 5 0 21 9 100 12 9 0 8 5 0 3 20 2 5 100 8 9 9 5 5 10 0 9 20 Sample Output2 1 0 1 90 1
[ { "submission_id": "aoj_1549_10848513", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nenum {\n NOTFOUND = 1000000000000000000LL\n};\n\nclass SuccinctBitVector {\nprivate:\n const uint64_t size; // ビットベクトルのサイズ\n static const uint64_t blockBitNum = 16;\n static const uint64_...
aoj_1552_cpp
Problem B: Mountain Climbing Problem なりよし君は山登りの計画を立てています。計画はとても大切です。高原・盆地が多いのか、峰・窪地が多いのか、山腹があるのか、状況によって用意する荷物も変わってきます。 なりよし君が持っている登山ガイドブックには、今回登山に利用する道の一定の距離毎の標高がスタート地点からゴール地点まで順に書かれています。 ガイドブックに書かれている標高をスタート地点からゴール地点まで順に a 1 , a 2 , a 3 , ..., a N とし、高原、盆地、山腹、峰、窪地を次のように定義します: 高原 a i-1 < a i = a i+1 = ... = a j > a j+1 ( 1 < i < j < N ) 盆地 a i-1 > a i = a i+1 = ... = a j < a j+1 ( 1 < i < j < N ) 山腹 a i-1 < a i = a i+1 = ... = a j < a j+1 ( 1 < i < j < N ) もしくは a i-1 > a i = a i+1 = ... = a j > a j+1 ( 1 < i < j < N ) 峰 a i-1 < a i > a i+1 ( 1 < i < N ) 窪地 a i-1 > a i < a i+1 ( 1 < i < N ) 高原 盆地 山腹 山腹 峰 窪地 あなたは、なりよし君の為に、高原・盆地・山腹・峰・窪地、それぞれの数を計算するプログラムを書くことにしました。 Input 入力は次のような形式で与えられる: N a 1 a 2 a 3 ... a N N は与えられる標高の数を表す整数である。 a i は標高を表す整数である( 1 ≤ i ≤ N )。それぞれ空白区切りで与えられる。 Constraints 入力は以下の条件を満たす。 1 ≤ N ≤ 100000 -100000 ≤ a i ≤ 100000 Output 高原の数、盆地の数、山腹、峰、窪地の数を順番に空白区切りで一行に出力せよ。 Sample Input 1 5 1 2 3 4 3 Sample Output 1 0 0 0 1 0 Sample Input 2 12 10 5 15 15 20 20 12 12 8 3 3 5 Sample Output 2 1 1 2 0 1
[ { "submission_id": "aoj_1552_5550112", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nvector<pair<char,int>> runlength(vector<char> vs){\n\tvector<pair<char,int>> res;\n\tfor(auto v:vs){\n\t\tif(res.empty() || res.back().first!=v) res.emplace_back(v,0);\n\t\tres.back().second++;\n\t}\n\tr...
aoj_1553_cpp
Problem C: Manhattan Warp Machine 1 Problem とある宇宙では、1次元の整数座標点上に星が存在し、宇宙人達はマンハッタンワープ装置を使い、星間を移動している。 このワープ装置には、 N 個のボタンが付いており、ボタン i を押すと、現在いる星からのマンハッタン距離が d i である任意の星にコスト c i でワープすることができる。 今、点0の星にいるある宇宙人が点 x の星に行きたいと考えている。 点 x の星に辿り着くまでの最小のコストを答えよ。 辿り着けない場合は-1を出力せよ。 1次元上の整数座標点には必ず星が存在する。 点 x 1 と点 x 2 間のマンハッタン距離は| x 1 - x 2 |で表される。 Input N x d 1 c 1 ... d N c N 入力は全て整数で与えられる。 1行目に N と行きたい星の座標 x が空白区切りで与えられる。 続く N 行に、移動可能なマンハッタン距離 d i とコスト c i が1行ずつ空白区切りで与えられる。 Constraints 入力は以下の条件を満たす。 1 ≤ N ≤ 15 0 ≤ x ≤ 10 5 1 ≤ d i ≤ 10 5 1 ≤ c i ≤ 100 与えられるマンハッタン距離 d i は全て異なる。 Output 点 x の星に辿り着くまでにかかる最小コストを1行に出力せよ。辿り着くことが不可能な場合は-1を出力せよ。 Sample Input 1 2 5 1 1 2 1 Sample Output 1 3 Sample Input 2 2 12 9 1 3 2 Sample Output 2 3 Sample Input 3 1 3 4 1 Sample Output 3 -1
[ { "submission_id": "aoj_1553_10025912", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\nconst int inf = 1e9;\nconst double INF = 1e12, EPS = 1e-9;\n\nint d[20], c[20];\nbool v[300000];\n\nint main(){\n\n ios::sync_with_stdio(false);\n cin.tie(NULL);\n \n ...
aoj_1548_cpp
Problem H: Yu-kun Likes a Directed Graph Background 会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。園児の一人であるゆう君は、プログラミングと同じくらいお絵描きが大好きだ。 これまでゆう君は、丸と矢印で沢山絵を書いてきた。矢印は必ず丸と丸を結ぶようにして描いてある。ある日ゆう君はこれらの絵がグラフであることを知る。 丸は頂点と呼ばれ、矢印は辺と呼ばれているらしい。更にゆう君は、矢印を描く際に、必ずその上に1つ正の整数を書いていた。このように、辺に重みがあり有向な辺からなるグラフは重み付き有向グラフと呼ばれる。 今日ゆう君は、新たに閉路という言葉を知った。閉路とは、連結した辺の列であり、それに含まれるどの頂点も高々1度しか現れないもので、最初の頂点と最後の頂点が同じであるようなものをいう。閉路の辺の重みの総和が負になる場合、そのような閉路は負の閉路と呼ばれる。 新たな言葉を沢山知ったゆう君は一つの問題を思いついた。 Problem 閉路を持たない重み付き有向グラフが与えられる。 異なる2つの頂点 i , j を選び、 i から j に重み w ( w < 0 ) の有向な辺を1本追加する。 これによってグラフ内に負の閉路ができるような i 、 j をみつけ、その負の閉路に属する辺の重みの総和の最大値を求めよ。 そのような i , j が存在しない場合は"NA"と出力すること。 Input V E w s 0 t 0 c 0 s 1 t 1 c 1 ... s (E-1) t (E-1) c (E-1) 1行目に頂点の数 V , 辺の数 E , 追加する辺の重み w が空白区切りで与えられる。 続く E 行に有向な辺の情報が s i t i c i として与えられる。 ( 0 ≤ i ≤ E-1 ) これは、 s i から t i に向けて重み c i の有向な辺が存在することを表す。 Constraints 入力は以下の条件を満たす。 2 ≤ V ≤ 100000 1 ≤ E ≤ 500000 -100 ≤ w < 0 0 ≤ c i ≤ 100 ( 0 ≤ i ≤ E-1 ) 0 ≤ s i , t i ≤ V-1 ( 0 ≤ i ≤ E-1 ) 入力中に同じ s i と t i のペアが現れることはない s i と t i は異なる Output 重み w の有向な辺を追加することで出来る負の閉路に属する辺の重みの総和の最大値を1行に出力せよ。 どこに辺を追加しても負の閉路が作れない場合は"NA"と出力すること。 Sample Input 1 3 2 -3 0 1 1 0 2 5 Sample Output 1 -2 Sample Input 2 3 2 -1 0 1 1 0 2 5 Sample Output 2 NA Sample Input 3 7 8 -8 0 1 5 0 4 3 1 2 10 1 3 1 3 6 6 4 3 1 4 5 2 4 6 2 Sample Output 3 -1 Sample Input 4 5 4 -30 0 1 1 1 3 15 1 4 4 2 1 3 Sample Output 4 -12
[ { "submission_id": "aoj_1548_8295743", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\nusing namespace std;\n\nint N, W;\nint M, A[1 << 19], B[1 << 19], C[1 << 19];\nbool dp[100009][109];\nvector<pair<int, int>> G[100009];\nqueue<pair<int, int>> Q;\n\nint main() {\n\t// Step 1. In...
aoj_1556_cpp
Problem F: Sum of Numbers Problem a 以上 b 以下の整数が書かれたカードがそれぞれ c 枚ずつあります。 これら c(b-a+1) 枚のカードの中から d 枚のカードを選択したとき、それらのカードに書かれている整数の合計が e になる場合の数を1,000,000,007で割った余りを求めてください。 Input 入力は以下の形式で与えられる。 a b c d e 1行目に、5つの整数 a b c d e が空白区切りで与えられる。 Constraints 入力は以下の制約を満たす。 1 ≤ a ≤ 1000 a ≤ b ≤ 1000 1 ≤ c ≤ 100 1 ≤ d ≤ 1000 c ≤ d ≤ c(b-a+1) 1 ≤ e ≤ 20000 Output 場合の数を1,000,000,007で割った余りを出力せよ。 Sample Input1 2 5 3 3 11 Sample Output1 3 {2,2,2,3,3,3,4,4,4,5,5,5}の中から3枚選び合計が11になるのは、 {2,4,5},{3,3,5},{3,4,4}の3通りである。 Sample Input2 1 2 3 4 100 Sample Output2 0 {1,1,1,2,2,2}の中から4枚選び合計が100になるのは0通りである。
[ { "submission_id": "aoj_1556_10431881", "code_snippet": "// AOJ #1556 Sum of Numbers\n// 2025.4.29\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst int MOD = 1000000007;\n\nconst int M_SIZE = 108;\nconst int J_SIZE = 21600;\nll dp[M_SIZE][J_SIZE];\nll dp1_arr[M_SIZE][J_SIZE];\...
aoj_1560_cpp
Problem J: Yu-kun Likes a lot of Money Background 会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。園児の一人であるゆう君は、プログラミングと同じくらいお金が大好きだ。ゆう君は、今日もお金を稼ぐために財宝の眠る島を訪れた。ゆう君は事前に財宝のありかの描かれた地図を手に入れている。その地図をもとに出来るだけ多くのお金を稼ぎたい。ゆう君は最大でどのくらいお金を手に入れることができるだろうか? Problem 地図、ゆう君の初期位置、財宝の種類とそれらから得られるお金、そして小さい岩を破壊するために必要な費用の情報が与えられる。地図の情報は h マス × w マスのフィールドとして与えられる。地図の各マスに書かれている文字とその意味は次の通りである。 '@' : ゆう君が最初にいる位置を表す。ゆう君が移動した後は道と同じように扱う。 '.' : 道を表す。このマスは自由に通ることができ費用もかからない。 '#' : 大きな岩を表す。このマスは通ることができない。 '*' : 小さな岩を表す。一定の金額を支払うことで壊すことができる。壊した後は道になる。 '0','1',...,'9','a','b',...,'z','A','B',...,'Z' : 財宝があるマスを表す。このマスを訪れることでそこに書かれている文字に対応する財宝の金額分のお金を得る。ただしお金を得ることが出来るのは最初に訪れた時のみである。 ゆう君は1回の移動で隣接する上下左右のいずれかのマスに移動することができる。 ただし、地図の外へ出るような移動はできない。 後払いをすることができるため、小さな岩を壊す際にそれに必要な金額をその時に所持している必要はない。そのため、ゆう君は最終的に小さな岩を壊す際にかかった金額の総和以上のお金を得ている必要がある。 ゆう君が得られる最大の金額を出力せよ。 Input 入力は以下の形式で与えられる。 h w n r 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 m 1 v 1 m 2 v 2 … m n v n 1行目に地図の縦の長さ h ,横の長さ w ,地図に含まれる財宝の数 n ,小さな岩を破壊するためにかかる費用 r が空白区切りで与えられる。 続く h 行に地図を表す各マスの情報 c i,j が w 個与えられる。 ( 1 ≤ i ≤ h , 1 ≤ j ≤ w ) 続く n 行に財宝の種類 m k とその財宝の金額 v k が空白区切りで与えられる。 ( 1 ≤ k ≤ n ) Constraints 入力は以下の制約を満たす。 1 ≤ h , w ≤ 8 0 ≤ n ≤ min( h × w -1,62) ただし、min( a , b )は a , b の最小値を表す 1 ≤ v i ≤ 10 5 ( 1 ≤ i ≤ n ) 1 ≤ r ≤ 10 5 c j,k , m l を除く全ての入力は整数として与えられる ( 1 ≤ j ≤ h , 1 ≤ k ≤ w , 1 ≤ l ≤ n ) 地図にはちょうどひとつ'@'が書かれている 地図にはちょうど n 個の財宝が書かれている 地図に書かれている財宝の種類は入力で与えられた m l のいずれかである 地図に同じ種類の財宝が2つ以上現れることはない Output ゆう君が得られる最大のお金の金額を1行に出力せよ。 Sample Input1 3 3 1 10 @0. ... ... 0 100 Sample Output1 100 Sample Input2 3 3 1 10 @#b .#. .#. b 100 Sample Output2 0 Sample Input3 3 3 1 20 @*C ..* ... C 10 Sample Output3 0
[ { "submission_id": "aoj_1560_6075206", "code_snippet": "#include <bits/stdc++.h>\n#define INF 1000000007\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nstruct uftree{\n\tint par[25];\n\tint rank[25];\n\tuftree(){\n\t}\n\tvoid init(int n){\n\t\tfor(int i=0;i<n;i++){\n\t\t\tpar[i]=i...
aoj_1558_cpp
Problem H: Puzzle and Hexagons Backgorund 超大人気ゲーム「パズル&ヘキサゴンズ」がついにリリースされた。このゲームは超面白すぎてドハマりする人が続出。あまりの熱中度に医師から中毒認定を受ける者も続出した。世界各国の有志達はこのゲームの中毒者達を助けるために「パズル&ヘキサゴンズ」のシミュレータを作り、危険な実機でのプレイを避けるよう促そうとした。あなたにはシミュレータ作りに協力して欲しい。 Problem 正六角形のマスを縦に H 個、横に W 個敷き詰めた盤面が与えられる。 Fig.1は H =4, W =7の時の盤面とそれに対応するマスの座標( x , y )を示す。 Fig.1 初期状態で各マスには色のついたブロックが存在する。 ブロックの色は以下のようにアルファベット一文字で表現される。 'R' ・・・赤 'G' ・・・緑 'B' ・・・青 'P' ・・・紫 'Y' ・・・黄 'E' ・・・水 次に操作の数 Q が与えられる。 各操作では回転の中心座標( x , y )が与えられ、そのマスの周囲にある6つのブロックを時計回りに一つ回転させることを示す。(Fig.2 参照)。 このとき、ブロックが存在しないマスも空のブロックが存在すると考えて時計回りに一つ回転させる。 ただし、指定された座標とその周辺の6つのマスの内いずれか一つでも H × W の盤面の中に存在していない場合は回転を行わない。 Fig.2 次に以下の処理ができなくなるまで繰り返す。 Fig.3において、ブロックAの位置からB, C, Dの位置のいずれのマスにもブロックが存在しないとき、ブロックAはCの位置に落下する。マスB, Dが存在しない場合はブロックも存在しないと考え、マスCが存在しない場合は落下の処理を行わない。 1の処理が可能なブロックが存在する場合は1に戻る。 同じ色のブロックが3つ以上繋がっている場合、そのブロックは全て消滅する。 2つのブロックが繋がるとはマスの一辺を共有することである。 注意:この一連の処理は、操作が一つも与えられていない状態(初期状態)でも行われる。 Fig.3 全ての操作を実行した後の最終的な盤面を出力せよ。 Input 入力は以下の形式で与えられる。 H W F 0, H−1 F 1, H−1 … F W−1, H−1 F 0, H−2 F 1, H−2 … F W−1, H−2 . . . F 0, 0 F 1, 0 … F W−1, 0 Q x 0 y 0 x 1 y 1 . . . x Q−1 y Q−1 1行目に、盤面の縦と横のサイズを表す2つの整数 H と W が与えられる。 2行目から H +1行目に、各添字に対応する盤面の色を表す文字列が与えられる。 H +2行目に、操作の数 Q が与えられる。 続く Q 行に回転の中心のマスの座標を表す x と y が与えられる。 Constraints 3 ≤ H ≤ 50 3 ≤ W ≤ 50 0 ≤ x < W 0 ≤ y < H 1 ≤ Q ≤ 100 F i, j ( 0 ≤ i < W , 0 ≤ j < H ) は'R','G','B','P','Y','E'のいずれかである。 Output 全ての操作を行った後の盤面を H 行で出力せよ。 ただし、ブロックが無いマスは'.'で表すこと。 Sample Input1 3 3 RGR RBP YEB 1 1 1 Sample Output1 … YBG EBP Fig.4はSample Input1における状態の遷移を表したものである。 Fig.4 Sample Input2 4 5 BYYGG RRRRR RRBRR YYGGB 2 3 1 3 1 Sample Output2 ..... ..... ..... B.BGB Sample Input3 4 4 BEEP ERYY BBRP RBYP 1 1 2 Sample Output3 .... .... .... .B.. 盤面の初期状態ですでに消えるブロックがあること注意。 両端にあるブロックの落下処理に注意。
[ { "submission_id": "aoj_1558_9166437", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define chmin(x,y) x = min(x,y)\n#define chmax(x,y) x = max(x,y)\n#define BIG_NUM 2000000000\n#define HUGE_NUM 4000000000000000000 //オーバーフローに...
aoj_1562_cpp
Divisor Problem 12以下の自然数 N が与えられるので、約数の個数がちょうど N 個であるような最小の自然数を求めよ。 Input 1つの自然数 N が 1 行で与えられる。 Constraints 1 ≤ N ≤ 12 Output 約数の個数がちょうど N 個であるような最小の自然数を1行に出力せよ。 Sample Input 1 1 Sample Output 1 1 Sample Input 2 2 Sample Output 2 2 Sample Input 3 3 Sample Output 3 4
[ { "submission_id": "aoj_1562_2884395", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint main(){\n\tint n;\n\tcin>>n;\n\tint ans=0;\n\tint count[10000]={};\n\tfor(int i=1;i<=10000;i++){\n\t\tfor(int j=1;j<=10000;j++){\n\t\t\tif(i % j == 0) count[i]++;\n\t\t}\n\t\tif(count[i] == n){\n\t\t\t...
aoj_1559_cpp
Problem I: Hopping Mind Problem チエノとカカオは同じ喫茶店で働く姉妹である。2人はとても仲が良く、ある日、とあるテーブルゲームで遊ぶことになった。 ゲームは R マス× C マスの盤面と、駒としてうさぎのTPを用いる。盤面の各マスは白か黒の色が塗られている。最初にTPを盤面の右下( R , C )におき、2人で次の行動を交互に行う。TPの現在の位置を( a , b )とすると、そこからジャンプ可能な位置( i , j )を1つ選び、TPをそこにジャンプさせる。TPがジャンプ可能な位置( i , j )は以下をすべて満たす。 1 ≤ i ≤ R かつ 1 ≤ j ≤ C かつ i ≤ a かつ j ≤ b かつ 1 ≤ ( a - i ) + ( b - j ) ≤ K ( i , j )は白いマスである 自分のターンにTPをジャンプさせることができなくなった場合、負けとなる。 チエノが先手、カカオが後手でこのゲームを行う。カカオは頭の中でゲームを最後まで先読みすることができ、常に最適な行動をとる。この時、チエノが勝つ方法が存在するかどうかを判定せよ。 Input 入力は以下の形式で与えられる。 R C K G 1,1 G 1,2 ... G 1,C G 2,1 G 2,2 ... G 2,C : G R,1 G R,2 ... G R,C 1行目に3つの整数 R , C , K が空白区切りで与えられる。次の R 行に盤面の情報として C 個の".”または"#”が与えられる。 G i,j は盤面の位置( i , j )の色を表し、”.”が白、"#”が黒を表す。 Constraints 1 ≤ R , C ≤ 1000 1 ≤ K ≤ 2000 G R,C は“.”である Output チエノが勝つ方法が存在する場合は”Chieno”を、存在しない場合は”Cacao”を1行に出力せよ。 Sample Input1 3 3 2 ... ... ... Sample Output1 Chieno Sample Input2 3 3 2 #.# .#. #.. Sample Output2 Cacao
[ { "submission_id": "aoj_1559_10760662", "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 99999999999999999\n#define MOD 1000000007\n#define EPS 0.000000001\nusing namespace std;\n\n\n#define SIZE 1005\n\nin...
aoj_1555_cpp
Problem E: Distinct Dictionary Background 辞書をこよなく愛する者がいた。彼らは自分だけの辞書を作ることが大好きである。 そこであなたは彼らの辞書に、自由にカスタマイズする機能を付け加えてあげることにした。 Problem 初めに、何の単語も入っていない空の辞書が存在する。 N 個の文字列 S id と Q 個のクエリが与えられる。 各クエリはクエリの種類 k と文字列を指す id で与えられる。 クエリの種類は以下の3つである。 k =1のとき S id を辞書に追加する。 k =2のとき S id を辞書から削除する。 k =3のとき辞書に追加された文字列の中で S id を先頭からの部分文字列に含み且つ辞書順最小の文字列の id を出力する。無い場合は-1を出力する。 各クエリに答えるプログラムを書いてほしい。 注意: 入力のサイズが大きいので高速な入力に対応する形式を推奨する。 Input 入力は以下の形式で与えられる。 N S 1 S 2 . . . S N Q k 1 id 1 k 2 id 2 . . . k Q id Q 1行目に、1つの整数 N が与えられる。2行目から N +1行に、 N 個の文字列 S id が与えられる。 N +2行目にクエリの数 Q が与えられ、続く Q 行に各クエリの種類を表す k と文字列を指す id が与えれる。 Constraints 文字列は全て異なる。 N 個の文字列 S id の長さの合計は10 6 を超えない。 既に辞書に追加されている文字列が再び追加されるような入力は存在しない。 辞書に追加されていない文字列を削除するような入力は存在しない。 与えられる文字列に含まれる文字は英小文字のみである。 1 ≤ N ≤ 10 5 1 ≤ id ≤ N 1 ≤ Q ≤ 10 5 1 ≤ | S id | ≤ 10 5 (ただし、| s |は文字列 s の長さを表す。) Output クエリごとに解答を一行に出力せよ。 Sample Input1 3 apple app banana 9 1 1 3 1 3 2 3 3 2 1 1 2 3 1 3 2 3 3 Sample Output1 1 1 -1 -1 2 -1 Sample Input2 7 aaaab aaa aaaa aaaaaabc abbbbbc ab abbb 13 1 1 3 2 1 4 3 2 1 3 3 2 2 3 3 3 1 5 1 7 3 6 2 5 3 6 Sample Output2 1 4 3 4 7 7
[ { "submission_id": "aoj_1555_5550778", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nnamespace atcoder {\n\nnamespace internal {\n\n// @param n `0 <= n`\n// @return minimum non-negative `x` s.t. `n <= 2**x`\nint ceil_pow2(int n) {\n int x = 0;\n while ((1U <...
aoj_1570_cpp
Lights of Apartment Problem エーちゃんとリカちゃんとハルトくんはマンションに遊びに来た。 3人は全ての部屋の電気を管理できる部屋に忍び込みいたずらをすることにした。 このマンションは n 個の立方体が1列に並んでいる形をしている。 各立方体は西から順に1辺の長さが1ずつ増えていて(1,2,3,..., n )、 i 番目の立方体は i 階あり、各階に縦 i ×横 i 個の部屋がある。 2番目以降の立方体の西側は1つ西の立方体の東側と接していて、全ての立方体の南側は真っ直ぐな道路に面している。 初め全ての部屋に電気がついている。 3人はそれぞれ次の行動をした。 エーちゃんは西から k 番目の全ての部屋の電気を消した。 リカちゃんは南から k 番目の全ての部屋の電気を消した。 ハルトくんは k 階の全ての部屋の電気を消した。 このようないたずらが m 回行われた後に電気がついている部屋の数を求めよ。 Input n m q 1 k 1 ... q m k m 入力は全て整数で与えられる。 1行目に立方体の数 n 、行動の数 m が与えられる。 2行目以降 m 行に行動した人の番号 q と k が与えられる。 q i が0の場合エーちゃん、 1の場合リカちゃん、2の場合ハルトくんが行動したことを表す。 3人は部屋がない場所の電気を消そうとすることもある。 Constraints 1 ≤ n , m ≤ 50000 0 ≤ q i ≤ 2 1 ≤ k i ≤ 2×10 9 同じ行動は一度しか与えられない Output 電気がついている部屋の数を1行に出力せよ。 Sample Input 1 3 1 0 4 Sample Output 1 27 Sample Input 2 3 2 2 2 2 3 Sample Output 2 14
[ { "submission_id": "aoj_1570_3089072", "code_snippet": "#include <cstdio>\n#include <iostream>\n#include <sstream>\n#include <fstream>\n#include <iomanip>\n#include <algorithm>\n#include <cmath>\n#include <string>\n#include <vector>\n#include <list>\n#include <queue>\n#include <stack>\n#include <set>\n#incl...
aoj_1568_cpp
String Conversion Problem ジェニファーとマリアンはカーラに文字列 S をプレゼントしました。 しかし、カーラは文字列 S をもらっても嬉しくありません。 文字列 T が欲しかったのです。 3人は協力して文字列 S を文字列 T に変えることにしました。 最初にジェニファーが文字を任意の順番に並び替えます。 次にマリアンが2種類のアルファベットの小文字を交換することを任意の回数行います。 この操作では、例えば以下のように、文字列中の同じ文字が全て交換されます。 aab → a と b を交換 → bba aab → a と c を交換 → ccb 最後にカーラがある1文字を別の1文字に置換することを T になるまで繰り返します。 ジェニファーとマリアンはカーラの置換回数が少なくなるようにしてあげることにしました。 カーラが行う置換の回数の最小値を求めてください。 Input n S T 1行目に文字列の長さ n が与えられる。 2行目に文字列 S 、3行目に文字列 T が与えられる。 Constraints 1 ≤ n ≤ 10 5 S , T は 'a'~'z' のみを含む | S | = | T | = n Output カーラの最少の置換回数を1行に出力せよ。 Sample Input 1 3 abc xyz Sample Output 1 0 Sample Input 2 5 aaabb xyxyz Sample Output 2 1
[ { "submission_id": "aoj_1568_2576345", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint a[26],b[26],n,ans;\nstring s,t;\n\nint main() {\n cin >> n >> s >> t;\n for(int i=0; i<n; i++) {\n a[s[i]-'a']++;\n b[t[i]-'a']++;\n }\n sort(a,a+26);\n sort(b,b+26);\n for(int i=0; i<26; ...
aoj_1567_cpp
Lucky Number Problem 0 から N -1 までの数字があります。 M +1日間のラッキーナンバーを以下の方法で選びたいと思います。 最初の日にラッキーナンバーをランダムに決めます。 i 日後のラッキーナンバーを A 、( i +1)日後のラッキーナンバーを B とすると、 B = ( A + j ) % N (ただし j は 0 ≤ j < N かつ ( j / K ) が偶数となる全ての整数である ) で求められる B のうちのいずれかからランダムに決めます。ただし、 a / b の結果は小数点以下を切り捨てた整数とし、 a % b は a を b で割った余りとします。 また K は N / K が割り切れて、 N / K が偶数となる数字であることが保証されます。 例えば 0,1,2,3 と数字があり、 K = 1 のとき 0 の次のラッキーナンバーは 0 or 2 1 の次のラッキーナンバーは 1 or 3 2 の次のラッキーナンバーは 0 or 2 3 の次のラッキーナンバーは 1 or 3 となります。 次に Q 個の質問が与えられます。 各質問の内容は以下のとおりです。 最初の日のラッキーナンバーが a のとき b 日後のラッキーナンバーが c になるような b 日後までのラッキーナンバーの選び方は何通りでしょうか? 選び方は膨大な数になると思われるので 1,000,000,007 で割った余りを求めてください。 例えば、 N =4 K =1 で、最初のラッキーナンバーが 0 で 3 日後のラッキーナンバーが 2 になるような組み合わせは 0 → 0 → 0 → 2 0 → 0 → 2 → 2 0 → 2 → 0 → 2 0 → 2 → 2 → 2 の4通りです。 Input 入力は以下の形式で与えられる。 N M K Q a 1 b 1 c 1 a 2 b 2 c 2 . . . a Q b Q c Q Constraints 1 ≤ N ≤ 5000 1 ≤ M ≤ 5000 1 ≤ K ≤ 5000 N / K が割り切れる N / K は偶数 1 ≤ Q ≤ 100000 0 ≤ a i < N ( 1 ≤ i ≤ Q ) 0 ≤ b i ≤ M ( 1 ≤ i ≤ Q ) 0 ≤ c i < N ( 1 ≤ i ≤ Q ) Output 各質問に対し、答えを求め 1,000,000,007 で割った余りを一行ずつ順番に出力してください。 Sample Input1 6 3 1 10 0 1 0 0 2 0 0 3 0 1 3 3 0 2 1 2 2 2 2 3 2 2 2 0 1 1 1 2 2 1 Sample Output1 1 3 9 9 0 3 9 3 1 0
[ { "submission_id": "aoj_1567_3079308", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<ll, ll> P;\n\n#define fi first\n#define se second\n#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)\n#define rep(i,n) repl(i,0,n)\...
aoj_1571_cpp
String Crossing Problem N 個の文字列{ S 1 , S 2 , ..., S N } が与えられる。 続いて Q 個のクエリが与えられる。 クエリの種類は以下の2つである。 a , b , c , d が入力され S a の b 文字目からの substring を S c の d -1文字までの substring の後に繋げる。これを新しい S c とする。 元の S c の d 文字目からの substring を S a の b -1文字までの substring の後に繋げる。これを新しい S a とする。 a , b , c が入力され S a の b 文字目を c に変更する。 例えば S 1 ="abcd", S 2 ="efgh"があり クエリの種類が1で a =1, b =2, c =2, d =3のとき a - -> bcd X ef - -> gh S 1 ="agh", S 2 ="efbcd"となる。 全てのクエリを処理した後の文字列 S 1 から S N を全て出力せよ。 Input 入力は以下の形式で与えられる。 N Q S 1 S 2 ... S N query 1 query 2 ... query Q 各 query は次のいずれかである。 1 a b c d or 2 a b c Constraints 1 ≤ N ≤ 10 5 1 ≤ Q ≤ 10 5 N 個の文字列の長さの合計は2×10 6 を越えない。 文字列 S i は全て英小文字であることが保証される。 クエリの種類が1のとき a ≠ c 1 ≤ b ≤ | S a | 1 ≤ d ≤ | S c | クエリの種類が2のとき 1 ≤ b ≤ | S a | c は英小文字であることが保証される。 高速な入出力を推奨する。 Output 各クエリを処理した後の文字列を S 1 から S N まで1行ずつ出力せよ。 S 1 S 2 ... S N Sample Input 1 2 1 abcd efgh 1 1 2 2 3 Sample Output 1 agh efbcd Sample Input 2 2 3 abcd efgh 1 1 2 2 3 2 1 3 x 2 2 4 x Sample Output 2 agx efbxd Sample Input 3 10 10 sjcvpauokb fmeaowomscy sepeqqfcosrjmonfsv zapc aromazjzqoeiqswvcaf clifpa dusudcz qeqdzdtdzlkhc gkpsjvdvadmf xrtyxnkolluagwxp 1 4 4 6 3 1 7 1 8 1 2 2 6 o 1 4 4 3 7 2 1 2 i 1 6 3 3 2 1 6 5 1 9 2 10 9 j 1 2 2 7 3 2 3 2 b Sample Output 3 sicvpauoeqqifpa fqdzdtdzlkhc sb zapfcosrjmonfsv aromazjzqoeiqswvcaf clepkb qemeaooomscy dusudcz gkpsjvdvadmf xrtyxnkojluagwxp
[ { "submission_id": "aoj_1571_10349818", "code_snippet": "// AOJ #1571 String Crossing\n// 2025.4.5\n\n#include <bits/stdc++.h>\nusing namespace std;\n#include <ext/rope>\nusing namespace __gnu_cxx;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() { // 整数の入力\n\tint n = 0; i...
aoj_1578_cpp
Rubik Dungeon Problem あなたは n × n × n の立方体のルービックキューブ型のダンジョンにやってきた。 あなたは現在( x 1 , y 1 , z 1 )の部屋にいる。 目標の宝は( x 2 , y 2 , z 2 )の部屋にある。 あなたは隣接している前後左右上下の部屋に単位時間で移動することができる。 各部屋には6つのボタンがあり、それぞれのボタンを押すことにより、単位時間で、ルービックキューブのように以下の操作を行うことができる: 現在いる部屋と x 座標の値が同じ全ての部屋を時計または反時計周りに90度回転することができる 現在いる部屋と y 座標の値が同じ全ての部屋を時計または反時計周りに90度回転することができる 現在いる部屋と z 座標の値が同じ全ての部屋を時計または反時計周りに90度回転することができる 回転している途中で他の部屋に移動することはできない。 宝のある部屋まで最短で移動した場合の時間を求めよ。 Input n x 1 y 1 z 1 x 2 y 2 z 2 入力は全て整数で与えられる。 1行目に n が与えられる。 2行目に現在地の座標( x 1 , y 1 , z 1 )、3行目に宝のある部屋の座標( x 2 , y 2 , z 2 )が空白区切りで与えられる。 Constraints 2 ≤ n ≤ 10 5 0 ≤ x 1 , y 1 , z 1 , x 2 , y 2 , z 2 ≤ n −1 ( x 1 , y 1 , z 1 ) ≠ ( x 2 , y 2 , z 2 ) Output 最短の時間を出力せよ。 Sample Input 1 3 0 0 0 1 2 2 Sample Output 1 3 Sample Input 2 3 0 0 0 0 0 2 Sample Output 2 2 回転させると宝も移動してしまうので、回転させずに移動する。
[ { "submission_id": "aoj_1578_3090330", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))\n#define all(x) (x).begin(),(x).end()\n#define pb push_back\n#define fi first\n#define se second\n#define dbg(x) cout<<#x\" = \"<<...
aoj_1561_cpp
Problem K: Manhattan Warp Machine 2 Problem とある宇宙では、3次元の格子点上に星が存在し、宇宙人達はマンハッタンワープ装置という装置を使い、星間を移動している。 このワープ装置には、 N 個のボタンが付いており、ボタン i を押すと、現在いる星からのマンハッタン距離が d i である任意の星にワープすることができる。 ワープ装置は改良され、コストがかからずに移動出来る。 今、(0, 0, 0)の星にいるある宇宙人がある M 個の星を訪れたいと考えている。 M 個の星を全て訪れるのに最短で何回ワープをする必要があるか答えよ。 もし、1つでも訪れることが不可能な星がある場合は-1を出力せよ。 M 個の星は好きな順番に訪れることが出来、全て訪れた後に(0, 0, 0)に戻って来る必要は無い。 3次元上の全ての格子点には必ず星が存在する。 点( x 1 , y 1 , z 1 )と点( x 2 , y 2 , z 2 )間のマンハッタン距離は| x 1 - x 2 | + | y 1 - y 2 | + | z 1 - z 2 |で表される。 Input N M d 1 ... d N x 1 y 1 z 1 ... x M y M z M 入力は全て整数で与えられる。 1行目に N , M が与えられる。 2行目に移動可能なマンハッタン距離 d i が空白区切りで与えられる。 3行目以降の M 行に、 M 個の訪れたい星の格子点の座標( x i , y i , z i )が1行ずつ空白区切りで与えられる。 Constraints 入力は以下の条件を満たす。 1 ≤ N ≤ 15 1 ≤ M ≤ 15 1 ≤ d i ≤ 3×10 5 -10 5 ≤ x i , y i , z i ≤ 10 5 与えられるマンハッタン距離 d i は全て異なる。 与えられる格子点の座標は全て異なる。 Output M 個の星全てを訪れるのにかかる最短ワープ回数を1行に出力せよ。 訪れることが不可能な星がある場合は-1を出力せよ。 Sample Input 1 2 1 1 2 5 0 0 Sample Output 1 3 Sample Input 2 2 2 9 3 3 0 0 3 9 0 Sample Output 2 2 Sample Input 3 1 1 4 3 0 0 Sample Output 3 -1
[ { "submission_id": "aoj_1561_5550709", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\n// template {{{\nusing i32 = int;\nusing u32 = unsigned int;\nusing i64 = long long;\nusing u64 = unsigned long long;\n\n#define range(i, l, r) for (i64 i = (i64)(l); i < (i64)(r); (i) += 1)\n#define r...
aoj_1569_cpp
Bomb Removal Problem 縦 H ×横 W のグリッド上に N 個の点火された導火線とそれぞれに対する1つの爆弾が配置されている。それぞれの導火線と爆弾は、 L i 個のマスからなる path i 上に配置され、 path i の j 番目のマスを( px ij , py ij )とする。各爆弾は path i の最後のマス( px iL i , py iL i )に存在する。導火線の火は初期状態(0秒時点)でマス( px i1 , py i1 )にあり、1秒間に1マス進む(( px i1 , py i1 ),( px i2 , py i2 ),...,( px iL i , py iL i )の順に進む)。 最初、ロボットがマス( sx , sy ) にいる。このロボットは今いるマスの隣接する8方向のマスに1進む、または、そのマスに留まるのに1秒かかる。ただし、グリッドの外や爆弾のあるマスには移動することができない。ロボットは導火線の火があるマス上にいるとき消火することができる(同じマスに2つ以上の導火線の火がある場合はそれら全てを消すことができる)。 爆弾は導火線の火がマス( px iL i , py iL i )に到達したときに爆発する( path i の途中で導火線の火がマス( px iL i , py iL i )を通過したときは爆弾は爆発しない)。 全ての爆弾を爆発させずに全ての導火線の火を消火させるための最短の時間を出力せよ。ただし、どのように移動しても爆弾を爆発させてしまう場合は-1を出力せよ。なお、初期状態(0秒時点)でロボットが導火線の火の上にいる場合、消火することができる。 Input 入力は以下の形式で与えられる。 W H N sx sy L 1 path 1 L 2 path 2 ... L N path N 1行目に3つの整数 W , H , N が空白区切りで与えられる。2行目に2つの整数 sx , sy が空白区切りで与えられる。3行目から N +2行目に L i と path i が与えられる。 path i は以下の形式で与えられる。 px i1 py i1 px i2 py i2 ... px iL i py iL i Constraints 2 ≤ H , W ≤ 20 1 ≤ N ≤ 5 1 ≤ sx ≤ W 1 ≤ sy ≤ H 2 ≤ L i ≤ H + W 1 ≤ px ij ≤ W 1 ≤ py ij ≤ H path i の隣り合うマスは上下左右に隣接している。 導火線は互いに独立している(他の導火線に影響しない)。 爆弾はマス( sx , sy )には置かれない。 Output ロボットが全ての導火線の火を消火させるための最短時間または -1 を出力せよ。 Sample Input 1 2 2 1 2 1 3 1 1 1 2 2 2 Sample Output 1 1 サンプル1に対応する図は以下の通りである。 ロボットは1秒後に左下の(1, 2)のマスに移動することにより消火することができる。 Sample Input 2 2 2 1 2 1 2 1 1 1 2 Sample Output 2 -1 サンプル2に対応する図は以下の通りである。 ロボットは、(1, 1)または(2, 2)のマスに移動する、または今いるマス(2, 1)に留まることができるがいずれにせよ1秒後に爆弾が爆発してしまう。 Sample Input 3 3 3 2 1 3 3 2 3 2 2 2 1 5 2 1 1 1 1 2 2 2 3 2 Sample Output 3 2 サンプル3に対応する図は以下の通りである。 ロボットは、(2, 2)のマスに移動し、(1, 2)のマスに移動することにより2秒で全ての導火線の火を消火することができる。 Sample Input 4 3 3 1 2 2 2 2 2 2 1 Sample Output 4 0 ロボットは、初期状態(0秒時点)で(2, 2)のマスにいて、導火線の火も(2, 2)のマスにあるので0秒で消火することができる。
[ { "submission_id": "aoj_1569_10946142", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing PII = pair<int, int>;\nusing LL = long long;\nusing VL = vector<LL>;\nusing VVL = vector<VL>;\nusing PLL = pair<LL, LL>;\nusing VS = vector<strin...
aoj_1574_cpp
Gossip Problem 1から n までの番号が順番に割り当てられている n 人のアイドルが横一列に並んでいる。 アイドル i は単位時間でアイドル i -1とアイドル i +1に情報を伝達することができる。ただし、アイドル1はアイドル2にしか情報を伝達できず、アイドル n はアイドル n -1にしか情報を伝達できない。 時刻0の時点で、番号が a 1 , a 2 , ..., a m である m 人のアイドルが秘密の情報を持っている。すべてのアイドルが秘密の情報を得ることのできる最小の時間を求めよ。 Input 入力は以下の形式で与えられる。 n m a 1 a 2 ... a m 1行目に2つの整数 n と m が空白区切りで与えられる。 2行目に m 個の整数 a 1 , a 2 , ..., a m が空白区切りで与えられる。 Constraints 2 ≤ n ≤ 10 5 1 ≤ m ≤ n 1 ≤ a i ≤ n a i の値は全て異なる a i は昇順で与えられる Output すべてのアイドルに情報が伝わる最小の時間を1行に出力する。 Sample Input 1 3 2 1 3 Sample Output 1 1 Sample Input 2 10 3 2 5 7 Sample Output 2 3 Sample Input 3 10 5 2 5 6 8 10 Sample Output 3 1 Sample Input 4 100000 1 1 Sample Output 4 99999
[ { "submission_id": "aoj_1574_2747836", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define _MACRO(_1, _2, _3, NAME, ...) NAME\n#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)\n#define _rep(i,n) _repl(i,0,n)\n#define rep(...) _MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)\n#define...
aoj_1577_cpp
Courage Test P「今日は肝試し企画の番組の収録だ。」 O型智絵里「き、きもだめし……」 四村かな子「智絵里ちゃん大丈夫?」 月野茜「さすがPさん!一行で状況が明晰判明にわかる素晴らしい解説です!さあ!火の玉に向かって走りましょう!」 P「ルールは簡単、みんなで協力してこのエリアのすべての神社にお参りしてくるだけだ。」 茜「オーソドックスですね!早速神社に向かって走りましょう!」 かな子「でもここ、日本有数の神社密度で行かなくちゃいけない神社がたくさんあるんじゃなかったっけ……」 かな子「でも茜ちゃんがいれば心強いよ!」 P「ちなみにスタートは全員違う神社からスタート、合流も禁止だ。それじゃあ智絵里とかな子は初期位置についてくれ」 智絵里「うぅ……」 かな子「お、お菓子を持っていけば大丈夫!」 智絵里「頑張る……」 茜「プロデューサー!私はどうすれば!」 P「お前は賑やかしだ」 茜「えっ」 P「座ってろ」 茜「」 Problem それぞれ1~ n の番号が付けられた n 個の神社と n -1本の道がある。各道は神社 a i と神社 b i を繋ぎ、双方向に移動することができる。それぞれの神社は、任意の神社から1本以上の道を経由して到達することができる。また、任意の2つの神社の間の(迂回しない)経路は一意に定まる。 O型智絵里ちゃんと四村かな子ちゃんがそれぞれ神社 u と神社 v にいる。 今、2人を任意に移動させることを考える。移動とは、ある神社から1本の道を経由して別の神社に進むことである。 その際、以下のルールを満たしたい。 2人合わせて全ての神社を訪問する。 2人の移動数を同じにする。 1つの神社, 道は1人しか通れず、さらに1度しか通ることができない。 神社 u , v は既に訪れているものとし、通ることはできない。 これらのルールを満たすことができるかどうかを判定し、できる場合は"Yes"を、できない場合は"No"を出力せよ。 Input 入力は以下の形式で与えられる。 n u v a 1 b 1 a 2 b 2 ... a n−1 b n−1 1行目に3つの整数 n , u , v が空白区切りで与えられる。 2行目から n 行目までに、2つの整数 a i , b i が空白区切りで与えられる。 Constraints 2 ≤ n ≤ 10 5 1 ≤ a i , b i , u , v ≤ n u ≠ v a i < b i ( a i , b i ) ≠ ( a j , b j ) ( i ≠ j ) Output ルールを満たすことができる場合は"Yes"を、できない場合は"No"を1行に出力する。 Sample Input 1 4 1 4 1 2 2 3 3 4 Sample Output 1 Yes Sample Input 2 8 3 6 1 2 2 4 3 4 4 5 5 6 6 7 7 8 Sample Output 2 No Sample Input 3 4 2 3 1 2 2 3 3 4 Sample Output 3 Yes Sample Input 4 6 1 5 1 2 1 4 2 3 4 5 3 6 Sample Output 4 No
[ { "submission_id": "aoj_1577_10239296", "code_snippet": "// AOJ #1577 Courage Test\n// 2025.2.22\n\n#include <bits/stdc++.h>\nusing namespace std;\nconst int INF = 1e9;\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 == '-') {\t...
aoj_1583_cpp
Equivalent Vertices Background 会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。 園児の一人であるゆう君は,プログラミングと同じくらいお絵描きが大好きだ。 これまでゆう君は丸と六角形と矢印で沢山絵を書いてきた。 ある日ゆう君はこれらの絵がグラフであることを知る。 丸と六角形は頂点と呼ばれ,矢印は辺と呼ばれているらしい。 ゆう君は2つの頂点間を結ぶようにして矢印を描き、更にその上に0か1を書く。 このように,辺に重み(0または1)があり有向な辺からなるグラフは重み付き有向グラフと呼ばれる。 また,頂点と数字 x (0または1)が与えられた時,その頂点から出ている矢印のうち, x と同じ重みを持つ矢印に従って別の頂点に移動することを x に従って遷移する と言う。 今日ゆう君は0と1からなるどのような数列に従って遷移しても最終的に到達する頂点の種類(丸または六角形)が一緒であるような頂点の対が存在することに気がついた。 これについてゆう君は一つの問題を思いついた。 Problem 重み付き有向グラフが与えられる。 このグラフの各頂点は0から順番に n -1まで番号が割り振られている。 また,各頂点はそこから重みが0の辺と1の辺の合計2本の辺が出て行く。 さらに,頂点には丸い頂点か六角形の頂点の2種類が存在し,各頂点はそれらのいずれかである。 以下にSample Input 2で与えられる重み付き有向グラフを示す。 図1. Sample Input 2 以下の形式で m 個の質問が与えられるので、それぞれについて答えを出力せよ。 頂点の番号 q が与えられるので,この頂点と 等価 な頂点の数を出力せよ 2つの頂点 a , b が以下の2つの条件を満たすとき, a と b は等価である。 a と b は同じ種類の頂点である 0と1からなる長さ1以上の任意の数列について,それに従って a と b のそれぞれから遷移を開始したとき最終的に到達する頂点の種類が同じである 例えば,図1の頂点3から数列0,0,1,0に従って遷移を開始すると, 3→2→1→1→1の順番で遷移し最終的に到達する頂点は1である。 Sample Input 2 において,頂点0と頂点2,頂点0と頂点4は等価である。 頂点0と頂点4について考える。 これらは両方とも丸い頂点であるため1つめの条件を満たす。 また,これらの頂点から0または1で遷移した結果到達する頂点は1か5である。 頂点1,5はそれぞれどのような数列に従って遷移してもその頂点に止まり続ける。 また,両方とも六角形の頂点である。 これより,頂点0と頂点4はどのような数列に従って遷移しても最終的に到達する頂点は六角形の頂点であるため2つめの条件も満たす。 条件を2つとも満たすので頂点0と頂点4は等価である。 最終的に到達する頂点は種類が同じであれば良いため,頂点の番号が同じである必要はないことに注意せよ。 また,頂点0と頂点1は等価でない。 頂点0は丸い頂点で頂点1は六角形頂点なので,1つめの条件を満たさない。 Input n m v 0 s 0 t 0 v 1 s 1 t 1 … v n−1 s n−1 t n−1 q 0 q 1 … q m−1 v i , s i , t i はそれぞれ i 番の頂点の種類,0による遷移先の頂点番号,1による遷移先の頂点番号 v i が0のとき, i 番の頂点は丸い頂点であり,1のとき六角形の頂点 q j は j 番目の質問で与えられる頂点の番号 Constraints 入力は全て整数として与えられる 1 ≤ n ≤ 3000 1 ≤ m ≤ n 0 ≤ s i , t i , q j ≤ n -1 0 ≤ v i ≤ 1 与えられるグラフの辺が双方向に移動できるものとしたとき,任意の2点を結ぶような辺の集合が存在する, つまり与えられるグラフは連結である Output 各質問で与えられる頂点の番号 q j に対して,それと等価な頂点の数をそれぞれ一行に出力せよ。 Sample Input 1 2 1 0 0 1 0 1 0 0 Sample Output 1 2 Sample Input 2 6 6 0 1 5 1 1 1 0 1 5 0 2 1 0 5 1 1 5 5 0 1 2 3 4 5 Sample Output 2 3 2 3 1 3 2
[ { "submission_id": "aoj_1583_10240168", "code_snippet": "// AOJ #1583 Equivalent Vertices\n// 2025.2.23\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n int n, m;\n cin >> n >> m;\n\n vector<int> typ(n), nxt0(n), nxt1(n), grp(n);\n ...
aoj_1579_cpp
Array Update 2 Problem 項数 N 、初項 a 、公差 d の等差数列 A がある。 以下の形式で、数列を書き換える M 個の命令文が与えられるので、与えられた順序で M 回 数列 A を書き換えたときの数列 A の K 項目の値を求めなさい。 i 番目の命令文は3つの整数 x i , y i , z i で与えられる。(1 ≤ i ≤ M ) x i が0だった場合、 y i 項目から z i 項目までの区間において、値の順序を反転する。 x i が1だった場合、 y i 項目から z i 項目までの区間において、それぞれの値を1増加させる。 x i が2だった場合、 y i 項目から z i 項目までの区間において、それぞれの値を半分にする(小数点以下は切り捨てる)。 Input N a d M x 1 y 1 z 1 x 2 y 2 z 2 ... x M y M z M K 1行目に、1つの整数 N が与えられる。 2行目に、2つの整数 a と d が空白区切りで与えられる。 3行目に、1つの整数 M が与えられる。 4行目からの M 行のうち i 行目には i 番目の命令文を表す 3 つの整数 x i , y i , z i が空白区切りで与えられる。 最後の行に、1つの整数 K が与えられる。 Constraints 2 ≤ N ≤ 200000 1 ≤ a ≤ 5 1 ≤ d ≤ 5 1 ≤ M ≤ 200000 0 ≤ x i ≤ 2 (1 ≤ i ≤ M ) 1 ≤ y i ≤ N (1 ≤ i ≤ M ) 1 ≤ z i ≤ N (1 ≤ i ≤ M ) y i < z i (1 ≤ i ≤ M ) 1 ≤ K ≤ N Output 入力で与えられた順番で数列 A を M 回更新したときの K 項目を出力せよ。 Sample Input 1 4 2 1 3 0 1 2 1 1 4 2 2 4 3 Sample Output 1 2 { 2 , 3 , 4 , 5 } ↓ 0 1 2 ... 1項目から2項目までの区間の値の順序を反転する { 3 , 2 , 4 , 5 } ↓ 1 1 4 ... 1項目から4項目までの区間の値をそれぞれ1増やす { 4 , 3 , 5 , 6 } ↓ 2 2 4 ... 2項目から4項目までの区間の値をそれぞれ半分にする(小数点以下切り捨てる) { 3 , 1 , 2 , 3 } よって3項目の値は2である。 Sample Input 2 5 1 2 3 1 2 3 2 3 5 0 1 5 1 Sample Output 2 4 { 1 , 3 , 5 , 7 , 9 } ↓ 1 2 3 ... 2項目から3項目までの区間の値をそれぞれ1増やす { 1 , 4 , 6 , 7 , 9 } ↓ 2 3 5 ... 3項目から5項目までの区間の値をそれぞれ半分にする(小数点以下切り捨てる) { 1 , 4 , 3 , 3 , 4 } ↓ 0 1 5 ... 1項目から5項目までの区間の値の順序を反転する { 4 , 3 , 3 , 4 , 1 } よって1項目の値は4である。
[ { "submission_id": "aoj_1579_5995535", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\ntypedef long long ll;\nusing namespace std;\n#define inf 0x3f3f3f3f\nconst int N = 2E5+1000;\nint n;\nint a,d;\nint m;\nint k;\nint x[N],y[N],z[N];\n//一切从简单的地方出发\nint sp[N],p = 0;\n...
aoj_1582_cpp
Circles and Ray Problem 2次元平面上にそれぞれ互いに共通部分をもたない N 個の円が与えられる。 また、円にはそれぞれ1から N までの番号が割り振られている。 あなたは、端点が1番目の円の円周上にあるような半直線を任意の数だけ設置することができる。 どの円も1本以上の半直線との共通部分を持っているようにするためには、最低何本の半直線を設置しなければならないかを求めなさい。 Input 入力は以下の形式で与えられる。 N x 1 y 1 r 1 x 2 y 2 r 2 ... x N y N r N 1行目に、1つの整数 N が与えられる。 2行目からの N 行のうち i 行目には i 番目の円のx座標、y座標、半径を表す3つの整数 x i , y i , r i が空白区切りで与えられる。 Constraints 2 ≤ N ≤ 16 -100 ≤ x i ≤ 100 (1 ≤ i ≤ N ) -100 ≤ y i ≤ 100 (1 ≤ i ≤ N ) 1 ≤ r i ≤ 100 (1 ≤ i ≤ N ) Output どの円も1本以上の半直線との共通部分を持っているようにするためには最低何本の半直線を設置しなければならないかを出力せよ。 Sample Input 1 3 0 0 2 3 3 1 6 1 1 Sample Output 1 1 Sample Input 2 4 1 2 3 12 2 2 7 6 2 1 9 3 Sample Output 2 2 Sample Input 3 5 0 0 5 0 10 1 10 0 1 -10 0 1 0 -10 1 Sample Output 3 4
[ { "submission_id": "aoj_1582_10240155", "code_snippet": "// AOJ #1582 Circles and Ray\n// 2025.2.23\n\n#include <bits/stdc++.h>\nusing namespace std;\n\nconst double EPS = 1e-9;\nconst double PI = acos(-1.0);\n\nstruct Circle { int x, y, r; };\n\nstruct RayCandidate {\n double px, py;\n double dx, dy;...
aoj_1580_cpp
Barter Problem ナナツ君は、うまか棒を x 本、ふがしを y 本持っている。 n 人のお菓子交換人がいる。 それぞれの交換人 i は、 ナナツ君のうまか棒 a i 本と交換人のふがし b i 本 ナナツ君のふがし c i 本と交換人のブタメソ d i 個 のどちらか1つの方法で1回だけ交換してくれる。 この制約の下で、最終的に所持しているブタメソの個数を最大化せよ。 Input 入力は以下の形式で与えられる。 n x y a 1 b 1 c 1 d 1 a 2 b 2 c 2 d 2 ... a n b n c n d n 1行目に整数 n が与えられる。 2行目に整数 x , y が空白区切りで与えられる。 3行目から n +2行目に整数 a i , b i , c i , d i が空白区切りで与えられる。 Constraints 1 ≤ n ≤ 100 0 ≤ x , y ≤ 300 1 ≤ a i , b i , c i , d i ≤ 300 \(\sum_{i=1}^na_i\) ≤ 300, \(\sum_{i=1}^nb_i\) ≤ 300, \(\sum_{i=1}^nc_i\) ≤ 300, \(\sum_{i=1}^nd_i\) ≤ 300 Output 最終的にナナツ君が所持しているブタメソの個数の最大値を1行に出力する Sample Input 1 3 3 3 3 2 4 1 5 1 2 1 3 1 3 2 Sample Output 1 3 Sample Input 2 3 3 4 3 1 3 1 3 1 4 1 3 1 2 1 Sample Output 2 2 Sample Input 3 4 5 0 5 1 1 1 2 1 2 1 4 1 1 1 3 1 3 1 Sample Output 3 2 Sample Input 4 4 0 0 1 10 1 10 2 5 2 5 3 25 5 6 1 15 2 20 Sample Output 4 0
[ { "submission_id": "aoj_1580_5998864", "code_snippet": "#include <bits/stdc++.h>\n#define pt(x) cout << x << endl;\n#define Mid ((l + r) / 2)\n#define lson (rt << 1)\n#define rson (rt << 1 | 1)\nusing namespace std;\nconst int N = 609;\nint t[2][N * 2][N * 2], n, x, y;\nint ans = 0;\nsigned main()\n{\n\tmem...
aoj_1581_cpp
Reflection Warp Machine Problem とある宇宙では、2次元の格子点上に n 個の星が存在し、宇宙人達はReflection Warp Machineを使い、星間を移動している。 この装置は、任意の位置、角度で直線を引くことができる。 この直線を対称軸として、現在いる座標から線対称の座標に移動することができる。 ただし、星が存在しない座標に移動することはできない。 一度引いた直線は何度でも利用できる。 現在、( x 0 , y 0 )の星にいるある宇宙人はすべての星を訪れたいと考えている。 星は好きな順番に訪れることができる。 すべての星を訪れるのに最小で何本の直線を引く必要があるか求めよ。 Input n x 0 y 0 ... x n−1 y n−1 入力は全て整数で与えられる。 1行目に n が与えられる。 2行目以降 n 行に i 番目の星の座標( x i , y i )が空白区切りで与えられる。 Constraints 2 ≤ n ≤ 8 −100 ≤ x i , y i ≤ 100 ( x i , y i ) ≠ ( x j , y j ) ( i ≠ j ) Output すべての星を訪れるために必要な最小の直線の数を1行に出力せよ。 Sample Input 1 3 0 0 0 1 1 0 Sample Output 1 2 Sample Input 2 4 0 0 0 1 0 2 0 3 Sample Output 2 2
[ { "submission_id": "aoj_1581_3806524", "code_snippet": "/* -*- coding: utf-8 -*-\n *\n * 1581.cc: Reflection Warp Machine\n */\n\n#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<stack>\n#...
aoj_1589_cpp
Problem C: Unhappy Class Problem 山之御船学園の1年G組は、不幸を背負った女子生徒たちが集まるクラスである。 彼女たちは、幸福になることを目標に、日々様々な試練に立ち向かう。 彼女たちのクラスでは、幸福になるための試練の一環として、幸福実技科目を受講することができる。 月曜から金曜のそれぞれ1限から N 限までに授業科目の枠があり、 M 個の受講可能な科目がある。 科目 i は、曜日 d i ( d i = 0, 1, 2, 3, 4がそれぞれ、月曜、火曜、水曜、木曜、金曜に対応する)の a i 限目から始まり、連続する k i コマで行われ、それを受講したときに得られる幸福度は t i である。 各生徒は、最大 L 個の科目を互いに重ならないように自由に選ぶことができる。どのように科目を選べば、最も高い幸福度を得られるだろうか。与えられた科目の情報から、得られる幸福度の最大値を求めてほしい。 Input 入力は以下の形式で与えられる。 N M L d 1 a 1 k 1 t 1 d 2 a 2 k 2 t 2 ... d M a M k M t M 1行目に3つの整数 N , M , L が空白区切りで与えられる。 2行目から M +1行目に4つの整数 d i , a i , k i , t i が空白区切りで与えられる。 Constraints 2 ≤ N ≤ 8 0 ≤ M ≤ 300 0 ≤ L ≤ min( N ×5, M ) 0 ≤ d i ≤ 4 1 ≤ a i ≤ N 1 ≤ k i a i + k i - 1 ≤ N 1 ≤ t i ≤ 100 Output 幸福度の和の最大値を1行に出力せよ。 Sample Input 1 3 7 3 0 1 1 1 0 1 1 2 1 1 3 4 1 1 1 1 1 2 1 2 2 1 1 3 2 2 2 1 Sample Output 1 9 Sample Input 2 5 10 5 0 1 1 2 0 2 1 2 0 1 2 3 1 2 1 2 1 4 2 3 2 1 1 1 2 1 1 2 3 3 2 3 4 1 1 2 4 2 1 2 Sample Output 2 13
[ { "submission_id": "aoj_1589_3865197", "code_snippet": "#include <iostream>\n#include <vector>\n#include <array>\n#include <string>\n#include <stack>\n#include <queue>\n#include <deque>\n#include <map>\n#include <set>\n#include <tuple>\n#include <bitset>\n#include <memory>\n#include <cmath>\n#include <algor...
aoj_1588_cpp
Problem B: Potatoes Problem がっちょ君は N 面の畑と M 個の芋を所有している。各畑にはそれぞれ1から N までの番号が付けられている。がっちょ君は畑に芋を植え収穫することで、芋の数を増やしたいと考えている。 がっちょ君は一人暮らしであり、1人では K 面までの畑しか管理することができない。また、各畑の土の状態や面積は様々で、芋の収穫数や植えることのできる芋の数も様々である。芋を畑 i に植えた場合、1年後には畑 i に植えた芋1つにつき a i 個の芋を収穫することができる。ただし、畑 i には最大でも b i 個の芋しか植えることができない。 がっちょ君が K 面以内の畑に M 個以内の芋を植えた時、1年後に所有することができる芋の数の最大値を求めてほしい。 Input 入力は以下の形式で与えられる。 N M K a 1 a 2 ... a N b 1 b 2 ... b N 1行目に3個の整数 N , M , K が空白区切りで与えられる。 2行目に N 個の整数 a i が空白区切りで与えられる。 3行目に N 個の整数 b i が空白区切りで与えられる。 Constraints 1 ≤ N ≤ 15 1 ≤ M ≤ 10 4 1 ≤ K ≤ min( N ,3) 1 ≤ a i ≤ 10 3 1 ≤ b i ≤ 10 4 Output 芋の数の最大値を1行で出力せよ。 Sample Input 1 5 100 3 2 3 4 5 6 50 40 20 10 5 Sample Output 1 280 畑1に40個、畑2に40個、畑3に20個植えることで収穫後に280個の芋を所有することができる。 Sample Input 2 5 100 3 2 3 4 5 100 50 40 20 10 1 Sample Output 2 339 畑2に40個、畑3に20個、畑5に1個植えることで300個の芋を収穫することができ、植えなかった芋と合わせて答えは339になる。
[ { "submission_id": "aoj_1588_3807608", "code_snippet": "/* -*- coding: utf-8 -*-\n *\n * 1588.cc: Potatoes\n */\n\n#include<cstdio>\n#include<cstdlib>\n#include<cstring>\n#include<cmath>\n#include<iostream>\n#include<string>\n#include<vector>\n#include<map>\n#include<set>\n#include<stack>\n#include<list>\n...
aoj_1593_cpp
Problem G: Star Problem 半径1の円に内接する正 N / K 角形の面積を求めよ。 ただし、正 N / K 角形を 「円周上に等間隔に N 個の点を取り、 K -1個おきにそれぞれの点を結んだ一番外側の図形」 と定義する。 例えば、5/2角形は次のように描くことができる。 まず、半径1の円周上に等間隔に5つの点を取る。 次に、それぞれの点を2-1=1つおきに結ぶ。 一番外側の図形が正5/2角形になる。 Input 入力は以下の形式で与えられる。 N K 2つの整数 N , K が1行に与えられる。 Constraints 入力は以下の制約を満たす。 5 ≤ N ≤ 10 6 1 < K < N /2 N , K は互いに素である整数 Output 半径1の円に内接する正 N / K 角形の面積を1行に出力せよ。10 -5 以下の誤差が許容される。 Sample Input 1 5 2 Sample Output 1 1.12256994 正5/2角形は上記の図形である。 Sample Input 2 20 3 Sample Output 2 2.93114293 正20/3角形は下のような図形である。 Sample Input 3 7 3 Sample Output 3 1.08395920 Sample Input 4 100000 3 Sample Output 4 3.14159265
[ { "submission_id": "aoj_1593_10848492", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\nconst int INF = 1e9;\nconst ll LINF = 1e18;\ntemplate<class S,class T> ostream& operator << (ostream& out,const pair<S,T>&...
aoj_1584_cpp
Sum of Sequences Problem n 個の要素を持つ数列 A 、 m 個の要素を持つ数列 B 、 それぞれが整数 c からなる q 個のクエリが与えられる。 各クエリについて、 数列 A の[ l a , r a ]を全て足した数と数列 B の[ l b , r b ]を全て足した数の差の絶対値が c になる l a , r a , l b , r b (0 ≤ l a ≤ r a ≤ n −1, 0 ≤ l b ≤ r b ≤ m −1, 配列の番号は0番から始まる) の組み合わせの数を求めよ。 Input n m q a 0 a 1 ... a n−1 b 0 b 1 ... b m−1 c 0 c 1 ... c q−1 入力は全て整数で与えられる。 1行目に数列の要素数 n と m 、クエリ数 q が与えられる。 2行目に数列 A の要素、3行目に数列 B の要素が空白区切りで与えられる。 4行目から q 行に各クエリの値 c i が与えられる。 Constraints 1 ≤ n , m ≤ 4×10 4 1 ≤ q ≤ 10 5 1 ≤ a i , b i ≤ 5 0 ≤ c i ≤ 2×10 5 Output 出力は q 行からなる。各クエリの組み合わせの数を順番に一行に出力せよ。 Sample Input 1 3 3 1 1 2 3 3 1 2 3 Sample Output 1 6 Sample Input 2 5 4 2 1 2 3 4 5 2 2 2 2 11 12 Sample Output 2 3 4
[ { "submission_id": "aoj_1584_5312473", "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#define...
aoj_1585_cpp
String in String Problem 文字列 S と Q 個のクエリが与えられる。 i 番目のクエリ(0 ≤ i ≤ Q -1)には閉区間[ l i , r i ]と文字列 M i が与えられる。 S の l i 文字目から r i 文字目までの部分文字列の中に文字列 M i はいくつ存在するか出力せよ。 Input 入力は以下の形式で与えられる。 S Q l 0 r 0 M 0 l 1 r 1 M 1 . . . l Q−1 r Q−1 M Q−1 1行目に文字列 S とクエリの数 Q が空白区切りで与えられる。 続く Q 行に整数 l i , r i , M i が空白区切りで与えられる。 Constraints 1 ≤| S | ≤ 100000 1 ≤ Q ≤ 100000 1 ≤ | M i | ≤ 100000 (0 ≤ i ≤ Q -1) 0 ≤ l i ≤ r i < | S | (0 ≤ i ≤ Q -1) 文字はすべて英小文字である 文字列 M の合計文字数は2000000を越えない Output 出力は Q 行からなる。各クエリに対する答えを順番に1行に出力せよ。 Sample Input 1 rupcrupc 5 0 3 rupc 0 7 rupc 2 7 ru 2 7 pc 1 5 u Sample Output 1 1 2 1 2 2 Sample Input 2 abatagaadbura 8 0 6 a 6 12 a 0 6 aa 0 3 a 3 5 a 5 9 a 1 8 b 1 12 b Sample Output 2 4 3 0 2 1 2 1 2 Sample Input 3 aaaaaaaaaa 5 0 9 aaa 0 9 aa 5 9 aaaa 2 8 aa 1 2 a Sample Output 3 8 9 2 6 2
[ { "submission_id": "aoj_1585_10072703", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nstruct Node {\n int next[26];\n int fail;\n vector<int> out;\n Node() : fail(0) {\n for(int i=0;i<26;i++) next[i]=-1;\n }\n};\n\nint main(){\n ios::sync_with_stdio(false);\n ...
aoj_1597_cpp
Problem K: Escape of Lappin the Phantom Thief Problem 怪盗ラッパンは宝石を盗みにやってきた。簡単に宝石を手にすることができたが、宝石にはセンサーが仕込まれており、警備ロボットに囲まれてしまった。 警備ロボットは宝石に向かって移動するように仕組まれている。センサーは簡単に外せそうになかったので、宝石を置いて逃走することにした。宝石をできるだけ警備ロボットから遠くに置いて、逃げるための時間稼ぎをすることにした。 敷地は n × m のマスからなる長方形の形をしていて、障害物はない。 k 体の警備ロボットは、それぞれマス( x i , y i )(0 ≤ x i ≤ n −1, 0 ≤ y i ≤ m −1)に配置されており、上下左右のマスに単位時間で移動できる。 警備ロボットは宝石があるマスに最短経路で移動する。 敷地内に自由に宝石を置けるとき、1台以上の警備ロボットが宝石のあるマスに到達するまでにかかる移動時間の最大値を求めよ。 Input n m k x 1 y 1 x 2 y 2 ... x k y k 入力はすべて整数で与えられる。 1行目に n , m , k が空白区切りで与えられる。 2行目以降 k 行に警備ロボットのいるマスの座標( x i , y i )が空白区切りで与えられる。 Constraints 1 ≤ n , m ≤ 5 × 10 4 1 ≤ k ≤ min(10 5 , n × m ) 0 ≤ x i ≤ n −1 0 ≤ y i ≤ m −1 与えられる座標はすべて異なる Output 警備ロボットが到達するまでに、最も時間がかかる場所への移動時間を1行に出力せよ。 Sample Input 1 20 10 1 0 0 Sample Output 1 28 Sample Input 2 20 10 2 0 0 17 5 Sample Output 2 15 Sample Input 3 20 10 3 0 0 17 5 6 9 Sample Output 3 11
[ { "submission_id": "aoj_1597_10240229", "code_snippet": "// AOJ #1597 Escape of Lappin the Phantom Thief\n// 2025.2.23\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nstruct Robot { int x, y, u, v; };\n\nstruct Event { int v, L, R, delta; };\n\nstruct SegTree {\n int n;\n v...
aoj_1594_cpp
Problem H: Hogemon Get Problem がっちょ君は人気のゲームHogemon Getに熱中している。がっちょ君が住んでいる会津国はそれぞれ1から N の番号がついている N 個の町からなる。また、会津国には M 本の道があり、すべての道は異なる2つの町を結んでいる。がっちょ君は道を双方向に移動することができるが、道以外を通って、ある町から別の町に行くことはできない。 Hogemon Getでは、町 i でボールを d i 個入手することができる。ただし、ある町で再びボールを入手するためには、最後にその町でボールを入手してから15分以上経過している必要がある。なお、がっちょ君は町1、町 N を含むすべての町を何度でも訪れることができる。 がっちょ君は最初、町1にいて、町 N に R 分以内で移動しなければならない。つまり、 R 分後に町 N にいる必要がある。がっちょ君は移動の際に、最大でいくつのボールを入手することができるだろうか。 Input 入力は以下の形式で与えられる。 N M R d 1 d 2 ... d N a 1 b 1 c 1 a 2 b 2 c 2 ... a M b M c M 入力はすべて整数である。 1行目に町の個数 N ,道の本数 M ,制限時間 R が空白区切りで与えられる。 2行目に町 i ( i =1,2,..., N )に訪れることで入手することができるボールの個数 d i が空白区切りで与えられる。 3行目から M +2行目に道 j ( j =1,2,..., M )の情報 a j , b j , c j が空白区切りで与えられる。 j 番目の道は町 a j と町 b j の間を c j 分で移動できることを表す。 Constraints 3 ≤ N ≤ 30 N -1 ≤ M ≤ min( N ×( N -1)/2, 300) 10 ≤ R ≤ 1000 0 ≤ d i ≤ 10 d 1 = d N = 0 1 ≤ a j < b j ≤ N 5 ≤ c j ≤ 100 町1から町 N へは R 分以内で移動できることが保証されている ある2つの町の組に対して2本以上の道があることはない Output 町1から町 N へ R 分以内に移動するまでに入手することができる最大のボールの個数を1行で出力せよ。 Sample Input 1 5 4 40 0 1 1 1 0 1 2 5 2 3 5 3 4 5 4 5 5 Sample Output 1 6 経過時間(分) 町の番号 ボールの数(個) 0 1 0 5 2 1 10 3 2 15 4 3 20 3 3 25 2 4 30 3 5 35 4 6 40 5 6 ※経過時間20分の町3では最後に町3でボールを入手してから15分経過していないのでボールを入手することができない。 Sample Input 2 4 3 100 0 3 1 0 1 2 5 2 3 30 3 4 5 Sample Output 2 16 経過時間(分) 町の番号 ボールの数(個) 0 1 0 5 2 3 20 2 6 35 2 9 50 2 12 65 2 15 95 3 16 100 4 16 Sample Input 3 5 4 50 0 1 1 10 0 1 2 10 2 3 10 2 4 10 4 5 10 Sample Output 3 22
[ { "submission_id": "aoj_1594_10501828", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nconst int inf = 1e9;\n\nint main() {\n\n ios_base::sync_with_stdio(0);\n cin.tie(0), cout.tie(0); \n\n int n, m, R;\n cin >> n >> m >> R;\n\n vector<int> d(n);\n for (auto& x : d...
aoj_1595_cpp
Problem I: Traffic Tree Problem それぞれ1から N までの番号が付いた N 個の頂点が、 N -1本の無向辺によって繋がれたグラフが与えられる。各頂点について、その頂点からスタートしてすべての頂点を訪れるための最短のステップ数を出力せよ。 ただし、ある頂点から1本の辺をたどって別の頂点に移動することを1ステップとする。 Input 入力は以下の形式で与えられる。 N u 1 v 1 . . . u N−1 v N−1 1行目に、1つの整数 N が与えられる。 続く N -1行のうち i 行目には i 番目の辺の両端の頂点番号を表す整数 u i , v i が空白区切りで与えられる。 Constraints 2 ≤ N ≤ 10 5 1 ≤ u i , v i ≤ N (1 ≤ i ≤ N -1) u i ≠ v i 与えられるグラフは連結である Output 頂点1から頂点 N について i 行目に頂点 i からスタートしてすべての頂点を訪れるための最短のステップ数を出力せよ。 Sample Input 1 2 1 2 Sample Output 1 1 1 Sample Input 2 6 1 2 1 3 3 4 3 5 5 6 Sample Output 2 7 6 8 7 7 6 Sample Input2の図
[ { "submission_id": "aoj_1595_10965798", "code_snippet": "#include <algorithm>\n#include <iostream>\n#include <iomanip>\n#include <limits.h>\n#include <map>\n#include <math.h>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <sstream>\n#include <string>\n#include <utility>\n#include <vector>\n...
aoj_1596_cpp
Problem J: Char Swap Problem 長さが偶数の文字列 S が与えられる。 あなたは、文字列 S の隣り合った2つの文字を入れ替える操作を何回でも行うことができる。 文字列 S を回文にするためには、最小で何回の操作を行う必要があるだろうか? 回文にすることが不可能な場合は-1を出力せよ。 Input 入力は以下の形式で与えられる。 S 1行に文字列 S が与えられる。 Constraints 2 ≤ | S | ≤ 4 × 10 5 文字列はすべて英小文字で構成されている 文字列の長さは偶数である Output 文字列 S を回文にするための最小の操作回数を1行に出力する。回文にすることが不可能な場合は代わりに-1を1行に出力をする。 Sample Input 1 acca Sample Output 1 0 Sample Input 2 acpcacpc Sample Output 2 3 Sample Input 3 aizu Sample Output 3 -1
[ { "submission_id": "aoj_1596_10702848", "code_snippet": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace std;\nusing namespace __gnu_pbds;\n\n// Ordered Set (PBDS)\ntemplate<typename T>\nusing ordered_set = tree<T, null_type, less<T>,...
aoj_1590_cpp
Problem D: One-Time Path Problem N 個の島と M 本の橋がある。 N 個の島にはそれぞれ1から N までの番号が割り振られている。 M 本の橋にもそれぞれ1から M までの番号が割り振られている。 がっちょ君は現在(時刻0の時点で)、1番目の島にいる。 がっちょ君は i 番目の橋を利用することにより、 a i 番目の島から b i 番目の島へと単方向に移動することができる。 しかし、時刻0の時点では、すべての橋は潮が満ちていて海の中に沈んでしまっている。 i 番目の橋は、時刻 c i になると潮が引いて渡れるようになる。 そして、時刻 c i を過ぎると、すぐにまた潮が満ちて i 番目の橋は再び沈んでしまう。 再び沈んでしまうと、またいつ潮が引いて渡れるようになるかはわからない。 そこで、がっちょ君はそうなった橋は永遠に渡れなくなると考えるようにした。 がっちょ君は、できるだけ長い間、1から N -1番目の島の景色を眺めていたいのだが、 N 番目の島に船を泊めてあるので、最終的には N 番目の島に到着していなければならない。また、船の中で両親を待たせているため、がっちょ君は N 番目の島についたらすぐに船で出発して家へと帰らなければならない。 がっちょ君が橋を渡ったり島の中を移動する時間はとても短いので、0と仮定してよい。がっちょ君が1から N -1番目のいずれかの島にいることのできる時間の最大値を求めよ。ただし、どのように移動してもがっちょ君が N 番目の島へと移動できない場合は代わりに-1を出力せよ。 Input 入力は以下の形式で与えられる。 N M a 1 b 1 c 1 a 2 b 2 c 2 … a M b M c M 1行目には、2つの整数 N , M が空白区切りで与えられる。 2行目から M +1行目のそれぞれの行 i には、3つの整数 a i , b i , c i が空白区切りで与えられる。 Constraints 2 ≤ N ≤ 10 5 1 ≤ M ≤ 2 × 10 5 1 ≤ a i < N 1 ≤ b i ≤ N 1 ≤ c i ≤ 10 9 a i ≠ b i Output がっちょ君が N 番目の島へと移動できる場合は、がっちょ君が1から N -1番目のいずれかの島にいることのできる時間の最大値を出力する。 N 番目の島へと移動できない場合は、代わりに-1を出力する。 Sample Input 1 3 2 1 2 10 2 3 20 Sample Output 1 20 まず、1番目の島で時刻が10になるまで待ってから、2番目の島へと移動する。 次に、2番目の島で時刻が20になるまで待ってから、3番目の島へと移動する。 以上より、1から2番目のいずれかの島にいる時間は20となる。 Sample Input 2 4 4 1 2 27 1 3 37 2 3 47 3 1 57 Sample Output 2 -1 4番目の島に繋がる橋がないため、4番目の島へ移動することができない。 Sample Input 3 3 3 1 2 13 2 3 17 2 3 15 Sample Output 3 17 Sample Input 4 3 2 1 2 20 2 3 10 Sample Output 4 -1 Sample Input 5 3 2 1 2 10 2 3 10 Sample Output 5 10
[ { "submission_id": "aoj_1590_10853810", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int, int> pii;\ntypedef pair<ll, ll> pll;\nconst int INF = 1e9;\nconst ll LINF = 1e18;\ntemplate<class S,class T> ostream& operator << (ostream& out,const pair<S,T>&...
aoj_1599_cpp
Problem M: Dial Problem ダイヤル式の南京錠がある。この南京錠には、5つのダイヤルと、決定ボタンが用意されている。 5つあるダイヤルを回転させて16進数で5ケタの整数を作り、決定ボタンを押して入力すると、入力した値とパスワードが一致していれば開く仕組みになっている。5つのダイヤルそれぞれには 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,0,1,2, ... の順で文字が現れるように書かれている。 持ち主はパスワードを忘れてしまった。しかし、パスワードの5ケタのうち、1つのケタだけが偶数で残り4つは奇数であったことは覚えている。 ただし、偶数とは0,2,4,6,8,a,c,eであり、奇数とは1,3,5,7,9,b,d,fである。 またこの鍵は不良品で、以下の条件でパスワードと異なる数字を入力しても開いてしまうことが分かっている。本来、入力した整数とパスワードが5つのケタすべてについて一致していなければ鍵は開かない。しかし、パスワードの偶数となっているケタが i ケタ目だった場合、 i ケタ目の一致判定を行うとき、その偶数の前後にある奇数が入力されていた場合でも i ケタ目は一致していると誤判定されてしまうのだ。 例えば、パスワードが"57677"の南京錠ならば、'6'の前後には'5'と'7'の奇数があるため"57677"のほかに、"57577"や"57777"を入力したときでも開いてしまう。 同様に、パスワードが"130bd"の南京錠ならば、'0'の前後には'1'と'f'の奇数があるため"130bd"のほかに、"13fbd"や"131bd"を入力したときでも開いてしまう。 今、パスワードの候補が N 個あり、それらのうちどれか1つが正解のパスワードであることが分かっている。しかたがないので持ち主は、鍵が開くまで何度も整数を入力して試し続けることにした。持ち主はできるだけ決定ボタンを押す回数を少なくしたい。 持ち主が最適な戦略をとったとき、最大で何回決定ボタンを押さなければならないだろうか? その回数を出力せよ。 また、実際に最大となるときに入力する整数の集合を昇順に出力せよ。 答えが一意に定まらない場合は、辞書順で最小となるものを出力せよ。 Input 入力は以下の形式で与えられる。 N password 1 password 2 ... password N 1行目に N が与えられる。 2行目から N +1行目の i 行目には、パスワードの候補を表す5ケタの16進数を表す文字列 password i が与えられる。 Constraints 1 ≤ N ≤ 6000 password i ≠ password j ( i < j ) 文字列は、0から9の数字または英小文字aからfの5文字で構成され、そのうち1文字は偶数であり、他の4文字は奇数である Output 鍵が開くまでに最適な戦略をとったときの決定ボタンを押す回数の最大値を1行に出力せよ。 続いて、最大となるときに入力する整数の集合を昇順に出力せよ。 もし、答えが一意に定まらない場合は、辞書順で最小となるものを出力せよ。 Sample Input 1 1 0111f Sample Output 1 1 0111f Sample Input 2 3 57567 57587 0d351 Sample Output 2 2 0d351 57577 Sample Input 3 6 1811b 1a11b f3b1e f3b1c b0df3 bedf3 Sample Output 3 3 1911b bfdf3 f3b1d
[ { "submission_id": "aoj_1599_3663954", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing Int = long long;\n//BEGIN CUT HERE\ntemplate<typename T,bool directed>\nstruct Dinic{\n struct edge {\n int to;\n T cap;\n int rev;\n edge(){}\n edge(int to,T cap,int rev):to(to),cap(...
aoj_1598_cpp
Problem L: RedBlue Story 天空都市AIZUのUZIA高校では、競技プログラミングの部活動がとても盛んである。 この部活には、 n 人のRed Coderと n 人のBlue Coderが所属している。 ある日の部活の時間に、Red CoderとBlue Coderでペアを組み、この部活から n 組、KCPというコンテストに参加することとなった。この高校では、ペアを組む学生同士は握手するという習わしがあるので、部員たちは今すぐ自分のパートナーを見つけることにした。 部員たちは全速力で走るため、まっすぐにしか進めない。また、部員たちは、それぞれの部員が移動する距離の総和をできるだけ小さくしたいと思っている。 なお、部室には2つの円形のテーブルが置いてある。 Problem 二次元平面上に2つの円、 n 個の赤色の点、 n 個の青色の点がある。 2つの円の中心座標はそれぞれ( x 1 , y 1 ), ( x 2 , y 2 )であり、半径はそれぞれ r 1 , r 2 である。 赤い点 i は、座標( rx i , ry i )にあり、青い点 j は、座標( bx j , by j )にある。 あなたは、以下の操作を n 回繰り返す必要がある。 まだ選ばれていない点の中から、赤色の点と青色の点を1つずつ選んで、2点の共通の目的地を設定し、その目的地に向かって2点をそれぞれまっすぐ移動させる。 目的地は二次元平面上であれば、どこに設定しても構わない。ただし、選んだ2点は移動の際に円の内部を通過することはできないので、そのような移動が発生する目的地を設定することはできない。 n 回の操作をしたときの移動距離の総和を最小化せよ。 n 回の操作を行えない場合は、代わりに"Impossible" (""は除く) と出力せよ。 Input 入力は以下の形式で与えられる。 n x 1 y 1 r 1 x 2 y 2 r 2 rx 1 ry 1 rx 2 ry 2 ... rx n ry n bx 1 by 1 bx 2 by 2 ... bx n by n 入力は全て整数で与えられる。 1行目に n が与えられる。 2行目に x 1 , y 1 , r 1 が空白区切りで与えられる。 3行目に x 2 , y 2 , r 2 が空白区切りで与えられる。 4から3+ n 行に赤い点の座標( rx i , ry i )が空白区切りで与えられる。 4+ n から3+2× n 行に青い点の座標( bx j , by j )が空白区切りで与えられる。 Constraints 入力は以下の条件を満たす。 1 ≤ n ≤ 100 -1000 ≤ x i , y i ≤ 1000 1 ≤ r i ≤ 50 -1000 ≤ rx i , ry i , bx i , by i ≤ 1000 同じ座標に複数の点が存在していることはない 任意の円の半径を絶対値10 -9 以内で変化させても、高々絶対値10 -3 しか変化しない 任意の円の半径を絶対値10 -9 以内で変化させても、"Impossible"なケースは"Impossible"のままである 解は10000を超えない どの点も、円から10 -3 以上離れていて、点が円周上に存在したり、点が円に内包されることはない 2つの円は共通面積を持たず、10 -3 以上離れていることが保証される Output n 回の操作をしたときの移動距離の総和の最小値を1行に出力せよ。なお、出力はジャッジ解の出力との絶対誤差が10 -2 以内であれば許容される。 n 回の操作を行えない場合は、代わりに"Impossible" (""は除く) と出力せよ。 Sample Input 1 2 3 3 2 8 3 2 0 3 3 7 8 0 8 7 Sample Output 1 13.8190642862 Sample Input 2 2 3 3 2 8 3 2 3 0 3 7 8 0 8 7 Sample Output 2 10.0000000000 Sample Input 3 2 3 3 2 8 3 2 0 0 0 5 11 0 11 5 Sample Output 3 22.0000000000 Sample Input 4 1 10 10 10 31 10 10 15 19 26 1 Sample Output 4 Impossible 点同士をつなぐことはできません。
[ { "submission_id": "aoj_1598_8305781", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long LL;\ntypedef long double LD;\nconst int N = 205;\nconst int M = 100005;\nconst LD INF = 1e18;\nconst LD eps = 1e-10;\n\ninline int sgn(LD x) {\n return x > eps? 1 : x < -eps? -1 : 0;\...
aoj_1604_cpp
Deadlock Detection In concurrent processing environments, a deadlock is an undesirable situation where two or more threads are mutually waiting for others to finish using some resources and cannot proceed further. Your task is to detect whether there is any possibility of deadlocks when multiple threads try to execute a given instruction sequence concurrently. The instruction sequence consists of characters ' u ' or digits from ' 0 ' to ' 9 ', and each of them represents one instruction. 10 threads are trying to execute the same single instruction sequence. Each thread starts its execution from the beginning of the sequence and continues in the given order, until all the instructions are executed. There are 10 shared resources called locks from L 0 to L 9 . A digit k is the instruction for acquiring the lock L k . After one of the threads acquires a lock L k , it is kept by the thread until it is released by the instruction ' u '. While a lock is kept, none of the threads, including one already acquired it, cannot newly acquire the same lock L k . Precisely speaking, the following steps are repeated until all threads finish. One thread that has not finished yet is chosen arbitrarily. The chosen thread tries to execute the next instruction that is not executed yet. If the next instruction is a digit k and the lock L k is not kept by any thread, the thread executes the instruction k and acquires L k . If the next instruction is a digit k and the lock L k is already kept by some thread, the instruction k is not executed. If the next instruction is ' u ', the instruction is executed and all the locks currently kept by the thread are released. After executing several steps, sometimes, it falls into the situation that the next instructions of all unfinished threads are for acquiring already kept locks. Once such a situation happens, no instruction will ever be executed no matter which thread is chosen. This situation is called a deadlock . There are instruction sequences for which threads can never reach a deadlock regardless of the execution order. Such instruction sequences are called safe . Otherwise, in other words, if there exists one or more execution orders that lead to a deadlock, the execution sequence is called unsafe . Your task is to write a program that tells whether the given instruction sequence is safe or unsafe. Input The input consists of at most 50 datasets, each in the following format. n s n is the length of the instruction sequence and s is a string representing the sequence. n is a positive integer not exceeding 10,000. Each character of s is either a digit (' 0 ' to ' 9 ') or ' u ', and s always ends with ' u '. The end of the input is indicated by a line with a zero. Output For each dataset, if the given instruction sequence is safe, then print "SAFE" in a line. If it is unsafe, then print "UNSAFE" in a line. Sample Input 11 01u12u0123u 6 01u10u 8 201u210u 9 01u12u20u 3 77u 12 9u8u845u954u 0 Output for the Sampl ...(truncated)
[ { "submission_id": "aoj_1604_10851564", "code_snippet": "#include <iostream>\n#include <string>\n#include <cstring>\nusing namespace std;\n#define rep(i,n) for(int i = 0; i < n; i++)\nbool dp[1024][10];\nvoid solve(int n){\n string s;\n cin >> s;\n int state = 0;\n memset(dp,0,sizeof(dp));\n ...
aoj_1607_cpp
Development of Small Flying Robots You are developing small flying robots in your laboratory. The laboratory is a box-shaped building with K levels, each numbered 1 through K from bottom to top. The floors of all levels are square-shaped with their edges precisely aligned east-west and north-south. Each floor is divided into R × R cells. We denote the cell on the z -th level in the x -th column from the west and the y -th row from the south as ( x , y , z ). (Here, x and y are one-based.) For each x , y , and z ( z > 1), the cell ( x , y , z ) is located immediately above the cell ( x , y , z − 1). There are N robots flying in the laboratory, each numbered from 1 through N . Initially, the i -th robot is located at the cell ( x i , y i , z i ). By your effort so far, you successfully implemented the feature to move each flying robot to any place that you planned. As the next step, you want to implement a new feature that gathers all the robots in some single cell with the lowest energy consumption, based on their current locations and the surrounding environment. Floors of the level two and above have several holes. Holes are rectangular and their edges align with edges of the cells on the floors. There are M holes in the laboratory building, each numbered 1 through M . The j -th hole can be described by five integers u 1 j , v 1 j , u 2 j , v 2 j , and w j . The j -th hole extends over the cells ( x , y , w j ) where u 1 j ≤ x ≤ u 2 j and v 1 j ≤ y ≤ v 2 j . Possible movements of robots and energy consumption involved are as follows. You can move a robot from one cell to an adjacent cell, toward one of north, south, east, or west. The robot consumes its energy by 1 for this move. If there is a hole to go through immediately above, you can move the robot upward by a single level. The robot consumes its energy by 100 for this move. The robots never fall down even if there is a hole below. Note that you can move two or more robots to the same cell. Now, you want to gather all the flying robots at a single cell in the K -th level where there is no hole on the floor, with the least energy consumption. Compute and output the minimum total energy required by the robots. Input The input consists of at most 32 datasets, each in the following format. Every value in the input is an integer. N M K R x 1 y 1 z 1 ... x N y N z N u 11 v 11 u 21 v 21 w 1 ... u 1 M v 1 M u 2 M v 2 M w M N is the number of robots in the laboratory (1 ≤ N ≤ 100). M is the number of holes (1 ≤ M ≤ 50), K is the number of levels (2 ≤ K ≤ 10), and R is the number of cells in one row and also one column on a single floor (3 ≤ R ≤ 1,000,000). For each i , integers x i , y i , and z i represent the cell that the i -th robot initially located at (1 ≤ x i ≤ R , 1 ≤ y i ≤ R , 1 ≤ z i ≤ K ). Further, for each j , integers u 1 j , v 1 j , u 2 j , v 2 j , and w j describe the position and the extent of the j -th hole (1 ≤ u 1 j ≤ u 2 j ≤ R , 1 ≤ v 1 j ≤ v 2 j ≤ R , 2 ≤ w j ≤ K ). Th ...(truncated)
[ { "submission_id": "aoj_1607_9405300", "code_snippet": "#line 2 \"cp-library/src/cp-template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing uint = unsigned int;\nusing ull = unsigned long long;\nusing i32 = int;\nusing u32 = unsigned int;\nusing...
aoj_1606_cpp
Complex Paper Folding Dr. G, an authority in paper folding and one of the directors of Intercultural Consortium on Paper Crafts, believes complexity gives figures beauty. As one of his assistants, your job is to find the way to fold a paper to obtain the most complex figure. Dr. G defines the complexity of a figure by the number of vertices. With the same number of vertices, one with longer perimeter is more complex. To simplify the problem, we consider papers with convex polygon in their shapes and folding them only once. Each paper should be folded so that one of its vertices exactly lies on top of another vertex. Write a program that, for each given convex polygon, outputs the perimeter of the most complex polygon obtained by folding it once. Figure 1 (left) illustrates the polygon given in the first dataset of Sample Input. This happens to be a rectangle. Folding this rectangle can yield three different polygons illustrated in the Figures 1-a to c by solid lines. For this dataset, you should answer the perimeter of the pentagon shown in Figure 1-a. Though the rectangle in Figure 1-b has longer perimeter, the number of vertices takes priority. (a) (b) (c) Figure 1: The case of the first sample input Figure 2 (left) illustrates the polygon given in the second dataset of Sample Input, which is a triangle. Whichever pair of its vertices are chosen, quadrangles are obtained as shown in Figures 2-a to c. Among these, you should answer the longest perimeter, which is that of the quadrangle shown in Figure 2-a. (a) (b) (c) Figure 2: The case of the second sample input Although we start with a convex polygon, folding it may result a concave polygon. Figure 3 (left) illustrates the polygon given in the third dataset in Sample Input. A concave hexagon shown in Figure 3 (right) can be obtained by folding it, which has the largest number of vertices. Figure 3: The case of the third sample input Figure 4 (left) illustrates the polygon given in the fifth dataset in Sample Input. Although the perimeter of the polygon in Figure 4-b is longer than that of the polygon in Figure 4-a, because the polygon in Figure 4-b is a quadrangle, the answer should be the perimeter of the pentagon in Figure 4-a. (a) (b) Figure 4: The case of the fifth sample input Input The input consists of multiple datasets, each in the following format. n x 1 y 1 ... x n y n n is the number of vertices of the given polygon. n is a positive integer satisfying 3 ≤ n ≤ 20. ( x i , y i ) gives the coordinates of the i -th vertex. x i and y i are integers satisfying 0 ≤ x i , y i < 1000. The vertices ( x 1 , y 1 ), ..., ( x n , y n ) are given in the counterclockwise order on the xy -plane with y axis oriented upwards. You can assume that all the given polygons are convex. All the possible polygons obtained by folding the given polygon satisfy the following conditions. Distance between any two vertices is greater than or equal to 0.00001. For any three consecutive vertices P, Q, and R, ...(truncated)
[ { "submission_id": "aoj_1606_10853765", "code_snippet": "#define _CRT_SECURE_NO_WARNINGS\n#include <iostream>\n#include <algorithm>\n#include <cmath>\n#include <cstring>\n#include <cassert>\n#include <vector>\n#include <deque>\ntypedef long long ll;\ntypedef long double ld;\n//typedef double ld;\nconst ld I...
aoj_1603_cpp
500-yen Saving "500-yen Saving" is one of Japanese famous methods to save money. The method is quite simple; whenever you receive a 500-yen coin in your change of shopping, put the coin to your 500-yen saving box. Typically, you will find more than one million yen in your saving box in ten years. Some Japanese people are addicted to the 500-yen saving. They try their best to collect 500-yen coins efficiently by using 1000-yen bills and some coins effectively in their purchasing. For example, you will give 1320 yen (one 1000-yen bill, three 100-yen coins and two 10-yen coins) to pay 817 yen, to receive one 500-yen coin (and three 1-yen coins) in the change. A friend of yours is one of these 500-yen saving addicts. He is planning a sightseeing trip and wants to visit a number of souvenir shops along his way. He will visit souvenir shops one by one according to the trip plan. Every souvenir shop sells only one kind of souvenir goods, and he has the complete list of their prices. He wants to collect as many 500-yen coins as possible through buying at most one souvenir from a shop. On his departure, he will start with sufficiently many 1000-yen bills and no coins at all. The order of shops to visit cannot be changed. As far as he can collect the same number of 500-yen coins, he wants to cut his expenses as much as possible. Let's say that he is visiting shops with their souvenir prices of 800 yen, 700 yen, 1600 yen, and 600 yen, in this order. He can collect at most two 500-yen coins spending 2900 yen, the least expenses to collect two 500-yen coins, in this case. After skipping the first shop, the way of spending 700-yen at the second shop is by handing over a 1000-yen bill and receiving three 100-yen coins. In the next shop, handing over one of these 100-yen coins and two 1000-yen bills for buying a 1600-yen souvenir will make him receive one 500-yen coin. In almost the same way, he can obtain another 500-yen coin at the last shop. He can also collect two 500-yen coins buying at the first shop, but his total expenditure will be at least 3000 yen because he needs to buy both the 1600-yen and 600-yen souvenirs in this case. You are asked to make a program to help his collecting 500-yen coins during the trip. Receiving souvenirs' prices listed in the order of visiting the shops, your program is to find the maximum number of 500-yen coins that he can collect during his trip, and the minimum expenses needed for that number of 500-yen coins. For shopping, he can use an arbitrary number of 1-yen, 5-yen, 10-yen, 50-yen, and 100-yen coins he has, and arbitrarily many 1000-yen bills. The shop always returns the exact change, i.e., the difference between the amount he hands over and the price of the souvenir. The shop has sufficient stock of coins and the change is always composed of the smallest possible number of 1-yen, 5-yen, 10-yen, 50-yen, 100-yen, and 500-yen coins and 1000-yen bills. He may use more money than the price of the souvenir, eve ...(truncated)
[ { "submission_id": "aoj_1603_11044339", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef int ll;\ntypedef unsigned long long ull;\ntypedef long double ld;\n#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)\n#define rrep(i,start,end) for (ll i = start;i >= (ll)(end);i--)\n#define rep...
aoj_1609_cpp
Look for the Winner! The citizens of TKB City are famous for their deep love in elections and vote counting. Today they hold an election for the next chairperson of the electoral commission. Now the voting has just been closed and the counting is going to start. The TKB citizens have strong desire to know the winner as early as possible during vote counting. The election candidate receiving the most votes shall be the next chairperson. Suppose for instance that we have three candidates A , B , and C and ten votes. Suppose also that we have already counted six of the ten votes and the vote counts of A , B , and C are four, one, and one, respectively. At this moment, every candidate has a chance to receive four more votes and so everyone can still be the winner. However, if the next vote counted is cast for A , A is ensured to be the winner since A already has five votes and B or C can have at most four votes at the end. In this example, therefore, the TKB citizens can know the winner just when the seventh vote is counted. Your mission is to write a program that receives every vote counted, one by one, identifies the winner, and determines when the winner gets ensured. Input The input consists of at most 1500 datasets, each consisting of two lines in the following format. n c 1 c 2 … c n n in the first line represents the number of votes, and is a positive integer no greater than 100. The second line represents the n votes, separated by a space. Each c i (1 ≤ i ≤ n ) is a single uppercase letter, i.e. one of 'A' through 'Z'. This represents the election candidate for which the i -th vote was cast. Counting shall be done in the given order from c 1 to c n . You should assume that at least two stand as candidates even when all the votes are cast for one candidate. The end of the input is indicated by a line containing a zero. Output For each dataset, unless the election ends in a tie, output a single line containing an uppercase letter c and an integer d separated by a space: c should represent the election winner and d should represent after counting how many votes the winner is identified. Otherwise, that is, if the election ends in a tie, output a single line containing `TIE'. Sample Input 1 A 4 A A B B 5 L M N L N 6 K K K K K K 6 X X X Y Z X 10 A A A B A C A C C B 10 U U U U U V V W W W 0 Output for the Sample Input A 1 TIE TIE K 4 X 5 A 7 U 8
[ { "submission_id": "aoj_1609_10810381", "code_snippet": "#include <iostream>\n#include <string>\n#include <algorithm>\nusing namespace std;\n\nint N;\nchar S[100009];\n\nvoid Testcase() {\n pair<int, char> votes[26], votes2[256];\n for (int i = 0; i < 26; i++) votes[i] = make_pair(0, 'A' + i);\n\n ...
aoj_1610_cpp
Bamboo Blossoms The bamboos live for decades, and at the end of their lives, they flower to make their seeds. Dr. ACM, a biologist, was fascinated by the bamboos in blossom in his travel to Tsukuba. He liked the flower so much that he was tempted to make a garden where the bamboos bloom annually. Dr. ACM started research of improving breed of the bamboos, and finally, he established a method to develop bamboo breeds with controlled lifetimes. With this method, he can develop bamboo breeds that flower after arbitrarily specified years. Let us call bamboos that flower k years after sowing " k -year-bamboos." k years after being sowed, k -year-bamboos make their seeds and then die, hence their next generation flowers after another k years. In this way, if he sows seeds of k -year-bamboos, he can see bamboo blossoms every k years. For example, assuming that he sows seeds of 15-year-bamboos, he can see bamboo blossoms every 15 years; 15 years, 30 years, 45 years, and so on, after sowing. Dr. ACM asked you for designing his garden. His garden is partitioned into blocks, in each of which only a single breed of bamboo can grow. Dr. ACM requested you to decide which breeds of bamboos should he sow in the blocks in order to see bamboo blossoms in at least one block for as many years as possible. You immediately suggested to sow seeds of one-year-bamboos in all blocks. Dr. ACM, however, said that it was difficult to develop a bamboo breed with short lifetime, and would like a plan using only those breeds with long lifetimes. He also said that, although he could wait for some years until he would see the first bloom, he would like to see it in every following year. Then, you suggested a plan to sow seeds of 10-year-bamboos, for example, in different blocks each year, that is, to sow in a block this year and in another block next year, and so on, for 10 years. Following this plan, he could see bamboo blossoms in one block every year except for the first 10 years. Dr. ACM objected again saying he had determined to sow in all blocks this year. After all, you made up your mind to make a sowing plan where the bamboos bloom in at least one block for as many consecutive years as possible after the first m years (including this year) under the following conditions: the plan should use only those bamboo breeds whose lifetimes are m years or longer, and Dr. ACM should sow the seeds in all the blocks only this year. Input The input consists of at most 50 datasets, each in the following format. m n An integer m (2 ≤ m ≤ 100) represents the lifetime (in years) of the bamboos with the shortest lifetime that Dr. ACM can use for gardening. An integer n (1 ≤ n ≤ 500,000) represents the number of blocks. The end of the input is indicated by a line containing two zeros. Output No matter how good your plan is, a "dull-year" would eventually come, in which the bamboos do not flower in an ...(truncated)
[ { "submission_id": "aoj_1610_10848308", "code_snippet": "#include \"bits/stdc++.h\"\n\nusing namespace std;\nusing ll = long long;\nconst double pi = acos(-1);\n#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)\n#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)\n#define R...
aoj_1614_cpp
Warp Drive The warp drive technology is reforming air travel, making the travel times drastically shorter. Aircraft reaching above the warp fields built on the ground surface can be transferred to any desired destination in a twinkling. With the current immature technology, however, building warp fields is quite expensive. Our budget allows building only two of them. Fortunately, the cost does not depend on the locations of the warp fields, and we can build them anywhere on the ground surface, even at an airport. Your task is, given locations of airports and a list of one way flights among them, find the best locations to build the two warp fields that give the minimal average cost . The average cost is the root mean square of travel times of all the flights, defined as follows. Here, m is the number of flights and t j is the shortest possible travel time of the j -th flight. Note that the value of t j depends on the locations of the warp fields. For simplicity, we approximate the surface of the ground by a flat two dimensional plane, and approximate airports, aircraft, and warp fields by points on the plane. Different flights use different aircraft with possibly different cruising speeds. Times required for climb, acceleration, deceleration and descent are negligible. Further, when an aircraft reaches above a warp field, time required after that to its destination is zero. As is explained below, the airports have integral coordinates. Note, however, that the warp fields can have non-integer coordinates. Input The input consists of at most 35 datasets, each in the following format. n m x 1 y 1 ... x n y n a 1 b 1 v 1 ... a m b m v m n is the number of airports, and m is the number of flights (2 ≤ n ≤ 20, 2 ≤ m ≤ 40). For each i , x i and y i are the coordinates of the i -th airport. x i and y i are integers with absolute values at most 1000. For each j , a j and b j are integers between 1 and n inclusive, and are the indices of the departure and arrival airports for the j -th flight, respectively. v j is the cruising speed for the j -th flight, that is, the distance that the aircraft of the flight moves in one time unit. v j is a decimal fraction with two digits after the decimal point (1 ≤ v j ≤ 10). The following are guaranteed. Two different airports have different coordinates. The departure and arrival airports of any of the flights are different. Two different flights have different departure airport and/or arrival airport. The end of the input is indicated by a line containing two zeros. Output For each dataset, output the average cost when you place the two warp fields optimally. The output should not contain an absolute error greater than 10 -6 . Sample Input 3 4 100 4 100 0 0 0 1 2 1.00 2 1 1.00 3 1 9.99 3 2 9.99 7 6 0 0 1 0 2 0 0 10 1 10 2 10 20 5 1 7 1.00 2 7 1.00 3 7 1.00 4 7 1.00 5 7 1.00 6 7 1.00 4 4 -1 -1 1 -1 -1 1 1 1 1 4 1.10 4 2 2.10 2 3 3.10 3 1 4.10 8 12 -91 0 -62 0 -18 0 2 0 23 0 55 0 63 0 80 0 2 7 3.96 3 2 2.25 2 4 5.48 8 7 ...(truncated)
[ { "submission_id": "aoj_1614_10579461", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\n\n#define rep(i,n) for(ll i=0;i<n;++i)\n#define all(a) (a).begin(),(a).end()\nll intpow(ll a, ll b){ ll ans = 1; while(b){...
aoj_1612_cpp
3D Printing We are designing an installation art piece consisting of a number of cubes with 3D printing technology for submitting one to Installation art Contest with Printed Cubes (ICPC). At this time, we are trying to model a piece consisting of exactly k cubes of the same size facing the same direction. First, using a CAD system, we prepare n ( n ≥ k ) positions as candidates in the 3D space where cubes can be placed. When cubes would be placed at all the candidate positions, the following three conditions are satisfied. Each cube may overlap zero, one or two other cubes, but not three or more. When a cube overlap two other cubes, those two cubes do not overlap. Two non-overlapping cubes do not touch at their surfaces, edges or corners. Second, choosing appropriate k different positions from n candidates and placing cubes there, we obtain a connected polyhedron as a union of the k cubes. When we use a 3D printer, we usually print only the thin surface of a 3D object. In order to save the amount of filament material for the 3D printer, we want to find the polyhedron with the minimal surface area. Your job is to find the polyhedron with the minimal surface area consisting of k connected cubes placed at k selected positions among n given ones. Figure E1. A polyhedron formed with connected identical cubes. Input The input consists of multiple datasets. The number of datasets is at most 100. Each dataset is in the following format. n k s x 1 y 1 z 1 ... x n y n z n In the first line of a dataset, n is the number of the candidate positions, k is the number of the cubes to form the connected polyhedron, and s is the edge length of cubes. n, k and s are integers separated by a space. The following n lines specify the n candidate positions. In the i -th line, there are three integers x i , y i and z i that specify the coordinates of a position, where the corner of the cube with the smallest coordinate values may be placed. Edges of the cubes are to be aligned with either of three axes. All the values of coordinates are integers separated by a space. The three conditions on the candidate positions mentioned above are satisfied. The parameters satisfy the following conditions: 1 ≤ k ≤ n ≤ 2000, 3 ≤ s ≤ 100, and -4×10 7 ≤ x i , y i , z i ≤ 4×10 7 . The end of the input is indicated by a line containing three zeros separated by a space. Output For each dataset, output a single line containing one integer indicating the surface area of the connected polyhedron with the minimal surface area. When no k cubes form a connected polyhedron, output -1. Sample Input 1 1 100 100 100 100 6 4 10 100 100 100 106 102 102 112 110 104 104 116 102 100 114 104 92 107 100 10 4 10 -100 101 100 -108 102 120 -116 103 100 -124 100 100 -132 99 100 -92 98 100 -84 100 140 -76 103 100 -68 102 100 -60 101 100 10 4 10 100 100 100 108 101 100 116 102 100 124 100 100 132 102 100 200 100 103 192 100 102 184 100 101 176 100 100 168 100 103 4 4 10 100 100 100 108 94 100 1 ...(truncated)
[ { "submission_id": "aoj_1612_10578406", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nnamespace atcoder {\n\n// Implement (union by size) + (path compression)\n// Reference:\n// Zvi Galil and Giuseppe F. Italiano,\n// Data structures and algorithms for disjoint set union problems\nstruct...
aoj_1611_cpp
Daruma Otoshi You are playing a variant of a game called "Daruma Otoshi (Dharma Block Striking)". At the start of a game, several wooden blocks of the same size but with varying weights are stacked on top of each other, forming a tower. Another block symbolizing Dharma is placed atop. You have a wooden hammer with its head thicker than the height of a block, but not twice that. You can choose any two adjacent blocks, except Dharma on the top, differing at most 1 in their weight, and push both of them out of the stack with a single blow of your hammer. The blocks above the removed ones then fall straight down, without collapsing the tower. You cannot hit a block pair with weight difference of 2 or more, for that makes too hard to push out blocks while keeping the balance of the tower. There is no chance in hitting three blocks out at a time, for that would require superhuman accuracy. The goal of the game is to remove as many blocks as you can. Your task is to decide the number of blocks that can be removed by repeating the blows in an optimal order. Figure D1. Striking out two blocks at a time In the above figure, with a stack of four blocks weighing 1, 2, 3, and 1, in this order from the bottom, you can hit middle two blocks, weighing 2 and 3, out from the stack. The blocks above will then fall down, and two blocks weighing 1 and the Dharma block will remain. You can then push out the remaining pair of weight-1 blocks after that. Input The input consists of multiple datasets. The number of datasets is at most 50. Each dataset is in the following format. n w 1 w 2 … w n n is the number of blocks, except Dharma on the top. n is a positive integer not exceeding 300. w i gives the weight of the i -th block counted from the bottom. w i is an integer between 1 and 1000, inclusive. The end of the input is indicated by a line containing a zero. Output For each dataset, output in a line the maximum number of blocks you can remove. Sample Input 4 1 2 3 4 4 1 2 3 1 5 5 1 2 3 6 14 8 7 1 4 3 5 4 1 6 8 10 4 6 5 5 1 3 5 1 3 0 Output for the Sample Input 4 4 2 12 0
[ { "submission_id": "aoj_1611_11035458", "code_snippet": "#include <iostream>\n#include <vector>\n\nusing namespace std;\n\n\nint main(){\n\n while(1){\n int n;\n cin >> n;\n if (n == 0) break;\n vector<int> w(n);\n for (int i=0; i<n; i++) cin >> w[i];\n\n vector<...
aoj_1605_cpp
Bridge Construction Planning There is a city consisting of many small islands, and the citizens live in these islands. Citizens feel inconvenience in requiring ferry rides between these islands. The city mayor decided to build bridges connecting all the islands. The city has two construction companies, A and B. The mayor requested these companies for proposals, and obtained proposals in the form: "Company A (or B) can build a bridge between islands u and v in w hundred million yen." The mayor wants to accept some of these proposals to make a plan with the lowest budget. However, if the mayor accepts too many proposals of one company, the other may go bankrupt, which is not desirable for the city with only two construction companies. However, on the other hand, to avoid criticism on wasteful construction, the mayor can only accept the minimum number (i.e., n − 1) of bridges for connecting all the islands. Thus, the mayor made a decision that exactly k proposals by the company A and exactly n − 1 − k proposals by the company B should be accepted. Your task is to write a program that computes the cost of the plan with the lowest budget that satisfies the constraints. Here, the cost of a plan means the sum of all the costs mentioned in the accepted proposals. Input The input consists of multiple datasets. The number of datasets is at most 30. Each dataset is in the following format. n m k u 1 v 1 w 1 l 1 ... u m v m w m l m The first line contains three integers n , m , and k , where n is the number of islands, m is the total number of proposals, and k is the number of proposals that are to be ordered to company A (2 ≤ n ≤ 200, 1 ≤ m ≤ 600, and 0 ≤ k ≤ n −1). Islands are identified by integers, 1 through n . The following m lines denote the proposals each of which is described with three integers u i , v i , w i and one character l i , where u i and v i denote two bridged islands, w i is the cost of the bridge (in hundred million yen), and l i is the name of the company that submits this proposal (1 ≤ u i ≤ n , 1 ≤ v i ≤ n , 1 ≤ w i ≤ 100, and l i = 'A' or 'B'). You can assume that each bridge connects distinct islands, i.e., u i ≠ v i , and each company gives at most one proposal for each pair of islands, i.e., { u i , v i } ≠ { u j , v j } if i ≠ j and l i = l j . The end of the input is indicated by a line with three zeros separated by single spaces. Output For each dataset, output a single line containing a single integer that denotes the cost (in hundred million yen) of the plan with the lowest budget. If there are no plans that satisfy the constraints, output −1. Sample Input 4 5 2 1 2 2 A 1 3 2 A 1 4 2 A 2 3 1 B 3 4 1 B 5 8 2 1 2 1 A 2 3 1 A 3 4 3 A 4 5 3 A 1 2 5 B 2 3 5 B 3 4 8 B 4 5 8 B 5 5 1 1 2 1 A 2 3 1 A 3 4 1 A 4 5 1 B 3 5 1 B 4 5 3 1 2 2 A 2 4 3 B 3 4 4 B 2 3 5 A 3 1 6 A 0 0 0 Output for the Sample Input 5 16 -1 -1
[ { "submission_id": "aoj_1605_10719736", "code_snippet": "/*\n\nhttps://hitonanode.github.io/cplib-cpp/combinatorial_opt/test/matroid_intersection_dijkstra.aoj1605.test.cpp\n\nedit by noya2\n\n*/\n\n#line 1 \"combinatorial_opt/test/matroid_intersection_dijkstra.aoj1605.test.cpp\"\n#define PROBLEM \"https://j...
aoj_1615_cpp
Gift Exchange Party A gift exchange party will be held at a school in TKB City. For every pair of students who are close friends, one gift must be given from one to the other at this party, but not the other way around. It is decided in advance the gift directions, that is, which student of each pair receives a gift. No other gift exchanges are made. If each pair randomly decided the gift direction, some might receive countless gifts, while some might receive only few or even none. You'd like to decide the gift directions for all the friend pairs that minimize the difference between the smallest and the largest numbers of gifts received by a student. Find the smallest and the largest numbers of gifts received when the difference between them is minimized. When there is more than one way to realize that, find the way that maximizes the smallest number of received gifts. Input The input consists of at most 10 datasets, each in the following format. n m u 1 v 1 ... u m v m n is the number of students, and m is the number of friendship relations (2 ≤ n ≤ 100, 1 ≤ m ≤ n ( n -1)/2). Students are denoted by integers between 1 and n , inclusive. The following m lines describe the friendship relations: for each i , student u i and v i are close friends ( u i < v i ). The same friendship relations do not appear more than once. The end of the input is indicated by a line containing two zeros. Output For each dataset, output a single line containing two integers l and h separated by a single space. Here, l and h are the smallest and the largest numbers, respectively, of gifts received by a student. Sample Input 3 3 1 2 2 3 1 3 4 3 1 2 1 3 1 4 4 6 1 2 1 3 1 4 2 3 3 4 2 4 0 0 Output for the Sample Input 1 1 0 1 1 2
[ { "submission_id": "aoj_1615_10945981", "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_1613_cpp
Deciphering Characters Image data which are left by a mysterious syndicate were discovered. You are requested to analyze the data. The syndicate members used characters invented independently. A binary image corresponds to one character written in black ink on white paper. Although you found many variant images that represent the same character, you discovered that you can judge whether or not two images represent the same character with the surrounding relation of connected components. We present some definitions as follows. Here, we assume that white pixels fill outside of the given image. White connected component : A set of white pixels connected to each other horizontally or vertically (see below). Black connected component : A set of black pixels connected to each other horizontally, vertically, or diagonally (see below). Connected component : A white or a black connected component. Background component : The connected component including pixels outside of the image. Any white pixels on the periphery of the image are thus included in the background component. connected disconnected connected connected Connectedness of white pixels Connectedness of black pixels Let C 1 be a connected component in an image and C 2 be another connected component in the same image with the opposite color. Let's think of a modified image in which colors of all pixels not included in C 1 nor C 2 are changed to that of C 2 . If neither C 1 nor C 2 is the background component, the color of the background component is changed to that of C 2 . We say that C 1 surrounds C 2 in the original image when pixels in C 2 are not included in the background component in the modified image. (see below) Two images represent the same character if both of the following conditions are satisfied. The two images have the same number of connected components. Let S and S' be the sets of connected components of the two images. A bijective function f : S → S' satisfying the following conditions exists. For each connected component C that belongs to S , f ( C ) has the same color as C . For each of C 1 and C 2 belonging to S , f ( C 1 ) surrounds f ( C 2 ) if and only if C 1 surrounds C 2 . Let's see an example. Connected components in the images of the figure below has the following surrounding relations. C 1 surrounds C 2 . C 2 surrounds C 3 . C 2 surrounds C 4 . C' 1 surrounds C' 2 . C' 2 surrounds C' 3 . C' 2 surrounds C' 4 . A bijective function defined as f ( C i ) = C' i for each connected component satisfies the conditions stated above. Therefore, we can conclude that the two images represent the same character. Make a program judging whether given two images represent the same character. Input The input consists of at most 200 datasets. The end of the input is represented by a line containing two zeros. Each dataset is formatted as follows. image 1 image 2 Each image has the following format. h w p (1,1) ... p (1, w ) ... p ( h ,1) ... p ( h , w ) h and w are the height an ...(truncated)
[ { "submission_id": "aoj_1613_10867681", "code_snippet": "#include <bits/stdc++.h>\n \n#define FOR(i,a,b) for( ll i = (a); i < (ll)(b); i++ )\n#define REP(i,n) FOR(i,0,n)\n#define YYS(x,arr) for(auto& x:arr)\n#define ALL(x) (x).begin(),(x).end()\n#define SORT(x) sort( (x).begin(),(x).end() )\n#define RE...
aoj_1618_cpp
A Garden with Ponds Mr. Gardiner is a modern garden designer who is excellent at utilizing the terrain features. His design method is unique: he first decides the location of ponds and design them with the terrain features intact. According to his unique design procedure, all of his ponds are rectangular with simple aspect ratios. First, Mr. Gardiner draws a regular grid on the map of the garden site so that the land is divided into cells of unit square, and annotates every cell with its elevation. In his design method, a pond occupies a rectangular area consisting of a number of cells. Each of its outermost cells has to be higher than all of its inner cells. For instance, in the following grid map, in which numbers are elevations of cells, a pond can occupy the shaded area, where the outermost cells are shaded darker and the inner cells are shaded lighter. You can easily see that the elevations of the outermost cells are at least three and those of the inner ones are at most two. A rectangular area on which a pond is built must have at least one inner cell. Therefore, both its width and depth are at least three. When you pour water at an inner cell of a pond, the water can be kept in the pond until its level reaches that of the lowest outermost cells. If you continue pouring, the water inevitably spills over. Mr. Gardiner considers the larger capacity the pond has, the better it is. Here, the capacity of a pond is the maximum amount of water it can keep. For instance, when a pond is built on the shaded area in the above map, its capacity is (3 − 1) + (3 − 0) + (3 − 2) = 6, where 3 is the lowest elevation of the outermost cells and 1, 0, 2 are the elevations of the inner cells. Your mission is to write a computer program that, given a grid map describing the elevation of each unit square cell, calculates the largest possible capacity of a pond built in the site. Note that neither of the following rectangular areas can be a pond. In the left one, the cell at the bottom right corner is not higher than the inner cell. In the right one, the central cell is as high as the outermost cells. Input The input consists of at most 100 datasets, each in the following format. d w e 1, 1 ... e 1, w ... e d , 1 ... e d, w The first line contains d and w , representing the depth and the width, respectively, of the garden site described in the map. They are positive integers between 3 and 10, inclusive. Each of the following d lines contains w integers between 0 and 9, inclusive, separated by a space. The x -th integer in the y -th line of the d lines is the elevation of the unit square cell with coordinates ( x, y ). The end of the input is indicated by a line containing two zeros separated by a space. Output For each dataset, output a single line containing the largest possible capacity of a pond that can be built in the garden site described in the dataset. If no ponds can be built, output a single line containing a zero. Sample ...(truncated)
[ { "submission_id": "aoj_1618_10542183", "code_snippet": "#include<iostream>\n#include<cmath>\n#include<string>\n#include<vector>\n#include<algorithm>\n#include<tuple>\n#include<map>\n#include<queue>\n#include<set>\n#include<unordered_set>\n#include<stack>\n#include<deque>\n#include<bitset>\n#include<cassert...
aoj_1624_cpp
Income Inequality We often compute the average as the first step in processing statistical data. Yes, the average is a good tendency measure of data, but it is not always the best. In some cases, the average may hinder the understanding of the data. For example, consider the national income of a country. As the term income inequality suggests, a small number of people earn a good portion of the gross national income in many countries. In such cases, the average income computes much higher than the income of the vast majority. It is not appropriate to regard the average as the income of typical people. Let us observe the above-mentioned phenomenon in some concrete data. Incomes of n people, a 1 , ... , a n , are given. You are asked to write a program that reports the number of people whose incomes are less than or equal to the average ( a 1 + ... + a n ) / n . Input The input consists of multiple datasets, each in the following format. n a 1 a 2 ... a n A dataset consists of two lines. In the first line, the number of people n is given. n is an integer satisfying 2 ≤ n ≤ 10 000. In the second line, incomes of n people are given. a i (1 ≤ i ≤ n ) is the income of the i -th person. This value is an integer greater than or equal to 1 and less than or equal to 100 000. The end of the input is indicated by a line containing a zero. The sum of n 's of all the datasets does not exceed 50 000. Output For each dataset, output the number of people whose incomes are less than or equal to the average. Sample Input 7 15 15 15 15 15 15 15 4 10 20 30 60 10 1 1 1 1 1 1 1 1 1 100 7 90 90 90 90 90 90 10 7 2 7 1 8 2 8 4 0 Output for the Sample Input 7 3 9 1 4
[ { "submission_id": "aoj_1624_9210748", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nint main(){\n vector<int>ans(0);\n while(true){\n double n;\n cin >> n;\n if(n==0){\n break;\n }\n vector<double>A(n);\n for(int i=0;i<n;i++){\n...
aoj_1616_cpp
Taro's Shopping Mammy decided to give Taro his first shopping experience. Mammy tells him to choose any two items he wants from those listed in the shopping catalogue, but Taro cannot decide which two, as all the items look attractive. Thus he plans to buy the pair of two items with the highest price sum, not exceeding the amount Mammy allows. As getting two of the same item is boring, he wants two different items. You are asked to help Taro select the two items. The price list for all of the items is given. Among pairs of two items in the list, find the pair with the highest price sum not exceeding the allowed amount, and report the sum. Taro is buying two items, not one, nor three, nor more. Note that, two or more items in the list may be priced equally. Input The input consists of multiple datasets, each in the following format. n m a 1 a 2 ... a n A dataset consists of two lines. In the first line, the number of items n and the maximum payment allowed m are given. n is an integer satisfying 2 ≤ n ≤ 1000. m is an integer satisfying 2 ≤ m ≤ 2,000,000. In the second line, prices of n items are given. a i (1 ≤ i ≤ n ) is the price of the i -th item. This value is an integer greater than or equal to 1 and less than or equal to 1,000,000. The end of the input is indicated by a line containing two zeros. The sum of n 's of all the datasets does not exceed 50,000. Output For each dataset, find the pair with the highest price sum not exceeding the allowed amount m and output the sum in a line. If the price sum of every pair of items exceeds m , output NONE instead. Sample Input 3 45 10 20 30 6 10 1 2 5 8 9 11 7 100 11 34 83 47 59 29 70 4 100 80 70 60 50 4 20 10 5 10 16 0 0 Output for the Sample Input 40 10 99 NONE 20
[ { "submission_id": "aoj_1616_10840723", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nint main(){\n while(true){\n int n, m;\n cin >> n >> m;\n if(!n) break;\n\n vector<int> price(n);\n for(auto& i:price) cin >> i;\n\n int ans = -1;\n\n for(i...
aoj_1622_cpp
Go around the Labyrinth Explorer Taro got a floor plan of a labyrinth. The floor of this labyrinth is in the form of a two-dimensional grid. Each of the cells on the floor plan corresponds to a room and is indicated whether it can be entered or not. The labyrinth has only one entrance located at the northwest corner, which is upper left on the floor plan. There is a treasure chest in each of the rooms at other three corners, southwest, southeast, and northeast. To get a treasure chest, Taro must move onto the room where the treasure chest is placed. Taro starts from the entrance room and repeats moving to one of the enterable adjacent rooms in the four directions, north, south, east, or west. He wants to collect all the three treasure chests and come back to the entrance room. A bad news for Taro is that, the labyrinth is quite dilapidated and even for its enterable rooms except for the entrance room, floors are so fragile that, once passed over, it will collapse and the room becomes not enterable. Determine whether it is possible to collect all the treasure chests and return to the entrance. Input The input consists of at most 100 datasets, each in the following format. N M c 1,1 ... c 1, M ... c N ,1 ... c N , M The first line contains N , which is the number of rooms in the north-south direction, and M , which is the number of rooms in the east-west direction. N and M are integers satisfying 2 ≤ N ≤ 50 and 2 ≤ M ≤ 50. Each of the following N lines contains a string of length M . The j -th character c i,j of the i -th string represents the state of the room ( i,j ), i -th from the northernmost and j -th from the westernmost; the character is the period (' . ') if the room is enterable and the number sign (' # ') if the room is not enterable. The entrance is located at (1,1), and the treasure chests are placed at ( N, 1), ( N,M ) and (1, M ). All of these four rooms are enterable. Taro cannot go outside the given N × M rooms. The end of the input is indicated by a line containing two zeros. Output For each dataset, output YES if it is possible to collect all the treasure chests and return to the entrance room, and otherwise, output NO in a single line. Sample Input 3 3 ... .#. ... 5 5 ..#.. ..... #.... ..... ..... 3 8 ..#..... ........ .....#.. 3 5 ..#.. ..... ..#.. 4 4 .... .... ..## ..#. 0 0 Output for the Sample Input YES NO YES NO NO
[ { "submission_id": "aoj_1622_10703013", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nconst int INF = 1e9;\n\ntemplate<typename flow>\nstruct max_flow {\n struct edge {\n int to;\n flow cap;\n int rev;\n };\n int V;\n vector<vector<edge> > G;\n vector<in...
aoj_1619_cpp
Making Lunch Boxes Taro has been hooked on making lunch boxes recently. Taro has obtained a new lunch box recipe book today, and wants to try as many of the recipes listed in the book as possible. Enough of the ingredients for all the recipes are at hand, but they all are in vacuum packs of two. If only one of them is used leaving the other, the leftover will be rotten easily, but making two of the same recipe is not interesting. Thus he decided to make a set of lunch boxes, each with different recipe, that wouldn't leave any unused ingredients. Note that the book may include recipes for different lunch boxes made of the same set of ingredients. How many lunch box recipes can Taro try today at most following his dogma? Input The input consists of at most 50 datasets, each in the following format. n m b 1,1 ... b 1, m ... b n, 1 ... b n,m The first line contains n , which is the number of recipes listed in the book, and m , which is the number of ingredients. Both n and m are positive integers and satisfy 1 ≤ n ≤ 500, 1 ≤ m ≤ 500 and 1 ≤ n × m ≤ 500 . The following n lines contain the information for each recipe with the string of length m consisting of 0 or 1. b i,j implies whether the i -th recipe needs the j -th ingredient. 1 means the ingredient is needed for the recipe and 0 means not. Each line contains at least one 1. The end of the input is indicated by a line containing two zeros. Output For each dataset, output the maximum number of recipes Taro can try. Sample Input 4 3 110 101 011 110 7 1 1 1 1 1 1 1 1 4 5 10000 01000 00100 00010 6 6 111111 011000 100000 000010 100001 100100 0 0 Output for the Sample Input 3 6 0 6
[ { "submission_id": "aoj_1619_10849110", "code_snippet": "#include<cstdio>\n#include<iostream>\n#include<map>\n#include<vector>\n#include<string>\n#include<algorithm>\n\nusing namespace std;\n\nstring xx(string a, string b)\n{\n for(int i = 0; i < a.size(); i++)\n a[i] = (a[i]-'0')^(b[i]-'0')+'0';\...
aoj_1620_cpp
Boolean Expression Compressor You are asked to build a compressor for Boolean expressions that transforms expressions to the shortest form keeping their meaning. The grammar of the Boolean expressions has terminals 0 1 a b c d - ^ * ( ) , start symbol <E> and the following production rule: <E>  ::= 0 | 1 | a | b | c | d | - <E>  | ( <E> ^ <E> ) | ( <E> * <E> ) Letters a , b , c and d represent Boolean variables that have values of either 0 or 1 . Operators are evaluated as shown in the Table below. In other words, - means negation (NOT), ^ means exclusive disjunction (XOR), and * means logical conjunction (AND). Table: Evaluations of operators Write a program that calculates the length of the shortest expression that evaluates equal to the given expression with whatever values of the four variables. For example, 0 , that is the first expression in the sample input, cannot be shortened further. Therefore the shortest length for this expression is 1. For another example, (a*(1*b)) , the second in the sample input, always evaluates equal to (a*b) and (b*a) , which are the shortest. The output for this expression, thus, should be 5 . Input The input consists of multiple datasets. A dataset consists of one line, containing an expression conforming to the grammar described above. The length of the expression is less than or equal to 16 characters. The end of the input is indicated by a line containing one ‘ . ’ (period). The number of datasets in the input is at most 200. Output For each dataset, output a single line containing an integer which is the length of the shortest expression that has the same value as the given expression for all combinations of values in the variables. Sample Input 0 (a*(1*b)) (1^a) (-(-a*-b)*a) (a^(b^(c^d))) . Output for the Sample Input 1 5 2 1 13
[ { "submission_id": "aoj_1620_10853829", "code_snippet": "#include<cstdio>\n#include<algorithm>\n#include<vector>\n#include<queue>\n#include<map>\n#include<cstring>\nusing namespace std;\n\nconst int N = 16;\n\nchar S[N + 2];\nint T[N + 2], p;\nmap<char, int> M;\nconst char *al = \"abcd\";\nconst char *nu = ...
aoj_1623_cpp
Equivalent Deformation Two triangles T 1 and T 2 with the same area are on a plane. Your task is to perform the following operation to T 1 several times so that it is exactly superposed on T 2 . Here, vertices of T 1 can be on any of the vertices of T 2 . Compute the minimum number of operations required to superpose T 1 on T 2 . (Operation): Choose one vertex of the triangle and move it to an arbitrary point on a line passing through the vertex and parallel to the opposite side. An operation example The following figure shows a possible sequence of the operations for the first dataset of the sample input. Input The input consists of at most 2000 datasets, each in the following format. x 11 y 11 x 12 y 12 x 13 y 13 x 21 y 21 x 22 y 22 x 23 y 23 x ij and y ij are the x- and y- coordinate of the j -th vertex of T i . The following holds for the dataset. All the coordinate values are integers with at most 1000 of absolute values. T 1 and T 2 have the same positive area size. The given six vertices are all distinct points. An empty line is placed between datasets. The end of the input is indicated by EOF. Output For each dataset, output the minimum number of operations required in one line. If five or more operations are required, output Many instead. Note that, vertices may have non-integral values after they are moved. You can prove that, for any input satisfying the above constraints, the number of operations required is bounded by some constant. Sample Input 0 1 2 2 1 0 1 3 5 2 4 3 0 0 0 1 1 0 0 3 0 2 1 -1 -5 -4 0 1 0 15 -10 14 -5 10 0 -8 -110 221 -731 525 -555 -258 511 -83 -1000 -737 66 -562 533 -45 -525 -450 -282 -667 -439 823 -196 606 -768 -233 0 0 0 1 1 0 99 1 100 1 55 2 354 -289 89 -79 256 -166 131 -196 -774 -809 -519 -623 -990 688 -38 601 -360 712 384 759 -241 140 -59 196 629 -591 360 -847 936 -265 109 -990 -456 -913 -787 -884 -1000 -1000 -999 -999 -1000 -998 1000 1000 999 999 1000 998 -386 -83 404 -83 -408 -117 -162 -348 128 -88 296 -30 -521 -245 -613 -250 797 451 -642 239 646 309 -907 180 -909 -544 -394 10 -296 260 -833 268 -875 882 -907 -423 Output for the Sample Input 4 3 3 3 3 4 4 4 4 Many Many Many Many
[ { "submission_id": "aoj_1623_9769018", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)\n#define rrep(i, a, b) for (ll i = (ll)(b)-1; i >= (ll)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)), (v)...
aoj_1625_cpp
Origami, or the art of folding paper Master Grus is a famous origami (paper folding) artist, who is enthusiastic about exploring the possibility of origami art. For future creation, he is now planning fundamental experiments to establish the general theory of origami. One rectangular piece of paper is used in each of his experiments. He folds it horizontally and/or vertically several times and then punches through holes in the folded paper. The following figure illustrates the folding process of a simple experiment, which corresponds to the third dataset of the Sample Input below. Folding the 10 × 8 rectangular piece of paper shown at top left three times results in the 6 × 6 square shape shown at bottom left. In the figure, dashed lines indicate the locations to fold the paper and round arrows indicate the directions of folding. Grid lines are shown as solid lines to tell the sizes of rectangular shapes and the exact locations of folding. Color densities represent the numbers of overlapping layers. Punching through holes at A and B in the folded paper makes nine holes in the paper, eight at A and another at B. Your mission in this problem is to write a computer program to count the number of holes in the paper, given the information on a rectangular piece of paper and folding and punching instructions. Input The input consists of at most 1000 datasets, each in the following format. n m t p d 1 c 1 ... d t c t x 1 y 1 ... x p y p n and m are the width and the height, respectively, of a rectangular piece of paper. They are positive integers at most 32. t and p are the numbers of folding and punching instructions, respectively. They are positive integers at most 20. The pair of d i and c i gives the i -th folding instruction as follows: d i is either 1 or 2. c i is a positive integer. If d i is 1, the left-hand side of the vertical folding line that passes at c i right to the left boundary is folded onto the right-hand side. If d i is 2, the lower side of the horizontal folding line that passes at c i above the lower boundary is folded onto the upper side. After performing the first i −1 folding instructions, if d i is 1, the width of the shape is greater than c i . Otherwise the height is greater than c i . ( x i + 1/2, y i + 1/2) gives the coordinates of the point where the i -th punching instruction is performed. The origin (0, 0) is at the bottom left of the finally obtained shape. x i and y i are both non-negative integers and they are less than the width and the height, respectively, of the shape. You can assume that no two punching instructions punch holes at the same location. The end of the input is indicated by a line containing four zeros. Output For each dataset, output p lines, the i -th of which contains the number of holes punched in the paper by the i -th punching instruction. Sample Input 2 1 1 1 1 1 0 0 1 3 2 1 2 1 2 1 0 0 10 8 3 2 2 2 1 3 1 1 0 1 3 4 3 3 3 2 1 2 2 1 1 1 0 1 0 0 0 0 0 0 Output for the Sample Input 2 3 8 1 3 6
[ { "submission_id": "aoj_1625_10854025", "code_snippet": "#include <iostream>\n#include <cstring>\n#define ll long long\n#define N 100000\n\nusing namespace std;\n\nint g[600][600], dx, dy;\n\nint main(void) {\n#ifdef _DEBUG\n\tfreopen(\"1.in\", \"r\", stdin);\n#else \n\tios::sync_with_stdio(0); cin.tie(0); ...
aoj_1628_cpp
Floating-Point Numbers In this problem, we consider floating-point number formats, data representation formats to approximate real numbers on computers. Scientific notation is a method to express a number, frequently used for numbers too large or too small to be written tersely in usual decimal form. In scientific notation, all numbers are written in the form m × 10 e . Here, m (called significand ) is a number greater than or equal to 1 and less than 10, and e (called exponent ) is an integer. For example, a number 13.5 is equal to 1.35 × 10 1 , so we can express it in scientific notation with significand 1.35 and exponent 1. As binary number representation is convenient on computers, let's consider binary scientific notation with base two, instead of ten. In binary scientific notation, all numbers are written in the form m × 2 e . Since the base is two, m is limited to be less than 2. For example, 13.5 is equal to 1.6875 × 2 3 , so we can express it in binary scientific notation with significand 1.6875 and exponent 3. The significand 1.6875 is equal to 1 + 1/2 + 1/8 + 1/16, which is 1.1011 2 in binary notation. Similarly, the exponent 3 can be expressed as 11 2 in binary notation. A floating-point number expresses a number in binary scientific notation in finite number of bits. Although the accuracy of the significand and the range of the exponent are limited by the number of bits, we can express numbers in a wide range with reasonably high accuracy. In this problem, we consider a 64-bit floating-point number format, simplified from one actually used widely, in which only those numbers greater than or equal to 1 can be expressed. Here, the first 12 bits are used for the exponent and the remaining 52 bits for the significand. Let's denote the 64 bits of a floating-point number by b 64 ... b 1 . With e an unsigned binary integer ( b 64 ... b 53 ) 2 , and with m a binary fraction represented by the remaining 52 bits plus one (1. b 52 ... b 1 ) 2 , the floating-point number represents the number m × 2 e . We show below the bit string of the representation of 13.5 in the format described above. In floating-point addition operations, the results have to be approximated by numbers representable in floating-point format. Here, we assume that the approximation is by truncation. When the sum of two floating-point numbers a and b is expressed in binary scientific notation as a + b = m × 2 e (1 ≤ m < 2, 0 ≤ e < 2 12 ), the result of addition operation on them will be a floating-point number with its first 12 bits representing e as an unsigned integer and the remaining 52 bits representing the first 52 bits of the binary fraction of m . A disadvantage of this approximation method is that the approximation error accumulates easily. To verify this, let's make an experiment of adding a floating-point number many times, as in the pseudocode shown below. Here, s and a are floating-point numbers, and the results of individual addition are approximated as describ ...(truncated)
[ { "submission_id": "aoj_1628_10670479", "code_snippet": "//#pragma GCC optimize(\"O3\")\n#include<bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define rep(i,n) for (ll i=0;i<(ll)n;i++)\n#define rrep(i,n) for (ll i=n-1;i>=(ll)0;i--)\n#define loop(i,m,n) for(ll i=m;i<=(ll)n;i++)\n#define rloop(...
aoj_1627_cpp
Playoff by all the teams The Minato Mirai Football Association hosts its annual championship as a single round-robin tournament, in which each team plays a single match against all the others. Unlike many other round-robin tournaments of football, matches never result in a draw in this tournament. When the regular time match is a tie, overtime is played, and, when it is a tie again, a penalty shootout is played to decide the winner. If two or more teams won the most number of matches in the round-robin, a playoff is conducted among them to decide the champion. However, if the number of teams is an odd number, it is possible that all the teams may have the same number of wins and losses, in which case all the teams participate in the playoff, called a "full playoff" here. Now, some of the tournament matches have already been played and we know their results. Whether or not a full playoff will be required may depend on the results of the remaining matches. Write a program that computes the number of win/loss combination patterns of the remaining matches that lead to a full playoff. The first datatset of the Sample Input represents the results of the first three matches in a round-robin tournament of five teams, shown in the following table. In the table, gray cells indicate the matches not played yet. Team 1 Team 2 Team 3 Team 4 Team 5 Team 1 lost lost Team 2 lost Team 3 won Team 4 won Team 5 won In this case, all the teams win the same number of matches with only two win/loss combination patterns of the remaining matches, which lead to a full playoff, as shown below. In the two tables, the differences are indicated in light yellow. Team 1 Team 2 Team 3 Team 4 Team 5 Team 1 won won lost lost Team 2 lost lost won won Team 3 lost won won lost Team 4 won lost lost won Team 5 won lost won lost Team 1 Team 2 Team 3 Team 4 Team 5 Team 1 won won lost lost Team 2 lost lost won won Team 3 lost won lost won Team 4 won lost won lost Team 5 won lost lost won Input The input consists of multiple datasets, each in the following format. n m x 1 y 1 ... x m y m n is an odd integer, 3, 5, 7, or 9, indicating the number of teams participating in the tournament. m is a positive integer less than n ( n −1)/2, which is the number of matches already finished. x i and y i give the result of the i -th match that has already taken place, indicating that team x i defeated team y i . Each of x i and y i is an integer 1 through n which indicates the team number. No team plays against itself, that is, for any i , x i ≠ y i . The match result of the same team pair appears at most once. That is, if i ≠ j , then ( x i , y i ) ≠ ( x j , y j ) and ( x i , y i ) ≠ ( y j , x j ) hold. The end of the input is indicated by a line containing a zero. The number of datasets does not exceed 100. Output For each dataset, output a single line containing one integer which indicates the number of possible future win/loss patterns that a full playoff will be required. Sample Input 5 ...(truncated)
[ { "submission_id": "aoj_1627_10848400", "code_snippet": "#include<iostream>\n#include<cstring>\n#include<cstdio>\nusing namespace std;\nint n,m;\nint sum;\nint x,y;\nint a[100][100];\nint cnt[100];\nvoid show()\n{\n\tfor(int i=1;i<=n;i++)\n\t{\n\t\tfor(int j=1;j<=n;j++)\n\t\tif(a[i][j]==-1) cout<<\"* \";els...
aoj_1626_cpp
Skyscraper "MinatoHarukas" Mr. Port plans to start a new business renting one or more floors of the new skyscraper with one giga floors, MinatoHarukas. He wants to rent as many vertically adjacent floors as possible, because he wants to show advertisement on as many vertically adjacent windows as possible. The rent for one floor is proportional to the floor number, that is, the rent per month for the n -th floor is n times that of the first floor. Here, the ground floor is called the first floor in the American style, and basement floors are out of consideration for the renting. In order to help Mr. Port, you should write a program that computes the vertically adjacent floors satisfying his requirement and whose total rental cost per month is exactly equal to his budget. For example, when his budget is 15 units, with one unit being the rent of the first floor, there are four possible rent plans, 1+2+3+4+5, 4+5+6, 7+8, and 15. For all of them, the sums are equal to 15. Of course in this example the rent of maximal number of the floors is that of 1+2+3+4+5, that is, the rent from the first floor to the fifth floor. Input The input consists of multiple datasets, each in the following format. b A dataset consists of one line, the budget of Mr. Port b as multiples of the rent of the first floor. b is a positive integer satisfying 1 < b < 10 9 . The end of the input is indicated by a line containing a zero. The number of datasets does not exceed 1000. Output For each dataset, output a single line containing two positive integers representing the plan with the maximal number of vertically adjacent floors with its rent price exactly equal to the budget of Mr. Port. The first should be the lowest floor number and the second should be the number of floors. Sample Input 15 16 2 3 9699690 223092870 847288609 900660121 987698769 999999999 0 Output for the Sample Input 1 5 16 1 2 1 1 2 16 4389 129 20995 4112949 206 15006 30011 46887 17718 163837 5994
[ { "submission_id": "aoj_1626_10867577", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std ;\nint ans , p , n ;\nvoid deal( int x ){\n if ( ( n / x ) % 2 == 1 ){\n int y = x - ( n / x ) / 2 ;\n if ( y > 0 ){\n if ( n / x > ans ){\n ans = n / x ;\n ...