group_id
stringlengths
12
18
problem_description
stringlengths
85
3.02k
candidates
listlengths
3
20
aoj_2697_cpp
Problem K Runner and Sniper You are escaping from an enemy for some reason. The enemy is a sniper equipped with a high-tech laser gun, and you will be immediately defeated if you get shot. You are a very good runner, but just wondering how fast you have to run in order not to be shot by the sniper. The situation is as follows: You and the sniper are on the $xy$-plane whose $x$-axis and $y$-axis are directed to the right and the top, respectively. You can assume that the plane is infinitely large, and that there is no obstacle that blocks the laser or your movement. The sniper and the laser gun are at $(0, 0)$ and cannot move from the initial location. The sniper can continuously rotate the laser gun by at most $\omega$ degrees per unit time, either clockwise or counterclockwise, and can change the direction of rotation at any time. The laser gun is initially directed $\theta$ degrees counterclockwise from the positive direction of the $x$-axis. You are initially at ($x$, $y$) on the plane and can move in any direction at speed not more than $v$ (you can arbitrarily determine the value of $v$ since you are a very good runner). You will be shot by the sniper exactly when the laser gun is directed toward your position, that is, you can ignore the time that the laser reaches you from the laser gun. Assume that your body is a point and the laser is a half-line whose end point is (0, 0). Find the maximum speed $v$ at which you are shot by the sniper in finite time when you and the sniper behave optimally. Input The input consists of a single test case. The input contains four integers in a line, $x$, $y$, $\theta$ and $\omega$. The two integers $x$ and $y$ $(0 \leq |x|, |y| \leq 1,000$, ($x$, $y$) $\ne$ (0, 0)) represent your initial position on the $xy$-plane. The integer $\theta$ $(0 \leq \theta < 360)$ represents the initial direction of the laser gun: it is the counterclockwise angle in degrees from the positive direction of the $x$-axis. The integer $\omega$ $(1 \leq \omega \leq 100)$ is the angle which the laser gun can rotate in unit time. You can assume that you are not shot by the sniper at the initial position. Output Display a line containing the maximum speed $v$ at which you are shot by the sniper in finite time. The absolute error or the relative error should be less than $10^{-6}$. Sample Input 1 100 100 0 1 Output for the Sample Input 1 1.16699564
[ { "submission_id": "aoj_2697_2139713", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\ndouble PI=acos(-1);\ndouble eps=1e-7;\ntypedef complex<double> P;\n\ndouble modf(double x){\n while(x>2*PI){\n x-=2*PI;\n }\n while(x<0){\n x+=2*PI;\n }\n return x;\n}\n\ndouble x,y,a,b;\n\ndoub...
aoj_2696_cpp
Problem J New Game AI Aoba is a beginner programmer who works for a game company. She was appointed to develop a battle strategy for the enemy AI (Artificial Intelligence) in a new game. In this game, each character has two parameters, hit point ($hp$) and defence point ($dp$). No two characters have the same $hp$ and $dp$ at the same time. The player forms a party by selecting one or more characters to battle with the enemy. Aoba decided to develop a strategy in which the AI attacks the weakest character in the party: that is, the AI attacks the character with the minimum hit point in the party (or, if there are several such characters, the character with the minimum defense point among them). She wrote a function selectTarget ($v$) that takes an array of characters representing a party and returns a character that her AI will attack. However, the project manager Ms. Yagami was not satisfied with the behavior of her AI. Ms. Yagami said this AI was not interesting. Aoba struggled a lot, and eventually she found that it is interesting if she substitutes one of the constant zeros in her program with a constant $C$. The rewritten program is as follows. Note that Character is a type representing a character and has fields $hp$ and $dp$ which represent the hit point and the defense point of the character respectively. int C = <constant integer>; Character selectTarget(Character v[]) { int n = length(v); int r = 0; for (int i = 1; i < n; i++) { if (abs(v[r].hp - v[i].hp) > C) { if (v[r].hp > v[i].hp) r = i; } else { if (v[r].dp > v[i].dp) r = i; } } return v[r]; } By the way, this function may return different characters according to the order of the characters in $v$, even if $v$ contains the same set of characters. Ms. Yagami wants to know how many characters in a party may become the target of the new AI. Aoba's next task is to write a program that takes a given party $v$ and a constant $C$, and then counts the number of characters that may become the return value of selectTarget ($v$) if $v$ is re-ordered. Input The input consists of a single test case. The first line contains two integers $N$ $(1 \leq N \leq 50,000)$ and $C$ $(0 \leq C \leq 10^9)$. The first integer $N$ represents the size of $v$. The second integer $C$ represents the constant $C$ in Aoba's program. The i -th line of the following $N$ lines contains two integers $hp_i$ and $dp_i$ $(0 \leq hp_i, dp_i \leq 10^9)$. $hp_i$ represents the hit point of the $i$-th character in $v$, and $dp_i$ represents the defense point of the $i$-th character in $v$. You can assume that $hp_i \ne hp_j$ or $dpi \ne dp_j$ if $i \ne j$ for any $1 \leq i, j \leq N$. Output Display the number of characters that may become the return value of selectTarget ($v$), if $v$ is shuffled in an arbitrary order. Sample Input 1 5 3 1 5 3 4 5 3 7 2 9 1 Output for the Sample Input 1 5 Sample Input 2 3 2 1 2 3 1 5 1 Output for the Sample Input 2 2 Sample Input 3 4 1 2 0 0 4 1 1 5 9 Outp ...(truncated)
[ { "submission_id": "aoj_2696_10319485", "code_snippet": "// AOJ #2098 Two-finger Programming\n// 2025.3.23\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\nconst int NMAX = 50005;\n\nint n, c;\nint usd[NMAX] = {0};\nint flag[NMAX] = {0};\nint nw = 1, tp = 1;\nint stk[NMAX];\nint c...
aoj_2693_cpp
Problem G JAG-channel II JAG (Japan Alumni Group) is a group of $N$ members that devotes themselves to activation of the competitive programming world. The JAG staff members talk every day on the BBS called JAG-channel. There are several threads in JAG-channel and these are kept sorted by the time of their latest posts in descending order. One night, each of the $N$ members, identified by the first $N$ uppercase letters respectively, created a thread in JAG-channel. The next morning, each of the $N$ members posted in exactly $K$ different threads which had been created last night. Since they think speed is important, they viewed the threads from top to bottom and posted in the thread immediately whenever they came across an interesting thread. Each member viewed the threads in a different period of time, that is, there was no post of other members while he/she was submitting his/her $K$ posts. Your task is to estimate the order of the members with respect to the periods of time when members posted in the threads. Though you do not know the order of the threads created, you know the order of the posts of each member. Since the threads are always kept sorted, there may be invalid orders of the members such that some members cannot post in the top-to-bottom order of the threads due to the previous posts of other members. Find out the lexicographically smallest valid order of the members. Input The input consists of a single test case. The first line contains two integers separated by a space: $N$ $(4 \leq N \leq 16)$ and $K$ $(N - 3 \leq K \leq N - 1)$. Then $N$ lines of strings follow. Each of the $N$ lines consists of exactly $K$ distinct characters. The $j$-th character of the $i$-th line denotes the $j$-th thread in which the member denoted by the $i$-th uppercase letter posted. Each thread is represented by its creator (e.g. ' B ' represents the thread created by member B, the second member). It is guaranteed that at least one valid order exists. Output Display a string that consists of exactly $N$ characters in a line, which denotes the valid order in which the members posted in the threads. The $i$-th character of the output should represent the member who posted in the $i$-th period. In case there are multiple valid orders, output the lexicographically smallest one. Sample Input 1 7 4 DEFG FEDA EFGB BGEA AGFD DABC CADE Output for the Sample Input 1 ABCDEFG Sample Input 2 4 3 CDB DAC BAD ABC Output for the Sample Input 2 DCBA Sample Input 3 16 13 NDHPFJIBLMCGK CMDJKPOLGIHNE MOLBIEJFPHADN KPNAOHBLMCGEI FCMLBHDOANJPK NHIGLOAPKJDMC KMLBIPHDEOANJ IEGCMLBOAPKJD JNAOEDHBLMCGF OEDHPFIBLMGKC GMLBIFPHDNAEO ENHGOPKJDMCAF JKPAOBLGEIHNF HPKFGJEIBLCOM LBINEJDAGFKPH FGMOCADJENIBL Output for the Sample Input 3 PONCAKJGIEDHMFBL
[ { "submission_id": "aoj_2693_10866702", "code_snippet": "//ΔAIZU2693\n#include<iostream>\n#include<cstdio>\n#include<fstream>\n#include<algorithm>\n#include<vector>\n#include<map>\n#include<set>\n#include<queue>\n#include<bitset>\n#include<cmath>\n#include<cstring>\n#include<cstdlib>\nusing namespace std;\n...
aoj_2698_cpp
Problem L Wall Making Game The game Wall Making Game , a two-player board game, is all the rage. This game is played on an $H \times W$ board. Each cell of the board is one of empty , marked , or wall . At the beginning of the game, there is no wall on the board. In this game, two players alternately move as follows: A player chooses one of the empty cells (not marked and not wall). If the player can't choose a cell, he loses. Towards each of the four directions (upper, lower, left, and right) from the chosen cell, the player changes cells (including the chosen cell) to walls until the player first reaches a wall or the outside of the board. Note that marked cells cannot be chosen in step 1, but they can be changed to walls in step 2. Fig.1 shows an example of a move in which a player chooses the cell at the third row and the fourth column. Fig.1: An example of a move in Wall Making Game. Your task is to write a program that determines which player wins the game if the two players play optimally from a given initial board. Input The first line of the input consists of two integers $H$ and $W$ $(1 \leq H, W \leq 20)$, where $H$ and $W$ are the height and the width of the board respectively. The following $H$ lines represent the initial board. Each of the $H$ lines consists of $W$ characters. The $j$-th character of the $i$-th line is ' . ' if the cell at the $j$-th column of the $i$-th row is empty, or ' X ' if the cell is marked. Output Print " First " (without the quotes) in a line if the first player wins the given game. Otherwise, print " Second " (also without the quotes) in a line. Sample Input 1 2 2 .. .. Output for the Sample Input 1 Second Sample Input 2 2 2 X. .. Output for the Sample Input 2 First Sample Input 3 4 5 X.... ...X. ..... ..... Output for the Sample Input 3 First
[ { "submission_id": "aoj_2698_10850936", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define nn 22\n\nint n,m;\nchar _map[nn][nn];\nint dp[nn][nn][nn][nn];\n\n\nint dfs(int x1,int y1,int x2,int y2){\n\tif(x1>x2 or y1>y2) return 0;\n\tif(dp[x1][y1][x2][y2]>-1) return dp[x1][y1][x2][y2];\n\...
aoj_2700_cpp
Airport Codes 空港コード JAG王国では国内の空港にそれぞれ空港コードを割り当てて識別をしている. 空港コードは,小文字の英語アルファベットで表記した空港の名前をもとに以下の規則で割り当てられる: 名前の最初の文字と,母音 (a,i,u,e,o) の直後の文字を順に取り出す. 取り出した文字列が k 文字未満ならそれを空港コードとし, k 文字以上なら,その取り出した文字列の先頭 k 文字を空港コードとして使う. 例えば k = 3 のとき,haneda には hnd , oookayama には ooo , tsu には t というコードが割り当てられる. しかしこのコードの割り当て方では,違う名前の空港でも同じコードが割り当てられることがあり,混乱を招いてしまう. 空港の名前の一覧が与えられるので,すべての空港のコードが異なるようにできるか判定して,可能な場合はすべての空港コードが異なるようにできる最小の k を求め,不可能な場合はその旨を伝えるプログラムを作成せよ. Input 入力は100個以下のデータセットからなる. それぞれのデータセットは次の形式で与えられる. n s 1 ... s n 1行目に空港の数 n (2 ≤ n ≤ 50) が整数で与えられ,続く n 行にはそれぞれ空港の名前 s i が文字列で与えられる. 空港の名前は' a 'から' z 'の小文字の英語アルファベットのみで構成され,いずれも文字数は1以上50以下である. また,与えられる空港の名前はすべて異なる.すなわち,1 ≤ i < j ≤ n のとき s i ≠ s j を満たす. 入力の終わりは1つのゼロだけからなる行で示される. Output それぞれのデータセットについて,すべての空港に相異なる空港コードを割り当てられるときは,そのような最小の k を1行に出力せよ. 不可能な場合は,-1を1行に出力せよ. Sample Input 3 haneda oookayama tsu 2 azusa azishirabe 2 snuke snake 4 haneda honda hanamaki hawaii 0 Output for Sample Input 1 4 -1 3
[ { "submission_id": "aoj_2700_10609632", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\ntemplate <typename A> void chmin(A &l, const A &r) {\n if(r < l)\n l = r;\n}\ntemplate <typename A> void chmax(A &l,...
aoj_2695_cpp
Problem I Midpoint One day, you found $L + M + N$ points on a 2D plane, which you named $A_1, ..., A_L, B_1, ...,B_M, C_1,...,C_N$. Note that two or more points of them can be at the same coordinate. These were named after the following properties: the points $A_1,...,A_L$ were located on a single straight line, the points $B_1,...,B_M$ were located on a single straight line, and the points $C_1,...,C_N$ were located on a single straight line. Now, you are interested in a triplet $(i, j, k)$ such that $C_k$ is the midpoint between $A_i$ and $B_j$. Your task is counting such triplets. Input The first line contains three space-separated positive integers $L$, $M$, and $N$ $(1 \leq L, M, N \leq 10^5)$. The next $L$ lines describe $A$. The $i$-th of them contains two space-separated integers representing the $x$-coordinate and the $y$-coordinate of $A_i$. The next $M$ lines describe $B$. The $j$-th of them contains two space-separated integers representing the $x$-coordinate and the $y$-coordinate of $B_j$. The next $N$ lines describe $C$. The $k$-th of them contains two space-separated integers representing the $x$-coordinate and the $y$-coordinate of $C_k$. It is guaranteed that the absolute values of all the coordinates do not exceed $10^5$. Output Print the number of the triplets which fulfill the constraint. Sample Input 1 2 2 3 0 0 2 0 0 0 0 2 0 0 1 1 1 1 Output for the Sample Input 1 3 Sample Input 2 4 4 4 3 5 0 4 6 6 9 7 8 2 11 3 2 0 5 1 4 3 7 4 10 5 1 2 Output for the Sample Input 2 8 Sample Input 3 4 4 4 0 0 3 2 6 4 9 6 7 14 9 10 10 8 13 2 4 2 5 4 6 6 8 10 Output for the Sample Input 3 3
[ { "submission_id": "aoj_2695_10850559", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define nn 65536*8\nstruct point{\n\tll x,y;\n\tpoint (ll xx=0,ll yy=0){\n\t\tx=xx,y=yy;\n\t}\n};\npoint operator-(point a,point b){\n\treturn point(a.x-b.x,a.y-b.y);\n}\npoint opera...
aoj_2701_cpp
Falling Block Puzzle ブロック落とし あなたはある落ち物パズルで遊んでいる. このパズルのフィールドは,下図のように各段に立方体のセルが2マス×2マスに並び,段が上に無限に並んでいる形をしている. それぞれのセルは,セルにぴったり収まるブロックが1つ存在するか,何もないかのどちらかである. このパズルは以下のように進行する. 初期状態としていくつかのブロックが設置されている. 2マス×2マス×2マスに収まるブロックの塊を上から落とす.ただし,落とす前に,ブロックがフィールドからはみ出さないようにして塊を水平方向に平行移動することができる. 落としたブロックのうち,ある1つの下面が,すでに置かれているブロックまたはフィールドの底に着いた時点で,すべてのブロックの落下が止まり停止する. それぞれの段について,4マス全てが埋まっていればその段のブロックは消滅し,その上にあるブロックが1段ずつ落下する.落下後のそれぞれのブロックの下のセルにブロックがなかったとしても,それ以上落下することはない. 2に戻る. 初期状態で置かれているブロックと,落とす塊がいくつか与えられるので,与えられる順に全ての塊を落とすことで,最大いくつの段を消すことができるかを求めるプログラムを作れ. Input 入力は100個以下のデータセットからなる.各データセットは以下の形をしている. (初期状態のブロックの高さ H ) (落とす塊の数 N ) (初期状態の1段目) ... (初期状態の H 段目) (1個目の落とす塊) ... ( N 個目の落とす塊) 各データセットの1行目には初期状態のブロックの高さ H (1 ≤ H ≤ 10)と落とす塊の数 N (1 ≤ N ≤ 3)が指定されている. 続いて初期状態のそれぞれ段の情報が以下の形式で与えられる. c 11 c 12 c 21 c 22 c ij はそれぞれのセルの情報を表し,' # 'はブロックが存在することを,' . 'はブロックがないことを表す.1段目から H 段目までのそれぞれの段について,全てのセルにブロックが存在したり,全てのセルにブロックがなかったりすることはないと仮定して良い. 続いてそれぞれの落とす塊の情報が以下の形式で与えられる. b 111 b 112 b 121 b 122 b 211 b 212 b 221 b 222 初期状態の形式と同様に' # 'はブロックが存在することを,' . 'はブロックがないことを表す.それぞれの塊には少なくとも1つのブロックが含まれる.塊に含まれるブロックは角や辺のみで接していることがあり,面で繋がっているとは限らない. 初期状態・ブロックの塊の入力の添え字の対応は以下の図を参照すること. 入力の終わりは,2つのゼロからなる1行で示される. なお,下図は,後に示す Sample Input 中の最初のデータセットを表している.このデータセットでは,ブロックの塊を斜めに平行移動した後に落下させることで1つの段を消すことができる. Output 各データセットに対して,最大でいくつの段を消すことができるかを1行に出力せよ. Sample Input 1 1 ## #. .. .. #. .. 1 1 .# #. #. .. .. .# 2 2 ## #. ## #. .. .# ## #. #. .. #. .. 1 3 #. .. ## ## ## ## ## ## ## ## ## ## ## ## 10 3 ## #. ## #. ## .# ## .# #. ## #. ## .# ## .# ## .# #. .# #. #. .# #. .# #. .. #. .. #. .. #. .. 10 3 ## .# ## .. ## #. .# .. ## .# .# ## .# ## #. ## ## .# .. .# #. #. .# #. #. #. #. .. .. .. .. .# 0 0 Output for Sample Input 1 0 3 6 6 0
[ { "submission_id": "aoj_2701_9359705", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing vll = vector<ll>;\nusing vvll = vector<vll>;\nusing vvvll = vector<vvll>;\n#define rep(i,n) for(ll i=0;i<ll(n);i++)\n#define all(A) A.begin(),A.end()\n\nvoid chmax(ll& p, ll q)...
aoj_2702_cpp
Alternate Escape アリスハウス AliceとBobはボードゲームで遊んでいる. このボードゲームは, H 行 W 列のマス目が書かれた盤面と1つのコマを使って遊ぶ. このゲームでは,盤面の左上のマスを1行1列目として,下方向に行を,右方向に列を数える. マス同士が隣接する辺と,マスが盤面の外側と接する辺には壁を置けるようになっていて,ゲームの開始時にはそれぞれの辺について壁の有無が指定されている.また,ゲームの開始時には,コマが盤面のマスのいずれか1箇所に置かれている. AliceとBobは交互に手番をこなすことでゲームを進める. ゲームはAliceの手番から始まる. Aliceの目的は,コマを盤面の外まで動かして,迷路から脱出させることである. Aliceが1手でできる行動は,コマを今ある位置のマスから,上下左右に隣接するマスのうち,間の辺に壁がない方向のいずれかに移動させることである. コマの今あるマスが盤面の外側に接していて,間の辺に壁がない場合,そこからコマを脱出させることができる. 一方,Bobの目的は,コマの脱出を妨害することである. Bobの手番では,壁の有無を反転させるか,何もせずに手番を終えるかを選ぶことができる. 壁の有無を反転させることを選んだ場合,盤面のすべてのマスの辺について,壁の有無が反転する. 盤面の初期状態と,コマの初期位置が与えられるので,AliceとBobの両者が最適な行動をとったときに,Aliceがコマを盤面から脱出させられるか判定せよ. ただし,Aliceの手番においてコマが4方向とも壁で囲まれてしまった場合は脱出できないとみなす. Input 入力は40個以下のデータセットからなる. それぞれのデータセットは次の形式で与えられる. H W R C Horz 1,1 Horz 1,2 ... Horz 1, W Vert 1,1 Vert 1,2 ... Vert 1, W +1 ... Vert H ,1 Vert H ,2 ... Vert H , W +1 Horz H +1,1 Horz H +1,2 ... Horz H +1, W 1行目には4つの整数 H , W (1 ≤ H , W ≤ 500), R , C (1 ≤ R ≤ H , 1 ≤ C ≤ W ) が与えられる. これらは盤面が H 行 W 列のマス目からなり,コマの初期位置が R 行 C 列目であることを表す. 続く 2 H + 1 行には盤面の初期状態が与えられる. 2 i 行目 (1 ≤ i ≤ H + 1) は, W 個の整数 Horz i ,1 , Horz i ,2 , ..., Horz i , W を含む. Horz i , j は, i 行 j 列目のマスの上側の辺に壁が有るとき1で,無いとき0である. ただし, Horz H +1, j は, H 行 j 列目のマスの下側の辺における壁の有無を表す. 2 i + 1 行目 (1 ≤ i ≤ H ) は, W + 1 個の整数 Vert i ,1 , Vert i ,2 , ..., Vert i , W +1 を含む. Vert i , j は, i 行 j 列目のマスの左側の辺に壁が有るとき1で,無いとき0である. ただし, Vert i , W +1 は, i 行 W 列目のマスの右側の辺における壁の有無を表す. 入力の終わりは,4つのゼロからなる1行で示される. Output それぞれのデータセットについて,Aliceがコマを盤面から脱出させられる場合は" Yes ",できない場合は" No "と1行に出力せよ. Sample Input 3 3 2 2 1 1 1 0 0 0 0 1 1 1 0 0 0 0 1 1 1 0 0 0 0 1 1 1 3 3 2 2 1 0 1 1 0 1 1 1 0 0 0 0 0 0 0 0 1 1 1 0 1 1 0 1 1 3 1 1 1 1 1 1 0 0 1 1 0 1 2 2 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 Output for Sample Input Yes No Yes No Hint 1つ目のデータセットではAliceは次のように動くことでコマを脱出させられる。 初期状態 Aliceがコマを左に動かす Bobは脱出を阻止するために壁を反転させる Aliceがコマを上に動かす Bobが次の手番で壁の有無を反転させてもさせなくても、 Aliceは次の手番でコマを脱出させられる。
[ { "submission_id": "aoj_2702_10946050", "code_snippet": "#include <cstdio>\n#include <cstdlib>\n#include <vector>\n#include <iostream>\n\nusing namespace std;\n\nint n, m, row, col;\n\nconst int N = 600;\n\nint ver[N][N];\nint hor[N][N];\nint visit[N][N][2][2];\nint deg[N][N][2][2];\n\nstruct Node {\n\tint ...
aoj_2705_cpp
Kuru Kuru Door くるくるドア ACM (Automatic Cleaning Machine) 社が2011年に開発した全自動円形掃除機 ICPC (Intelligent Circular Perfect Cleaner) は, 日中に自動で動き出し,自分が通り過ぎた場所のゴミを掃除する機能を備えている. 加えて,この ICPC は,バッテリー残量が低下した場合に自動的に電源の位置まで戻る機能も備えている. この度,JAG (Japan Alumni Group) と名乗る謎の組織から, ACM 社に対し大量の ICPC の発注があった. しかし,その発注には条件が付いていた. それは,彼らが本拠地としているビルの内部にある「くるくるドア」に ICPC を対応させることである. くるくるドアは,以下に述べるように平面上の物体としてモデル化されている. くるくるドアは,原点で接続された長さ R の 2 n (≥ 4) 枚のドアの組として表される. これらのドアは,角度 π/ n おきに並んでいる. くるくるドアは手動であり,ドアのどこに触れても触れた方向( ICPC との重なりが解消される方向)へ原点中心に一斉に回転させることができるが, 平行移動させることはできない. 初期状態では,ドアのちょうど2枚が y 軸に平行である. また,原点を中心とする半径 R の円のうち, y 座標の絶対値が R sin(π/(2 n )) 以上である部分および, y 軸上で y 座標の絶対値が R 以上の部分は壁になっている. これらの壁は押すことができない. 平面上で ICPC は半径 r の円として表される. ICPC の電源は点 T = ( x t , y t ) にあり, ICPC はその中心座標が電源の位置に重なっている時バッテリーを充電することができる. はじめに ICPC の中心座標が点 S = ( x s , y s ) となる位置にあるとき, 電源の位置まで ICPC が最短経路を通って戻れるようにすることが, JAG から出された ICPC 発注の条件である. なお, ICPC がくるくるドアに対して正確な挙動を行うことを確かめるために, ICPC の初期位置と電源の位置は,くるくるドアを挟んで反対側となるように与えられる. ACM 社は,凄腕プログラマーのあなたに,くるくるドアの寸法, ICPC の中心座標 S と半径,電源の位置 T が与えられたとき,ICPCが電源の位置へ到達できるかを判定し,到達可能な場合は ICPC の中心座標が移動する最短経路の長さを求めるプログラムの作成を依頼した. Input 入力は,複数のデータセットから構成され,1つの入力に含まれるデータセットの数は60個以下である. 各データセットの形式は次の通りである. n r R x s y s x t y t n はくるくるドアを構成するドアの枚数の半分を表す整数であり,2 ≤ n ≤ 10 と仮定して良い. r , R はそれぞれ ICPC の半径,くるくるドアの一枚あたりの長さを表す整数であり,1以上10以下と仮定して良い. ( x s , y s ) は ICPC の初期位置の中心座標,( x t , y t ) は電源の位置を表す. x s , y s , x t , y t はそれぞれ整数であり,-1,000 ≤ x s ≤ - r - 1, -1,000 ≤ y s ≤ 1,000, r + 1 ≤ x t ≤ 1,000, -1,000 ≤ y t ≤ 1,000 を満たすと仮定して良い. 加えて,点 ( x s , y s ), ( x t , y t ) はそれぞれ原点から距離 R + r + 10 -6 以上離れていると仮定して良い. すなわち, ICPC と電源は初期状態でいずれもくるくるドアの外側にあり,互いにくるくるドアを挟んで反対側に位置している. また, r および R が 10 -6 だけ変化しても, ICPC の電源位置への到達可能性は変わらないと仮定して良い. 入力の終わりは1つのゼロだけからなる行で示される. 下図は後に示す Sample Input 中の最初のデータセットにおける, ICPC の初期位置 S ,電源位置 T ,くるくるドアおよび壁の配置を表している. Output 各データセットについて, ICPC が電源の位置に到達できる場合は,電源の位置までの移動距離の最小値を1行に出力せよ. この場合,出力の絶対誤差は 10 -6 以内でなくてはならない. 到達できない場合は,-1 を1行に出力せよ. Sample Input 3 1 4 -5 5 5 -5 10 1 10 -10 5 5 -10 5 3 5 -20 0 20 0 8 1 9 -14 58 6 24 2 2 8 -57 -113 42 -31 4 1 4 -4 -5 4 5 0 Output for Sample Input 17.5032106371 41.3850388846 -1 102.0656847372 178.1399151364 19.5548716821
[ { "submission_id": "aoj_2705_6022652", "code_snippet": "/*\tauthor: Kite_kuma\n\tcreated: 2021.11.01 22:38:19 */\n\n// #ifdef LOCAL\n// #define _GLIBCXX_DEBUG\n// #endif\n#include <bits/stdc++.h>\nusing namespace std;\n\n#pragma region macros\n\n#define foa(s, v) for(auto &s : v)\n#define all(v) (v).begin(...
aoj_2703_cpp
Dice Stamp サイコロスタンプ あなたは地元の縁日で,今までに見たことがないゲームの出店を発見した. N 個の6面サイコロを,ボード上に落として転がすゲームだ. より正確には, N 個のボタンが N 個のサイコロに1対1に紐付いており,ボタンを押すことで対応したサイコロがボードに落ちる. ボタンを好きなように N 回押し,サイコロを N 回落として転がすことで得点を得るゲームである. ゲームのより詳細なルールを説明しよう. ゲームで使用する N 個のサイコロはすべて各辺の長さが1の立方体であり,ボードは長さが1の正方形のマスに区切られた十分に広い平面である. ゲーム開始前,ボードの各マスにはすべて0が書かれている. 各サイコロの各面には整数が書かれている.これは1から6までとは限らないし,サイコロごとに違う数が書かれていることもある. ゲームで用いる筐体には N 個のボタンが付いており, N 個のサイコロと1対1に紐付いている. いずれかのボタンを押すと,対応したサイコロが機械から排出されてボードに落ち,何度か回転する. 回転の途中,サイコロの下面は必ずボードのいずれかのマスにぴったりと重なる. 下面がマスに触れる度,そのマスに書かれていた数が,サイコロの下面に書かれた数で上書きされる. これは落下により初めてボードに触れたときも含む. 回転が止まった後,サイコロはボードから取り除かれ,元の排出装置へと戻される. ボタンを N 回押した後,ボードに書かれた数の和が最終得点となる. 同じボタンを複数回押すことはできるが,1つ前に排出したサイコロの回転が終わり,排出装置に戻るまで次のボタンを押すことはできない. さて,出店のおっちゃんはサイコロの排出の仕方はランダムだと主張しているが,注意深いあなたは,他の客が遊ぶ様子を観察することで,同じボタンを押した時の挙動がそれまでのボタンの押し方に依らず完全に同一であることに気付いた. より具体的には, i 番目のボタンを押したときの挙動は以下のように決定的である. i 番目のサイコロが排出される. このサイコロは内部で決められたマスに決められた向きで落下する.この向きは必ず,マスの正方形と下面の正方形とがぴったりと重なる向きである. サイコロは前後左右4方向いずれかに回転することを繰り返す.回転回数やそれぞれの回転の方向も,内部で決められている. 決められた回転が終了すると,サイコロはボードから取り除かれ,排出装置に戻される. ここで,便宜上3次元空間を考え,マスの辺に平行な向きにそれぞれ x 軸と y 軸をとり,サイコロ上面が向く方向を z 軸正方向とする. この時,サイコロの回転は x , y 軸の正,負方向の4通りであり,それぞれ下図のようになる.ただし,図中の記号は後述の入力形式に対応している. 決定的に動くとはなんて詐欺だ,と憤りを感じたものの,あなたは N 回のボタンの押し方によって最終得点を変えられることに気が付いた. あなたは入念な観察により各サイコロの各面に書かれた数や落とされる初期位置・向き・その後の回転の仕方に至るまで完全な情報を揃えた. 集めた情報に基づいて,最善のボタンの押し方で得られるこのゲームの最高得点を求めよ. Input 入力は40個以下のデータセットからなる.各データセットは以下の形式で表される. N 1番目のサイコロの情報 ... N 番目のサイコロの情報 入力の最初の行は,サイコロの個数を表す1つの整数 N からなる.1 ≤ N ≤ 15 と仮定してよい. 以降, N 個のサイコロの情報が続く. それぞれのサイコロの情報は,以下の形式で表される. x y l r f b d u rot 1行目は2つの整数 x , y からなり,排出されたときにサイコロが落とされるマスの中心の座標 ( x , y ) を表す.-1,000 ≤ x , y ≤ 1,000 と仮定してよい. 2行目は6つの整数 l , r , f , b , d , u からなり,各面に書かれた数を表す. l , r , f , b , d , u はそれぞれ,落とされたときに x 軸負方向, x 軸正方向, y 軸負方向, y 軸正方向, z 軸負方向, z 軸正方向を向いている面に書かれた数である.1 ≤ l , r , f , b , d , u ≤ 100 と仮定してよい. 3行目は回転の仕方を表す文字列 rot からなる. rot は ' L ', ' R ', ' F ', ' B ' のみからなる文字列であり,1文字以上,30文字以下である. rot の j 番目の文字は j 回目の回転の方向を表しており,文字が ' L ', ' R ', ' F ', ' B ' のときそれぞれ, x 軸負方向, x 軸正方向, y 軸負方向, y 軸正方向に回転することを示す. 入力の終わりは,1つのゼロを含む1行で示される. Output 各データセットについて, N 回のボタンの押し方を工夫することで得られる最高得点を1行で出力せよ. 各出力行はこの数値以外の文字を含んではならない. Sample Input 1 0 0 1 2 3 4 5 6 RRRRBBBBLLLLFFFF 2 0 0 1 1 1 1 1 1 RRR 2 2 100 100 100 100 100 100 FFF 1 1000 -1000 1 2 3 4 5 6 LFRB 4 -3 -4 1 2 3 4 5 6 BBBBBBBB 4 -3 11 12 13 14 15 16 LLLLLLLL 3 4 21 22 23 24 25 26 FFFFFFFF -4 3 31 32 33 34 35 36 RRRRRRRR 3 -2 -2 9 3 1 1 1 1 RRRRBLLLBRRBLB 0 -3 2 5 2 5 2 1 BBLBBRBB 3 0 10 7 2 10 1 5 LLFLLBLL 0 Output for Sample Input 64 403 10 647 96
[ { "submission_id": "aoj_2703_10853814", "code_snippet": "#include<iostream>\n#include<cstdio>\n#include<vector>\n#include<cstring>\n#include<algorithm>\nusing namespace std;\nint dx[4] = {1, 0, 0, -1};\nint dy[4] = {0, 1, -1, 0};\n//l r f b d u\n//0 1 2 3 4 5\nint go[4][6] = {\n {5, 4, 2, 3, 0, 1},\n ...
aoj_2704_cpp
Stamp Rally スタンプラリー 日本アミューズメントグループ (Japan Amusement Group, JAG) では,島国を模したテーマパークでのイベントを企画している. このイベントでは,参加者は橋を渡るたびに橋ごとに決められたスタンプをスタンプ帳に順番に押していく. 用意されたスタンプは以下の7種類のどれかである. a ( ) [ ] + * スタートからゴールまで橋を渡り歩いて,押されたスタンプの列が正しい数式になればクリアである. ただし橋を渡る向きは決まっていて,逆向きに渡ることはできない. 同じ橋を何度も渡ってよく,最終的にゴール地点に到着するのであれば一度ゴール地点に着いた後に引き続きスタンプを集めてもよい. 正しい数式とは以下の BNF で定義される <expression> である. <expression> ::= <term> | <expression> "+" <term> <term> ::= <factor> | <term> "*" <factor> <factor> ::= "a" | "(" <expression> ")" | "[" <expression> "]" スタート・ゴールと橋ごとのスタンプを決めたので,関係者で試しにやってみたがなかなかクリアする人が現れない. もしかしたら,この設定ではクリアすることができないのかもしれない. スタート・ゴールと橋の情報が与えられるので,クリア可能かどうかを判定するプログラムを書きなさい. Input 入力は50個以下のデータセットからなる.各データセットは以下の形式で表される. n m s t a 1 b 1 c 1 ... a m b m c m データセットの最初の行は,空白文字1個で区切られた4個の整数 n , m , s , t からなる. n は島の数であり, 1 ≤ n ≤ 200 と仮定してよい. それぞれの島には 1 から n までの番号が付けられている. m は橋の数であり, 1 ≤ m ≤ 100,000 と仮定してよい. s はスタートの島の番号, t はゴールの島の番号である.スタートとゴールが同じ島であることもある. 続く m 行のそれぞれは,空白文字1個で区切られた2個の整数と1個の文字からなる. a i , b i は i 番目の橋によって島 a i から島 b i へ渡れることを表し, c i は i 番目の橋で押すスタンプを表す.ある2つの島の間に複数の橋がかかっていたり,1つの島の中で橋がかかっていたりすることもある. 入力の終わりは,4つのゼロからなる1行で示される. Output 各データセットに対して,クリアできるならば" Yes "を,できないならば" No "を1行に出力せよ. Sample Input 4 5 1 4 1 2 ( 1 3 a 2 4 a 3 4 ) 3 2 + 4 4 1 2 1 3 ( 3 4 a 4 1 + 3 2 a 3 4 1 1 1 2 a 2 2 + 2 3 a 3 1 a 5 8 1 5 1 1 [ 1 2 ( 2 1 * 2 2 a 2 3 a 3 3 ) 3 4 ] 4 5 ) 2 14 1 1 1 2 a 1 2 ( 1 2 ) 1 2 [ 1 2 ] 1 2 + 1 2 * 2 1 a 2 1 ( 2 1 ) 2 1 [ 2 1 ] 2 1 + 2 1 * 0 0 0 0 Output for Sample Input Yes No No Yes No
[ { "submission_id": "aoj_2704_10848353", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 205;\nint id(char c) {\n\tif (c == 'a') return 0;\n\tif (c == '+') return 1;\n\tif (c == '*') return 2;\n\tif (c == '(') return 3;\n\tif (c == ')') return 4;\n\tif (c == '[') return 5;\n\tr...
aoj_2707_cpp
監獄 無限人の囚人たちがいる。はじめ、囚人たちは 0, 1, 2, ... と番号が振られている。 次の操作を N 回行う。 0 番目の囚人を釈放し、 k, 2k, 3k, ... 番目の囚人たちを処刑する。 その後、残った囚人たちに番号を振り直す。このとき、元の番号が小さい囚人から順に 0, 1, 2, ... と番号を振る。 N 回目の操作で釈放される囚人がはじめに振られていた番号を求めよ。 Constraints 1 ≤ N ≤ 10^5 2 ≤ k ≤ 10^5 答えは 10^{18} 以下である。 Input Format 入力は以下の形式で標準入力から与えられる。 N k Output Format 答えを一行に出力せよ。 Sample Input 1 4 2 Sample Output 1 7 Sample Input 2 1 3 Sample Output 2 0 Sample Input 3 100000 100000 Sample Output 3 99999
[ { "submission_id": "aoj_2707_10865966", "code_snippet": "//#define __USE_MINGW_ANSI_STDIO 0\n#include <bits/stdc++.h>\n\nusing namespace std;\ntypedef long long ll;\ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<ll> VL;\ntypedef vector<VL> VVL;\ntypedef pair<int, int> PII;\n\n#define FOR(...
aoj_2706_cpp
幾何問題を解こう A君は今日も幾何の問題を解いている。 幾何の問題を解く時は浮動小数点誤差に気をつけることが大事である。 浮動小数点誤差とは、2進法の有限小数で数を表す際におこる丸めによって起きる誤差である。 例えば、10進法での 0.1 は2進法では 0.00011001100110011 ... という無限小数になるが、 これを有限の桁で丸める際に誤差が発生してしまう。 正の整数 p , q が10進法で与えられる。 有理数 p / q を有限桁数の小数で表現することができるような b 進法( b は2以上の整数)を求めよ。 複数ある場合は最も小さいものを出力せよ。 Constraints 0 < p < q < 10^9 Input Format 入力は以下の形式で標準入力から与えられる。 p q Output Format 答えを一行に出力せよ。 Sample Input 1 1 2 Sample Output 1 2 1/2 は 2 進法で 0.1 です Sample Input 2 21 30 Sample Output 2 10 21/30 は 10 進法で 0.7 です
[ { "submission_id": "aoj_2706_5768373", "code_snippet": "#include <bits/stdc++.h>\n\n#define lol long long\n#define gcd(x, y) __gcd(x, y)\n#define mt make_tuple\n#define mp make_pair\n#define fi first\n#define se second\n#define fixed(x) fixed << setprecision(x)\n#define all(x) x.begin(),x.end()\nusing names...
aoj_2708_cpp
ABC Gene 文字列 ABC で表される遺伝子配列がある。あなたは次の操作を何回か行い、この遺伝子配列を書き換えていくことができる。 文字 A , B , C のうち 1 つを選ぶ。これを x とおく。遺伝子配列に含まれるすべての x をそれぞれ ABC へ同時に置き換える。 A , B , C だけからなる文字列 S が与えられる。遺伝子配列を S に一致させられるか判定せよ。 Constraints 1 ≤ |S| ≤ 5,000 S は A , B , C だけからなる。 Input Format 入力は以下の形式で標準入力から与えられる。 S Output Format 遺伝子配列を S に一致させられるならば Yes を、一致させられないならば No を一行に出力せよ。 Sample Input 1 ABC Sample Output 1 Yes 遺伝子配列ははじめから ABC である。 Sample Input 2 AABCC Sample Output 2 Yes B を選んで操作を行うと ABC → AABCC となる。 Sample Input 3 AABCABC Sample Output 3 No 例えば、 C を選んで操作を行っても AABCC → AABCABC とはならない。すべての C をそれぞれ ABC へ同時に置き換えるので、実際は AABCC → AABABCABC となる。
[ { "submission_id": "aoj_2708_10848475", "code_snippet": "//#define __USE_MINGW_ANSI_STDIO 0\n#include <bits/stdc++.h>\n\nusing namespace std;\ntypedef long long ll;\ntypedef vector<int> VI;\ntypedef vector<VI> VVI;\ntypedef vector<ll> VL;\ntypedef vector<VL> VVL;\ntypedef pair<int, int> PII;\n\n#define FOR(...
aoj_2710_cpp
坑道数式 ある日廃坑を探検していたあなたは、坑道に長い数式 S が書かれているのを発見した。大きな数が好きなあなたは、チョークを取り出し、数式を計算した結果ができるだけ大きくなるように ( または ) を書き加えることにした。書き加えた後も数式になっていなければならないとすると、最大でいくつにできるか。 文字と文字の間は十分広く空いていて、 ( または ) であればいくつでも書き加えることができる。最終的に数式になっていれば、最初のかっこの対応が崩れるように ( または ) を書いてもよい(Sample 2参照)。 また、ここでは以下のBNFで定義される<expr>を数式と呼ぶ。数式中の数は全て一桁である。 <expr> ::= "(" <expr> ")" | <term> "+" <term> | <term> "-" <term> <term> ::= <digit> | <expr> <digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" Constraints 3 ≤ |S| ≤ 200 S は数式を表す。 Input Format 入力は以下の形式で標準入力から与えられる。 S Output Format 答えを整数で出力せよ。 Sample Input 1 1-(2+3-4+5) Sample Output 1 5 1-(2+3-(4+5))が最大となる。 Sample Input 2 1-(2+3+4) Sample Output 2 0 (1-(2+3)+4)が最大となる。 Sample Input 3 1-(2+3) Sample Output 3 -4 1-(2)+(3)はここでいう数式ではないことに注意。
[ { "submission_id": "aoj_2710_10849109", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing ll = long long;\n\nusing P = pair<int, int>;\n\nconstexpr int INF = 1e9;\n\nP memo[201][201];\n\nP rec(int l, int r, string const& s) {\n P& res = memo[l][r];\n if(res != make_pair(-INF, IN...
aoj_2709_cpp
真っ暗な部屋 目を覚ますと、A君は真っ暗な部屋の中にいた。 どうやらA君は N 個の部屋から構成されたダンジョンに迷い込んでしまったようだ。 あなたはA君がどの部屋に迷い込んだのかを知ることはできなかったが、幸いにもダンジョンのマップを手に入れることができた。 A君の進むべき道を示し明るい部屋に導こう。 N 個の部屋のうち M 個の部屋が真っ暗な部屋であり、それぞれ D_1 , D_2 , ..., D_M 番目の部屋が真っ暗な部屋であることが分かっている。 また、全ての部屋からちょうど K 本の一方通行の道が順に並んでおり、 i 番目の部屋から出る道はそれぞれ v_{i,1} , v_{i,2} , ..., v_{i,K} 番目の部屋に繋がっている。 あなたは、A君に対し今いる部屋から a_1 , a_2 , ..., a_l 番目の道を順に進ませることができる。 ただし、A君は明るい部屋に到達したらそれ以降の指示は無視する。 あなたは、指示の前後においてA君が今いる部屋の情報を知ることはできないため、A君がどの部屋にいたとしても明るい部屋に辿り着けるような指示列を伝えなければならない。 そのような指示のうち、最も短いものの長さを答えよ。 Constraints 2 ≤ N ≤ 100 1 ≤ M ≤ min(16, N − 1) 1 ≤ K ≤ N 1 ≤ D_i ≤ N D_i は全て異なる 1 ≤ v_{i, j} ≤ N 全ての暗い部屋は少なくとも1つの明るい部屋に到達可能である Input Format 入力は以下の形式で標準入力から与えられる。 N M K D_1 D_2 ... D_M v_{1,1} v_{1,2} ... v_{1,K} v_{2,1} v_{2,2} ... v_{2,K} ... v_{N,1} v_{N,2} ... v_{N,K} Output Format 答えを一行に出力せよ。 Sample Input 1 4 2 2 1 2 2 4 3 1 4 2 1 3 Sample Output 1 2 1, 1 という指示を出すと A君の初期位置が部屋1である場合、2つ目の移動で部屋3に到達する A君の初期位置が部屋2である場合、1つ目の移動で部屋3に到達する Sample Input 2 3 2 2 1 2 2 3 1 2 2 1 Sample Output 2 3 2, 1, 2 という指示を出すと A君の初期位置が部屋1である場合、1つ目の移動で部屋3に到達する A君の初期位置が部屋2である場合、3つ目の移動で部屋3に到達する Sample Input 3 6 3 3 1 2 3 4 1 1 2 5 2 3 3 6 4 4 4 5 5 5 6 6 6 Sample Output 3 3 非連結であるケースや、自己辺、多重辺があるケースに気をつけよう
[ { "submission_id": "aoj_2709_10889003", "code_snippet": "#include<bits/stdc++.h>\n// #include<atcoder/all>\n// #include<boost/multiprecision/cpp_int.hpp>\n\nusing namespace std;\n// using namespace atcoder;\n// using bint = boost::multiprecision::cpp_int;\nusing ll = long long;\nusing ull = unsigned long lo...
aoj_2719_cpp
Problem C: Kuru Kuru Sushi Zephir is an owner of the kuru kuru sushi restaurants. In these restaurants, sushis are placed on a rotating circular belt conveyor and delivered to customers. Zephir owns $n$ restaurants numbered from $0$ to $n - 1$. Due to his passion for rotation, these restaurants are placed on the circumference of a circle, and have huge belt conveyors under the ground between adjacent restaurants. One day, the ingredients of sushis were short in some restaurants. Therefore, he decided to transport some ingredient foods between restaurants by using these belt conveyors. The $i$-th ($0 \leq i \leq n - 2$) belt conveyor connects the restaurants $i$ and $i+1$. The ($n - 1$)-th belt conveyor connects the restaurants $n - 1$ and $0$. The length of the $i$-th belt conveyor is $w_i$. He wants to transport $q$ foods from some restaurants to other restaurants. The $i$-th food should be transported from restaurant $s_i$ to $t_i$. Zephir wants to minimize the total cost of transportation. The transportation cost of each food can be changed by the settings of the direction of the belt conveyors. Each belt conveyor can transport foods in only a single direction, which can be set by Zephir. Moreover, the settings cannot be changed while transporting all $q$ foods. The transportation cost of the $i$-th food is the sum of the length of the belt conveyors in the shortest path from restaurant $s_i$ to $t_i$. He should set the direction of belt conveyors to transport all foods. Write a program to calculate the minimum value of the total cost, which is the sum of the transportation costs of all the $q$ foods. Input Each input is formatted as follows: $n$ $q$ $w_0$ $w_1$ ... $w_{n-1}$ $s_0$ $t_0$ ... $s_{q-1}$ $t_{q-1}$ The first line contains two integers $n$ and $q$. $n$ ($3 \leq n \leq 10^5$) denotes the number of the restaurants, and $q$ ($1 \leq q \leq 10^5$) denotes the number of the foods to transport. The next line contains $n$ integers. $w_i$ ($1 \leq w_i \leq 10^5$) denotes the length of the $i$-th belt conveyor. Each of the following $q$ lines contains two integers $s_i$ ($0 \leq s_i \leq n - 1$) and $t_i$ ($0 \leq t_i \leq n - 1$). Each denotes the $i$-th food should be transported from restaurant $s_i$ to $t_i$. You may assume $s_i \ne t_i$ for any $0 \leq i \leq q - 1$, and $s_i \ne s_j$ or $t_i \ne t_j$ if $i \ne j$ for any $0 \leq i, j \leq q - 1$. Output Print the minimum total cost of the foods transportation. If it is impossible to transport all foods to each destination, print -1. Sample Input 4 4 1 1 1 1 0 1 0 3 2 3 3 0 Output for the Sample Input 6 Sample Input 5 3 4 5 6 7 8 0 1 0 3 4 2 Output for the Sample Input 32
[ { "submission_id": "aoj_2719_10323024", "code_snippet": "// AOJ #2719 Kuru Kuru Sushi\n// 2025.3.25\n\n#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() {\n\tint n = 0; int c = gc();\n\tdo n = 10*n + (c & ...
aoj_2712_cpp
Escape 頂点に正の値を持つ無向グラフが与えられる。 頂点には 1 から N の番号がついており、 i 番目の頂点は w_i の値を持っている。 1 番目の頂点からスタートし、直前に通った辺を通ることができないという制約のもとでグラフ上を移動することができる。 各頂点では,初めて訪れた時に限りその頂点が持つ値の点数を得られる。 取得できる点数の総和の最大値を求めよ。 Constraints 1 ≤ N ≤ 100000 N − 1 ≤ M ≤ 100000 1 ≤ w_i ≤ 1000 1 ≤ u_i, v_i ≤ N 多重辺・自己辺は存在しない グラフは連結である Input Format 入力は以下の形式で標準入力から与えられる。 N M w_1 w_2 ... w_N u_1 v_1 u_2 v_2 ... u_M v_M 1 行目には グラフ の頂点数 N と辺の数を表す整数 M が入力される。 2 行目には各頂点が持つ値 w_i が入力される。 さらに続けて M 行に、各辺により繋がれる 2 頂点の番号が入力される。 Output Format 答えを1行に出力せよ。 Sample Input 1 6 6 1 2 3 4 5 6 1 2 2 3 3 4 1 4 4 5 5 6 Sample Output 1 21 頂点 1→2→3→4→5→6 と進むことで全ての頂点の点数を集めることができます。 Sample Input 2 7 8 1 3 3 5 2 2 3 1 2 2 3 3 1 1 4 1 7 1 5 1 6 5 6 Sample Output 2 16 頂点 1→2→3→1→5→6→1→4 と進むことで16点を集めることができます。
[ { "submission_id": "aoj_2712_10858280", "code_snippet": "#include<iostream>\n#include<vector>\n#include<utility>\n\nusing namespace std;\n\ntypedef pair<int, int> Pii;\n\nvector<int> graph[100001];\nvector<int> dim(100001, 0);\nvector<Pii> nodes(100001);\nvector<bool> used(100001, false);\n\nint dfs(int nod...
aoj_2713_cpp
H - Bit Operation Game N 頂点の根付き木が与えられる。 頂点には 0 から N − 1 の番号がついており、 0 番目の頂点が根を表す。 根には T = 0 が、それ以外の頂点には T=T&X T=T&Y T=T|X T=T|Y T=T^X T=T^Y のいずれかの操作が書かれている。 ここでの演算子 &, |, ^ はそれぞれビット演算子 and, or, xor, を意味する。 A君とB君はこの木を使って以下のゲームを M 回行った。 二人は根からスタートし、子頂点を選び進むという操作を、A君から始め葉に到達するまで交互に行う。 通ったノードに書かれている操作を、通った順に適用した時の、最終的な T の値がスコアになる。 B君はできるだけスコアを小さくしたいと考えており、またA君は大きくしたいと考えている。 M回のゲームの X , Y の値が与えられるので、二人が最適な選択をした時の各ゲームのスコアを出力せよ。 Constraints 1 ≤ N ≤ 100000 1 ≤ M ≤ 100000 0 ≤ X, Y < 2^{16} Input Format 入力は以下の形式で標準入力から与えられる。 N M o_1 o_2 ... o_{N−1} u_1 v_1 u_2 v_2 ... u_{N−1} v_{N−1} X_1 Y_1 X_2 Y_2 ... X_M Y_M 1 行目には木の頂点数 N と、行われるゲーム数を表す整数 M が入力される。 2 行目から N 行目にかけて、 1 ... N−1 番目の頂点に書かれている操作が入力される。 さらに続けて N−1 行に、各辺により繋がれる 2 頂点の番号が入力される。 最後に M 回のゲームにおける X , Y の値が M 行に渡り入力される。 Output Format 各ゲームでの最終的な T の値をそれぞれ M 行に出力せよ。 Sample Input 1 6 3 T=T|X T=T|Y T=T|Y T=T^Y T=T&X 0 1 0 2 1 3 1 4 2 5 5 6 3 5 0 0 Sample Output 1 4 6 0 X = 5, Y = 6 の場合、頂点 0 -> 2 -> 5 と進み、T = 5 & 6 = 4 になります X = 3, Y = 5 の場合、頂点 0 -> 1 -> 4 と進み、T = 3 ^ 5 = 6 になります X = 0, Y = 0 の場合、どこを通っても T は 0 から変化しません
[ { "submission_id": "aoj_2713_10853981", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nmap<vector<int>, int> memo;\nvector<int> op;\n\nusing graph = vector<vector<int>>;\n\nint proc(int state, int op) {\n if(state == 0) {\n if(op == 0 || op == 1) {\n return 0;\n ...
aoj_2711_cpp
ほぼ周期文字列 文字列 S が与えられる。この文字列 S に対し、 Q 個のクエリに答えよ。 i 番目のクエリでは、 S[l_i,\ r_i] から1文字まで変えてよいとき、 S[l_i,\ r_i] を周期 t_i の文字列にできるかどうかを判定せよ。 S[l,\ r] は文字列 S の l 文字目から r 文字目までの部分文字列を表す。 文字列 W が周期 t の文字列であるとは、 i\ =\ 1,\2,\... ,\ |W| − t に対し、 W_{i} = W_{i+t} となることとする。 Constraints 1 ≤ |S| ≤ 10^5 1 ≤ Q ≤ 10^5 1 ≤ l_i ≤ r_i ≤ |S| 1 ≤ t_i ≤ r_i − l_i+1 S はアルファベットの小文字のみからなる Input Format 入力は以下の形式で標準入力から与えられる。 S Q l_1 r_1 t_1 ... l_Q r_Q t_Q Output Format Q 行にわたって出力せよ。 i 行目には、 i 番目のクエリの答えを Yes または No で出力せよ。 Sample Input 1 abcabcaxcabc 4 1 9 3 8 12 3 1 4 2 2 3 2 Sample Output 1 Yes Yes No Yes Sample Input 2 isuruu 4 3 6 1 3 6 2 3 6 3 2 4 1 Sample Output 2 Yes Yes Yes No
[ { "submission_id": "aoj_2711_9799895", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(), (v).end()\n#define UNIQUE(v) sort(ALL(v)...
aoj_2716_cpp
Leapfrog Problem Statement N 個のマスが円状に並んでいる。マスは時計回りに 1,\ 2,\ ... ,\ N と番号が振られている。各 i ( 1 ≤ i ≤ N−1 ) について、 i 番目のマスと i+1 番目のマスは隣り合う。また、 N 番目のマスと 1 番目のマスは隣り合う。 これらのうち M 個のマスには、互いに区別できない駒が 1 個ずつ置かれている。はじめ、 x_1,\ x_2,\ ... ,\ x_M 番目のマスに駒が置かれている。次の操作を何回か行い、 y_1,\ y_2,\ ... ,\ y_M 番目のマスに駒が置かれているようにしたい。 時計回りまたは反時計回りに連続する 3 個のマスを選び、順に A,\ B,\ C とおく。 A と B にそれぞれ駒があり C に駒がないならば、 A の駒を C へ移動する。 y_1,\ y_2,\ ... ,\ y_M 番目のマスに駒が置かれているようにできるか判定せよ。できるならば、必要な操作の回数の最小値を求めよ。 Constraints 3 ≤ N ≤ 3,000 1 ≤ M ≤ N 1 ≤ x_1<x_2< ... <x_M ≤ N 1 ≤ y_1<y_2< ... <y_M ≤ N Input Format 入力は以下の形式で標準入力から与えられる。 N M x_1 x_2 ... x_M y_1 y_2 ... y_M Output Format y_1,\ y_2,\ ... ,\ y_M 番目のマスに駒が置かれているようにできるならば、必要な操作の回数の最小値を一行に出力せよ。できないならば、代わりに -1 を一行に出力せよ。 Sample Input 1 7 2 1 2 5 6 Sample Output 1 3 次のように 3 回の操作を行えばよい。 2 番目のマスの駒を 7 番目のマスへ移動する。 1 番目のマスの駒を 6 番目のマスへ移動する。 7 番目のマスの駒を 5 番目のマスへ移動する。 Sample Input 2 3 1 1 2 Sample Output 2 -1 Sample Input 3 2999 3 1 2 3 2 3 4 Sample Output 3 4491004
[ { "submission_id": "aoj_2716_10853361", "code_snippet": "#include <string>\n#include <vector>\n#include<iostream>\n#include<cstdio>\n#include<cstdlib>\n#include<stack>\n#include<queue>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<list>\n#include<deque>\n#include<bitset>\n#include<se...
aoj_2717_cpp
Problem A: Where is the Boundary An island country JAGAN in a certain planet is very long and narrow, and extends east and west. This long country is said to consist of two major cultural areas | the eastern and the western. Regions in the east tend to have the eastern cultural features and regions in the west tend to have the western cultural features, but, of course, the boundary between the two cultural areas is not clear, which has been an issue. You are given an assignment estimating the boundary using a given data set. More precise specification of the assignment is as follows: JAGAN is divided into $n$ prefectures forming a line in the east-west direction. Each prefecture is numbered 1, 2, ..., $n$ from WEST to EAST . Each data set consists of $m$ features, which has 'E' (east) or 'W' (west) for each prefecture. These data indicate that each prefecture has eastern or western features from $m$ different point of views, for example, food, clothing, and so on. In the estimation, you have to choose a cultural boundary achieving the minimal errors. That is, you have to minimize the sum of 'W's in the eastern side and 'E's in the western side. In the estimation, you can choose a cultural boundary only from the boundaries between two prefectures. Sometimes all prefectures may be estimated to be the eastern or the western cultural area. In these cases, to simplify, you must virtually consider that the boundary is placed between prefecture No. 0 and No. 1 or between prefecture No. $n$ and No. $n+1$. When you get multiple minimums, you must output the most western (least-numbered) result. Write a program to solve the assignment. Input Each input is formatted as follows: $n$ $m$ $d_{11} ... d_{1n}$ : : $d_{m1} ... d_{mn}$ The first line consists of two integers $n$ ($1 \leq n \leq 10,000$), $m$ ($1 \leq m \leq 100$), which indicate the number of prefectures and the number of features in the assignment. The following m lines are the given data set in the assignment. Each line contains exactly $n$ characters. The $j$-th character in the $i$-th line $d_{ij}$ is 'E' (east) or 'W' (west), which indicates $j$-th prefecture has the eastern or the western feature from the $i$-th point of view. Output Print the estimated result in a line. The output consists of two integers sorted in the ascending order which indicate two prefectures touching the boundary. Sample Input 2 1 WE Output for the Sample Input 1 2 Sample Input 3 2 WWE WEE Output for the Sample Input 1 2 Both estimates "1 2" and "2 3" achieve 1 error as the minimum. From the restriction that you must adopt the most western estimate, you must output "1 2". Sample Input 3 1 WWW Output for the Sample Input 3 4 In this case, all the prefectures are western. As described in the problem statement, you must virtually consider that the boundary is placed between third and fourth prefectures. Sample Input 3 1 WEW Output for the Sample Input 1 2 You cannot assume that 'E's and 'W's are separated.
[ { "submission_id": "aoj_2717_10518617", "code_snippet": "#include <iostream>\n#include <iomanip>\n#include <cassert>\n#include <vector>\n#include <algorithm>\n#include <utility>\n#include <numeric>\n#include <tuple>\n#include <ranges>\nnamespace ranges = std::ranges;\nnamespace views = std::views;\n// #incl...
aoj_2718_cpp
Problem B: Vector Field In 20015, JAG (Jagan Acceleration Group) has succeeded in inventing a new accelerator named Force Point for an experiment of proton collision on the two-dimensional $xy$-plane. If a proton touches a Force Point, it is accelerated to twice its speed and its movement direction is veered. A proton may be veered by a Force Field in four ways: the positive or negative directions parallel to the $x$- or the $y$-axis. The direction in which a proton veers is determined by the type of the Force Point. A Force Point can accelerate a proton only once because the Force Point disappears immediately after the acceleration. Generating many Force Points on the two-dimensional plane, which is called a 2D Force Point Field, allows us to accelerate a proton up to a target speed by sequentially accelerating the proton with the Force Points in the 2D Force Point Filed. The Force Point generation method is still under experiment and JAG has the following technical limitations: JAG cannot generate a Force Point with a specified position and a type. JAG cannot generate a Force Point after putting a proton into a 2D Force Point Field. JAG cannot move Force Points. JAG cannot change a protons direction except by the effect of Force Points. JAG can use only one proton for a 2D Force Point Field. JAG can put a proton moving in any direction with its speed 1 at any position in a 2D Force Point Field. In order to achieve the maximum speed of a proton, the engineers at JAG have to choose the optimal initial position and the optimal initial direction of the proton so that the proton is accelerated by as many Force Points as possible, after carefully observing the generated 2D Force Point Field. By observing a generated 2D Force Point Field, the number of the generated Force Points is known to be $n$. The position ($x_i$, $y_i$) and the direction veering type $d_i$ of the $i$-th point are also known. Your task is to write a program to calculate the maximum speed of a proton by acceleration on a given 2D Force Point Field when JAG puts a proton optimally. Input The input consists of a single test case which describes a 2D Force Point Field in the following format. $n$ $x_1$ $y_1$ $d_1$ ... $x_n$ $y_n$ $d_n$ The first line contains one integer $n$ ($1 \leq n \leq 3,000$) which is the number of the Force Points on the 2D Force Point Field. Each of the next $n$ lines contains two integers $x_i$ and $y_i$ ($|x_i|$, $|y_i| \leq 10^9$) and one character $d_i$ ($d_i$ is one of '>', 'v', '<' or '^'). $x_i$ and $y_i$ represent a coordinate of the $i$-th Force Point, and $d_i$ is the direction veering type of the $i$-th force point. A force point with a type '>' changes protons direction to the positive direction of the $x$-axis, 'v' represents the positive direction of the $y$-axis, '<' represents the negative direction of the $x$-axis, and '^' represents the negative direction of the $y$-axis. You can assume that any two Force Points are not generated on the same ...(truncated)
[ { "submission_id": "aoj_2718_10856665", "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_2720_cpp
Problem D: Identity Function You are given an integer $N$, which is greater than 1. Consider the following functions: $f(a) = a^N$ mod $N$ $F_1(a) = f(a)$ $F_{k+1}(a) = F_k(f(a))$ $(k = 1,2,3,...)$ Note that we use mod to represent the integer modulo operation. For a non-negative integer $x$ and a positive integer $y$, $x$ mod $y$ is the remainder of $x$ divided by $y$. Output the minimum positive integer $k$ such that $F_k(a) = a$ for all positive integers $a$ less than $N$. If no such $k$ exists, output -1. Input The input consists of a single line that contains an integer $N$ ($2 \leq N \leq 10^9$), whose meaning is described in the problem statement. Output Output the minimum positive integer $k$ such that $F_k(a) = a$ for all positive integers $a$ less than $N$, or -1 if no such $k$ exists. Sample Input 3 Output for the Sample Input 1 Sample Input 4 Output for the Sample Input -1 Sample Input 15 Output for the Sample Input 2
[ { "submission_id": "aoj_2720_10946047", "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_2721_cpp
Problem E: Enclose Points There are $N$ points and $M$ segments on the $xy$-plane. Each segment connects two of these points and they don't intersect each other except at the endpoints. You are also given $Q$ points as queries. Your task is to determine for each query point whether you can make a polygon that encloses the query point using some of the given segments. Note that the polygon should not necessarily be convex. Input Each input is formatted as follows. $N$ $M$ $Q$ $x_1$ $y_1$ ... $x_N$ $y_N$ $a_1$ $b_1$ ... $a_M$ $b_M$ $qx_1$ $qy_1$ ... $qx_Q$ $qy_Q$ The first line contains three integers $N$ ($2 \leq N \leq 100,000$), $M$ ($1 \leq M \leq 100,000$), and $Q$ ($1 \leq Q \leq 100,000$), which represent the number of points, the number of segments, and the number of queries, respectively. Each of the following $N$ lines contains two integers $x_i$ and $y_i$ ($-100,000 \leq x_i, y_i \leq 100,000$), the coordinates of the $i$-th point. The points are guaranteed to be distinct, that is, $(x_i, y_i) \ne (x_j, y_j)$ when $i \ne j$. Each of the following $M$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i < b_i \leq N$), which indicate that the $i$-th segment connects the $a_i$-th point and the $b_i$-th point. Assume that those segments do not intersect each other except at the endpoints. Each of the following $Q$ lines contains two integers $qx_i$ and $qy_i$ ($-100,000 \leq qx_i, qy_i \leq 100,000$), the coordinates of the $i$-th query point. You can assume that, for any pair of query point and segment, the distance between them is at least $10^{-4}$. Output The output should contain $Q$ lines. Print "Yes" on the $i$-th line if there is a polygon that contains the $i$-th query point. Otherwise print "No" on the $i$-th line. Sample Input 4 5 3 -10 -10 10 -10 10 10 -10 10 1 2 1 3 1 4 2 3 3 4 -20 0 1 0 20 0 Output for the Sample Input No Yes No Sample Input 8 8 5 -20 -20 20 -20 20 20 -20 20 -10 -10 10 -10 10 10 -10 10 1 2 1 4 2 3 3 4 5 6 5 8 6 7 7 8 -25 0 -15 0 0 0 15 0 25 0 Output for the Sample Input No Yes Yes Yes No Sample Input 8 8 5 -20 -10 -10 -10 -10 10 -20 10 10 -10 20 -10 20 10 10 10 1 2 2 3 3 4 1 4 5 6 6 7 7 8 5 8 -30 0 -15 0 0 0 15 0 30 0 Output for the Sample Input No Yes No Yes No
[ { "submission_id": "aoj_2721_11060906", "code_snippet": "#ifdef NACHIA\n#define _GLIBCXX_DEBUG\n#else\n#define NDEBUG\n#endif\n#include <iostream>\n#include <string>\n#include <vector>\n#include <algorithm>\n#include <set>\nusing namespace std;\nusing ll = long long;\nconst ll INF = 1ll << 60;\n#define REP(...
aoj_2729_cpp
Delete Files You are using an operating system named "Jaguntu". Jaguntu provides "Filer", a file manager with a graphical user interface. When you open a folder with Filer, the name list of files in the folder is displayed on a Filer window. Each filename is displayed within a rectangular region, and this region is called a filename region. Each filename region is aligned to the left side of the Filer window. The height of each filename region is 1, and the width of each filename region is the filename length. For example, when three files "acm.in1", "acm.c~", and "acm.c" are stored in this order in a folder, it looks like Fig. C-1 on the Filer window. You can delete files by taking the following steps. First, you select a rectangular region with dragging a mouse. This region is called selection region. Next, you press the delete key on your keyboard. A file is deleted if and only if its filename region intersects with the selection region. After the deletion, Filer shifts each filename region to the upside on the Filer window not to leave any top margin on any remaining filename region. For example, if you select a region like Fig. C-2, then the two files "acm.in1" and "acm.c~" are deleted, and the remaining file "acm.c" is displayed on the top of the Filer window as Fig. C-3. You are opening a folder storing $N$ files with Filer. Since you have almost run out of disk space, you want to delete unnecessary files in the folder. Your task is to write a program that calculates the minimum number of times to perform deletion operation described above. Input The input consists of a single test case. The test case is formatted as follows. $N$ $D_1$ $L_1$ $D_2$ $L_2$ ... $D_N$ $L_N$ The first line contains one integer $N$ ($1 \leq N \leq 1,000$), which is the number of files in a folder. Each of the next $N$ lines contains a character $D_i$ and an integer $L_i$: $D_i$ indicates whether the $i$-th file should be deleted or not, and $L_i$ ($1 \leq L_i \leq 1,000$) is the filename length of the $i$-th file. If $D_i$ is 'y', the $i$-th file should be deleted. Otherwise, $D_i$ is always 'n', and you should not delete the $i$-th file. Output Output the minimum number of deletion operations to delete only all the unnecessary files. Sample Input 1 3 y 7 y 6 n 5 Output for the Sample Input 1 1 Sample Input 2 3 y 7 n 6 y 5 Output for the Sample Input 2 2 Sample Input 3 6 y 4 n 5 y 4 y 6 n 3 y 6 Output for the Sample Input 3 2
[ { "submission_id": "aoj_2729_4293550", "code_snippet": "#include <iostream>\n#include <vector>\n\ntemplate <class T>\nstd::vector<T> vec(int len, T elem) { return std::vector<T>(len, elem); }\n\nconstexpr int INF = 1 << 30;\n\nvoid solve() {\n int n;\n std::cin >> n;\n\n std::vector<bool> erase(n);...
aoj_2725_cpp
Problem I: Live Programming A famous Japanese idol group, JAG48, is planning the program for its next live performance. They have $N$ different songs, $song_1$, $song_2$, ..., and $song_N$. Each song has three integer param- eters, $t_i$, $p_i$, and $f_i$: $t_i$ denotes the length of $song_i$, $p_i$ denotes the basic satisfaction points the audience will get when $song_i$ is performed, and $f_i$ denotes the feature value of songi that affects the audience's satisfaction. During the live performance, JAG48 can perform any number (but at least one) of the $N$ songs, unless the total length of the chosen songs exceeds the length of the live performance $T$. They can decide the order of the songs to perform, but they cannot perform the same song twice or more. The goal of this live performance is to maximize the total satisfaction points that the audience will get. In addition to the basic satisfaction points of each song, the difference between the feature values of the two songs that are performed consecutively affects the total satisfaction points. If there is no difference, the audience will feel comfortable. However, the larger the difference will be, the more frustrated the audience will be. Thus, the total satisfaction points will be calculated as follows: If $song_x$ is the first song of the live performance, the total satisfaction points just after $song_x$ is equal to $p_x$. If $song_x$ is the second or subsequent song of the live performance and is performed just after $song_y$, $p_x -(f_x -f_y)^2$ is added to the total satisfaction points, because the audience will get frustrated if $f_x$ and $f_y$ are different. Help JAG48 find a program with the maximum total satisfaction points. Input The input is formatted as follows. $N$ $T$ $t_1$ $p_1$ $f_1$ : : : $t_N$ $p_N$ $f_N$ The first line contains two integers $N$ and $T$: the number of the available $song_s$ $N$ ($1 \leq N \leq 4,000$), and the length of the live performance $T$ ($1 \leq T \leq 4,000$). The following $N$ lines represent the parameters of the songs. The $i$-th line of them contains three integers, which are the parameters of $song_i$: the length $t_i$ ($1 \leq t_i \leq 4,000$), the basic satisfaction points $p_i$ ($1 \leq p_i \leq 10^8$), and the feature value $f_i$ ($1 \leq f_i \leq 10^4$). You can assume that there is at least one song whose length is less than or equal to $T$. Output Output the maximum total satisfaction points that the audience can get during the live performance. Sample Input 2 10 10 200 1 10 100 100 Output for the Sample Input 200 Sample Input 3 15 5 100 1 5 100 2 5 100 4 Output for the Sample Input 295 Sample Input 3 10 5 200 200 5 200 201 5 300 1 Output for the Sample Input 399 Sample Input 3 20 5 100 200 5 100 201 5 300 1 Output for the Sample Input 300 Sample Input 5 61 14 49 7 31 46 4 30 55 5 52 99 1 34 70 3 Output for the Sample Input 103
[ { "submission_id": "aoj_2725_8170564", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n#define overload2(a, b, c, ...) c\n#define overload3(a, b, c, d, ...) d\n#define overload4(a, b, c, d, e ...) e\n#define overload5(a, b, c, d, e, f ...) f\n#define overload6(a, b, c, d, e, f, g ...) g\n#d...
aoj_2726_cpp
Problem J: Black Company JAG Company is a sweatshop (sweatshop is called "burakku kigyo" in Japanese), and you are the CEO for the company. You are planning to determine $N$ employees' salary as low as possible (employees are numbered from 1 to $N$). Each employee's salary amount must be a positive integer greater than zero. At that time, you should pay attention to the employees' contribution degree. If the employee $i$'s contribution degree $c_i$ is greater than the employee $j$'s contribution degree $c_j$ , the employee i must get higher salary than the employee $j$'s salary. If the condition is not satisfied, employees may complain about their salary amount. However, it is not necessarily satisfied in all pairs of the employees, because each employee can only know his/her close employees' contribution degree and salary amount. Therefore, as long as the following two conditions are satisfied, employees do not complain to you about their salary amount. If the employees $i$ and $j$ are close to each other, $c_i < c_j \Leftrightarrow p_i < p_j$ must be satisfied, where $p_i$ is the employee $i$'s salary amount. If the employee $i$ is close to the employees $j$ and $k$, $c_j < c_k \Leftrightarrow p_j < p_k$ must be satisfied. Write a program that computes the minimum sum of all employees' salary amount such that no employee complains about their salary. Input Each input is formatted as follows: $N$ $c_1$ ... $c_N$ $M$ $a_1$ $b_1$ ... $a_M$ $b_M$ The first line contains an integer $N$ ($1 \leq N \leq 100,000$), which indicates the number of employees. The second line contains $N$ integers $c_i$ ($1 \leq c_i \leq 100,000$) representing the contribution degree of employee $i$. The third line contains an integer $M$ ($0 \leq M \leq 200,000$), which specifies the number of relationships. Each of the following $M$ lines contains two integers $a_i$ and $b_i$ ($a_i \ne b_i, 1 \leq a_i, b_i \leq N$). It represents that the employees $a_i$ and $b_i$ are close to each other. There is no more than one relationship between each pair of the employees. Output Print the minimum sum of all employees' salary amounts in a line. Sample Input 3 1 3 3 2 1 2 1 3 Output for the Sample Input 5 Sample Input 3 1 2 3 2 1 2 1 3 Output for the Sample Input 6 Sample Input 4 1 1 2 2 2 1 2 3 4 Output for the Sample Input 4 Sample Input 5 1 2 5 5 1 6 1 2 4 1 2 3 5 2 4 3 4 5 Output for the Sample Input 10 Sample Input 6 4 3 2 1 5 3 7 4 2 1 5 2 6 6 5 4 1 1 6 6 3 Output for the Sample Input 13
[ { "submission_id": "aoj_2726_10849700", "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_2728_cpp
Change a Password Password authentication is used in a lot of facilities. The office of JAG also uses password authentication. A password is required to enter their office. A password is a string of $N$ digits '0'-'9'. This password is changed on a regular basis. Taro, a staff of the security division of JAG, decided to use the following rules to generate a new password from an old one. The new password consists of the same number $N$ of digits to the original one and each digit appears at most once in the new password. It can have a leading zero. (Note that an old password may contain same digits twice or more.) The new password maximizes the difference from the old password within constraints described above. (Definition of the difference between two passwords is described below.) If there are two or more candidates, the one which has the minimum value when it is read as an integer will be selected. The difference between two passwords is defined by min ($|a - b|, 10^N - |a - b|$), where $a$ and $b$ are the integers represented by the two passwords. For example, the difference between "11" and "42" is 31, and the difference between "987" and "012" is 25. Taro would like to use a computer to calculate a new password correctly, but he is not good at programming. Therefore, he asked you to write a program. Your task is to write a program that generates a new password from an old password. Input The input consists of a single test case. The first line of the input contains a string $S$ which denotes the old password. You can assume that the length of $S$ is no less than 1 and no greater than 10. Note that old password $S$ may contain same digits twice or more, and may have leading zeros. Output Print the new password in a line. Sample Input 1 201 Output for the Sample Input 1 701 Sample Input 2 512 Output for the Sample Input 2 012 Sample Input 3 99999 Output for the Sample Input 3 49876 Sample Input 4 765876346 Output for the Sample Input 4 265874931
[ { "submission_id": "aoj_2728_11021300", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing a2 = array<ll, 2>;\nusing a3 = array<ll, 3>;\n\nbool chmin(auto& a, const auto& b) { return a > b ? a = b, 1 : 0; }\nbool chmax(auto& a, const auto& b) { return a < b ? a = b...
aoj_2722_cpp
Problem F: Marching Course Since members of Kitafuji High School Brass Band Club succeeded in convincing their stern coach of their playing skills, they will be able to participate in Moon Light Festival as a marching band. This festival is a prelude in terms of appealing their presence for the coming domestic contest. Hence, they want to attract a festival audience by their performance. Although this festival restricts performance time up to $P$ minutes, each team can freely determine their performance course from a provided area. The provided area consists of $N$ checkpoints, numbered 1 through $N$, and $M$ bidirectional roads connecting two checkpoints. Kitafuji Brass Band already has the information about each road: its length and the expected number of people on its roadside. Each team must start at the checkpoint 1, and return back to the checkpoint 1 in $P$ minutes. In order to show the performance ability of Kitafuji Brass Band to a festival audience, their stern coach would like to determine their performance course so that many people listen their march as long as possible. The coach uses "impression degree" to determine their best course. If they play $m$ minutes on the road with length $d$ and the expected number $v$ of people, then the impression degree will be $m \times v/d$. The impression degree of a course is the sum of impression degree of their performance on the course. Marching bands must move at a constant speed during marching: 1 unit length per 1 minute. On the other hand, they can turn in the opposite direction at any time, any place including a point on a road. The impression degree is accumulated even if they pass the same interval two or more times. Your task is to write a program to determine a course with the maximum impression degree in order to show the performance ability of Kitafuji Brass Band to an audience as much as possible. Input The input is formatted as follows. $N$ $M$ $P$ $s_1$ $t_1$ $d_1$ $v_1$ : : : $s_M$ $t_M$ $d_M$ $v_M$ The first line contains three integers $N$, $M$, and $P$: the number of checkpoints $N$ ($2 \leq N \leq 200$), the number of roads $M$ ($N - 1 \leq M \leq N(N - 1)/2$), and the performance time $P$ ($1 \leq P \leq 1,000$). The following $M$ lines represent the information about roads. The $i$-th line of them contains four integers $s_i$, $t_i$, $d_i$, and $v_i$: the $i$-th road bidirectionally connects between checkpoints $s_i$ and $t_i$ ($1 \leq s_i, t_i \leq N, s_i \ne t_i$) with length $d_i$ ($1 \leq d_i \leq 1,000$) and the expected number $v_i$ ($1 \leq v_i \leq 1,000$) of people. You can assume that any two checkpoints are directly or indirectly connected with one or more roads. You can also assume that there are no pair of roads having the same pair of endpoints. Output Output the maximum impression degree of a course for a $P$-minute performance. The absolute error should be less than $10^{-4}$. Sample Input 3 3 4 1 2 1 1 2 3 2 4 3 1 1 1 Output for the Sample Input 6 Sample I ...(truncated)
[ { "submission_id": "aoj_2722_10853940", "code_snippet": "#include<bits/stdc++.h>\n\n#define clr(x,y) memset((x),(y),sizeof(x))\n\nusing namespace std;\ntypedef long long LL;\n\nconst int INF = 1000000000;\nconst int maxn = 200 + 10;\n\nstruct Edge {\n int to,dist,v;\n};\n\n\nvector <Edge> G[maxn+5];\ndoubl...
aoj_2730_cpp
Line Gimmick You are in front of a linear gimmick of a game. It consists of $N$ panels in a row, and each of them displays a right or a left arrow. You can step in this gimmick from any panel. Once you get on a panel, you are forced to move following the direction of the arrow shown on the panel and the panel will be removed immediately. You keep moving in the same direction until you get on another panel, and if you reach a panel, you turn in (or keep) the direction of the arrow on the panel. The panels you passed are also removed. You repeat this procedure, and when there is no panel in your current direction, you get out of the gimmick. For example, when the gimmick is the following image and you first get on the 2nd panel from the left, your moves are as follows. Move right and remove the 2nd panel. Move left and remove the 3rd panel. Move right and remove the 1st panel. Move right and remove the 4th panel. Move left and remove the 5th panel. Get out of the gimmick. You are given a gimmick with $N$ panels. Compute the maximum number of removed panels after you get out of the gimmick. Input The input consists of two lines. The first line contains an integer $N$ ($1 \leq N \leq 100,000$) which represents the number of the panels in the gimmick. The second line contains a character string $S$ of length $N$, which consists of ' > ' or ' < '. The $i$-th character of $S$ corresponds to the direction of the arrow on the $i$-th panel from the left. ' < ' and ' > ' denote left and right directions respectively. Output Output the maximum number of removed panels after you get out of the gimmick. Sample Input 1 7 >><><<< Output for the Sample Input 1 7 Sample Input 2 5 >><<< Output for the Sample Input 2 5 Sample Input 3 6 ><<><< Output for the Sample Input 3 6 Sample Input 4 7 <<><<>> Output for the Sample Input 4 5
[ { "submission_id": "aoj_2730_10295503", "code_snippet": "#include \"bits/stdc++.h\"\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing ull = unsigned long long;\n#define rep(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)\n#define drep(i, m, n) for (ll i = m - 1; i >= n; i--)\n#define ...
aoj_2724_cpp
Problem H: Laser Cutter Ciel is going to do woodworking. Ciel wants to make a cut in a wooden board using a laser cutter. To make it simple, we assume that the board is a two-dimensional plane. There are several segments on the board along which Ciel wants to cut the board. Each segment has a direction and Ciel must cut those segments along their directions. Those segments are connected when you ignore the directions, that is, any two points on the segments are directly or indirectly connected by the segments. While the laser cutter is powered on, it emits a laser which hits the board at a point and cuts the board along its trace. The laser initially points to $(x, y)$. Ciel can conduct the following two operations: Move the laser cutter with its power on and cut (a part of) a segment along its direction, or Move the laser cutter to any position with its power off. Ciel should not necessarily cut the whole segment at a time; she can start or stop cutting a segment at any point on the segments. Ciel likes to be efficient, so she wants to know the shortest route such that the laser cutter cuts the whole parts of all the segments and then move back to the initial point. Your task is to write a program that calculates the minimum total moving distance of the laser cutter. Input The first line of the input contains an integer $n$ ($1 \leq n \leq 300$), the number of segments. The next line contains two integers $x$ and $y$ ($-1,000 \leq x, y \leq 1,000$), which is the initial position $(x, y)$ of the laser. The $i$-th of the following $n$ lines contains four integers $sx_i$, $sy_i$, $tx_i$ and $ty_i$ ($-1,000 \leq sx_i, sy_i, tx_i, ty_i \leq 1,000$), which indicate that they are the end points of the $i$-th segment, and that the laser cutter can cut the board in the direction from $(sx_i, sy_i)$ to $(tx_i, ty_i)$. The input satisfies the following conditions: For all $i$ ($1 \leq i \leq n$), $(sx_i, sy_i) \ne (tx_i, ty_i)$. The initial point $(x, y)$ lies on at least one of the given segments. For all distinct $i, j$ ($1 \leq i, j \leq n$), the $i$-th segment and the $j$-th segment share at most one point. Output Output a line containing the minimum total moving distance the laser cutter needs to travel to cut all the segments and move back to the initial point. The absolute error or the relative error should be less than $10^{-6}$. Sample Input 3 0 1 0 0 0 1 0 1 0 2 0 2 0 3 Output for the Sample Input 6.0000000000000000 Sample Input 2 0 1 0 0 0 2 -1 1 1 1 Output for the Sample Input 6.8284271247461900 Sample Input 5 0 0 0 0 1 0 1 1 -1 1 -1 1 -1 -1 -1 -1 1 -1 1 -1 1 1 Output for the Sample Input 10.0000000000000000
[ { "submission_id": "aoj_2724_10850438", "code_snippet": "#include<cstdio>\n#include<cstdlib>\n#include<algorithm>\n#include<cstring>\n#include<cmath>\n#define cl(x) memset(x,0,sizeof(x))\n#define read(x) scanf(\"%d\",&(x));\nusing namespace std;\n\ninline double sqr(int x){ return x*x; }\n\nconst int N=305;...
aoj_2731_cpp
Shifting a Matrix You are given $N \times N$ matrix $A$ initialized with $A_{i,j} = (i - 1)N + j$, where $A_{i,j}$ is the entry of the $i$-th row and the $j$-th column of $A$. Note that $i$ and $j$ are 1-based. You are also given an operation sequence which consists of the four types of shift operations: left, right, up, and down shifts. More precisely, these operations are defined as follows: Left shift with $i$: circular shift of the $i$-th row to the left, i.e., setting previous $A_{i,k}$ to new $A_{i,k-1}$ for $2 \leq k \leq N$, and previous $A_{i,1}$ to new $A_{i,N}$. Right shift with $i$: circular shift of the $i$-th row to the right, i.e., setting previous $A_{i,k}$ to new $A_{i,k+1}$ for $1 \leq k \leq N - 1$, and previous $A_{i,N}$ to new $A_{i,1}$. Up shift with $j$: circular shift of the $j$-th column to the above, i.e., setting previous $A_{k,j}$ to new $A_{k-1,j}$ for $2 \leq k \leq N$, and previous $A_{1,j}$ to new $A_{N,j}$. Down shift with $j$: circular shift of the $j$-th column to the below, i.e., setting previous $A_{k,j}$ to new $A_{k+1,j}$ for $1 \leq k \leq N - 1$, and previous $A_{N,j}$ to new $A_{1,j}$. An operation sequence is given as a string. You have to apply operations to a given matrix from left to right in a given string. Left, right, up, and down shifts are referred as 'L', 'R', 'U', and 'D' respectively in a string, and the following number indicates the row/column to be shifted. For example, "R25" means we should perform right shift with 25. In addition, the notion supports repetition of operation sequences. An operation sequence surrounded by a pair of parentheses must be repeated exactly $m$ times, where $m$ is the number following the close parenthesis. For example, "(L1R2)10" means we should repeat exactly 10 times the set of the two operations: left shift with 1 and right shift with 2 in this order. Given operation sequences are guaranteed to follow the following BNF: <sequence> := <sequence><repetition> | <sequence><operation> | <repetition> | <operation> <repetition> := '('<sequence>')'<number> <operation> := <shift><number> <shift> := 'L' | 'R' | 'U' | 'D' <number> := <nonzero_digit> |<number><digit> <digit> := '0' | <nonzero_digit> <nonzero_digit> := '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' Given $N$ and an operation sequence as a string, make a program to compute the $N \times N$ matrix after operations indicated by the operation sequence. Input The input consists of a single test case. The test case is formatted as follows. $N$ $L$ $S$ The first line contains two integers $N$ and $L$, where $N$ ($1 \leq N \leq 100$) is the size of the given matrix and $L$ ($2 \leq L \leq 1,000$) is the length of the following string. The second line contains a string $S$ representing the given operation sequence. You can assume that $S$ follows the above BNF. You can also assume numbers representing rows and columns are no less than 1 and no more than $N$, and the number of each repetition is no less than 1 ...(truncated)
[ { "submission_id": "aoj_2731_10867660", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair<ll, ll> P;\n\n#define each(i,a) for (auto&& i : a)\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_#...
aoj_2735_cpp
Shortest Bridge There is a city whose shape is a 1,000 $\times$ 1,000 square. The city has a big river, which flows from the north to the south and separates the city into just two parts: the west and the east. Recently, the city mayor has decided to build a highway from a point $s$ on the west part to a point $t$ on the east part. A highway consists of a bridge on the river, and two roads: one of the roads connects $s$ and the west end of the bridge, and the other one connects $t$ and the east end of the bridge. Note that each road doesn't have to be a straight line, but the intersection length with the river must be zero. In order to cut building costs, the mayor intends to build a highway satisfying the following conditions: Since bridge will cost more than roads, at first the length of a bridge connecting the east part and the west part must be as short as possible. Under the above condition, the sum of the length of two roads is minimum. Your task is to write a program computing the total length of a highway satisfying the above conditions. Input The input consists of a single test case. The test case is formatted as follows. $sx$ $sy$ $tx$ $ty$ $N$ $wx_1$ $wy_1$ ... $wx_N$ $wy_N$ $M$ $ex_1$ $ey_1$ ... $ex_M$ $ey_M$ At first, we refer to a point on the city by a coordinate ($x, y$): the distance from the west side is $x$ and the distance from the north side is $y$. The first line contains four integers $sx$, $sy$, $tx$, and $ty$ ($0 \leq sx, sy, tx, ty \leq 1,000$): points $s$ and $t$ are located at ($sx, sy$) and ($tx, ty$) respectively. The next line contains an integer $N$ ($2 \leq N \leq 20$), where $N$ is the number of points composing the west riverside. Each of the following $N$ lines contains two integers $wx_i$ and $wy_i$ ($0 \leq wx_i, wy_i \leq 1,000$): the coordinate of the $i$-th point of the west riverside is ($wx_i, wy_i$). The west riverside is a polygonal line obtained by connecting the segments between ($wx_i, wy_i$) and ($wx_{i+1}, wy_{i+1}$) for all $1 \leq i \leq N -1$. The next line contains an integer $M$ ($2 \leq M \leq 20$), where $M$ is the number of points composing the east riverside. Each of the following $M$ lines contains two integers $ex_i$ and $ey_i$ ($0 \leq ex_i, ey_i \leq 1,000$): the coordinate of the $i$-th point of the east riverside is ($ex_i, ey_i$). The east riverside is a polygonal line obtained by connecting the segments between ($ex_i, ey_i$) and ($ex_{i+1}, ey_{i+1}$) for all $1 \leq i \leq M - 1$. You can assume that test cases are under the following conditions. $wy_1$ and $ey_1$ must be 0, and $wy_N$ and $ey_M$ must be 1,000. Each polygonal line has no self-intersection. Two polygonal lines representing the west and the east riverside have no cross point. A point $s$ must be on the west part of the city. More precisely, $s$ must be on the region surrounded by the square side of the city and the polygonal line of the west riverside and not containing the east riverside points. A point $t$ must ...(truncated)
[ { "submission_id": "aoj_2735_10323059", "code_snippet": "// AOJ #2735 Shortest Bridge\n// 2025.3.25\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ld = long double;\n\n#define gc() getchar_unlocked()\n\nint Cin() {\n\tint n = 0; int c = gc();\n\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');...
aoj_2734_cpp
Donut Decoration Donut maker's morning is early. Mr. D, who is also called Mr. Donuts, is an awesome donut maker. Also today, he goes to his kitchen and prepares to make donuts before sunrise. In a twinkling, Mr. D finishes frying $N$ donuts with a practiced hand. But these donuts as they are must not be displayed in a showcase. Filling cream, dipping in chocolate, topping somehow cute, colorful things, etc., several decoration tasks are needed. There are $K$ tasks numbered 1 through $K$, and each of them must be done exactly once in the order $1, 2, ..., K$ to finish the donuts as items on sale. Instantly, Mr. D arranges the $N$ donuts in a row. He seems to intend to accomplish each decoration tasks sequentially at once. However, what in the world is he doing? Mr. D, who stayed up late at yesterday night, decorates only a part of the donuts in a consecutive interval for each task. It's clearly a mistake! Not only that, he does some tasks zero or several times, and the order of tasks is also disordered. The donuts which are not decorated by correct process cannot be provided as items on sale, so he should trash them. Fortunately, there are data recording a sequence of tasks he did. The data contain the following information: for each task, the consecutive interval $[l, r]$ of the decorated donuts and the ID $x$ of the task. Please write a program enumerating the number of the donuts which can be displayed in a showcase as items on sale for given recorded data. Input The input consists of a single test case. The test case is formatted as follows. $N$ $K$ $T$ $l_1$ $r_1$ $x_1$ ... $l_T$ $r_T$ $x_T$ The first line contains two integers $N$ and $K$, where $N$ ($1 \leq N \leq 200,000$) is the number of the donuts fried by Mr. D, and $K$ ($1 \leq K \leq 200,000$) is the number of decoration tasks should be applied to the donuts. The second line contains a single integer $T$ ($1 \leq T \leq 200,000$), which means the number of information about tasks Mr. D did. Each of next $T$ lines contains three integers $l_i$, $r_i$, and $x_i$ representing the $i$-th task Mr. D did: the $i$-th task was applied to the interval $[l_i, r_i]$ ($1 \leq l_i \leq r_i \leq N$) of the donuts inclusive, and has ID $x_i$ ($1 \leq x_i \leq K$). Output Output the number of the donuts that can be provided as items on sale. Sample Input 1 3 2 3 1 2 1 2 3 2 3 3 1 Output for the Sample Input 1 1 Sample Input 2 5 3 6 2 3 1 1 3 2 4 5 1 2 4 3 3 5 2 5 5 3 Output for the Sample Input 2 2 Sample Input 3 10 1 2 2 9 1 5 7 1 Output for the Sample Input 3 5
[ { "submission_id": "aoj_2734_10851292", "code_snippet": "#include<vector>\n#include<cstdio>\n#include<cstdlib> \n#include<cstring>\n#include<algorithm>\nusing namespace std;\n#define M 800010\n#define rep(i,x,y) for(int i=(x);i<=(y);i++)\n#define For(i,x,y) for(int i=(x);i<(y);i++)\ninline int read(){\n\tch...
aoj_2732_cpp
Modern Announce Network Today, modern teenagers use SNS to communicate with each other. In a high school, $N$ students are using an SNS called ICPC (International Community for Programming Contest). Some pairs of these $N$ students are 'friends' on this SNS, and can send messages to each other. Among these $N$ students, $A$ first grade students, $B$ second grade students, and $C$ third grade students are members of the Programming Society. Note that there may be some students who are not the members of the Programming Society, so $A+B +C$ can be less than $N$. There are good relationships between members of the same grade in the Society. Thus, there is a chat group in the SNS for each grade, and the Society members of the same grade can communicate with each other instantly via their group chat. On the other hand, the relationships between any different grades are not so good, and there are no chat group for the entire Society and the entire high school. In order to broadcast a message to all the Society members on the SNS, the administrator of the Society came up with a method: the administrator tells the message to one of the $N$ students and have them spread the message on the SNS via the chat groups and their friends. (The administrator itself does not have an account for the SNS.) As the members of the same grade can broadcast the message by the chat group for the grade, we can assume that if one of a grade gets the message, all other members of that grade also get the message instantly. Therefore, if the message is told to at least one member of each grade, we can assume that the message is broadcasted to the all members of the Society on the SNS. Because it is bothering to communicate between friends, we want to minimize the number of communications between friends. What is the minimum number of communications between friends to broadcast a message to all the Society members? Who is the first person to whom the administrator should tell the message to achieve the minimum communications? Input The input consists of a single test case. The test case is formatted as follows: $N$ $A$ $B$ $C$ $a_1$ ... $a_A$ $b_1$ ... $b_B$ $c_1$ ... $c_C$ $M$ $x_1$ $y_1$ ... $x_M$ $y_M$ The first line contains four integers $N$, $A$, $B$, and $C$. $N$ ($3 \leq N \leq 10,000$) denotes the number of students in the SNS, and $A$, $B$ and $C$ ($1 \leq A,B,C \leq N$ and $A + B + C \leq N$) denote the number of members of the first, second, and third grade respectively. Each student on the SNS is represented by a unique numeric ID between 1 and $N$. The second line contains $A$ integers $a_1, ..., a_A$ ($1 \leq a_1, ..., a_A \leq N$), which are the IDs of members of the first grade. The third line contains $B$ integers $b_1, ..., b_B$ ($1 \leq b_1, ..., b_B \leq N$), which are the IDs of members of the second grade. The fourth line contains $C$ integers $c_1, ..., c_C$ ($1 \leq c_1, ... , c_C \leq N$), which are the IDs of members of the third grade. You can assume ...(truncated)
[ { "submission_id": "aoj_2732_10866710", "code_snippet": "#include <queue>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n#include <iostream>\n#include <algorithm>\ntypedef long long ll;\n#define rep(i, a, b) for (int i = a; i <= b; ++ i)\nconst int maxn = 10005, maxm = 2000005, inf = 1000000007...
aoj_2736_cpp
Longest Shortest Path You are given a directed graph and two nodes $s$ and $t$. The given graph may contain multiple edges between the same node pair but not self loops. Each edge $e$ has its initial length $d_e$ and the cost $c_e$. You can extend an edge by paying a cost. Formally, it costs $x \cdot c_e$ to change the length of an edge $e$ from $d_e$ to $d_e + x$. (Note that $x$ can be a non-integer.) Edges cannot be shortened. Your task is to maximize the length of the shortest path from node $s$ to node $t$ by lengthening some edges within cost $P$. You can assume that there is at least one path from $s$ to $t$. Input The input consists of a single test case formatted as follows. $N$ $M$ $P$ $s$ $t$ $v_1$ $u_1$ $d_1$ $c_1$ ... $v_M$ $u_M$ $d_M$ $c_M$ The first line contains five integers $N$, $M$, $P$, $s$, and $t$: $N$ ($2 \leq N \leq 200$) and $M$ ($1 \leq M \leq 2,000$) are the number of the nodes and the edges of the given graph respectively, $P$ ($0 \leq P \leq 10^6$) is the cost limit that you can pay, and $s$ and $t$ ($1 \leq s, t \leq N, s \ne t$) are the start and the end node of objective path respectively. Each of the following $M$ lines contains four integers $v_i$, $u_i$, $d_i$, and $c_i$, which mean there is an edge from $v_i$ to $u_i$ ($1 \leq v_i, u_i \leq N, v_i \ne u_i$) with the initial length $d_i$ ($1 \leq d_i \leq 10$) and the cost $c_i$ ($1 \leq c_i \leq 10$). Output Output the maximum length of the shortest path from node $s$ to node $t$ by lengthening some edges within cost $P$. The output can contain an absolute or a relative error no more than $10^{-6}$. Sample Input 1 3 2 3 1 3 1 2 2 1 2 3 1 2 Output for the Sample Input 1 6.0000000 Sample Input 2 3 3 2 1 3 1 2 1 1 2 3 1 1 1 3 1 1 Output for the Sample Input 2 2.5000000 Sample Input 3 3 4 5 1 3 1 2 1 2 2 3 1 1 1 3 3 2 1 3 4 1 Output for the Sample Input 3 4.2500000
[ { "submission_id": "aoj_2736_10854093", "code_snippet": "#include <stdio.h>\n#include <algorithm>\n#include <vector>\n#include <queue>\nusing namespace std;\n\nconst int non = 10000000;\n\nstruct edge {\n\tint x, y, f, c, r;\n};\nvector<edge> E[202];\n\nvoid add(int x, int y, int f, int c) {\n\tint p = E[x]...
aoj_2740_cpp
C - みさわさんの根付き木 Problem Statement あなたは親友であるみさわさんの誕生日が近いことに気づき,根付きの二分木をプレゼントすることにした. ここで,根付きの二分木とは,以下のようなグラフ構造である.(図 1) 各頂点には,その頂点の親と呼ばれる頂点がちょうど 1 つだけ存在し,親と辺で結ばれている.ただし,根と呼ばれる 1 つの頂点のみ,例外的に親を持たない. 各頂点は,左の子と呼ばれる頂点をちょうど1つ持つか,あるいは持たない.左の子を持つ場合,左の子とは辺で結ばれており,左の子の親はその頂点である. 各頂点は,右の子と呼ばれる頂点をちょうど1つ持つか,あるいは持たない.右の子を持つ場合,右の子とは辺で結ばれており,右の子の親はその頂点である. 図 1. 2 つの根付きの二分木とその合成の例 あなたは手作りの品を贈りたいと考えたので,市販の根付きの二分木を 2 つ買ってきて重ね合わせて合成することで,さらによい根付きの二分木を 1 つ作ることにした. あなたが買ってきた 2 つの木の各頂点には非負の整数が書かれている. みさわさんは少ない頂点数で各数値が大きいような,コストパフォーマンスがよい木が好みなので,以下の手順に沿って新しい二分木を作ることにする. 2 つの二分木それぞれの根に書かれた整数の和を,新しい二分木の根に書く整数とする. どちらの二分木の根も左の子を持っている場合,それらを根とする二分木それぞれを合成した二分木を作り,新しい二分木の根の左の子とする.そうでない場合,新しい二分木の根は左の子を持たない. どちらの二分木の根も右の子を持っている場合,それらを根とする二分木それぞれを合成した二分木を作り,新しい二分木の根の右の子とする.そうでない場合,新しい二分木の根は右の子を持たない. あなたは実際に合成する作業を行う前に,できあがる根付きの二分木がどのようになるのか確かめることにした. 買ってきた 2 つの根付きの二分木の情報が与えられるので,上記の手順に従って合成される新しい根付きの二分木を求めるプログラムを作成せよ. ここで,根付きの二分木の情報は以下のような形式で文字列として表現するものとする. (左の子を表す文字列)[根に書かれた整数](右の子を表す文字列) 節点が存在しない木は空文字列とする.例えば図 1 の合成されてできた新しい根付きの二分木は (()[6]())[8](((()[4]())[7]())[9]()) のように書く. Input 入力は次の形式で表される. $A$ $B$ $A$,$B$ はそれぞれ買ってきた根付きの二分木の情報を表す文字列であり,長さは $7$ 以上 $1000$ 以下である. 与えられる情報は前述の形式に従っており,余計な空白文字等を含まない. また,節点が存在しない根付き木が入力されることはない. 各節点に書かれた整数は $0$ 以上 $1000$ 以下であると仮定してよい. ただし,出力の各節点に書かれる整数はこの範囲に収まらない場合もあることに注意せよ. Output 2 つの根付きの二分木を合成してできあがる新しい根付きの二分木の情報を 1 行で出力せよ.特に,行末の改行を除く余計な空白文字等を含まないよう注意せよ. Sample Input 1 ((()[8]())[2]())[5](((()[2]())[6](()[3]()))[1]()) (()[4]())[3](((()[2]())[1]())[8](()[3]())) Output for the Sample Input 1 (()[6]())[8](((()[4]())[7]())[9]()) Sample Input 2 (()[1]())[2](()[3]()) (()[1](()[2]()))[3]() Output for the Sample Input 2 (()[2]())[5]() Sample Input 3 (()[1]())[111]() ()[999](()[9]()) Output for the Sample Input 3 ()[1110]() Sample Input 4 (()[2](()[4]()))[8]((()[16]())[32](()[64]())) ((()[1]())[3]())[9](()[27](()[81]())) Output for the Sample Input 4 (()[5]())[17](()[59](()[145]())) Sample Input 5 ()[0]() ()[1000]() Output for the Sample Input 5 ()[1000]()
[ { "submission_id": "aoj_2740_3532990", "code_snippet": "#include <string>\n#include <iostream>\n#include <vector>\n\nusing namespace std;\n#define vec vector<int>\n\nconst int SIZE=10000000;\n\nvec Aar(SIZE,-1);\nvec Bar(SIZE,-1);\nvec Xar(SIZE,-1);\n\n\nvoid input(vec & ar,const string & str,int & i,int k)...
aoj_2733_cpp
Cube Dividing Pablo Cubarson is a well-known cubism artist. He is producing a new piece of work using a cuboid which consists of $A \times B \times C$ unit cubes. He plans to make a beautiful shape by removing $N$ units cubes from the cuboid. When he is about to begin the work, he has noticed that by the removal the cuboid may be divided into several parts disconnected to each other. It is against his aesthetics to divide a cuboid. So he wants to know how many parts are created in his plan. Your task is to calculate the number of connected components in the cuboid after removing the $N$ cubes. Two cubes are connected if they share one face. Input The input consists of a single test case. The test case is formatted as follows: $A$ $B$ $C$ $N$ $X_1$ $Y_1$ $Z_1$ ... $X_N$ $Y_N$ $Z_N$ The first line contains four integers $A$, $B$, $C$ and $N$. $A$, $B$ and $C$ ($1 \leq A,B,C \leq 10^6$) denote the size of the cuboid $-$ the cuboid has an $A$ unit width from left to right and a $B$ unit height from bottom to top, and a $C$ unit depth from front to back. $N$ ($0 \leq N \leq 20,000, N \leq A \times B \times C - 1$) denotes the number of the cubes removed in the Cubarson's plan. Each of the following $N$ lines contains three integers $X_i$ ($0 \leq X_i \leq A-1$), $Y_i$ ($0 \leq Y_i \leq B-1$) and $Z_i$ ($0 \leq Z_i \leq C - 1$). They denote that the cube located at the $X_i$-th position from the left, the $Y_i$-th from the bottom and the $Z_i$-th from the front will be removed in the plan. You may assume the given positions are distinct. Output Print the number of the connected components in the cuboid after removing the specified cubes. Sample Input 1 2 2 2 4 0 0 0 1 1 0 1 0 1 0 1 1 Output for the Sample Input 1 4 Sample Input 2 3 3 3 1 1 1 1 Output for the Sample Input 2 1 Sample Input 3 1 1 3 2 0 0 0 0 0 2 Output for the Sample Input 3 1
[ { "submission_id": "aoj_2733_10319512", "code_snippet": "// AOJ #2733 Cube Dividing\n// 2025.3.23\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() { // 整数の入力\n\tint n = 0; int c = gc();\n\tif (c == '-...
aoj_2741_cpp
D - インビジブル Problem Statement あなたは友達と" インビジブル "というカードゲームを遊ぼうとしている. このカードゲームでは," 得点カード "と" 妨害カード "という2種類のカードを使う. それぞれの得点カードには,正の値が書かれている.このカードゲームのルールは次の通りである. ゲームはプレイヤー1とプレイヤー2の2人のプレイヤーで行われる.ゲームはプレイヤー1のターンから始まる. 場には,1つのスタックと2つのデッキがある.スタックは,2人のプレイヤーが置いたカードからなる.また,それぞれのプレイヤーが持つデッキはそのプレイヤーが持つ得点カードと妨害カードからなる.プレイヤーは自分,もしくは相手デッキのカードの順番をいつでも確認できる.ゲームの開始時点ではスタックには1枚もカードはない. 2人のプレイヤーは交互に次の2つの行動のどちらかをちょうど1回行う. 自分のデッキの一番上のカードをスタックの一番上に置く.ただし,この行動は自分のデッキにカードが1枚も存在しない時には行うことができない. 自分のターンをパスする. プレイヤーがターンをパスした時,次の処理を行う. 各プレイヤーは次の2つの条件を満たすスタック中のすべての得点カードを得る.得た得点カードは場から取り除かれる. 自分がスタックにおいた得点カードである. 相手が置いたどの妨害カードよりも上にある (スタック中に相手の妨害カードが存在しないとき,プレイヤーは自分がスタックに置いたすべてのカードを得る). スタックのカードをすべて取り除く. もしスタックにカードがない状態で両プレイヤーが連続してパスした場合,ゲームを終了する. 各プレイヤーの最終的なスコアは,各プレイヤーが得た得点カードに書かれた数の総和である. 各プレイヤーは,自分のスコアから相手のスコアを引いた値を最大化するために最適な行動をとる. あなたの仕事は,与えられた各プレイヤーのデッキに対し,各プレイヤーが最適に行動したときのプレイヤー1のスコアとプレイヤー2のスコアの差を計算することである. Input 入力は次のような形式の単一テストケースからなる. $n$ $m$ $a_1$ $a_2$ $\dots$ $a_n$ $b_1$ $b_2$ $\dots$ $b_m$ 1行目は山札の枚数を表す正の整数 $n$, $m$ ($1 \le n, m \le 50$) からなる. 2行目は $n$ 個の整数からなり,$a_i$ はプレイヤー1のデッキの上から $i$ 番目のカードを表す ($1 \le i \le n$).$a_i$ は $1$ 以上,$1{,}000{,}000$ 以下,または $-1$ である. 3行目は $m$ 個の整数からなり,$b_j$ はプレイヤー2のデッキの上から $j$ 番目のカードを表す ($1 \le j \le m$).$b_j$ は $1$ 以上,$1{,}000{,}000$ 以下,または $-1$ である. $a_i$, $b_j$ が正の整数の時は得点カードを表し,$-1$ の時は妨害カードを表す. Output お互いのプレイヤーが最適に行動した時の (プレイヤー1のスコア) - (プレイヤー2のスコア) を出力せよ. Sample Input 1 2 2 100 -1 200 300 Output for the Sample Input 1 -100 Sample Input 2 3 5 10 30 -1 -1 90 20 10 -1 Output for the Sample Input 2 0 Sample Input 3 4 5 15 20 10 30 50 30 10 20 25 Output for the Sample Input 3 -60
[ { "submission_id": "aoj_2741_10946046", "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_2737_cpp
Optimal Tournament In 21XX, an annual programming contest "Japan Algorithmist GrandPrix (JAG)" has been one of the most popular mind sport events. You, the chairperson of JAG, are preparing this year's JAG competition. JAG is conducted as a knockout tournament. This year, $N$ contestants will compete in JAG. A tournament is represented as a binary tree having $N$ leaf nodes, to which the contestants are allocated one-to-one. In each match, two contestants compete. The winner proceeds to the next round, and the loser is eliminated from the tournament. The only contestant surviving over the other contestants is the champion. The final match corresponds to the root node of the binary tree. You know the strength of each contestant, $A_1,A_2, ..., A_N$, which is represented as an integer. When two contestants compete, the one having greater strength always wins. If their strengths are the same, the winner is determined randomly. In the past JAG, some audience complained that there were too many boring one-sided games. To make JAG more attractive, you want to find a good tournament configuration. Let's define the boringness of a match and a tournament. For a match in which the $i$-th contestant and the $j$-th contestant compete, we define the boringness of the match as the difference of the strength of the two contestants, $|A_i - A_j|$. And the boringness of a tournament is defined as the sum of the boringness of all matches in the tournament. Your purpose is to minimize the boringness of the tournament. You may consider any shape of the tournament, including unbalanced ones, under the constraint that the height of the tournament must be less than or equal to $K$. The height of the tournament is defined as the maximum number of the matches on the simple path from the root node to any of the leaf nodes of the binary tree. Figure K-1 shows two possible tournaments for Sample Input 1. The height of the left one is 2, and the height of the right one is 3. Write a program that calculates the minimum boringness of the tournament. Input The input consists of a single test case with the following format. $N$ $K$ $A_1$ $A_2$ ... $A_N$ The first line of the input contains two integers $N$ ($2 \leq N \leq 1,000$) and $K$ ($1 \leq K \leq 50$). You can assume that $N \leq 2^K$. The second line contains $N$ integers $A_1, A_2, ..., A_N$. $A_i$ ($1 \leq A_i \leq 100,000$) represents the strength of the $i$-th contestant. Output Output the minimum boringness value achieved by the optimal tournament configuration. Sample Input 1 4 3 1 3 4 7 Output for the Sample Input 1 6 Sample Input 2 5 3 1 3 4 7 9 Output for the Sample Input 2 10 Sample Input 3 18 7 67 64 52 18 39 92 84 66 19 64 1 66 35 34 45 2 79 34 Output for the Sample Input 3 114
[ { "submission_id": "aoj_2737_10867807", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MAXN = 1005;\nconst int INF = 1e9;\nint n, K;\nint s[MAXN];\nint f[MAXN][MAXN][55], g[MAXN][MAXN][55];\n\nint main() {\n scanf(\"%d %d\", &n, &K);\n for (int i = 1; i <= n; i++)\n ...
aoj_2742_cpp
E - 選挙活動 Problem Statement あなたは次回の選挙の候補者であるX氏の支援者である. X氏は駅前での街頭演説を予定しており,できるだけ多くの有権者に見てもらえる場所で演説しようと考えている. 駅前は $N$ 個の障害物と $M$ 人の有権者が存在している二次元平面として与えられる. 各障害物は多角形であらわされ,その多角形の内側の領域が障害物となる.多角形の辺上は障害物に含まれない. また,有権者は平面上の点としてあらわされる. ある有権者の位置とX氏の位置を結ぶ線分上に障害物が存在しないとき,その有権者はX氏を見ることができる. あなたの仕事は,駅前の障害物と有権者の情報をもとに,もっとも多くの有権者に見てもらえる地点を探すことだ. 最大で何人の有権者から見えるように演説することができるかを求めよ. Input 入力は以下のフォーマットで与えられる. $N$ $M$ $polygon_1$ $polygon_2$ $...$ $polygon_N$ $x_1$ $y_1$ $x_2$ $y_2$ $...$ $x_M$ $y_M$ データセットの最初の行は空白文字1つで区切られた2個の整数 $N$, $M$ からなる. $N$ $(1 \le N \le 5)$ は駅前にある障害物の個数であり,$M$ $(1 \le M \le 10)$ は駅前にいる有権者の数である. 続く行から $N$ 個の障害物の情報が与えられる.1つの障害物を表す入力は以下の形式で与えられる. $L$ $x_1$ $y_1$ $x_2$ $y_2$ $...$ $x_L$ $y_L$ 各障害物情報の最初の行はその障害物をあらわす多角形に含まれる頂点の数 $L$ である. その後の $L$ 行には多角形の頂点の座標を表す整数の組が反時計回りに記されている.なお,障害物を構成する頂点数の合計は $15$ 個以下となる. $N$ 個の障害物の情報の後に続く $M$ 行には有権者のいる座標を表す整数の組が与えられる. また,各テストケースは以下の条件を満たす: $ 0 \le |x_i|, |y_i| \le 20 $ 多角形の頂点または有権者のいる場所の座標はすべて互いに異なる. 多角形の頂点または有権者のいる場所の座標のうち3点が同一直線状に存在することはない. 2つの異なる多角形同士は交差を持たない. 各多角形は自己交差を持たない. 多角形の内部に有権者が存在することはない. Output 最大で何人の有権者が演説を見られるようになるかを1行に出力せよ. Sample Input 1 1 2 4 5 5 15 5 15 15 5 15 0 10 20 10 Output for the Sample Input 1 2 Sample Input 2 1 2 6 0 0 20 0 12 9 20 20 0 20 8 11 1 10 19 10 Output for the Sample Input 2 1 Sample Input 3 4 2 3 0 0 20 0 20 1 3 0 18 19 19 19 20 3 3 2 4 2 4 17 3 16 3 17 3 17 18 2 9 18 10 Output for the Sample Input 3 1 Sample Input 4 3 3 3 0 -2 -1 -3 -1 -6 3 2 2 3 2 4 3 3 -4 4 -4 5 -5 6 0 -3 3 3 -5 5 Output for the Sample Input 4 3 Sample Input 5 2 2 4 2 0 1 2 -1 2 -2 0 4 3 3 3 4 -3 4 -3 3 2 1 -2 1 Output for the Sample Input 5 2 Sample Input 6 1 1 4 1 1 -1 1 -1 -1 1 -1 0 2 Output for the Sample Input 6 1 各入力例の障害物および有権者の配置は,それぞれ以下の図のようになっている.
[ { "submission_id": "aoj_2742_10851262", "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_2750_cpp
百人一首 百人一首は日本の伝統的なゲームである. このゲームでは N 枚のカードが用いられ,それぞれのカードには1つの文字列が書かれている.細かいルールは省略するが, A君は N 枚のカードに書かれている文字列を何らかの順序でそれぞれちょうど1回ずつ読むことになった. 百人一首ではその接頭辞が非常に重要である.百人一首で使用される文字列として接頭辞は似ている言葉が多く, 連続して似たような文字列を読むとA君も混乱してしまう. そこでA君は,連続する二つのカードの文字列の接頭辞ができるだけ異なるように,カードを並べ替えようと考えた. A君の目標は,「山札の読みやすさ」と呼ばれる指標を最小化するような山札を求めることである.ここで山札とは,この百人一首で読まれる N 枚のカードを並べ替えたものを指す.山札の読みやすさとは,山札内にある隣接したカードの類似度の総和を言う.2 枚のカードの類似度はそれらに書かれた文字列どうしの最長共通接頭辞の長さとして定義される.なお,文字列 t と u の最長共通接頭辞とは, t と u 両方の接頭辞であるような文字列のうち,最長のものを指す. 例えば,2 枚のカードに書かれた文字列がそれぞれ " jag " と " japan " であれば,これらのカードの類似度は 2 である.一方," wan " と " nyan " であれば類似度は 0 である. ところで,「山札の読みやすさ」が最小となる山札は複数存在するかもしれない.A君は,最小解としてありうる山札のうち,辞書順最小の山札を求めたい.ここで,山札 P と Q について P が辞書順で小さいとは,ある正の整数 i が存在して,1 番目から i-1 番目のカードまではそれぞれ同一のカードであり,かつ i 番目のカードの文字列は辞書順において山札 P のカードのほうが小さいことを言う. あなたの仕事は,「山札の読みやすさ」を最小にするような山札の中で,辞書順最小となるものを求めることである. Input 入力は複数のデータセットからなる. 各データセットは次の形式で表される. N s 1 ... s N 1行目にはカードの枚数を表す整数 N ( 1 ≤ N ≤ 100,000 ) が与えられる. 続く N 行にはカードに書かれた1文字以上の文字列が書かれてあり, 1行は英小文字のみで構成される.また,同一セット内において,各 s i は互いに異なることが保証されている. さらに, s i の長さの合計は 400,000 以下であることが保証されている. 入力の終わりは, 1つのゼロからなる行で示す. Output 各データセットについて,「山札の読みやすさ」が最小となる山札の中で辞書順最小の山札の情報を, N 行に出力せよ.具体的には,そのような山札の i 番目のカードに書かれた文字列を, i 行目に出力することになる. Sample Input 3 icpc icfp topcoder 4 apple application appointment acmicpc 3 a aa aaa 4 a aza azb b 0 Output for Sample Input icfp topcoder icpc apple acmicpc application appointment aa a aaa a aza b azb 辞書順の定義に注意すること.今回の定義では,文字列を連結した時に辞書順最小になるということを意味するわけではない.例えば,3番目の入力例では [aaa,a,aa] も「山札の読みやすさ」が最小となる解の一つであり,連結すれば同じ文字列になるため,辞書順最小の解に見えるかもしれない.しかしながら,今回の定義では先頭の文字列 "aa" と "aaa" が優先的に比較されるため,辞書順最小の解は [aa,a,aaa] となる.
[ { "submission_id": "aoj_2750_7136839", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\nint main(){\n int n;\n while(cin >> n && n!=0){\n vector<string> s(n);\n for(int i=0;i<n;i++)cin >> s[i];\n sort(s.begin(),s.end());\n vector<int> lcp(n-1);\n for(i...
aoj_2743_cpp
F - 土地相続 Problem Statement ある $N$ 人の兄弟は親の遺産相続の話し合いをしていた. 彼らの親の残した莫大な遺産の中には広大な土地も含まれていた. その土地は南北に $H$ km, 東西に $W$ km に広がる長方形の形をしている. この土地は 1 km 四方の区画単位で管理されており, 土地の北端から $i$ 〜 $i+1$ km かつ西端から $j$ 〜 $j+1$ km の範囲にある 1 km 四方の区画を区画 $(i, j)$ と呼ぶ. ($i$, $j$ は $0 \leq i < H$, $0 \leq j < W$ を満たす整数である.) 土地の価格は区画ごとに決まっており,区画 $(i, j)$ の価格は $a_{i, j}$ で表される. 兄弟は次のように土地を分けて相続することにした. $N$ 人の兄弟それぞれが区画をいくつか選び相続する. 各兄弟が相続する土地が 1 つの長方形を成すように区画を選ばなければならない. $N$ 人の兄弟が相続する土地は重なってはならない. 誰も相続しない区画があってもよい.誰も相続しない区画は放棄される. ある人が相続する土地の範囲に含まれる区画の価格の和をその土地の価格と呼ぶ. 兄弟は,各々が相続する土地の価格がなるべく公平になるように土地を分けたい. あなたの仕事は, $N$ 人の中で相続する土地の価格が 最も低い人の土地の価格を最大にするような土地の分け方を考えることである. そのように土地を分けた時の,相続する土地の価格が最も低い人の土地の価格を答えるプログラムを作成せよ. Input 入力は以下のような形式で与えられる. $H$ $W$ $N$ $a_{0,0}$ $a_{0,1}$ ... $a_{0,W-1}$ ... $a_{H-1,0}$ $a_{H-1,1}$ ... $a_{H-1,W-1}$ $H$, $W$ $(2 \leq H,W \leq 200)$ は遺産の土地の南北の長さと東西の長さをそれぞれ表す. $N$ $(2 \leq N \leq 4)$ は土地を相続する兄弟の人数を表す. $a_{i, j}$ $(0 \leq a_{i,j} \leq 10^4)$ は区画 $(i, j)$ の価格を表す. Output 相続する土地の価格が最も低い人の土地の価格を最大にするように分けた時の, 最も低い土地の価格を 1 行で出力せよ. Sample Input 1 3 3 2 1 2 2 3 1 0 0 4 3 Output for the Sample Input 1 7 図のように分けるのが最適である. Sample Input 2 3 3 2 0 1 0 1 1 1 0 1 0 Output for the Sample Input 2 1 Sample Input 3 2 5 3 8 3 0 5 6 2 5 2 5 2 Output for the Sample Input 3 11 Sample Input 4 3 3 4 3 3 4 3 3 4 3 3 4 Output for the Sample Input 4 7 Sample Input 5 4 4 4 2 2 2 2 2 1 2 1 2 2 2 2 2 1 2 1 Output for the Sample Input 5 7
[ { "submission_id": "aoj_2743_10493832", "code_snippet": "// AOJ 2743 Land Inheritance\n// 2025.5.16\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ull = unsigned long long;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() {\n\tint n = 0; int...
aoj_2744_cpp
G - リングと紐 Problem Statement 芸術家のみさわさんは新しい作品を作ろうとしている. 新しい作品の材料として,みさわさんは金属の輪と白黒2色の紐を大量に用意した. みさわさんは,異なる 2 つの輪をいずれかの色の紐で結ぶことを繰り返すことによって作品を作ることが出来る. 1 つの輪を複数の輪と繋ぐことも可能であるが,それぞれの輪のペアを結ぶ紐は高々 1 本である. みさわさんは,せっかく作品を作るからにはよい作品を作りたいと考えた. みさわさんの考える「よい作品」とは,以下のように定義されるものである. 1 つの輪は 1 つのよい作品である. 2 つのよい作品を用意したとき,それらに含まれている紐で繋がれていない輪のペア全てを白い紐で繋いだものは, 1 つのよい作品である.(図1. 操作1) よい作品の白い紐を黒い紐に,黒い紐を白い紐にすべて入れ替えたものはよい作品である.(図1. 操作2) 以上で定義されたもののみがよい作品である. 図 1 はよい作品の一例である. 図 1. よい作品の例 以上の条件を満たすような渾身の「よい作品」を作り上げたみさわさんは,この作品に含まれる 0 個以上の輪を選び,それらを天井から吊るして展示することにした. このとき,なるべく黒い紐が映えるように展示したい. すなわち,天井から吊るされている輪とそれ以外の輪の間を繋ぐ黒い紐の本数が最大になるようにしたい. そのように展示した時の,天井から吊るされている輪とそれ以外の輪の間を繋ぐ黒い紐の本数を求めよ. Input 入力は以下の形式で与えられる. $N$ $M$ $u_1$ $v_1$ ... $u_M$ $v_M$ $N$ $(1 \leq N \leq 5000)$ は作品に含まれる輪の数である. $M$ $(1 \leq M \leq 100000)$ は作品に含まれる黒い紐の本数である. $u_i$, $v_i$ ($1 \leq u_i, v_i \leq N$)は $i$ 番目の黒い紐が $u_i$ 番目の輪と $v_i$ 番目の輪を繋いでいることを表す. 入力で指定されなかった輪のペアに関しては白い紐で繋がれている. 入力で与えられる作品は「よい作品」であることが保証されている. Output 天井から吊るされている輪とそれ以外の輪の間を繋ぐ黒い紐の本数の最大値を求めよ. Sample Input 1 5 4 1 2 2 3 3 1 4 5 Output for the Sample Input 1 3 Sample Input 2 6 5 1 2 1 3 1 4 1 5 3 4 Output for the Sample Input 2 4 Sample Input 3 5 8 2 5 5 4 1 3 1 5 4 1 2 4 3 4 3 2 Output for the Sample Input 3 6
[ { "submission_id": "aoj_2744_9731980", "code_snippet": "#line 1 \"library/Template/template.hpp\"\n#include <bits/stdc++.h>\nusing namespace std;\n\n#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)\n#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)\n#define ALL(v) (v).begin(),...
aoj_2757_cpp
F : 卵 / Eggs 問題文 1 か月前のことである. 小学生の肉西君は夏休みの宿題をやっていなかった. そこで自由研究は家にあった卵の強度を調べることにした. この研究において,卵を高さ H から落としても割れず, 高さ H+1 から落とすと割れるとき, その卵の強度は H であると定義する. ここで H は非負整数であり,非負整数以外の高さから落とすことは無いとする. 肉西くんは卵を 1 つ落下させる実験を行う. 実験の結果は割れるか割れないかのいずれかである. また,卵の強度は全て同じである.つまり,どの卵を用いても実験の結果は同じである. 肉西くんは高さ 1 から N までの整数の高さの段からなる階段と, 強度が不明な E 個の卵を用意した. 高さ 0 では割れず,高さ N+1 では割れるということは既にわかっている. 肉西くんは各段と同じ高さから地面に向かって落とし,その度に卵が割れたか割れなかったかを調べる. このとき割れた卵は二度と使えないが,割れなかった場合は再利用できる. この実験を卵が残っている限り続けることができる. 何度か実験を繰り返し,上に定めた H が求まったとき,卵の強度が求まったとする. 夏休み終了まで後数日しか無い. 最小の回数で実験を終わらせないと間に合わない. そこで,肉西くんの兄であるあなたは,卵の強度を知るために 落とす回数が少なくなるように最適な方法をとった場合に 必要な実験回数の最大値を求めるプログラムを書くことにした. 入力 T N_1 E_1 … N_T E_T 1 つのファイルに複数のテストケースが含まれる. 1 行目に整数 T が与えられる. 1+i 行目に i 番目のテストケース E_i, N_i が与えられる 制約 整数である 1 ≤ T ≤ 1000 1 ≤ N_i ≤ 10^{18} 1 ≤ E_i ≤ 50 出力が 50 を超えるような入力は含まれない 出力 i 番目のテストケースに対する答えを i 行目に出力せよ. 全体で T 行にわたる. サンプル サンプル入力1 3 5 1 5 2 1 2 サンプル出力1 5 3 1 1 つ目の場合 卵が 1 つしかないため 1 段目から順に落としていくしかない 2 つ目の場合 まず 2 段目から落とす 2 段目から落として割れた場合 1 段目から落とす 2 段目から落として割れなかった場合 4 段目から落とす 1 段目から落として割れた場合実験終了 1 段目から落として割れなかった場合実験終了 4 段目から落として割れた場合 3 段目から落とす 4 段目から落として割れなかった場合 5 段目から落とす 3 段目から落として割れた場合実験終了 3 段目から落として割れなかった場合実験終了 5 段目から落として割れた場合実験終了 5 段目から落として割れなかった場合実験終了 3 つ目の場合 1 段目から落として実験終了
[ { "submission_id": "aoj_2757_3355114", "code_snippet": "#include <iostream>\n#include <algorithm>\nusing namespace std;\n\nlong long Q, dp[60][200009], A[2000009];\n\nvoid init() {\n\tfor (int i = 1; i <= 2000000; i++) {\n\t\tlong long V1 = A[i - 1] + 1;\n\t\tlong long V2 = 1LL * i * (i - 1) / 2LL + 1;\n\t\...
aoj_2748_cpp
夏合宿の朝は早い JAG夏合宿の朝は早い. 正確にはそこまで早くないが,早いと感じる参加者が多いという. 例年合宿の会場となる施設では,退去時に参加者がシーツの回収や掃除をしなければならない. 1 つの部屋でも退去が遅れると,来年以降の施設の利用に関わるため,参加者は一人たりとも寝坊してはならない. そうは言っても皆人間,寝坊することもある. しかし,起きた人が連絡先を知っている人にモーニングコールをすることで,一人も寝坊しないよう努めることができるはずだ. JAG夏合宿の運営を任されたあなたは,寝坊を絶対に防ぐ手を打つための前準備として,どのくらいの確率で全員がきちんと起きられるのかを調べることにした. 準備としてまず,各参加者の寝坊する確率と,各々が連絡先を知っている人のリストを入手した. ここで,部屋は個室であるため,各々が寝坊するか否かは,他の参加者が寝坊するか否かとは独立である. 起きた人は必ずすべての知っている連絡先にモーニングコールをすること,また,モーニングコールを受けた人は必ず起きることを仮定するとき,全員がきちんと起きられる確率をこれらの情報から計算せよ. Input 入力は複数のデータセットからなる. 各データセットは次の形式で表される. N p 1 m 1 a (1,1) ... a (1, m 1 ) ... p N m N a (N,1) ... a (N, m N ) N は参加者の数であり,100 を越えない正の整数である. p i は i 番目の参加者の寝坊する確率であり,小数点以下 2 桁以内の 0 以上 1 以下の実数である. m i は i 番目の参加者が知っている連絡先の数であり,0 以上 N 以下の整数である. a (i, j) は i 番目の参加者が知っている j 番目の連絡先が a (i, j) 番目の参加者のものであることを表す. a (i, j) は N を越えない正の整数である. 入力の終わりは,1 つのゼロからなる行で示す. Output 各データセットについて,全員が起床できる確率を 1 行に出力せよ.出力には 0.00001 以上の誤差を含んではならない. Sample Input 2 0.60 1 2 0.60 0 2 0.60 1 2 0.60 1 1 5 0.10 1 2 0.20 1 3 0.30 1 4 0.40 1 5 0.50 1 1 5 0.10 0 0.20 1 1 0.30 1 1 0.40 1 1 0.50 1 1 5 0.10 4 2 3 4 5 0.20 0 0.30 0 0.40 0 0.50 0 4 0.10 1 2 0.20 0 0.30 1 4 0.40 1 3 5 0.10 0 0.20 0 0.30 0 0.40 0 0.50 0 0 Output for Sample Input 0.400000000 0.640000000 0.998800000 0.168000000 0.900000000 0.792000000 0.151200000
[ { "submission_id": "aoj_2748_10648019", "code_snippet": "#pragma GCC target(\"avx2\")\n#pragma GCC optimize(\"O3\")\n#pragma GCC optimize(\"unroll-loops\")\n\n// #ifndef ONLINE_JUDGE\n// #define _GLIBCXX_DEBUG // 配列外参照のエラー\n// #endif\n\n// スタンダードライブラリ\n#include <bits/stdc++.h>\nusing namespace std;\n\n// 型関...
aoj_2747_cpp
カーテン もうすぐ夏がやってくる.あなたは夏に向けて部屋の模様替えをすることにした.今年の夏はとても日差しが強いことが予想されており,まぶしいのが苦手なあなたには辛い季節になりそうだ.そこで,あなたは部屋の明るさを調節するために,部屋の窓にカーテンを取り付けようと考えた. 取り付けるカーテンは長方形であり,辺が地面に対して垂直,もしくは平行であるように取り付ける.また,あなたの部屋の窓は非常に特殊な形をしており,各辺が地面に平行または垂直であるような N 角形で表される.そのため,カーテンを取り付けたときにカーテンに覆われていない窓の面積がどのくらいになるのかを求めるのは難しい.部屋の明るさを調節するためにも,カーテンを取り付ける位置を決めた時にどのくらい窓が覆えるかを知ることは重要である.そこで,あなたは窓とカーテンの設置位置と形状が与えられたときに,カーテンに覆われていない窓の面積を求めるプログラムを作ることにした. 例として次のような窓とカーテンの設置の仕方を考える.この場合はカーテンに隠れていない窓の面積は 8 となる.この例はサンプル入力の 3 ケース目に対応する. Input 入力は複数データセットからなる. 各データセットは次の形式で与えられる. N x 1 y 1 : : x N y N a 1 b 1 a 2 b 2 a 3 b 3 a 4 b 4 最初の行には窓の持つ頂点数を表す整数 N が与えられる ( 4 ≤ N ≤ 100 ).続く N 行には窓の相異なる頂点のx座標を表す整数 x i とy座標を表す整数 y i が与えられる ( -20,000 ≤ x i , y i ≤ 20,000, 1 ≤ i ≤ N ).ここで,y軸の正の向きはx軸の正の向きから90度分反時計回りに回転させた方向とする.さらに,続く 4 行にはカーテンの相異なる頂点のx座標を表す整数 a j と y座標を表す整数 b j が与えられる ( -20,000 ≤ a j , b j ≤ 20,000, 1 ≤ j ≤ 4 ).窓,カーテンともに,それぞれの頂点は反時計回り順に与えられる.また,窓,カーテンを表す図形はそれぞれ自己交差がない図形である. 入力の終わりは1つの 0 からなる行で示す. Output 各データセットについて,カーテンに覆われていない窓の面積を 1 行に出力せよ.ただし,面積は必ず整数値になることに注意せよ. Sample Input 4 0 0 10 0 10 10 0 10 0 0 5 0 5 10 0 10 6 0 0 10 0 10 5 5 5 5 10 0 10 2 0 8 0 8 10 2 10 12 1 1 1 3 -1 3 -1 1 -3 1 -3 -1 -1 -1 -1 -3 1 -3 1 -1 3 -1 3 1 2 2 -2 2 -2 -2 2 -2 4 20000 20000 -20000 20000 -20000 -20000 20000 -20000 1000 1000 -1000 1000 -1000 -1000 1000 -1000 4 1000 1000 -1000 1000 -1000 -1000 1000 -1000 20000 20000 -20000 20000 -20000 -20000 20000 -20000 4 0 0 10 0 10 10 0 10 20 0 30 0 30 10 20 10 0 Output for Sample Input 50 30 8 1596000000 0 100
[ { "submission_id": "aoj_2747_10683663", "code_snippet": "#include <bits/stdc++.h>\n#include <unordered_map>\n#include <stdlib.h>\nusing namespace std;\n#define rep(i, a, n) for(ll i = a; i < n; i++)\n#define rrep(i, a, n) for(ll i = a; i >= n; i--)\n#define ll long long\n#define pii pair<int, int>\n#define ...
aoj_2749_cpp
ぼくのかんがえたさいきょうのおふとん あなたは新生活に備え,おふとんを N 枚買った. i 番目のおふとんは s i のぬくもり供給力をもつ. これから M 日間の気温の予測から, j 日目には d j のぬくもり需要が予想される. ぬくもりが足りなくても多すぎても快適さが損なわれるので, j 日目に掛けているおふとんのぬくもり供給力の総和と d j の差の絶対値を j 日目の不快度と呼ぶことにする. この M 日分の不快度の合計ができるだけ少なくなるようにしたい. ところで,あなたの部屋は残念ながらとても狭く,ベッドと押入れしかない. そのため,ベッドにおふとんを 1 枚増やすには,そのとき押入れの一番上にあるおふとんをベッドの一番上に乗せるしかない. 逆に,ベッドのおふとんを 1 枚減らすには,そのときベッドの一番上にあるおふとんを押入れの一番上に置くしかない. また,1 日に動かせるおふとんの枚数に制限は無いが,一度に 1 枚ずつしか動かすことができない. さて,あなたはこれから買ってきたおふとんを押入れにしまう予定である. このときに限り,おふとんを好きな順序で押入れにしまうことができる. どのようにおふとんを押入れに収納し,その後,日々どのようにおふとんを出し入れすれば,快適に毎日を過ごせるだろうか. M 日間の不快度の和を最小化するとき,その和の値を求めよ. なお,一度も使われないおふとんがあってもよく,おふとんを一枚も使わない日があってもよい. Input 入力は複数のデータセットからなる. 各データセットは以下の形式である. N M s 1 s 2 ... s N d 1 d 2 ... d M データセットの 1 行目には,おふとんの枚数 N と,気温が予測されている日数 M を表す整数がスペース区切りで与えられる. 2 行目には N 個の整数 s 1 , s 2 , ..., s N がスペース区切りで与えられ, s i は i 番のおふとんのぬくもり供給力を表す. 3 行目には M 個の整数 d 1 , d 2 , ..., d M がスペース区切りで与えられ, d j は j 日目のぬくもり需要を表す. これらの整数は, 1 ≤ N ≤ 15 , 1≤ M ≤ 100 , 1 ≤ s i , d j ≤ 1,000,000 を満たす. 入力の終わりは N = M = 0 のデータセットで表される. このデータセットについて出力を行ってはならない. Output 各データセットについて, M 日間の不快度の和の最小値を 1 行に出力せよ. Sample Input 1 1 5 6 1 1 5 2 1 1 20 5 4 1 2 4 5 9 8 4 3 3 5 2 1 10 4 7 5 5 2 2 2 2 2 1 3 5 7 9 2 5 2 5 2 5 2 5 2 0 0 Output for Sample Input 1 2 5 1 1 5 4 5 つ目のケースについては,上から 5, 2, 3, 1 と押入れに置き,1 日目は 3 枚取り出し |10 - (5 + 2 + 3)| = 0,2 日目は 2 枚しまい |4 - 5| = 1,3 日目は 1 枚取り出し |7 - (5+2)| = 0 で,合計 0 + 1 + 0 = 1 となる.
[ { "submission_id": "aoj_2749_10848993", "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_2751_cpp
野球観戦 先日,あなたの競技プログラミング仲間であるOさんは野球観戦に出かけた. 観戦した試合は,チームXとチームYの合計 4 試合の対戦だったが,一方的な展開となり,全試合でチームXが勝利した. しかも,4 試合を通じたXの合計得点は 33 点だったのに対し,Yの合計得点はたったの 4 点だった. あまりに一方的な内容のため,試合内容への興味が薄れてしまったOさんは,試合を観戦している間も競技プログラミングの作問ネタをついつい考えてしまっていた. その甲斐もあって,Oさんは以下のような問題を思いついた. 野球チームXとYが対戦し,Xが勝った試合数,Yが勝った試合数,引き分けの試合数がそれぞれ A, B, C 試合だったとする. また,全 A+B+C 試合を通じたX,Yの総得点は,それぞれ S X 点, S Y 点だったとする.得点は全て 0 以上の整数である. XとYが合計 A+B+C 試合対戦したとき,全試合の結果としてこのような条件を満たす各試合のスコアの並びは何通り有り得るだろうか? ここで,ある試合で勝利する条件は,その試合における得点が,相手チームの得点よりも多いことであり,等しい場合は引き分けとなる. また,各試合のスコアの並びを求める際,対戦した全試合の結果を比べた時,XとYの得点の組み合わせが同じでも,その順序が異なれば区別する必要がある. 例えば,XとYが 2 試合対戦した結果,共に 1 勝ずつし,引き分けが無く,XとYの総得点が共に 1 点ずつだったとする. この場合,各試合におけるX,Yの得点を (Xの得点) - (Yの得点) という表記で表し,合計 2 試合の結果を並べると,以下の 2 通りが与えられた条件を満たす. 1 - 0, 0 - 1 0 - 1, 1 - 0 試合の順序を区別して数えるので,これらは区別される. あなたにはこの答えを求めるプログラムを作って欲しい. ただし,求める数はとても大きい数になり得るため,求める数を 1,000,000,007 で割った余りを答えるようにして欲しい. Input 入力は複数のデータセットからなる. 各データセットは 1 行からなり,次の形式で表される. A B C S X S Y ここで, A はチームXの勝利数, B はチームYの勝利数, C は引き分けの試合数, S X はチームXの総得点, S Y はチームYの総得点を表す. A, B, C, S X , S Y は全て 0 以上 1,000,000 以下の整数であり,かつ 0 < A+B+C を満たす. 入力の終わりは,5 つのゼロからなる行で示される. Output 各データセットについて,与えられた条件を満たす場合の数を 1,000,000,007 で割った余りのみからなる行を 1 行で出力せよ. Sample Input 1 1 0 1 1 0 0 2 3 2 4 0 0 33 4 5 4 2 20 25 4726 87361 2742 23497 162843 328324 420923 12782 834286 538297 0 0 0 0 0 Output for Sample Input 2 0 114660 512095631 673703234 166259450
[ { "submission_id": "aoj_2751_10855496", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\nll mod = 1000000007;\n\nll kai[3000010];\nll inv_kai[3000010];\n\nll beki(ll x, ll a)\n{\n\tif(x==0) return 0;\n\tif(a==0) return 1;\n\tif(a==1) return x;\n\tll tmp=beki(x, a/2...
aoj_2754_cpp
C : 壺 / Pots 問題文 ここに N 個の不思議な形の壺がある. i 番目の壺は K_i 個の直円柱を下から順に鉛直に繋げた形状である. 繋がっている順番は変えることができない. A 氏は体積 M の水を持っている. この水をそれぞれの壺に好きな量ずつに分けて注ぐ. 水が全く入っていない壺が存在しても構わない. また,全ての壺が水で満たされたとき,それ以上水を注ぐ事はできない.それぞれの壺の水面の高さの総和の最大値を求めよ. 入力 N \ M K_1 \ S_{11} \ H_{11} \ … \ S_{1 K_1} \ H_{1 K_1} K_2 \ S_{21} \ H_{21} \ … \ S_{2 K_2} \ H_{2 K_2} … K_N \ S_{N1} \ H_{N1} \ … \ S_{N K_N} \ H_{N K_N} 1 行目に N, M が, 1+i 行目には i 番目の壺の情報が入力される. K_i は直円柱の数であり, S_{ij}, H_{ij} はそれぞれ壺を構成する下から j 番目の直円柱の底面積と高さである. 制約 整数である 1 ≤ N ≤ 200 1 ≤ M ≤ 200 1 ≤ K_i ≤ 20 1 ≤ S_{ij} ≤ 20 1 ≤ H_{ij} ≤ 20 出力 答えを 1 行で出力せよ. 0.00001 以下の絶対誤差を含んでも良い. サンプル サンプル入力1 2 15 2 3 3 7 2 2 7 1 1 4 サンプル出力1 6.33333333 サンプル入力2 2 14 1 2 4 2 5 2 1 4 サンプル出力2 6 サンプル 1, 2 の入出力を図示すると次のようになる. サンプル入力3 2 25 4 8 9 1 9 6 5 2 8 4 1 7 4 4 1 6 4 3 サンプル出力3 13
[ { "submission_id": "aoj_2754_10850464", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define FOR(i,s,t )for(int i = s; i <t ; i++)\nusing LL = long long; using ll = LL;\nusing PLL = pair<LL, LL>; using pll = PLL;\nusing PII = pair<int, int>; using pii = PII;\n#define SZ(a) (int)a.size...
aoj_2761_cpp
D: みゃんぱすーれつ - Myampus Sequence - 物語 みゃんぱすー. ウチ,田舎の分校に通う小学一年生なん. あんなー,今日の授業はプログラミングだったん. みんな苦戦してたけど,まっつんのにぃにぃ,凄い勢いでタイピングしてたん. さすが中三なんなー. にぃにぃ,プログラムが完成してどこか行ってしまったん. そんで,画面見たら,色んな数列が出力されてたん. だんだん出力と同じような数列を書きたくなったん,ウチ,出力テキストに同じような数列をいくつも書き加えてたん. そしたら,にぃにぃが戻ってきて,すごい怒られたん. ウチ,にぃにぃの声初めて聞いたから,びっくりしたん. でな,にぃにぃに謝ってプログラムを見せてもらったん. プログラムが出力してたん,「みゃんぱすーれつ」いうん. 数列がみゃんぱすーれつか調べて,にぃにぃの機嫌を直したいん. でも,ウチ,プログラミング初めてなん,教えてほしいのん. 問題 N 個の整数からなる数列と M 個の関数からなるプログラムが与えられる.プログラムが開始されると 1 番目の関数を呼び出し,この関数の処理が終了すると,プログラムは終了する.また, i ( 1 ≤ i ≤ M ) 番目の関数は,次のどちらか一方の処理を行い,関数の処理を終了する. 整数 a_i を出力する. b_i ( 1 ≤ b_i ≤ M ) 番目の関数を呼び出し,呼び出した関数の処理が終了したら, c_i ( 1 ≤ c_i ≤ M ) 番目の関数を呼び出し,呼び出した関数の処理の終了を待つ. どちらの処理を行うかは,処理の度にランダムに決定される.つまり,プログラムが開始されると1個の数列を出力して終了する.ここで,プログラムが出力する数列としてあり得るものを「みゃんぱすーれつ」と定義する. 与えられた数列がみゃんぱすーれつか判定せよ. 入力形式 入力は次の形式で与えられる. N x_1 ... x_N M a_1 b_1 c_1 ... a_M b_M c_M 1行目に,判定すべき数列の長さ N が与えられる. 2行目に,判定すべき整数列 x_1 , x_2 , ..., x_N が順に与えられる. 3行目に,関数の個数 M が与えられる. i + 3 ( 1 ≤ i ≤ M ) 行目に, i 番目の関数の情報を表す a_i , b_i , c_i が順に与えられる. 入力は次の制約を満たす. 1 ≤ N ≤ 100 i = 1, ... , N に対して, x_i は 0 ≤ x_i ≤ 9 を満たす整数 1 ≤ M ≤ 10 i = 1, ... , M に対して, a_i は 0 ≤ a_i ≤ 9 を満たす整数 i = 1, ... , M に対して, b_i は 1 ≤ b_i ≤ M を満たす整数 i = 1, ... , M に対して, c_i は 1 ≤ c_i ≤ M を満たす整数 出力形式 与えれた数列がみゃんぱすーれつの場合には “Yes” と出力し,数列がみゃんぱすーれつではない場合には “No” と出力せよ.また,出力の最後に改行せよ. 入力例1 3 3 5 3 3 5 2 3 3 3 1 7 1 2 出力例1 Yes 次のようにプログラムが動作すると,与えられた数列を生成します. プログラムが関数 1 を呼び出す. 関数 1 が関数 2 と関数 3 を順に呼び出す. 関数 2 が 3 を出力する. 関数 3 が関数 1 と関数 2 を順に呼び出す. 関数 1 が 5 を出力する. 関数 2 が 3 を出力する. 入力例2 10 9 9 9 9 9 9 9 9 9 9 4 6 2 3 7 3 1 8 1 2 9 2 1 出力例2 No どのような処理を行っても,関数 4 を呼び出すことがないので, 9 を出力することができません.そのため, 9 を含む数列を出力することはできません. 入力例3 2 2 4 4 1 2 3 2 1 1 3 4 1 4 1 1 出力例3 No このプログラムは, 2, 4, 1 のように, 2, 4 を含む数列を生成することがありますが, 2, 4 自体を生成することはありません.
[ { "submission_id": "aoj_2761_3000665", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nint N, M;\nint x[110], a[15], b[15], c[15];\nint dp[15][110][110];\n\nint solve(int idx, int l, int r) {\n assert(r - l > 0);\n\n int val = dp[idx][l][r];\n if(val >= 0) return val;\n\n val = 0;\n ...
aoj_2756_cpp
E : 台風 / Typhoon 問題文 南北方向に H ,東西方向に W の大きさの町がある. 町には一辺の長さが 1 の正方形の区画に隙間なく整備されており,全ての区画に 1 軒ずつ家が建っている. この町のある区画の上空で台風が発生し,被害を与えた後,ある区画の上空で温帯低気圧に変化した. 変化した後は被害を与えない. 下図のように,台風は高さ 3 , 幅 3 の正方形であり, ★のついたマスを中心と呼ぶことにする.台風は 8 近傍に区画単位で移動する. つまり,台風の中心は辺または頂点を共有する区画(現在の区画を含む)に移るように,全体を伴って移動する. ただし,町の外に台風がはみ出ることはなく, 台風の中心は,下の図の網掛けのように,北から 0 番目と H − 1 番目,西から 0 番目と W − 1 番目の区間は通らないように移動する. 家は台風が一度上空に来ると,以下のように被害の程度が変化する. 損壊ナシ → 一部損壊 → 半壊 → 全壊 → 跡形ナシ だが,幸い跡形ナシとなった家は無かったようだ. 各家の被害の状況が与えられるので,台風が発生した地点と,温帯低気圧に変化した地点を求めよ. ただし,発生した区画を北から s_i 番目,西から s_j 番目, 温帯低気圧に変化した区画を北から t_i 番目,西から t_j 番目とすると, 2 つの地点は 10000 t_i + t_j ≤ 10000 s_i + s_j を満たすように定まる. 入力 H \ W D_{11} \ … \ D_{1W} D_{21} \ … \ D_{2W} … D_{H1} \ … \ D_{HW} D_{ij} は北から i 番目, 西から j 番目の家の被害の程度を以下のように表す整数である. 0 : 損壊ナシ 1 : 一部損壊 2 : 半壊 3 : 全壊 制約 整数である 入力は答えが一意に定まるようなもののみ与えられる 3 ≤ H,W ≤ 500 0 ≤ D_{ij} ≤ 3 出力 答えを以下のように 1 行で出力せよ. s_i \ s_j \ t_i \ t_j サンプル サンプル入力1 7 5 0 0 0 0 0 0 1 1 1 0 0 2 2 2 0 0 3 3 3 0 0 2 2 2 0 0 1 1 1 0 0 0 0 0 0 サンプル出力1 4 2 2 2 サンプル入力2 6 6 0 0 0 1 1 1 0 0 0 2 2 2 0 0 1 3 3 2 1 2 3 3 2 1 1 2 3 2 1 0 1 2 2 1 0 0 サンプル出力2 4 1 1 4 サンプル入力3 4 4 2 2 2 0 2 2 2 0 2 2 2 0 0 0 0 0 サンプル出力3 1 1 1 1
[ { "submission_id": "aoj_2756_10853236", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define FOR(i,s,t )for(int i = s; i <t ; i++)\nusing LL = long long; using ll = LL;\nusing PLL = pair<LL, LL>; using pll = PLL;\nusing PII = pair<int, int>; using pii = PII;\n#define SZ(a) (int)a.size...
aoj_2765_cpp
A: 秤 / Steelyard 問題文 情太くんは長さ $2L$ の棒を使って下の図のような秤を作った. 棒には等間隔に $2L+1$ 個の穴があけられ,左から順に $-L, -L+1, \cdots, -1, 0, 1, \cdots, L-1, L$ という番号が付けられている.そして $0$ 番の穴の場所を天井から紐で吊るしてある. 情太くんは,秤の穴に $N$ 個のおもりを吊るした.$i$ 番目のおもりを吊るした穴の番号は $x_i$ で,おもりの重さは $w_i$ である.おもりが $1$ つも吊るされない穴や,複数のおもりが吊るされる穴も存在しうる. 情太くんが吊るしたおもりによっては,秤が傾いているかもしれない.姉の立子さんは,追加でいくつかの重りを吊るすことで秤を水平にしたい (おもりの座標と重さ積の総和が $0$ になるとき秤は水平になる).条件を満たすおもりの吊るし方を $1$ つ出力しなさい.候補が複数ある場合はどれを出力してもよい. 入力 $L$ $N$ $x_1 \ w_1$ $\vdots$ $x_N \ w_N$ 入力の制約 $1 \leq L \leq 30$ $1 \leq N \leq 30$ $|x_i| \leq L$ $1 \leq w_i \leq 30$ 全て整数 出力 答えは次のような形式で出力せよ. $1$ 行目の $N’$ は立子さんが吊るしたおもりの数である. $1+i$ 行目の $x_i’, w_i’$ は,それぞれ立子さんが吊るしたおもりの位置と重さである. $N'$ $x_1' \ w_1'$ $\vdots$ $x_N' \ w_N'$ 出力の制約 立子さんが追加で吊り下げるおもりは,以下の条件を満たす必要がある. $0 \leq N' \leq 50000$ $|x_i'| \leq L$ $1 \leq w_i' \leq 50000$ 全て整数 サンプル サンプル入力1 3 3 1 1 2 1 3 1 サンプル出力1 1 -3 2 他にも,例えば以下のような出力も正解として扱われる. 2 -3 1 -3 1 これらを図示すると次のようになる. サンプル入力2 3 3 1 1 0 2 -1 3 サンプル出力2 1 2 1 サンプル入力3 10 4 1 2 2 3 -7 1 -1 1 サンプル出力3 0 秤は最初から釣り合っていることもある.
[ { "submission_id": "aoj_2765_4968352", "code_snippet": "#include <iostream>\n#include <vector>\nusing namespace std;\n\n\nint main(void){\n long long L,N;\n long long weight=0;\n\n cin >> L >> N;\n for(int i=0;i<N;i++){\n long long x,w;\n cin >> x >> w;\n weight += x*w;\n ...
aoj_2760_cpp
C: 買い出し - Shopping - 物語 磯野の姉であるサゾエさんは,『あれ』や『あれ』で遊んでいる磯野と中島のために夕食を作ってあげることにしました.あいにく,冷蔵庫には食材が残り僅かしか無く,サゾエさんは買い出しに行くことにしました.幾つか食材を買おうとしているサゾエさんですが,大変陽気であるため一度に一品のみレジで会計してしまいます.このままでは遊んでいた磯野と中島が買い物を終える前に帰ってきてしまい,夕食を準備することが出来ないかもしれません.そこで,サゾエさんの友人であるあなたは,サゾエさんのためにサゾエさんの買い物にかかる最小の時間を求めてください. 問題 とあるスーパーに N 個のレジがあり,それぞれ 1 〜 N までの番号がついている. また,客が M 人居て,それぞれ 1 〜 M までの番号がついている. i 番目の客は,時刻 a_i に c_i 番目のレジに並び,会計に b_i の時間がかかる.1人の客は一度だけ会計をする. ここで,ある客が並んだレジに既に人が並んでいる場合,その客は既に並んでいる人全員の会計が終了した後に会計をする. i 番目の客と, i+1 番目の客が同じレジに並んでいたとすると, i 番目の客の会計開始時刻が t であった場合, i 番目の客の会計終了時刻は t+b_i , i+1 番目の客の会計開始時刻は t+b_i ,そして, i+1 番目の客の会計終了時刻は, t+b_i+b_{i+1} となる. サゾエさんは1人の客である.しかし,サゾエさんは例外的に K 回会計をする.また,会計にかかる時間は0,すなわちサゾエさんの会計開始時刻を t とすると,会計終了時刻は t となり,次の客の会計開始時刻は t となる.その後,サゾエさんが再度レジに並ぶ際,会計終了時刻 t から品定めにかかる時間 D だけ経ってから並ばなければいけない.つまり,再度レジに並ぶことができる時刻は t+D となる. また,サゾエさんは時刻 S にスーパーにやって来る.そして,時刻 S から D だけ経った時刻 S+D に,初めてサゾエさんはレジに並ぶことが出来る. N , M , K , D , S と, 1 番目〜 M 番目までの客の情報がそれぞれ与えられるので,サゾエさんがスーパーに来てから最後の会計を終えるまでにかかる時間の最小値を求めよ.この時,異なる2人の客が同じ時刻に同じレジに並ぶことは無いとし,サゾエさんとほかの人が同時にレジに並ぼうとした場合,サゾエさんは常にほかの客に順番を譲ることとする. 入力形式 入力は次の形式で与えられる. N M K D S a_1 b_1 c_1 ... a_M b_M c_M 1行目にスーパーにあるレジの数 N ( 1 ≤ N ≤ 10^{15} ),訪れる客の数 M ( 1 ≤ M ≤ 10^5 ),サゾエさんがレジを回る回数 K ( 1 ≤ K ≤ 10^4 ),サゾエさんの品定めにかかる時間 D ( 1 ≤ D ≤ 10^4 ),サゾエさんがスーパーに来る時刻 S ( 1 ≤ S ≤ 10^4 )が空白区切りで与えられる. 続く M 行に訪れる客の情報が与えられる. M 行のうち, i ( 1 ≤ i ≤ M )行目には, i 番目の客がレジに来る時刻 a_i ( 1 ≤ a_i ≤ 10^4 ),会計にかかる時間 b_i ( 1 ≤ b_i ≤ 10^4 ),やって来るレジの番号 c_i ( 1 ≤ c_i ≤ N )が空白区切りで与えられる.ここで 2 ≤ M の時,全ての i ( 1 ≤ i ≤ M−1 )について, a_i ≤ a_{i+1} が成立する. なお,入力が非常に大きくなる場合があるため,入力の受け取りには高速な関数を用いることを推奨する. 出力形式 サゾエさんがスーパーに来てから最後の会計を終えるまでにかかる時間の最小値を1行で出力せよ. 入力例1 3 9 3 2 3 1 2 3 1 1 2 2 3 1 3 4 2 4 1 3 4 1 1 5 1 1 6 2 3 7 2 2 出力例1 6 時間5にレジ3,時間7にレジ1,時間9にレジ2に並ぶ. サゾエさんの入店時間が時間3なので初めてレジに並んでから時間6で3回の会計が終了する. また,時間9に並ぶレジはレジ1,もしくはレジ3でも最適解になる 入力例2 3 9 3 1 3 1 2 3 1 1 2 2 3 1 3 4 2 4 1 3 4 1 1 5 1 1 6 2 3 7 2 2 出力例2 5 入力例3 1 3 3 2 1 1 1 1 2 2 1 3 2 1 出力例3 9
[ { "submission_id": "aoj_2760_10401979", "code_snippet": "// AOJ #2760 Shopping\n// 2025.4.21\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\n#define gc() getchar_unlocked()\n\nint Cin() {\n\tint n = 0, c = gc();\n\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\n\treturn n;...
aoj_2771_cpp
G: 塗るだけ / Paint 問題文 情太くんと立子さんは, 平面の世界にある庭付きの家に住んでいる.二人は点とみなせ, 庭は $N$ 頂点の単純多角形 (隣り合わないどの $2$ 辺も交差も接触もしない多角形) の形をしている. ある日,二人は一本の伸び縮みするローラーを手に入れた. ローラーは線分とみなせ,ローラーが通過した領域に色を塗ることができる. 二人はローラーを使って庭全体に色を塗りたいと思っている. 作業の前に,二人は以下のような準備を上から順に行う. 庭の内部の任意の $1$ 箇所に杭を打つ. 庭の外部のある $1$ 点に集まる. 十分に長い紐を用意し,片方の端を情太くん,もう一方の端を立子さんの体に結ぶ. 情太くんがローラーの片方の端を,立子さんがもう一方の端を持つ. 準備ができたら,以下のルールに従って塗り始める. ただし,ローラーは杭の上空を通過させることができるが,紐はできない. 庭のどの部分から塗り始めてもよい. 庭の外部または周上を移動できるが,内部に入ってはいけない. 塗り終わるまでローラーから手を離してはいけない. さらに,塗り終わったときに以下の条件を満たしていなければならない. 二人が再びある $1$ 点に集まっている. 庭の内部の任意の点の上をローラーが通過している. 二人の体の紐を解き,両端を結んで輪を作る.この輪を外から引っ張ったときに,杭に引っかかって抜けない. さて,二人はできるだけ離れたくないので,塗っている最中にとる二人の距離の最大値を最小化したい. 二人に代わってそのような値を求めてほしい. 入力 入力は以下の形式で与えられる.$N$ は多角形を構成する頂点の数, $(x_i,y_i)$ はある頂点から始めて時計回りまたは反時計回りに見たときの $i$ 番目の点の座標である. $N$ $x_1 \ y_1$ $\vdots$ $x_N \ y_N$ 制約 $3 \leq N \leq 50$ $|x_i|, |y_i| \leq 100$ 全て整数である 頂点は時計回りまたは反時計回りに与えられる 庭の形は単純多角形である 周上の連続する $3$ 点は一直線上に存在しない 出力 答えを $1$ 行で出力せよ.$10^{-5}$ 未満の絶対誤差は許容される. サンプル 各サンプルでの庭の形と塗り終わるまでのローラーの動きを図示すると以下のようになる.整数は時刻を表す.図示する都合上ローラーの動きは飛び飛びに示したが,実際には連続的に動いている. ローラーが太くなっている部分で距離の最大値を達成している. サンプル入力1 4 0 0 0 1 1 1 1 0 サンプル出力1 1 サンプル入力2 3 0 0 0 1 1 0 サンプル出力2 0.7071067811865476 サンプル入力3 8 0 0 5 0 5 3 0 3 0 2 4 2 4 1 0 1 サンプル出力3 1.4142135623730951
[ { "submission_id": "aoj_2771_4964531", "code_snippet": "#include <cmath>\n#include <cstdio>\n#include <cstdlib>\n#include <cstring>\n\n#include <algorithm>\n#include <bitset>\n#include <complex>\n#include <iostream>\n#include <map>\n#include <numeric>\n#include <queue>\n#include <set>\n#include <stack>\n#in...
aoj_2764_cpp
G: 旅費支給 - Travel Support - 物語 私,香坂穂乃果16歳!学生アイドルをやっています!! 学生アイドルの良さをみんなに伝えるためのライブをやろうと,たくさんの学生アイドルの人たちを呼んでアキバスタジアムでライブをします. だけど,遠くから来るライブ参加者もいてとっても電車賃がかかるの.だからu’sのメンバーでバイトをして,ライブ参加者たちに電車賃を渡すことにしたんだ!ただ,バイト代が入るのが遅いから電車賃を渡すのがライブ参加者の出発に間に合わないことがあったり,バイト代が足りなくて電車賃の全額は渡せないことがあるんだ.そんな時は,ライブ参加者に足りない分のお金を事前に用意してもらうのだけど,どのくらい用意して貰えばいいんだろう? そこで,あなたにライブ参加者が用意する必要のある最小の金額を求めて欲しいんだ! おねがい真衣ちゃん,電車賃貸して? 問題 N 個の都市があり,それぞれ 1 〜 N までの番号がつけられている. i ( 1 ≤ i ≤ N )番目の都市は人口が t_i ( i ≠ j ならば t_i ≠ t_j )である. 現在,都市1に K 人のライブ参加者が集まろうとしている.許された交通手段が M 通りあり, i 番目の交通手段では都市 a_i と都市 b_i ( 1 ≤ a_i, b_i ≤ N )を c_i 円の費用で双方向に移動ができる.移動にはどの交通手段でも1日を必要とする. i ( 1 ≤ i ≤ K )番目の参加者は,都市 x_i を出発し必要な費用が最小になるような経路で都市1に向かう.費用が最小な経路が複数ある場合は,移動日数が少ない経路を選ぶ.それでも複数の経路がある場合は,人口が少ない都市へ優先して移動するような経路を選ぶ.これにより経路が定まり到着するまでの日数がわかるので,参加者はそれぞれライブが行われる日にちょうど都市1に到着するように移動を開始する. また, i ( 1 ≤ i ≤ K )番目の参加者にはライブの d_i 日前に p_i 円の支給がある事が知らされている.支給後の移動には可能なかぎり支給された費用を用いるが,支給前にかかっている費用や,賄いきれない支給後の費用は,各参加者が事前に用意する必要がある. それぞれの参加者が事前に用意する費用の最小値を求めよ. 入力形式 入力は次の形式で与えられる. N M t_1 ... t_N a_1 b_1 c_1 ... a_M b_M c_M K x_1 d_1 p_1 ... x_K d_K p_K 1行目には2つの整数 N ( 1 ≤ N ≤ 100,000 ), M ( 0 ≤ M ≤ 500,000 )が空白区切りで与えられ,それぞれ都市数と許された交通手段の数を表す.2行目には N 個の整数 t_i ( 1 ≤ i ≤ N , 1 ≤ t_i ≤ 500,000 )が空白区切りで与えられる. t_i は都市 i の人口を表し, i ≠ j ならば t_i ≠ t_j を満たす. 続く M 行は交通手段についての入力であり, i 番目の交通手段について3つの整数 a_i , b_i , c_i が空白区切りで与えられる.これは, i 番目の交通手段で都市 a_i と b_i ( 1 ≤ a_i, b_i ≤ N , a_i ≠ b_i )を行き来でき, c_i ( 1 ≤ c_i ≤ 10,000 )円の費用がかかることを表す.また,任意の2つの都市を直接結ぶ2つ以上の交通手段は無い.全ての都市は,いくつかの交通手段を用いて互いに行き来できる. 次の行には参加者の人数を表す整数 K ( 1 ≤ K ≤ 100,000 )が与えられる. 続く K 行は参加者たちについての入力であり, i 番目の参加者について3つの整数 x_i, d_i, p_i が空白区切りで与えられる.これは, i 番目の参加者が最初は都市 x_i ( 1 ≤ x_i ≤ N )におり,ライブの d_i ( 0 ≤ d_i ≤ 100,000 )日前に p_i ( 0 ≤ p_i ≤ 100,000 )円の支給を受けることを表す. 出力形式 それぞれの参加者が事前に用意する費用の最小値を,1番目の参加者から順に改行区切りで出力せよ. 入出力が非常に大きくなる可能性があるので,入出力の関数には高速なものを用いることを推奨する. 入力例1 5 6 100 80 70 60 50 1 2 500 2 5 100 1 3 400 1 4 200 3 5 700 4 5 800 1 5 3 600 出力例1 0 移動全体にかかる費用の最小値は600円で,2日間で都市1につくことができる.3日前に十分な費用が支給されるので,事前に費用を用意する必要はない. 入力例2 5 6 400 200 500 300 100 1 2 500 2 5 100 1 3 400 1 4 200 3 5 200 4 5 800 1 5 1 800 出力例2 100 費用が最小でありかつ日数が最小となる経路は 5−2−1 と 5−3−1 の2つの経路があるが,都市5から次の都市へ移動する際に,都市3より都市2の方が人口が少ないので経路 5−2−1 を選ぶ.合計の費用は600円なの支給額で全額支払い可能であるが,支給されるより前の費用は立て替えなければならないので, 5−2 間の費用である100円だけ事前に用意する必要がある. 入力例3 10 13 100 90 80 70 60 50 40 30 20 10 1 2 5 1 4 4 2 3 3 3 5 2 4 5 6 4 6 7 4 7 2 5 8 1 5 9 8 6 7 10 6 9 7 6 10 3 7 10 10 10 2 0 0 2 1 3 3 0 100000 3 1 3 3 1 100000 3 2 100000 3 100000 100000 8 1 5 9 2 11 10 0 0 出力例3 5 2 8 5 3 0 0 7 7 14
[ { "submission_id": "aoj_2764_10593094", "code_snippet": "#include <iostream>\n#include <vector>\n#include <queue>\n#include <algorithm>\n#include <climits>\n\nusing namespace std;\n\nconst int MAX_N = 100000;\nconst int MAX_D = 20;\ntypedef long long ll;\nconst ll INF = 1LL << 60;\n\nstruct Edge {\n int ...
aoj_2763_cpp
F: みこみー文字列 - Miko Mi String - 物語 みっこみっこみ〜!みんなのアイドル,田澤みこみこ!今日は〜,みこと一緒に〜,文字列アルゴリズムの練習,しよ☆ みこのとっておきの キャラ作り 合言葉,「みっこみっこみ〜」は,ローマ字にすると “MikoMikoMi” になるみこ!つまり, A= “Mi”, B= “Ko” とすると, ABABA の形で書けるってことなの!こんな風に,適切に A と B を決めると ABABA の形に分解できる文字列のことを「みこみー文字列」って言うわ!文字列の名前にまでなっちゃうなんて,みこはみんなの人気者みこね! みんなでみこみー文字列をも〜っと使っていくために,与えられた文字列がみこみー文字列かどうか判定するプログラムを作ることにしたんだけど,みこ,FizzBuzzより長いプログラムなんて書けな〜い!だから〜,みこのために〜,みこみー文字列を判定するプログラムを書いて欲しいな☆ ……アンタ,いま寒いって言った!? 問題 アルファベット大文字・小文字からなる文字列 S が与えられる. ここで, S = ABABA と書けるような,空でない2つの文字列 A, B が存在するならば, S は「みこみー文字列」であるという. このとき,アルファベットの大文字と小文字は違う文字として区別するものとする. 与えられた文字列がみこみー文字列であるかどうかを判定するプログラムを作成せよ. 入力形式 入力は文字列 S を含む1行のみからなる. S は以下の条件を満たすと仮定してよい. 1 ≤ |S| ≤ 10^6 .ただし, |S| は文字列 S の長さを表す. S は大文字,もしくは小文字のアルファベットのみからなる. なお,入力が非常に大きくなる場合があるため,入力の受け取りには高速な関数を用いることを推奨する. 出力形式 S がみこみー文字列であるならば, S = ABABA を満たすような A, B について,“Love AB !”と出力せよ.ただし,複数の A, B の組が条件を満たす場合, |AB| が最小のものを出力せよ. S がみこみー文字列でないならば,“mitomerarenaiWA”と出力せよ. 入力例1 NicoNicoNi 出力例1 Love Nico! 入力例2 KashikoiKawaiiElichika 出力例2 mitomerarenaiWA 入力例3 LiveLiveL 出力例3 Love Live! 入力例4 AizunyanPeroPero 出力例4 mitomerarenaiWA 入力例5 AAAAAAAAAAAAAA 出力例5 Love AAAAA!
[ { "submission_id": "aoj_2763_8469801", "code_snippet": "/* -*- coding: utf-8 -*-\n *\n * 2763.cc: Miko Mi String\n */\n\n#include<cstdio>\n#include<algorithm>\n \nusing namespace std;\n\n/* constant */\n\nconst int MAX_N = 1000000;\nconst int P = 4073;\nconst int MOD = 1000000009;\n\n/* typedef */\n\ntempla...
aoj_2770_cpp
F: リレー / Relay 問題文 湖に浮かぶ $N$ 個の小島からなるビワコという村がある. ビワコ村には $N-1$ 本の簡単な作りの橋がある. 島には $0$ から $N-1$ まで,橋には $0$ から $N-2$ までの番号が付けられている. $i$ 番の橋は $i+1$ 番の島と $p_i$ 番の島を直接つなぎ,長さは $w_i$ である. 村人はどの島の間もいくつかの橋を通って相互に移動できるようになっている. ある村人の提案により,ビワコ村でリレー大会が開催されることとなった. しかし,ビワコ村には閉路がなくトラックを用意できないため, 現在ある橋を $1$ つだけ掛け替えることによって閉路を作ろうと考えた. この操作によって用意できる閉路のうち,長さが最大となるものの長さを求めなさい. 入力 $N$ $p_0 \ w_0$ $\vdots$ $p_{n-2} \ w_{n-2}$ 制約 $2 \leq N \leq 100000$ $0 \leq p_i \leq N-1$ $1 \leq w_i \leq 1000$ 全て整数 全ての島と島の間が到達可能である 出力 答えを $1$ 行で出力せよ. サンプル 各サンプルを図示すると次のようになる. サンプル入力1 5 0 1 0 2 0 3 0 4 サンプル出力1 9 サンプル入力2 12 0 6 1 1 0 3 3 4 0 2 5 1 6 1 0 2 8 1 9 1 10 2 サンプル出力2 19 サンプル入力3 2 0 1 サンプル出力3 1
[ { "submission_id": "aoj_2770_10401948", "code_snippet": "// AOJ #2770 Relay\n// 2025.4.21\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\n#define gc() getchar_unlocked()\n\nint Cin() {\n\tint n = 0, c = gc();\n\tif (c == '-') {\tc = gc();\n\t\tdo n = 10*n + (c & 0xf), c = gc(); w...
aoj_2768_cpp
D: スキャナー / Scanner 問題文 ここに $N$ 枚の紙がある。あなたは $3$ 台のスキャナーを並列に用いることで、 全ての紙をスキャンしたいと考えている。それぞれの紙はスキャンにかかる時間が決まっており、 $i$ 番目の紙をスキャンするのにかかる時間は $T_i$ である。 紙をスキャンする順番は任意であるが、$1$ 台のスキャナーで複数の紙を同時にスキャンすることはできない。 全ての紙のスキャンが終了し、スキャンを行っているスキャナーがなくなるまでにかかる時間を最小化しなさい。 入力 $N$ $T_1$ $T_2$ $T_3$ $\vdots$ $T_N$ 制約 $1 \leq N \leq 50$ $1 \leq T_i \leq 50$ 入力は全て整数 出力 答えを $1$ 行で出力してください. サンプル サンプル入力1 4 1 1 1 1 サンプル出力1 2 サンプル入力2 9 15 20 27 4 10 7 34 30 36 サンプル出力2 61 サンプル入力3 6 20 18 46 16 9 48 サンプル出力3 55
[ { "submission_id": "aoj_2768_10851361", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define FOR(i,s,t) for(int i = s; i< t ;i++)\n\n#define SZ(x) (int)x.size()\nusing LL = long long; using ll = LL;\nconst int INF = 1e9; const LL LINF = 1e18;\n\n#define MAXT 1005\nll N;\nvector<int> T;\nb...
aoj_2769_cpp
E: 28 問題文 トランプゲームの大富豪において,ランクが $2$, $8$ のカードは強力です. そこで,$10$ 進数表記で数字の $2$, $8$ のみからなる整数を 良い整数 と呼ぶことにします. 良い整数を小さいものから列挙すると $2, 8, 22, 28, 82, 88, \cdots$ となります. $n$ を正の整数とします.$n$ が良い整数の積の形で表現できるとき, 最大でいくつの積になるか求めてください.できないなら $-1$ と出力してください. 入力 $n$ 制約 $1 \leq n \leq 10^{18}$ 出力 答えを $1$ 行で出力してください. サンプル サンプル入力1 1 サンプル出力1 -1 サンプル入力2 2 サンプル出力2 1 サンプル入力3 88 サンプル出力3 3 $2 \times 2 \times 22$ と表せます. サンプル入力4 100 サンプル出力4 -1 サンプル入力5 173553147234869248 サンプル出力5 11 $2^6 \times 28 \times 2222^3 \times 8828$ と表せます.
[ { "submission_id": "aoj_2769_5972328", "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_2773_cpp
B: 周期数列 - Periodic Sequence - 問題 H大学の教授を務めているPeriod博士は、万物に潜むとされる周期と呼ばれる性質を研究している。一般的に知られている基本的な周期としては、数列に潜む周期が考えられるだろう。すなわち、長さ N の数列 S = S_1, S_2, ... , S_N が以下の性質を満たすならば、周期 t ( t \≤ N ) を持つという事実である。 1 \≤ i \≤ N − t について、 S_i=S_{i+t} である。 今、Period博士が着目しているのは、周期を用いてより簡易な記述ができる数列である。例えば、長さ N の数列が周期 t ( \≤ N ) を持つとき、ある整数 k を用いて N=kt と書けるならば、その数列は長さ t の数列 S_1, ... , S_t が k 個連続したものである、と記述できる。Period博士は数列を例のように記述できたとき、その数列は k -partであると言うことにした。 Period博士は、 k が最も大きい k -partに興味を示している。そこで助手であるあなたは、入力として数列を受け取り、それが k -partであるとき最も大きい k を出力するプログラムの作成を任されることとなった。Period博士の要求に正確に応えるプログラムを作成しよう。 入力形式 N S_1 ... S_N 1行目には、数列の長さを表す整数 N が与えられる。2行目には、長さ N の数列の各要素を表す整数 S_i ( 1 \≤ i \≤ N ) が空白区切りで与えられる。また、入力は 1 \≤ N \≤ 200,000 と 1 \≤ S_i \≤ 100,000 ( 1 \≤ i \≤ N ) を満たす。 出力形式 与えられた数列に対して、 k -partであるときの k の最大値を1行に出力せよ。 入力例1 6 1 2 3 1 2 3 出力例1 2 入力例2 12 1 2 1 2 1 2 1 2 1 2 1 2 出力例2 6 入力例3 6 1 2 3 4 5 6 出力例3 1
[ { "submission_id": "aoj_2773_10865813", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing LL = long long; using ll = LL;\nusing PII = pair<int, int>; using pii = PII;\nusing PLL = pair<LL, LL>; using pll = PLL;\nusing VI = vector<int>; using VL = vector<LL>;\nconst ll LINF = 1e18;\ncon...
aoj_2774_cpp
C: 成長する点 - Growing Point - 問題 粘菌コンピュータというものがある。 ある種の粘菌には「餌を求め、餌と餌の最短距離をつなぐ形に変形する」 という性質がある。 これを利用し、餌を「入力」、形を「出力」とみなして コンピュータとして利用することができる。 今、二次元平面上に1つの粘菌の拠点と N 個の餌が存在する。それぞれの餌には 1 から N までの異なる番号が与えられ、拠点には番号0が与えられている。 この粘菌はある餌を食べるために、その餌と最も近い拠点の最短距離を結ぶ管状に 成長し、食べた位置に新たに拠点を形成する。 新たに形成した拠点は拠点を形成する直前に食べた餌と同じ番号を持つ。 粘菌は拠点以外の場所から成長することはできない。 以降では、拠点と餌を二次元平面上の点、管状に成長した粘菌を複数の線分として考える。 すべての拠点と線分からなる構造を粘菌網と呼ぶ。 粘菌は1つの餌を食べるために次のような操作を繰り返す。 まだ食べていない餌の中で粘菌網に最も近い餌を選ぶ。そのような餌が複数存在する場合は番号が最も小さい餌を選ぶ。 選んだ餌と最も近い拠点を選ぶ。そのような拠点が複数存在する場合は、最も拠点の番号が小さいものから取る。 選んだ拠点と餌を結ぶ線分を引く。以降ではこのとき選んだ餌も拠点として扱う。 この粘菌は生きるために必要な栄養を取るのに M 個の餌を食べる必要がある。 粘菌が M 個の餌を食べるまでに引いたすべての線分の長さの合計を求めよ。 また、出力する値は0.0001以下の誤差を含んでいても良い。 以下の図では入力例2の粘菌の様子を示している。 入力形式 N M X Y px_1 py_1 ... px_n py_n 1 行目には餌の数 N ( 1 \≤ N \≤ 5,000 )、食べる餌の個数 M ( 1 \≤ M \≤ N )、番号0の拠点の座標 X , Y ( −5,000 \≤ X, Y \≤ 5,000 )が整数値で与えられる。続く N 行には番号順に餌の座標 px_i , py_i ( 1 \≤ i \≤ N )が整数値で与えられる。( −5,000 \≤ px_i, py_i \≤ 5,000 ) また、番号が異なる餌は異なる座標に存在し、それぞれの餌と番号0の拠点の座標は異なる。 出力形式 成長した距離の合計を1行で出力せよ。また、出力する値は0.0001以下の誤差を含んでいても良い。 入力例1 2 2 0 0 3 3 4 0 出力例1 7.16227766017 最初は番号1の餌と拠点の距離が $3\sqrt{2}$ と番号2の餌と拠点の距離が 4 なので 番号2の餌が選ばれる。その後番号1の餌と粘菌網との距離が 3 になり、番号1の餌が選ばれる。 入力例2 4 4 1 3 3 3 2 1 3 1 1 1 出力例2 6.2360679775 図のように餌1、2、4、3の順に粘菌は餌を食べていく 入力例3 16 15 -4077 763 -2480 2841 -2908 -1096 676 -4080 -4988 -2634 3004 -1360 -2272 1773 -4344 -3631 -355 4426 -3740 3634 -3330 2191 -3423 -2999 -3438 2281 4754 -1500 -3440 -3873 -2089 -3419 1426 2793 出力例3 25349.9626798834
[ { "submission_id": "aoj_2774_10858226", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing LL = long long; using ll = LL;\nusing PII = pair<int, int>; using pii = PII;\nusing PLL = pair<LL, LL>; using pll = PLL;\nusing VI = vector<int>; using VL = vector<LL>;\nconst ll LINF = 1e18;\ncon...
aoj_2775_cpp
D: Complex Oracle - Complex Oracle - 問題 ※この問題はリアクティブ問題です.すなわち,サーバー側に用意されたプログラムと対話的に応答することで正答を導くプログラムを作成する必要があります. また、サーバー側のプログラムも計算機資源を共有する関係上、サーバーだけで最大 3 sec程度の実行時間、最大 300 MB程度のメモリを使用する場合がありますので、TLE・MLEにお気をつけください。 あいずにゃんは若ヶ松高校のプログラミングコンテスト部、通称ぷろこん部に所属する2年生である。見目麗しい。あいずにゃんはその小さな体も無限に存在するチャームポイントの1つであるが、本人はコンプレックスを感じているようだ。そのせいか、一列に並んでいる人たちを見ると、それぞれの人の前方にその人より身長が高い人が何人並んでいるかを瞬時に数えられる能力を持っている。 プロコンの名門・律命館大学中等部卒のエリート競技プログラマであり、ぷろこん部の部長であるりっつちゃんは、あいずにゃんのこの能力を使えば、並んでいる人たちがそれぞれ何番目に背が高いのかを当てることができるのではないかと考えた。 今、りっつちゃんは N 人が並んでいる列をあいずにゃんに見せている。りっつちゃんは、人が N 人並んでいることは知っているが、その並び順がどうなっているかはわからない。りっつちゃんはあいずにゃんに “ l 番目の人から r 番目の人までコンプレックス度” を教えてもらうことができる。ここで、 l 番目の人から r 番目の人までのコンプレックス度とは、 i ( l \≤ i \≤ r ) 番目の人それぞれに関して、 l 番目から i − 1 番目までの間にいる自分 ( i ) より背が高い人の人数の総和である。(より厳密な定義は入出力形式を参照のこと) (うらやましいことに) ぷろこん部の部員であるあなたは、(とても、とても心苦しいが) あいずにゃんをオラクルとして利用することで身長順を当てるプログラムを書くようりっつちゃんに命じられた。つらい。せめてあいずにゃんに負担をかけないよう、少ない質問回数で求められるよう頑張ろう。 入力出力形式 サーバーは背の順を表す長さ N の数列 p を保持している。これは入力として与えられない。 p の各要素 p_i は 1 以上 N 以下であり、それぞれ値は異なる。 p_i が大きい値を持つ方が背が高いことを表す。 サーバーはまず、 p の長さ N を表す 1 つの整数からなる 1 行を入力として解答プログラムに与える。 次に解答プログラムが 2 つの整数 l, r ( 1 \≤ l \≤ r \≤ N ) からなるクエリをサーバーに送る。クエリの出力形式は ? l r である。これは例えばC/C++だと、 printf("? %d %d\n", l, r); fflush(stdout); などと書けばよい。出力の度にフラッシュすることを推奨する。 すると、サーバー側は以下の値を表す 1 つの整数 C からなる1行を入力として解答プログラムに与える。 C = (\{ (i, j) | p_i > p_j {\rm for} l \≤ i<j \≤ r \}の要素数) ただし、クエリとして正しくない l, r ( l, r が 1 以上 N 以下の数ではない、または l>r である) が与えられた場合、サーバーは −1 を C として返す。 この応答を何度か繰り返し、解答プログラムが p を推定できたならば、 p を以下の形式で出力する。 ! p_1 ... p_N 推定した順列の出力は1度しかできず、この出力が p と異なる場合、誤答となる。200,000回以内のクエリで正しい p を出力できた場合、正答となる。 各クエリで改行を行うよう気をつけること。 入力制約 1 \≤ N \≤ 100,000 推定される長さ N の数列 p は順列。すなわち、 p_i \in \{1, ... , N\} {\rm for} 1 \≤ i \≤ N 、および p_i \neq p_j {\rm for} 1 \≤ i < j \≤ N を満たす サーバーへのクエリ回数は200,000回まで サーバーから返答される C は32bit整数型では収まらない大きさになりうることに注意せよ 入力出力例 サーバーの出力 サーバーへの入力 4 ? 1 3 2 ? 2 4 1 ... ... ! 4 1 3 2
[ { "submission_id": "aoj_2775_10850453", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing LL = long long; using ll = LL;\nusing PII = pair<int, int>; using pii = PII;\nusing PLL = pair<LL, LL>; using pll = PLL;\nusing VI = vector<int>; using VL = vector<LL>;\n#define FOR(i,s,t) for(int...
aoj_2776_cpp
E: Arai's - Arai's - 問題 スクールアイドル時代から、国民的人気を誇ってきた女性アイドルグループArai's。今では、たくさんの「あらい」さんが所属する大規模なグループとして、世界レベルで活躍している。そして今日、新たなるプロジェクトの始動が決定した。彼女たちは、小さなユニットをいくつか結成することで、さらなる売上の向上を試みることになったのである。 Arai'sには「荒井」さんが A 人,「新井」さんが B 人在籍しており、合計で A+B 人の「あらい」さんからなる。新ユニットは、「荒井」さん一人と「新井」さん一人のペアで構成する。(ここで、同じ「あらい」さんが複数のユニットに所属していはいけない。)ただし、ある「荒井」さんが一部の「新井」さんのことを良く思っていないうえに、同様にある「新井」さんが一部の「荒井」さんのことを良く思っていない。「あらい」さんたちはユニットを組む際に、良く思っていない「あらい」さんをペアとして認めてくれず、一方でも認めてくれなければユニットを組むことはできない。 Arai'sのマネージャーであるあなたは、なるべくたくさんのユニットを作りたいと考えているが、メンバーの交友関係からその限界を感じていた。そこであなたは、「あらい」さんたちと個別に面談し、ユニットを組ませたい「あらい」さんについての、良い噂を聞かせることを考えた。面談をした「あらい」さんは、噂に聞いた「あらい」さんを見直し、ユニットのペアとして認めるようになる。 しかし、あなたはそれほど時間をとることができないため、最大 K 回までしか噂を聞かせることができない。あなたは限られた時間の中で、結成できるユニットの数を最大化しようと試みた。あなたが結成できるユニットの数の最大値を求めよ。 入力形式 A B K a_1 ... a_A b_1 ... b_B 1行目には、「荒井」さんの人数 A と「新井」さんの人数 B ( 1 \≤ A, B \≤ 200 であり A , B は整数)、噂を聞かせることができる人数 K ( 0 \≤ K \≤ 200 であり K は整数)が空白区切りで与えられる。 続く A 行には、それぞれ長さ B の 0 と 1 のみからなる文字列が与えられる。そのうち i 行目の文字列 a_i の j 文字目が 1 であるとき、 i 番目の「荒井」さんは j 番目の「新井」さんを良く思っていない。 続く B 行には、それぞれ長さ A の 0 と 1 のみからなる文字列が与えられる。そのうち i 行目の文字列 b_i の j 文字目が 1 であるとき、 i 番目の「新井」さんは j 番目の「荒井」さんを良く思っていない。 出力形式 あなたが結成できるユニット数の最大値を1行に出力せよ。 入力例1 3 3 4 111 111 111 111 111 111 出力例1 2 入力例2 3 3 2 101 100 010 001 011 111 出力例2 3 入力例3 5 6 3 101101 110110 010111 110110 110111 01010 10101 11101 01011 11011 11011 出力例3 4
[ { "submission_id": "aoj_2776_10851385", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing LL = long long; using ll = LL;\nusing PII = pair<int, int>; using pii = PII;\nusing PLL = pair<LL, LL>; using pll = PLL;\nusing VI = vector<int>; using VL = vector<LL>;\nconst ll LINF = 1e18;\ncon...
aoj_2777_cpp
F: きっちり - Kitsuchiri - 問題 若ヶ松高校二年へ組の合津千里(あつちり)さんは数列が「きっちり」していなければ気がすまない。 合津さんによると、「きっちり」した数列とは、長さが偶数で左右対称である数列のことである。 すなわち、長さ N ( N は偶数)の数列 S について、次の条件を満たせば数列 S は「きっちり」している。 S_1 = S_N, S_2 = S_{N − 1}, ... , S_{N/2} = S_{N − N/2 + 1} 二年へ組の数学担当の先生は、合津さんから「きっちりしてください。」と要望され、授業で用いる数列を作り直さなければならない。 先生は、数列の l 番目から r 番目までのそれぞれの要素に数字 x を足すクエリをいくつも適用することで、数列を「きっちり」させようと奮闘しているが、うまくいかないようだ。 二年へ組に所属する凄腕プログラマーのあなたの仕事は、先生が絶望してしまう前に、先生が作り直している数列が「きっちり」しているかを調べるプログラムを作ることだ。 入力形式 入力は以下の形式からなる。 N S_1 ... S_N Q q_1 ... q_Q Q はクエリの総数であり、 1 \≤ i \≤ Q について、各 q_i は l , r , x を順に1つの半角スペースで区切って与えらる。 また、次の制約を満たす。 2 \≤ N \≤ 500,000 である。 N は偶数である。 1 \≤ j \≤ N について、 −100,000,000 \≤ S_j \≤ 100,000,000 である。 i 番目の各クエリを適用した後の数列の各要素 T_{i,j} は −100,000,000 \≤ T_{i,j} \≤ 100,000,000 を満たす。 1 \≤ Q \≤ 100,000 である。 1 \≤ l \≤ r \≤ N である。 −1,000 \≤ x \≤ 1,000 である。 出力形式 クエリ i まで処理した後の数列が「きっちり」していれば“1”を、そうでなければ“0”を i 行目に出力せよ。 入力例1 10 0 1 2 3 4 4 3 2 1 0 7 2 6 0 2 4 5 7 9 10 2 4 5 3 8 100 4 6 1000 7 7 1000 出力例1 1 0 0 1 1 0 1 入力例2 10 4 4 4 4 4 4 4 4 6 4 5 9 9 -2 1 10 1000 1 10 -1000 3 8 100 5 6 1 出力例2 1 1 1 1 1
[ { "submission_id": "aoj_2777_10850964", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\nusing LL = long long; using ll = LL;\nusing PII = pair<int, int>; using pii = PII;\nusing PLL = pair<LL, LL>; using pll = PLL;\nusing VI = vector<int>; using VL = vector<LL>;\nconst ll LINF = 1e18;\ncon...
aoj_2779_cpp
H: われわれの努力について - About Our Effort - 問題 ※この問題はおまけの問題と捉えていただけるとありがたく、 できれば先に他の問題のほうをお楽しみいただければと思っておりまして,ですので他の問題を通し終えて暇になり,かつその暇を このコンテストで潰そうという気になってくれた方に挑戦していただければと思います。 D問題のクエリが遅くてすみませんでした。 しかし我々といたしましても決してただ怠慢をしていたわけではないのです。 私たちなりに努力したのです。 その努力の片鱗を味わっていただくため、このような問題を出題した次第であります。 問題: D問題のサーバー側の処理をするプログラムを作成せよ。 なおこの問題は本当にD問題の要求を満たすプログラムを作成することが目標ということですので、言語による向き/不向きなどは一切考慮いたしませんのであしからず。最速実行速度を達成された方はもれなくAOJに収録される際にジャッジプログラムとして採用される可能性がありますのでぜひ挑戦下さい。 入力形式 入力は以下の形式で与えられる。 N p_1 ... p_N Q l_1 r_1 ... l_Q r_Q 1行目では順列 p の長さ N が与えられる。2行目では順列 p を表す N 個の整数が空白区切りで与えられる。3行目ではクエリの数を表す整数 Q が与えられる。続く Q 行の i 行目は空白区切りの2つの整数 l_i , r_i からなり、クエリが区間 [l_i, r_i] のコンプレックス度を聞くものであることを表す。 入力は以下の制約を満たす。 1 \≤ N \≤ 100,000 1 \≤ p_i \≤ N , p_i \neq p_j (i \neq j) 1 \≤ Q \≤ 200,000 1 \≤ l_i \≤ r_i \≤ N 出力形式 Q 個の各クエリ i に関して、 p の区間 [l_i, r_i] のコンプレックス度を i 行目に出力せよ。ただし、 p の区間 [l_i, r_i] のコンプレックス度とは、 (\{ (i, j) | p_i > p_j {\rm for} l \≤ i<j \≤ r \}の要素数) と定義される。 入力例1 4 4 1 3 2 2 1 3 2 4 出力例1 2 1 入力例2 1 1 1 1 1 出力例2 0
[ { "submission_id": "aoj_2779_4926964", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\nusing ll = long long;\n\n#define SIZE 200000\n#define SQ 300\n#define LLINF 100000000000000\n\nstruct BIT {\n vector<int> data;\n\n BIT(int n): data(n + 2, 0) {};\n\n void add(int k, int x) {\n k+...
aoj_2778_cpp
G: 運命力 - Destiny Draw - 問題 Mr. D はカードゲームで勝負をすることになった。 このカードゲームでは、 N 枚のカードの山を用いる。 また、山のカードには、上から順に 1, 2, 3, ... , N の番号がついている。 D で始まるものすべてを背負う彼に敗北は許されないが、不幸にも Mr. D はカードゲームが不得手である。 そこで、自分が引くカードをコントロールすることで勝利を手にすることにした。 Mr. D は K 種類のシャッフルが可能である。 K 種類のうち i 番目のシャッフルでは上から a_i 枚目から a_i+b_i − 1 枚目までのちょうど b_i 枚を引き抜き、上に重ねる。 i 番目のシャッフルを 1 回するとき、それぞれ t_i 秒を要する。 ちょうど T 秒のシャッフルの後、一番上のカードを C にする方法は何通りあるか。 数が多くなる可能性があるので、 10^9+7 で割った余りを出力せよ。 入力形式 入力は次の形式で与えられる。 N K C T a_1 b_1 t_1 a_2 b_2 t_2 ... a_K b_K t_K N は整数で 2 \≤ N \≤ 40 を満たす。 K は整数で 1 \≤ K \≤ \frac{N (N+1)}{2} を満たす C は整数で 1 \≤ C \≤ N を満たす T は整数で 1 \≤ T \≤ 1,000,000 を満たす a_i ( i = 1, 2, ... , K ) は整数で 1 \≤ a_i \≤ N を満たす b_i ( i = 1, 2, ... , K ) は整数で 1 \≤ b_i \≤ N − a_i + 1 を満たす a_i = a_j かつ b_i = b_j を満たすとき i = j に限定される t_i は整数で 1 \≤ t_i \≤ 5 を満たす 出力形式 答えを 10^9+7 で割った余りを一行で出力せよ。 入力例1 4 1 1 6 3 2 3 出力例1 1 入力例2 4 1 1 5 3 2 3 出力例2 0 入力例3 6 2 2 5 1 2 1 2 5 3 出力例3 3 ちょうど 5 秒で一番上のカードを 2 にする方法は以下の 3 通りである。 1→1→2 1→2→1 2→1→1 入力例4 6 8 3 10 1 4 5 1 3 3 1 6 5 1 2 2 1 1 4 2 5 1 4 3 1 2 1 3 出力例4 3087
[ { "submission_id": "aoj_2778_10259979", "code_snippet": "// AOJ #2778 Destiny Draw\n// 2025.3.2\n\n#include <bits/stdc++.h>\nusing namespace std;\nconst long long MOD = 1000000007;\n\nint main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n\n int n, K, C, T;\n cin >> n >> K >> C >> T;\n vector<...
aoj_2780_cpp
Best Matched Pair You are working for a worldwide game company as an engineer in Tokyo. This company holds an annual event for all the staff members of the company every summer. This year's event will take place in Tokyo. You will participate in the event on the side of the organizing staff. And you have been assigned to plan a recreation game which all the participants will play at the same time. After you had thought out various ideas, you designed the rules of the game as below. Each player is given a positive integer before the start of the game. Each player attempts to make a pair with another player in this game, and formed pairs compete with each other by comparing the products of two integers. Each player can change the partner any number of times before the end of the game, but cannot have two or more partners at the same time. At the end of the game, the pair with the largest product wins the game. In addition, regarding the given integers, the next condition must be satisfied for making a pair. The sequence of digits obtained by considering the product of the two integers of a pair as a string must be increasing and consecutive from left to right. For example, 2, 23, and 56789 meet this condition, but 21, 334, 135 or 89012 do not. Setting the rules as above, you noticed that multiple pairs may be the winners who have the same product depending on the situation. However, you can find out what is the largest product of two integers when a set of integers is given. Your task is, given a set of distinct integers which will be assigned to the players, to compute the largest possible product of two integers, satisfying the rules of the game mentioned above. Input The input consists of a single test case formatted as follows. $N$ $a_1$ $a_2$ ... $a_N$ The first line contains a positive integer $N$ which indicates the number of the players of the game. $N$ is an integer between 1 and 1,000. The second line has $N$ positive integers that indicate the numbers given to the players. For $i = 1, 2, ... , N - 1$, there is a space between $a_i$ and $a_{i+1}$. $a_i$ is between 1 and 10,000 for $i = 1, 2, ..., N$, and if $i \ne j$, then $a_i \ne a_j$. Output Print the largest possible product of the two integers satisfying the conditions for making a pair. If any two players cannot make a pair, print -1. Sample Input 1 2 1 2 Output for the Sample Input 1 2 Sample Input 2 3 3 22 115 Output for the Sample Input 2 345 Sample Input 3 2 1 11 Output for the Sample Input 3 -1 Sample Input 4 2 5 27 Output for the Sample Input 4 -1 Sample Input 5 2 17 53 Output for the Sample Input 5 -1 Sample Input 6 10 53 43 36 96 99 2 27 86 93 23 Output for the Sample Input 6 3456
[ { "submission_id": "aoj_2780_10295740", "code_snippet": "// AOJ #2780 Best Matched Pair\n// 2025.3.14\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\n#define gc() getchar_unlocked()\n#define pc(c) putchar_unlocked(c)\n\nint Cin() { // 整数の入力\n\tint n = 0; int c = gc();\n\tdo n = ...
aoj_2782_cpp
We don't wanna work! ACM is an organization of programming contests. The purpose of ACM does not matter to you. The only important thing is that workstyles of ACM members are polarized: each member is either a workhorse or an idle fellow. Each member of ACM has a motivation level. The members are ranked by their motivation levels: a member who has a higher motivation level is ranked higher. When several members have the same value of motivation levels, the member who joined ACM later have a higher rank. The top 20% highest ranked members work hard, and the other (80%) members never (!) work. Note that if 20% of the number of ACM members is not an integer, its fraction part is rounded down. You, a manager of ACM, tried to know whether each member is a workhorse or an idle fellow to manage ACM. Finally, you completed to evaluate motivation levels of all the current members. However, your task is not accomplished yet because the members of ACM are dynamically changed from day to day due to incoming and outgoing of members. So, you want to record transitions of members from workhorses to idle fellows, and vice versa. You are given a list of the current members of ACM and their motivation levels in chronological order of their incoming date to ACM. You are also given a list of incoming/outgoing of members in chronological order. Your task is to write a program that computes changes of workstyles of ACM members. Input The first line of the input contains a single integer $N$ ($1 \leq N \leq 50,000$) that means the number of initial members of ACM. The ($i$ + 1)-th line of the input contains a string $s_i$ and an integer $a_i$ ($0 \leq a_i \leq 10^5$), separated by a single space. $s_i$ means the name of the $i$-th initial member and $a_i$ means the motivation level of the $i$-th initial member. Each character of $s_i$ is an English letter, and $1 \leq |s_i| \leq 20$. Note that those $N$ lines are ordered in chronological order of incoming dates to ACM of each member. The ($N$ + 2)-th line of the input contains a single integer $M$ ($1 \leq M \leq 20,000$) that means the number of changes of ACM members. The ($N$ + 2 + $j$)-th line of the input contains information of the $j$-th incoming/outgoing member. When the $j$-th information represents an incoming of a member, the information is formatted as "$+ t_j b_j$", where $t_j$ is the name of the incoming member and $b_j$ ($0 \leq b_j \leq 10^5$) is his motivation level. On the other hand, when the $j$-th information represents an outgoing of a member, the information is formatted as "$- t_j$", where $t_j$ means the name of the outgoing member. Each character of $t_j$ is an English letter, and $1 \leq |t_j| \leq 20$. Note that uppercase letters and lowercase letters are distinguished. Note that those $M$ lines are ordered in chronological order of dates when each event happens. No two incoming/outgoing events never happen at the same time. No two members have the same name, but members who left ACM once ma ...(truncated)
[ { "submission_id": "aoj_2782_10851164", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define D(x) cout << #x \" = \" << (x) << endl\n#define un(x) x.erase(unique(x.begin(), x.end()), x.end())\n#define sf(n) scanf(\"%d\", &n)\n#define sff(a,b) scanf(\"%d %d\", &a, &b)...
aoj_2781_cpp
Help the Princess! The people of a certain kingdom make a revolution against the bad government of the princess. The revolutionary army invaded the royal palace in which the princess lives. The soldiers of the army are exploring the palace to catch the princess. Your job is writing a program to decide that the princess can escape from the royal palace or not. For simplicity, the ground of the palace is a rectangle divided into a grid. There are two kinds of cells in the grid: one is a cell that soldiers and the princess can enter, the other is a cell that soldiers or the princess cannot enter. We call the former an empty cell, the latter a wall. The princess and soldiers are in different empty cells at the beginning. There is only one escape hatch in the grid. If the princess arrives the hatch, then the princess can escape from the palace. There are more than or equal to zero soldiers in the palace. The princess and all soldiers take an action at the same time in each unit time. In other words, the princess and soldiers must decide their action without knowing a next action of the other people. In each unit time, the princess and soldiers can move to a horizontally or vertically adjacent cell, or stay at the current cell. Furthermore the princess and soldiers cannot move out of the ground of the palace. If the princess and one or more soldiers exist in the same cell after their move, then the princess will be caught. It is guaranteed that the princess can reach the escape hatch via only empty cells if all soldiers are removed from the palace. If there is a route for the princess such that soldiers cannot catch the princess even if soldiers make any moves, then the princess can escape the soldiers. Note that if the princess and a soldier arrive the escape hatch at the same time, the princess will be caught. Can the princess escape from the palace? Input Each dataset is formatted as follows. $H$ $W$ $map_1$ $map_2$ ... $map_H$ The first line of a dataset contains two positive integers $H$ and $W$ delimited by a space, where $H$ is the height of the grid and $W$ is the width of the grid ($2 \leq H, W \leq 200$). The $i$-th line of the subsequent $H$ lines gives a string $map_i$, which represents situation in the ground of palace. $map_i$ is a string of length $W$, and the $j$-th character of $map_i$ represents the state of the cell of the $i$-th row and the $j$-th column. '@', '\$', '%', '.', and '#' represent the princess, a soldier, the escape hatch, an empty cell, and a wall, respectively. It is guaranteed that there exists only one '@', only one '%', and more than or equal to zero '\$' in the grid. Output Output a line containing a word "Yes", if the princess can escape from the palace. Otherwise, output "No". Sample Input 1 2 4 %.@\$ ..\$\$ Output for the Sample Input 1 Yes Sample Input 2 3 4 .%.. .##. .@\$. Output for the Sample Input 2 Yes Sample Input 3 2 3 %\$@ ### Output for the Sample Input 3 No Sample Input 4 2 3 @#\$ .%. Output for the ...(truncated)
[ { "submission_id": "aoj_2781_10730387", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nint n, m;\nint x, y;\nchar str[210][210];\nchar tmp[210][210];\nint dirx[] = {1, -1, 0, 0};\nint diry[] = {0, 0, 1, -1};\nint val (int a, int b) {\n if (a<0 || b<0 || a>=n || b>=m) return 0;\n retu...
aoj_2783_cpp
Parentheses Dave loves strings consisting only of '(' and ')'. Especially, he is interested in balanced strings. Any balanced strings can be constructed using the following rules: A string "()" is balanced. Concatenation of two balanced strings are balanced. If $T$ is a balanced string, concatenation of '(', $T$, and ')' in this order is balanced. For example, "()()" and "(()())" are balanced strings. ")(" and ")()(()" are not balanced strings. Dave has a string consisting only of '(' and ')'. It satis es the followings: You can make it balanced by swapping adjacent characters exactly $A$ times. For any non-negative integer $B$ ($B < A$), you cannot make it balanced by $B$ swaps of adjacent characters. It is the shortest of all strings satisfying the above conditions. Your task is to compute Dave's string. If there are multiple candidates, output the minimum in lexicographic order. As is the case with ASCII, '(' is less than ')'. Input The input consists of a single test case, which contains an integer $A$ ($1 \leq A \leq 10^9$). Output Output Dave's string in one line. If there are multiple candidates, output the minimum in lexicographic order. Sample Input 1 1 Output for the Sample Input 1 )( There are infinitely many strings which can be balanced by only one swap. Dave's string is the shortest of them. Sample Input 2 4 Output for the Sample Input 2 )())(( String "))(()(" can be balanced by 4 swaps, but the output should be ")())((" because it is the minimum in lexicographic order.
[ { "submission_id": "aoj_2783_4919488", "code_snippet": "#include <random>\n#include \"bits/stdc++.h\"\n//#include <atcoder/all>\n\nusing namespace std;\n// using namespace atcoder;\n\nusing ll = long long;\nusing ld = long double;\nusing P = pair<int, int>;\nconstexpr ld eps = 1e-12;\nconstexpr int inf = nu...
aoj_2784_cpp
Similarity of Subtrees Define the depth of a node in a rooted tree by applying the following rules recursively: The depth of a root node is 0. The depths of child nodes whose parents are with depth $d$ are $d + 1$. Let $S(T, d)$ be the number of nodes of $T$ with depth $d$. Two rooted trees $T$ and $T'$ are similar if and only if $S(T, d)$ equals $S(T', d)$ for all non-negative integer $d$. You are given a rooted tree $T$ with $N$ nodes. The nodes of $T$ are numbered from 1 to $N$. Node 1 is the root node of $T$. Let $T_i$ be the rooted subtree of $T$ whose root is node $i$. Your task is to write a program which calculates the number of pairs $(i, j)$ such that $T_i$ and $T_j$ are similar and $i < j$. Input The input consists of a single test case. $N$ $a_1$ $b_1$ $a_2$ $b_2$ ... $a_{N-1}$ $b_{N-1}$ The first line contains an integer $N$ ($1 \leq N \leq 100,000$), which is the number of nodes in a tree. The following $N -1$ lines give information of branches: the $i$-th line of them contains $a_i$ and $b_i$, which indicates that a node $a_i$ is a parent of a node $b_i$. ($1 \leq a_i, b_i \leq N, a_i \ne b_i$) The root node is numbered by 1. It is guaranteed that a given graph is a rooted tree, i.e. there is exactly one parent for each node except the node 1, and the graph is connected. Output Print the number of the pairs $(x, y)$ of the nodes such that the subtree with the root $x$ and the subtree with the root $y$ are similar and $x < y$. Sample Input 1 5 1 2 1 3 1 4 1 5 Output for the Sample Input 1 6 Sample Input 2 6 1 2 2 3 3 4 1 5 5 6 Output for the Sample Input 2 2 Sample Input 3 13 1 2 1 3 2 4 2 5 3 6 3 7 4 8 4 9 6 10 7 11 8 12 11 13 Output for the Sample Input 3 14
[ { "submission_id": "aoj_2784_10946041", "code_snippet": "#include <bits/stdtr1c++.h>\n\n#define MAX 100100\n#define clr(ar) memset(ar, 0, sizeof(ar))\n#define read() freopen(\"lol.txt\", \"r\", stdin)\n#define dbg(x) cout << #x << \" = \" << x << endl\n\nusing namespace std;\n\nconst unsigned long long base...
aoj_2786_cpp
Share the Ruins Preservation Two organizations International Community for Preservation of Constructions (ICPC) and Japanese Archaeologist Group (JAG) engage in ruins preservation. Recently, many ruins were found in a certain zone. The two organizations decided to share the preservation of the ruins by assigning some of the ruins to ICPC and the other ruins to JAG. Now, ICPC and JAG make a rule for assignment as follows: Draw a vertical straight line from the north to the south, avoiding to intersect ruins. Ruins located to the west of the line are preserved by ICPC. On the other hand, ruins located to the east of the line are preserved by JAG. (It is possible that no ruins are located to the east/west of the line; in this case, ICPC/JAG will preserve no ruins.) A problem is where to draw a straight line. For each organization, the way to preserve its assigned ruins is to make exactly one fence such that all the assigned ruins are in the region surrounded by the fence. Furthermore, they should minimize the length of such a fence for their budget. If the surrounded areas are vast, expensive costs will be needed to maintain the inside of areas. Therefore, they want to minimize the total preservation cost, i.e. the sum of the areas surrounded by two fences. Your task is to write a program computing the minimum sum of the areas surrounded by two fences, yielded by drawing an appropriate straight line. Input The input consists of a single test case. $N$ $x_1$ $y_1$ $x_2$ $y_2$ ... $x_N$ $y_N$ The first line contains an integer $N$ ($1 \leq N \leq 100,000$), which is the number of founded ruins. The following $N$ lines represent the location of the ruins. The $i$-th line of them consists of two integers $x_i$ and $y_i$, which indicate the location of the $i$-th ruin is $x_i$ east and $y_i$ north from a certain location in the zone. You can assume the following things for the ruins: $-10^9 \leq x_i, y_i \leq 10^9$ You can ignore the sizes of ruins. That is, you can assume ruins are points. No pair of ruins has the same location. Output Print the minimum total preservation cost yielded by drawing an appropriate straight line. You should round off the cost to the nearest integer. Sample Input 1 8 -10 0 -10 5 -5 5 -5 0 10 0 10 -5 5 -5 5 0 Output for the Sample Input 1 50 Sample Input 2 5 0 0 0 1 0 2 1 0 1 1 Output for the Sample Input 2 0 Sample Input 3 6 1 5 1 6 0 5 0 -5 -1 -5 -1 -6 Output for the Sample Input 3 6 Sample Input 4 10 2 5 4 6 9 5 8 8 1 3 6 4 5 9 7 3 7 7 3 9 Output for the Sample Input 4 17
[ { "submission_id": "aoj_2786_10850971", "code_snippet": "#include<bits/stdc++.h>\n\n#define PB push_back\n#define MP make_pair\n#define F first\n#define S second\n\n#define FRI freopen(\"zin.txt\",\"r\",stdin)\n#define FRO freopen(\"out.txt\",\"w\",stdout)\n#define DB(x) #x\" =>\",x\n#define debug(args...) ...
aoj_2785_cpp
Escape from the Hell One day, Buddha looked into the hell and found an office worker. He did evil, such as enforcing hard work on his subordinates. However, he made only one good in his life. He refused an unreasonable request from his customer to save the lives of his subordinates. Buddha thought that, as the reward of the good, the office worker should have had a chance to escape from the hell. Buddha took a spider silk and put down to the hell. The office worker climbed up with the spider silk, however the length of the way $L$ meters was too long to escape one day. He had $N$ energy drinks and drunk one of them each day. The day he drunk the i-th energy drink he could climb $A_i$ meters in the daytime and after that slided down $B_i$ meters in the night. If he could reach at the height greater than or equal to the $L$ meters in the daytime, he could escape without sliding down. After the $N$ days the silk would be cut. He realized that other sinners climbed the silk in the night. They climbed $C_i$ meters in the $i$-th night without sliding down in the daytime. If they catched up with the office worker, they should have conflicted and the silk would be cut. Therefore he needed to escape before other sinners catched him. Your task is to write a program computing the best order of energy drink and output the earliest day which he could escape. If he could not escape, your program should output -1. Input The input consists of a single test case. $N$ $L$ $A_1$ $B_1$ $A_2$ $B_2$ ... $A_N$ $B_N$ $C_1$ $C_2$ ... $C_N$ The first line contains two integers $N$ ($1 \leq N \leq 10^5$) and $L$ ($1 \leq L \leq 10^9$), which mean the number of energy drinks and the length of the spider silk respectively. The following $N$ lines show the information of the drinks: the $i$-th of them indicates the $i$-th energy drink, he climbed up $A_i$ ($1 \leq A_i \leq 10^9$) meters and slided down $B_i$ ($1 \leq B_i \leq 10^9$) meters. Next $N$ lines show how far other sinners climbed: the $i$-th of them contains an integer $C_i$ ($1 \leq C_i \leq 10^9$), which means they climbed up $C_i$ meters in the $i$-th day. Output Print the earliest day which he could escape. If he could not escape, print -1 instead. Sample Input 1 3 9 6 3 5 2 3 1 2 2 2 Output for the Sample Input 1 2 Sample Input 2 5 20 3 2 4 2 6 3 8 4 10 5 4 2 3 4 5 Output for the Sample Input 2 -1 Sample Input 3 5 20 6 5 7 3 10 3 10 14 4 7 2 5 3 9 2 Output for the Sample Input 3 3 Sample Input 4 4 12 8 4 6 4 2 1 2 1 1 1 4 4 Output for the Sample Input 4 -1
[ { "submission_id": "aoj_2785_10946038", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define si(a) scanf(\"%d\",&a)\n#define f first\n#define s second\n#define mp(a,b) make_pair(a,b)\n#define MAX 100005\n#define INF 1LL<<50\ntypedef pair <int, int> Pii;\n\nstruct Less {\n bool ope...
aoj_2787_cpp
Pipe Fitter and the Fierce Dogs You, a proud pipe fitter of ICPC (International Community for Pipe Connection), undertake a new task. The area in which you will take charge of piping work is a rectangular shape with $W$ blocks from west to east and $H$ blocks from north to south. We refer to the block at the $i$-th from west and the $j$-th from north as $(i, j)$. The westernmost and northernmost block is $(1, 1)$, and the easternmost and southernmost block is $(W,H)$. To make the area good scenery, the block $(i, j)$ has exactly one house if and only if both of $i$ and $j$ are odd numbers. Your task is to construct a water pipe network in the area such that every house in the area is supplied water through the network. A water pipe network consists of pipelines. A pipeline is made by connecting one or more pipes, and a pipeline with l pipes is constructed as follows: choose a first house, and connect the house to an underground water source with a special pipe . choose an $i$-th house ($2 \leq i \leq l$), and connect the $i$-th house to the ($i - 1$)-th house with a common pipe . In this case, there is a condition to choose a next $i$-th house because the area is slope land. Let $(x, y)$ be the block of the ($i - 1$)-th house. An $i$-th house must be located at either $(x - 2, y + 2)$, $(x, y + 2)$, or $(x + 2, y + 2)$. A common pipe connecting two houses must be located at $(x - 1, y + 1)$, $(x, y + 1)$, or $(x + 1, y + 1)$, respectively. In addition, you should notice the followings when you construct several pipelines: For each house, exactly one pipeline is through the house. Multiple pipes can be located at one block. In your task, common pipes are common, so you can use any number of common pipes. On the other hand, special pipes are special, so the number of available special pipes in this task is restricted under ICPC regulation. Besides the restriction of available special pipes, there is another factor obstructing your pipe work: fierce dogs. Some of the blocks which do not contain a house seem to be home of fierce dogs. Each dog always stays at his/her home block. Since several dogs must not live at the same block as their home, you can assume each block is home of only one dog, or not home of any dogs. The figure below is an example of a water pipe network in a 5 $\times$ 5 area with 4 special pipes. This corresponds to the first sample. Locating a common pipe at a no-dog block costs 1 unit time, but locating a common pipe at a dog-living block costs 2 unit time because you have to fight against the fierce dog. Note that when you locate multiple pipes at the same block, each pipe-locating costs 1 unit time for no-dog blocks and 2 for dog-living blocks, respectively. By the way, special pipes are very special, so locating a special pipe costs 0 unit time. You, a proud pipe fitter, want to accomplish this task as soon as possible. Fortunately, you get a list of blocks which are home of dogs. You have frequently participated in programmi ...(truncated)
[ { "submission_id": "aoj_2787_10851470", "code_snippet": "#include <cstdio>\n#include <cstring>\n#include <cmath>\n#include <algorithm>\n#include <vector>\n#include <string>\n#include <map>\n#include <set>\n#include <cassert>\nusing namespace std;\n#define rep(i,a,n) for (int i=a;i<n;i++)\n#define per(i,a,n)...
aoj_2796_cpp
F: 紙の折りたたみ / Folding Paper 問題文 高さ $H$ マス、幅 $W$ マスの格子状に区切られた長方形の紙がある。 $0$ から数えて上から $i$ 行目、左から $j$ 列目のマスには整数 $i \times W+j$ が書かれている。 $H=2, W=3$ の例を下の図に示す。 AOR イカちゃんは、この紙に対して次のような操作を順に行った。 $1$ マス分の面積になるまでマスの区切り線に沿って繰り返し折る。この時の折る線や山谷、順番などは任意である。紙を破らない任意の折り方ができる。 $4$ 辺を切り落として $H \times W$ 枚の紙に分割する。 上から順にめくっていき、書かれていた整数を並べた数列 $S$ を作る。 例えば、下の図のように折った後切った場合、$S$ として $4, 3, 0, 5, 2, 1$ が得られる。このように、間に差し込むような折り方も可能である。 あなたは AOR イカちゃんから数列 $S$ を受け取った。 しかし、AOR イカちゃんのことを信用していないので、これが本物か確かめたい。 $S$ と一致するような紙の折り方が存在するなら "YES" を、しないなら "NO" を出力するプログラムを書け。 入力 $H \ W$ $S_0 \cdots S_{HW-1}$ 入力の制約 $1 \le H, W \le 500$ $S$ は $0$ から $H \times W - 1$ までの整数を $1$ つずつ含む 出力 "YES" または "NO" を $1$ 行で出力せよ。 サンプル サンプル入力1 1 4 0 1 2 3 サンプル出力1 YES サンプル入力2 2 3 4 3 0 5 2 1 サンプル出力2 YES 問題文中の例である。 サンプル入力3 1 4 0 2 1 3 サンプル出力3 NO サンプル入力4 2 2 0 1 3 2 サンプル出力4 YES $2$ 回半分に折る。
[ { "submission_id": "aoj_2796_4958013", "code_snippet": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<bitset...
aoj_2792_cpp
B: イカったー / SNS 問題文 AOR イカちゃんは最近少し機嫌が悪い。 どうやら、”イカったー”のフォロー数とフォロワー数の比が気に入らないようだ 。現在、AOR イカちゃんのフォロー数は $A$ 人、フォロワー数は $B$ 人であり、比は $A:B$ である。 そこで、AOR イカちゃんはフォロー数とフォロワー数の比が気に入った整数比になるように、 フォロー数 を増減させることにした。 なお気に入った整数比とは、比に含まれるどちらの値も $1$ 以上 $N$ 以下の整数となるように表せる比である。 しかし、AOR イカちゃんはできるだけフォロー数を変更したくないので、変更前との差の絶対値をできるだけ小さくしたい。 AOR イカちゃんの機嫌を良くするために、少なくともフォロー数をいくつ変更する必要があるかを求めるプログラムを作成せよ。 入力 $A \ B \ N$ 入力の制約 $1 \le A, \ B \le 10^{12}$ $1 \leq N \leq 100 $ 出力 気に入った整数比にできる、$A$ の変化量の絶対値の最小値を出力せよ。 サンプル サンプル入力1 19 30 3 サンプル出力1 1 サンプル入力2 3 7 7 サンプル出力2 0 サンプル入力3 3 7 1 サンプル出力3 4 サンプル入力4 102 30 3 サンプル出力4 12 フォローを $12$ 人減らすことで $90:30 \ (=3:1)$ になり、比の大きい方の数字が $3$ 以下となります。 このとき、変化量は $12$ です。 サンプル入力5 3 4 2 サンプル出力5 1 一人フォローを外すと $2:4 \ (=1:2)$ に、フォローすると $4:4 \ (=1:1)$ になり、どちらも増減の絶対値は $1$ でそれが答えです。 サンプル入力6 1 100 2 サンプル出力6 49 最低でも $1$ 人はフォローしていなければいけない事に注意してください。
[ { "submission_id": "aoj_2792_2000870", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\nlong long cl(long long a,long long b,long long n){\n long long na=a+(b-a%b);\n if(na/b>n){\n na=n*b;\n return abs(a-na);\n }else{\n long long na2=a-a%b;\n if(na2==0) return abs(a-na);\n ...
aoj_2788_cpp
Multisect We are developing the world's coolest AI robot product. After the long struggle, we finally managed to send our product at revision $R_{RC}$ to QA team as a release candidate. However, they reported that some tests failed! Because we were too lazy to set up a continuous integration system, we have no idea when our software corrupted. We only know that the software passed all the test at the past revision $R_{PASS}$. To determine the revision $R_{ENBUG}$ ($R_{PASS} < R_{ENBUG} \leq R_{RC}$) in which our software started to fail, we must test our product revision-by-revision. Here, we can assume the following conditions: When we test at the revision $R$, the test passes if $R < R_{ENBUG}$, or fails otherwise. It is equally possible, which revision between $R_{PASS} + 1$ and $R_{RC}$ is $R_{ENBUG}$. From the first assumption, we don't need to test all the revisions. All we have to do is to find the revision $R$ such that the test at $R - 1$ passes and the test at $R$ fails. We have $K$ testing devices. Using them, we can test at most $K$ different revisions simultaneously. We call this "parallel testing". By the restriction of the testing environment, we cannot start new tests until a current parallel testing finishes, even if we don't use all the $K$ devices. Parallel testings take some cost. The more tests fail, the more costly the parallel testing becomes. If $i$ tests fail in a parallel testing, its cost is $T_i$ ($0 \leq i \leq K$). And if we run parallel testings multiple times, the total cost is the sum of their costs. Of course we want to minimize the total cost to determine $R_{ENBUG}$, by choosing carefully how many and which revisions to test on each parallel testing. What is the minimum expected value of the total cost if we take an optimal strategy? Input The input consists of a single test case with the following format. $R_{PASS}$ $R_{RC}$ $K$ $T_0$ $T_1$ ... $T_K$ $R_{PASS}$ and $R_{RC}$ are integers that represent the revision numbers of our software at which the test passed and failed, respectively. $1 \leq R_{PASS} < R_{RC} \leq 1,000$ holds. $K$ ($1 \leq K \leq 30$) is the maximum number of revisions we can test in a single parallel testing. $T_i$ is an integer that represents the cost of a parallel testing in which $i$ tests fail ($0 \leq i \leq K$). You can assume $1 \leq T_0 \leq T_1 \leq ... \leq T_K \leq 100,000$. Output Output the minimum expected value of the total cost. The output should not contain an error greater than 0.0001. Sample Input 1 1 10 2 1 1 1 Output for the Sample Input 1 2.0 Sample Input 2 1 100 1 100 100 Output for the Sample Input 2 670.7070707 Sample Input 3 100 200 4 1 1 2 2 3 Output for the Sample Input 3 4.6400000 Sample Input 4 2 3 4 1 2 3 4 5 Output for the Sample Input 4 0.0 Sample Input 5 998 1000 4 10 100 1000 10000 100000 Output for the Sample Input 5 55.0
[ { "submission_id": "aoj_2788_10853223", "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;\n//using namespace __gnu_pbds; //new version c++\n//using namespace pb_ds;\n\n#define PB push_back\n#define MP make_pair...
aoj_2797_cpp
G: DAG トリオ / DAG Trio この問題は D: DAG Trio (Easy) と制約のみが異なる同じ設定の問題です。 プロローグ 弟は最近「だぐとりお」が欲しいとしきりに呟いています。 心配になった兄が調べたところ、弟のクラスでは $k$-DAG (有向グラフであって、ある $k$ 個の辺を削除すると、辺の向きを無視したときの連結成分数を $k$ にでき、かつそれら全てが DAG である) が流行っており、 3-DAG を特に DAG トリオと呼ぶことを突き止めました。 弟に尊敬されたい兄は、与えられたグラフが DAG トリオかどうかを判別するプログラムを作成することにしました。 問題文 $N$ 頂点 $M$ 辺の有向グラフが与えられます。 各頂点には $1$ から $N$ まで番号が振られています。 各有向辺には $1$ から $M$ まで番号が振られています。 有向辺 $i$ は頂点 $a_i$ から $b_i$ に向かいます。 グラフは連結かつ単純です (辺の向きを無視すると、任意の 2 点間に道があり自己ループと多重辺がありません)。 与えられたグラフが DAG トリオならば ”YES”、そうでないなら ”NO” を出力してください。 入力 $N \ M$ $a_1 \ b_1$ $a_2 \ b_2$ $\vdots$ $a_M \ b_M$ 制約 $3 \le N \le 500$ $\max(3, N−1) \le M \le 30000$ $1 \le a_i, b_i \le N$ グラフは辺の向きを無視したときに連結である。 各 $i$ に対して$a_i \neq b_i$ 異なる $i, j$ に対して $\{a_i, b_i\} \neq \{a_j, b_j\}$ 出力 ”YES” または ”NO” を $1$ 行で出力してください。 サンプル サンプル入力1 3 3 1 2 2 3 3 1 サンプル出力1 YES サンプル入力2 6 7 1 2 2 3 4 3 4 5 5 6 6 4 3 6 サンプル出力2 YES サンプル入力3 7 10 4 2 4 7 4 6 2 7 2 5 2 1 5 6 1 3 6 3 4 3 サンプル出力3 NO サンプル入力4 4 4 1 2 3 2 4 3 2 4 サンプル出力4 YES サンプル入力5 8 9 5 1 3 8 1 2 4 8 4 7 7 5 6 5 3 2 4 2 サンプル出力5 YES
[ { "submission_id": "aoj_2797_2238723", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n#define MAX_N 500\n#define MAX_M 50000\nstruct edge{ int from,to,id; };\n\nint N,M;\nint a[MAX_M],b[MAX_M];\n\nvector<edge> G[MAX_N];\n\nbool visited[MAX_N];\nint depth[MAX_N];\nint cnt[MAX_N];\nint par[MA...
aoj_2794_cpp
D: DAG トリオ / DAG Trio この問題は G: DAG Trio (Hard) と制約のみが異なる同じ設定の問題です。 プロローグ 弟は最近「だぐとりお」が欲しいとしきりに呟いています。 心配になった兄が調べたところ、弟のクラスでは $k$-DAG (有向グラフであって、ある $k$ 個の辺を削除すると、辺の向きを無視したときの連結成分数を $k$ にでき、かつそれら全てが DAG である) が流行っており、 3-DAG を特に DAG トリオと呼ぶことを突き止めました。 弟に尊敬されたい兄は、与えられたグラフが DAG トリオかどうかを判別するプログラムを作成することにしました。 問題文 $N$ 頂点 $M$ 辺の有向グラフが与えられます。 各頂点には $1$ から $N$ まで番号が振られています。 各有向辺には $1$ から $M$ まで番号が振られています。 有向辺 $i$ は頂点 $a_i$ から $b_i$ に向かいます。 グラフは連結かつ単純です (辺の向きを無視すると、任意の 2 点間に道があり自己ループと多重辺がありません)。 与えられたグラフが DAG トリオならば ”YES”、そうでないなら ”NO” を出力してください。 入力 $N \ M$ $a_1 \ b_1$ $a_2 \ b_2$ $\vdots$ $a_M \ b_M$ 制約 $3 \le N \le 500$ $\max(3, N−1) \le M \le 1000$ $1 \le a_i, b_i \le N$ グラフは辺の向きを無視したときに連結である。 各 $i$ に対して$a_i \neq b_i$ 異なる $i, j$ に対して $\{a_i, b_i\} \neq \{a_j, b_j\}$ 出力 ”YES” または ”NO” を $1$ 行で出力してください。 サンプル サンプル入力1 3 3 1 2 2 3 3 1 サンプル出力1 YES サンプル入力2 6 7 1 2 2 3 4 3 4 5 5 6 6 4 3 6 サンプル出力2 YES サンプル入力3 7 10 4 2 4 7 4 6 2 7 2 5 2 1 5 6 1 3 6 3 4 3 サンプル出力3 NO サンプル入力4 4 4 1 2 3 2 4 3 2 4 サンプル出力4 YES サンプル入力5 8 9 5 1 3 8 1 2 4 8 4 7 7 5 6 5 3 2 4 2 サンプル出力5 YES
[ { "submission_id": "aoj_2794_2503049", "code_snippet": "#include <bits/stdc++.h>\n#define int long long\n#define N 1000\n#define V_MAX 1000\nusing namespace std;\nconst int INF = 1LL<<55;\nconst int mod = (1e9)+7;\nconst double EPS = 1e-8;\nconst double PI = 6.0 * asin(0.5);\ntemplate<class T> T Max(T &a,T ...
aoj_2789_cpp
Compressed Formula You are given a simple, but long formula in a compressed format. A compressed formula is a sequence of $N$ pairs of an integer $r_i$ and a string $s_i$, which consists only of digits ('0'-'9'), '+', '-', and '*'. To restore the original formula from a compressed formula, first we generate strings obtained by repeating $s_i$ $r_i$ times for all $i$, then we concatenate them in order of the sequence. You can assume that a restored original formula is well-formed. More precisely, a restored formula satisfies the following BNF: <expression> := <term> | <expression> '+' <term> | <expression> '-' <term> <term> := <number> | <term> * <number> <number> := <digit> | <non-zero-digit> <number> <digit> := '0' | <non-zero-digit> <non-zero-digit> := '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' Here, '+' means addition, '-' means subtraction, and '*' means multiplication of integers. Your task is to write a program computing the answer of a given formula modulo 1,000,000,007, where $x$ modulo $m$ is a non-negative integer $r$ such that there exists an integer $k$ satisfying $x = km + r$ and $0 \leq r < m$; it is guaranteed that such $r$ is uniquely determined for integers $x$ and $m$. Input The input consists of a single test case. $N$ $r_1$ $s_1$ $r_2$ $s_2$ ... $r_N$ $s_N$ The first line contains a single integer $N$ ($1 \leq N \leq 10^4$), which is the length of a sequence of a compressed formula. The following $N$ lines represents pieces of a compressed formula. The $i$-th line consists of an integer $r_i$ ($1 \leq r_i \leq 10^9$) and a string $s_i$ ($1 \leq |s_i| \leq 10$), where $t_i$ is the number of repetition of $s_i$, and $s_i$ is a piece of an original formula. You can assume that an original formula, restored from a given compressed formula by concatenation of repetition of pieces, satisfies the BNF in the problem statement. Output Print the answer of a given compressed formula modulo 1,000,000,007. Sample Input 1 1 5 1 Output for the Sample Input 1 11111 Sample Input 2 2 19 2* 1 2 Output for the Sample Input 2 1048576 Sample Input 3 2 1 1-10 10 01*2+1 Output for the Sample Input 3 999999825 Sample Input 4 4 3 12+45-12 4 12-3*2*1 5 12345678 3 11*23*45 Output for the Sample Input 4 20008570
[ { "submission_id": "aoj_2789_10865828", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll INF = 1ll << 60;\n#define REP(i, n) for(ll i=0; i <ll(n); i++)\ntemplate <class T> using V = vector<T>;\ntemplate <class A, class B>\nbool chmax(A &a, B b) { return a < b ...
aoj_2795_cpp
E: 札 / Fuda この問題はリアクティブ問題です。 サーバー側に用意されたプログラムと対話的に応答することで正答を導くプログラムを作成する必要があります。 問題文 湖に浮かぶ $N$ 個の小島からなるビワコという村がある。 各小島には $0$ から $N-1$ まで番号が振られている。 村には島と島の間に架かる橋が $M$ 本あり、$0$ から $M-1$ まで番号が振られている。 橋は双方向に移動可能である。 ここで言う橋とは単に島と島の間の通り道ということであり、取り除くと互いに到達不可能な島の組が増える意味ではない。 最近、橋の両端に掲示板を設置し、その橋を渡った先の島から橋を $1$ 本だけ使って移動できる島 (今いる島を含まない) の番号の書かれた札を貼ることで、 スムーズな移動を実現する計画が持ち上がった。 一枚の札には一つの移動先しか書くことが出来ないため、移動できる島の数だけ札が必要になる。 この札の作成を命じられたあなたは、札は全部で何枚必要かを調べることにした。 村には、かつて全ての橋を架けた橋職人がいる。 また、各島にはその島に住んでいる村人がいる。 村は広大で全ての掲示板を見て回るのは骨が折れるため、本部から電話で彼らに質問をすることで必要な札の枚数を求めることにした。 橋職人への質問では、まず、何番の橋の情報が知りたいかを橋職人に伝える。 すると、どの島とどの島の間に該当する橋を架けたかを確実に教えてくれる (edgクエリ)。 住人への質問では、まず聞きたい島の番号$i$を伝える。 すると、島 $i$ にかかる橋の数とその橋がどこにかかっているかを教えてくれる (lst クエリ)。 しかし、貧弱な通信網を使用しているため、各橋に対して、80% の確率で情報が抜け落ちてしまう。 札の発注まで時間がないので全体で多くても $3N$ 回しか質問ができない。 以上の条件のもとで、必要な札の数を求めるプログラムを書きなさい。 入出力仕様 以降では、あなたが提出したプログラムを「解答」、ジャッジ側が用意したプログラムを「ジャッジ」と呼ぶ。 入出力の詳細な仕様を以下に示すが、先にサンプルを見ると速い。 島と橋の数 ジャッジは、まず島の数 $N$ と橋の数の合計 $M$ を $1$ 行に出力する。 N M 続いて、解答は edg, lst クエリを最大 $3N$ 回、 ans をちょうど 1 回ジャッジに対して送ることができる。 edg クエリ 解答の edg クエリに対し、ジャッジは橋$i$の両端の島の番号を答える。解答は次の形式で出力せよ。 edg i これに対して、ジャッジは次の形式で応答する。 a b $a$, $b$ は橋 $i$ が結ぶ島の番号である。 lst クエリ 解答の lst クエリに対し、ジャッジは小島 $i$ にかかる橋の本数と、それぞれの橋の行き先の島の番号を答える。解答は次の形式で出力せよ。 lst i これに対して、ジャッジは次の形式で応答する。 k v1 v2 v3 … vk $k$ は島 $i$ にかかる橋の数である。 続く $v_1 \cdots v_k$ は頂点$i$から橋を一度だけ使って移動可能な頂点の番号のリストである。 各橋の行き先 $v_j$ に対し、$v_j \ge 0$ の場合はその情報がしっかり伝わったことを表す。$v_j = -1$ の場合は情報が抜け落ちていることを表す。 $-1$ かどうかは乱数で決められ、80% の確率で $-1$ となる。したがって、同じ $i$ に対して複数回このクエリを実行すると、結果は変わりうる。また、順番も不定である。 ans クエリ 解答の ans クエリによって、答えを出力できる。このクエリは 1 度しか行うことが出来ず、結果が正しくない場合は直ちに誤答となる。 このクエリの実行後に、解答は直ちに正常終了せよ。 $X$ を答えとして出力したい場合の形式は以下の通りである。 ans X 制約と注意 $1 \le N \le 100$ $0 \le M \le N(N-1)/2$ 橋は異なる 2 つの島の間に架かっている。 異なる橋が、同じ 2 つの島を結ぶことはない。 ans 以外のクエリは高々 $3N$ 回しか投げられない。 ジャッジプログラムの出力は全て $1$ 行かつスペース区切りの整数からなる。 仕様に従わない出力の結果は不定である。 解答はジャッジの応答をすぐに標準入力で受け取らなければならない。 解答はクエリを出力した直後にバッファを空にせよ。最小の解答プログラム例を下に示す。C、C++以外の言語は各自で調べてほしい。 C #include <stdio.h> int main(){ int n,m; scanf("%d %d", & n, &m); printf("ans 100\n"); // 答えをジャッジに提出 fflush(stdout); // フラッシュ return 0; } C++ #include <iostream> using namespace std; int main(){ int n,m; cin >> n >> m; cout << "ans 100" << endl; // 改行直後にフラッシュ // cout << "ans 100\n" << flush; // 上と同じ } サンプル 解答プログラムとジャッジプログラムの入出力例を以下に示す。 問題文の図中の村に対する答えを求めている。
[ { "submission_id": "aoj_2795_2258530", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\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 mp(a,b) make_pair((a),(b))\n#define pb(a) push_back((a))\n#define all(x) (x).begin(),(x).end()\n#define uniq(x...
aoj_2790_cpp
Non-redundant Drive The people of JAG kingdom hate redundancy. For example, the N cities in JAG kingdom are connected with just $N - 1$ bidirectional roads such that any city is reachable from any city through some roads. Under the condition, the number of paths from a city to another city is exactly one for all pairs of the cities. This is a non-redundant road network :) One day, you, a citizen of JAG kingdom, decided to travel as many cities in the kingdom as possible with a car. The car that you will use has an infinitely large tank, but initially the tank is empty. The fuel consumption of your car is 1 liter per 1 km, i.e. it consumes 1 liter of gasoline to move 1 km. Each city has exactly one gas station, and you can supply $g_x$ liters of gasoline to your car at the gas station of the city $x$. Of course, you have a choice not to visit some of the gas stations in your travel. But you will not supply gasoline twice or more at the same gas station, because it is redundant. Each road in the kingdom has a distance between two cities: the distance of $i$-th road is $d_i$ km. You will not pass the same city or the same road twice or more, of course, because it is redundant. If a quantity of stored gasoline becomes zero, the car cannot move, and hence your travel will end there. But then, you may concern about an initially empty tank. Don't worry. You can start at any gas station of the cities in the kingdom. Furthermore, each road directly connects the gas stations of the its two ends (because the spirit of non-redundancy avoids redundant moves in a city), you therefore can supply gasoline to your car even if your car tank becomes empty just when you arrive the city. Your task is to write a program computing the maximum number of cities so that you can travel under your non-redundancy policy. Input The input consists of a single test case. $N$ $g_1$ $g_2$ ... $g_N$ $a_1$ $b_1$ $d_1$ $a_2$ $b_2$ $d_2$ ... $a_{N-1}$ $b_{N-1}$ $d_{N-1}$ The first line contains an integer $N$ ($1 \leq N \leq 100,000$), which is the number of cities in JAG kingdom. The second line contains $N$ integers: the $i$-th of them is $g_i$ ($1 \leq g_i \leq 10,000$), the amount of gasoline can be supplied at the gas station of the city $i$. The following $N - 1$ lines give information of roads: the $j$-th line of them contains $a_j$ and $b_j$ , which indicates that the $j$-th road bidirectionally connects the cities $a_j$ and $b_j$ ($1 \leq a_j, b_j \leq N, a_j \ne b_j$) with distance $d_j$ ($1 \leq d_j \leq 10,000$). You can assume that all cities in the kingdom are connected by the roads. Output Print the maximum number of cities you can travel from any city under the constraint such that you can supply gasoline at most once per a gas station. Sample Input 1 5 5 8 1 3 5 1 2 4 2 3 3 2 4 3 1 5 7 Output for the Sample Input 1 4 Sample Input 2 2 10 1 1 2 10 Output for the Sample Input 2 2 Sample Input 3 5 1 3 5 1 1 1 2 5 2 3 3 2 4 3 1 5 5 Output for the Sample Input 3 3
[ { "submission_id": "aoj_2790_10907762", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nconst ll INF = 1ll << 60;\n#define REP(i,n) for(ll i=0; i<ll(n); i++)\ntemplate <class T> using V = vector<T>;\ntemplate <class A, class B> void chmax(A& l, const B& r){ if(l < r) ...
aoj_2800_cpp
C : Mod!Mod! 物語 じゃん!探してますよ目撃証言! 会津に怪盗が現れた!みんなのウマウマ棒が盗まれた!犯人は誰だ!? 解きあかせ!Mod!Mod! 問題文 "アイズ"...それは選ばれし者の心に膨らむ奇跡のつぼみ...。特殊能力"アイズ"を使えばどんなものだって盗むことができる。 会津一の大怪盗、あいずまるは世界を謎で満たすために n 人の探偵から「ウマウマ棒」を盗むことにした。ウマウマ棒とはあいずまるが大好きなただのお菓子であり、 n 人の探偵はそれぞれ数本のウマウマ棒を持っている。また、あいずまるは強欲なためそれぞれの探偵からウマウマ棒を盗むとき、その探偵が所持する全てのウマウマ棒を盗む。 ウマウマ棒の3本同時食いにハマっているあいずまるは、3本以上のウマウマ棒が手元にあるとき、誘惑に負けて手持ちのウマウマ棒が3本未満になるまで、3本ずつウマウマ棒を食べてしまう。しかし、あいずまるは手元にウマウマ棒がないとショックでアイズを失ってしまい、それ以上ウマウマ棒を盗むことができなくなってしまう。つまり、ウマウマ棒を盗むためには手持ちのウマウマ棒の本数を1本以上にしておく必要があり、0本になるとそれ以上ウマウマ棒を盗むことができなくなる。 少しでも多くの探偵からウマウマ棒を盗みたいあいずまるは、どの探偵から順にウマウマ棒を盗むかによって何人の探偵からウマウマ棒を盗めるのかが変わることに気づいた。しかし、あいずまるには難しいことは分からない。「ハテー?」あいずまるの優秀な部下であるあなたは、あいずまるの代わりに最大で何人の探偵からウマウマ棒を盗むことができるのかを求めるプログラムを書いてあげることにした。 探偵の人数 n と、 n 人の探偵からそれぞれ何本のウマウマ棒を盗むのかが与えられるので、最適な順番で探偵からウマウマ棒を盗んだとき、最大で何人の探偵からウマウマ棒を盗むことができるかを出力するプログラムを作成せよ。ただし、はじめの手持ちのウマウマ棒の本数は0であるが、最初に限り手持ちが0本でもウマウマ棒を盗むことができるとする。 入力形式 入力は2行からなり、以下の形式で与えられる。 n a_1 a_2 … a_n 1行目には、ウマウマ棒を盗む探偵の数である整数 n が与えられる。 2行目には、各探偵から盗むウマウマ棒の本数 n 個が空白区切りで与えられる。 制約 1 ≤ n ≤ 500{,}000 1 ≤ a_i ≤ 9 ( 1 ≤ i ≤ n ) 出力形式 最適な順番で探偵からウマウマ棒を盗んだとき、最大で何人の探偵からウマウマ棒を盗むことができるか一行に出力せよ。 入力例1 6 2 5 2 5 2 1 出力例1 5 2 5 1 2 5の順で盗むと5人から盗むことができる。どのような順序で盗んでも6人から盗むことはできない。 入力例2 3 3 6 9 出力例2 1 どの1人から盗んでも手持ちのウマウマ棒の本数は0になってしまい、アイズを失ってしまう。 入力例3 6 1 2 3 4 5 6 出力例3 6
[ { "submission_id": "aoj_2800_8322213", "code_snippet": "#include <iostream>\nusing namespace std;\n\nlong long N;\nlong long A[1 << 19];\nlong long C[3];\n\nint main() {\n\t// Step 1. Input\n\tcin >> N;\n\tfor (int i = 1; i <= N; i++) cin >> A[i];\n\tfor (int i = 1; i <= N; i++) C[A[i] % 3] += 1;\n\n\t// St...
aoj_2801_cpp
D: 日焼け - Suntan - 物語 あいずにゃんは若ヶ松高校のプログラミングコンテスト部、通称ぷろこん部に所属する2年生である。天使のようにかわいい。 あいずにゃんは今年の夏フェスに参加する予定なので、聴きに行くバンドのスケジュールを立てた。 ここで心配なのが日焼けだ。 ライブはすべて野外で行われるが、あいずにゃんは日焼けしやすい体質なので長時間野外で紫外線を浴びすぎるとすぐ日焼けしてしまう。 ライブがない間は屋内に避難することでできるだけ紫外線を回避するつもりだが、ライブ中はどうしても日に当たってしまう。 そこであいずにゃんは日焼け止めを塗ることで紫外線対策をすることを考えた。 問題 日焼け止めを塗ると、塗った時間から T 分間だけその効果を得ることができる。 日焼け止めは1回しか塗ることができないため、効果的に使いたい。 あいずにゃんは、ライブの開始時刻から終了時刻までは野外にいて、それ以外では屋内にいる。 あいずにゃんが聴く予定のライブスケジュールが与えられるので、野外にいる間で日焼け止めの効果を得られる最大の時間の長さを求めよ。 入力形式 入力は次の形式で与えらえる。 T N s_1 t_1 ... s_N t_N 1行目では日焼け止めの効果を得られる時間を表す整数 T が与えられる。 2行目ではあいずにゃんが聴くライブの数を表す整数 N が与えられる。 続く N 行には、あいずにゃんが i 番目に聴くライブの開始時刻を表す整数 s_i と終了時刻を表す整数 t_i が空白区切りで与えられる。 制約 1 ≤ T ≤ 10^{15} 1 ≤ N ≤ 10^5 0 ≤ s_i < t_i ≤ 10^{15} ( 1 ≤ i ≤ N ) (i+1) 番目のライブの開始時刻は i 番目のライブの終了時刻と同じかそれよりも遅い。すなわち、 t_i ≤ s_{i+1} ( 1 ≤ i < N ) 出力 野外にいる間で日焼け止めの効果を得られる最大の時間の長さを1行で出力せよ。 入力例1 20 1 0 10 出力例1 10 入力例2 20 1 0 100 出力例2 20 入力例3 9 3 1 5 9 11 13 20 出力例3 7 入力例4 25 5 9 12 15 20 21 25 28 40 45 60 出力例4 21
[ { "submission_id": "aoj_2801_10570469", "code_snippet": "#include<bits/stdc++.h>\n#define rep(i,s,n) for (int i = (int)(s); i < (int)(n); i++)\n#define all(v) begin(v),end(v)\nusing namespace std;\nusing ll = long long;\n\nbool chmin(auto &a, auto b){ return a > b ? a = b, 1 : 0; }\nbool chmax(auto &a, auto...
aoj_2806_cpp
B: 重さの範囲 問題 重さの異なる $N$ 色のボールがキューにたくさん入っています。キューには、先頭から $1,2,3, \dots ,N-1,N,1,2,3, \dots ,N-1,N,1,2,3, \dots$ というふうに昇順にボールが並んでいて、 色 $N$ のボールの後ろにはまた色 $1$ のボールから順に入っています。 同じ色のボールはそれぞれ同じ重さであり、色 $i$ のボールの重さは $A_i$ です。 この状態から、キューの先頭からボールを $M$ 個取り出し、これを 1 つのグループとする作業を繰り返します。 そして、キューから取り出した各色のボールの総数がすべて等しくなったときにグループを作る作業をやめます。 なお、キューには十分な数のボールが入っており、グループを作る作業をやめるまでにキューの中身が空になることはありません。 たとえば、 $N=8, M=2$ のとき、{色1,色2}, {色3,色4}, {色5,色6}, {色7,色8} の 4 グループができます (このとき各色のボールは 1 つずつ存在する)。 $N=4, M=3$ のとき、{色1,色2,色3}, {色4,色1,色2}, {色3,色4,色1}, {色2,色3,色4} の $4$ グループができます (このとき各色のボールはそれぞれ3つずつ存在する)。 このとき、各グループにおいて、 含まれるボールの重さの最大値と最小値の差をそのグループの 重さの範囲 と呼ぶことにします。 各グループの重さの範囲の総和を出力してください。 制約 $1 \le N \le 1000$ $1 \le M \le N$ $0 \le A_i \le 100$ 入力は全て整数である 入力形式 入力は以下の形式で与えられます。 $N \ M$ $A_1 \cdots A_N$ 出力 答えを 1 行で出力してください。また、末尾に改行も出力してください。 サンプル サンプル入力 1 8 2 23 61 57 13 91 41 79 41 サンプル出力 1 170 重さの範囲の総和は $38+44+50+38=170$ となります。 サンプル入力 2 4 3 72 46 67 5 サンプル出力 2 222 重さの範囲の総和は $26+67+67+62=222$ となります。 サンプル入力 3 4 2 1 2 3 5 サンプル出力 3 3 重さの範囲の総和は $1+2=3$ となります。
[ { "submission_id": "aoj_2806_3448657", "code_snippet": "#include<iostream>\n#include<queue>\nusing namespace std;\n#define inRange(x,a,b) (a <= x && x < b)\n\nint main(){\n int n, m;\n cin >> n >> m;\n int a[n];\n for(int i = 0; i < n; i++) cin >> a[i];\n priority_queue<pair<int,int>> l, r;\...
aoj_2807_cpp
C: Fractal Tree 問題 AORイカちゃんは、フラクタルな(自己相似的な)構造を持つ根付き木が好きである。 $N$ 頂点から成る重み付き根付き木 $T$ を用いて、以下のようなフラクタル構造を持つ根付き木 $T'$ を表現することを考える。 $T'$ は、$T$ の各頂点 $x$ に対して、$x$ を根として $T$ と同様の木構造 (コストも同じ) を持つ木を付け加えたものである。 $T'$の根は $T$ のものと同じものである。 こうして表現される木は例えば下図のようになる。 AOR イカちゃんは、$T'$ に対して深さ優先探索をしようとしているが、全ての頂点を辿ると時間がとてもかかることに気づいた。 そこで、深さ優先探索時の遷移の際に確率 $p$ で遷移し、確率 $1-p$ で遷移しない方針で深さ優先探索を行い、いくつかのノード訪問をサボることにした。 $T$ と確率 $p$ が与えられるので、$T’$ に対して深さ優先探索を行う際に辿る全ての辺のコストの和の期待値を求めよ。 $T$ の情報は頂点数 $N$ と $N-1$ 本の辺の情報で与えられ、頂点 $1$ が根である。 各頂点は $1,2,\dots,N$ とラベリングされており、 $i \ (1 \le i \le N-1)$ 番目の辺は頂点 $x_i$ と $y_i$ をコスト $c_i$ で結んでいる。 今回の問題で扱う、確率 $p$ で子に遷移する深さ優先探索の非決定的アルゴリズムは以下のように表現される。 出力される $\mathrm{answer}$ が辿る辺のコストの総和である。 空のスタック $S$ を用意する。 $\mathrm{answer}=0$ とする $S$ に $T'$ の根頂点をプッシュする。 $S$ の先頭の要素を取り出し、これを $x$ とする。 $x$ の各子 $c$ に対し、それぞれ確率 $p$ で次の操作を行い、確率 $1-p$ で何もしない。 $S$ に頂点 $c$ を追加する。そして $\mathrm{answer}$ に $x$ から $c$ に繋がっている辺の重みを加える。 Sが空でなければ、3. に遷移する。 $\mathrm{answer}$ を出力する。 制約 $2 \le N \le 10^5$ $0 \le p \le 1.0$ (小数点第 2 位まで与えられる。) $1 \le x_i,y_i \le N$ $1 \le c_i \le 1000$ $c_i$ は整数である 与えられるグラフは $N$ 頂点の根付き木である。すなわち、頂点 $N$、辺数 $N-1$、連結という性質を持つグラフであり、頂点 $1$ が根である。 入力形式 入力は以下の形式で与えられる。 $p$ $N$ $x_1 \ y_1 \ c_1$ $\vdots$ $x_{N-1} \ y_{N-1} \ c_{N-1}$ 出力 答えを 1 行で出力せよ。相対誤差または絶対誤差が $10^{-6}$ 以下なら AC となる。また、末尾に改行も出力せよ。 サンプル サンプル入力1 0.75 4 1 2 1 2 3 3 3 4 10 サンプル出力1 24.8569335938 サンプル入力2 0.75 4 1 2 1 1 3 3 3 4 10 サンプル出力2 35.0390625 問題文の図の木を与える例である。
[ { "submission_id": "aoj_2807_9117947", "code_snippet": "#include <bits/stdc++.h>\n#include <vector>\nusing namespace std;\nint inf=100000000;\nusing ll=long long;\nusing pint=pair<int,int>;\nusing pll=pair<ll,ll>;\nusing pque_int=priority_queue<int>;\n#define rep(i,n) for (int i=0; i < (int) n; i++)\n#defin...
aoj_2804_cpp
G: 最小包含矩形 - Minimum Enclosing Rectangle - 物語 みんなぁ、こんにちは! 八森中ぷろこん部の藍座あいりだよぉ。 突然だけど、あいりがこの前解けなかった問題をみんなに解いて欲しいんだぁ~。 この前部活で ICPC2010のA問題 を解いたんだけど、その時の問題が難しかったんだぁ。 あ、ICPCについて説明しなきゃ! ICPCは、い、いんたーなしょなる……ちゅうがくせい……ぷろぐらみんぐ……こんてすとの略で、日本語に直すと、国際中学生対抗プログラミングコンテストらしいよぉ! あいりにはちょっとむずかしい言葉がいっぱいだよぉ。 あいり達はこの世界大会に出るために毎日頑張ってるんだぁ! 部活から帰った後お姉ちゃんにも聞いてみたんだけど、「わ、わからないわ……A問題からわからないなんて……お姉ちゃん失格ね……」って落ち込んじゃって…… でも、「『プロ』の人たちが一杯集まるところを知ってるの、お姉ちゃんちょっと連絡してみるわ。あいり、そこで聞いてみなさい!」って言ってここの合宿を教えてもらったんだぁ~ 。 ここに集まる『プロ』のみんなには簡単かもしれないけど……この問題を解いてほしいの! 問題 長さが 1 の正方形が N 個、 2 次元平面上にある。これら N 個の正方形を全て内包する、最小面積の長方形を求めよ。 入力形式 入力は N 行からなる。 N n_1 d_1 n_2 d_2 n_3 d_3 ... n_{N − 1} d_{N − 1} 最初の行には、正方形の個数 N が与えられる。 以下 N − 1 行は、正方形の置き方を表している。そのうち上から i 行目は、空白区切りで1つの整数 n_{i} と1つの文字 d_{i} が与えられる。これは i 番目の正方形の置き方を指示している。ここで正方形の番号付けは、最初の正方形を 0 番目とし、その後置いた順番に 1 , 2 , ..., N − 1 と番号付けられるものとする。 0 番目の正方形に対する置き方の指示はない。 i 番目の正方形に対する置き方の指示 n_i, d_i は、 i 番目の正方形を n_i 番目の正方形に対して d_i で示される方向に隣接して置くことを指示する。 ここで、 n_i は i 未満の非負整数である。また、 d_i は 0,1,2,3 のいずれかの値をとり、それぞれ d_i の値が、 0 ならば左側、 1 ならば下側、 2 ならば右側、 3 ならば上側を表す。 以下にそれぞれの入力例に対応した最終的な正方形の配置を図示している。左から入力例1、入力例2、入力例3、入力例4の最終的な正方形の配置となっている。図中の番号は正方形の番号に相当する。また、図中の緑線は求める最小面積の長方形を示している。 制約 入力はすべて整数 1 ≤ N ≤ 100,000 1 ≤ n_i < i ( 1 ≤ i < N ) 0 ≤ d_i ≤ 3 ( 1 ≤ i < N ) 既に正方形が置かれている位置に、新たな正方形を置くような指示は与えられない。 出力形式 与えられた N 個の点を全て包含する最小面積の長方形の面積を1行で出力する。 出力には 10^{−5} 以上の誤差を含んではならない。 入力例1 1 出力例1 1 入力例2 5 0 0 0 1 0 2 0 3 出力例2 8 入力例3 12 0 0 1 0 2 0 3 1 4 1 5 1 6 2 7 2 8 2 9 3 10 3 出力例3 16 入力例4 10 0 2 1 2 2 2 3 2 2 1 5 1 6 1 7 1 8 1 出力例4 30
[ { "submission_id": "aoj_2804_8322784", "code_snippet": "#include <iostream>\n#include <cmath>\n#include <vector>\n#include <algorithm>\nusing namespace std;\n\nstruct Point {\n\tlong long px, py;\n};\n\nPoint operator-(const Point& a1, const Point& a2) {\n\treturn Point{ a1.px - a2.px, a1.py - a2.py };\n}\n...
aoj_2808_cpp
D: パスワード 問題 AOR イカちゃんは英小文字のみからなる強力なパスワードを作りたいと思っています。 友達から $N$ 個の危険なパスワードの例を教えてもらった AOR イカちゃんは、 以下の条件を全て満たすようなパスワードを作ることにしました。 長さは1文字以上である。 どの危険なパスワードの、どの連続した部分文字列とも異なる。 1, 2 の条件を満たした中で、最も短い文字列である。 1,2,3の条件を満たした中で、辞書順に並べたとき先頭に来る文字列である。 AOR イカちゃんに代わって強力なパスワードを生成するプログラムを書いてください。 入力 入力は以下の形式で標準入力から与えられる。 $N$ $S_1$ $\vdots$ $S_N$ 1 行目には、文字列の数を表す整数 $N$ が与えられる。 2 行目からの $N$ 行には、文字列 $S_i$ が与えられる。 $|S_i|$ とは文字列の長さであり、1 文字以上である。 $1 \le N \le 100,000$ を満たす。 $1 \le \sum_{1\le i \le N} |S_i| \le 400,000$ を満たす。 文字列は英小文字のみを含む。 出力 答えを 1 行で出力せよ。また、末尾に改行も出力せよ。 サンプル サンプル入力 1 5 password login admin root master サンプル出力 1 b サンプル入力 2 3 abcdefghijklmnopqrstuvwxy qwertyuiopasdfghjklxcvbnm qawsxedcrfvtgbyhnujmikolp サンプル出力 2 z サンプル入力 3 1 abcdefghijklmnopqrstuvwxyz サンプル出力 3 aa
[ { "submission_id": "aoj_2808_10853905", "code_snippet": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef unsigned long long ull;\n\nstruct RollingHash\n{\n\tvector<ull> hash, pow;\n\tRollingHash(string s, ull _b = 1000000007)\n\t{\n\t\t_b = _b;\n\t\tint sz = s.length();\n\t\thash.assign(sz + 1, 0)...
aoj_2810_cpp
F: カードゲーム 問題 カードを使ったゲームを $Q$ 回行います。 カードには $1 \cdots N$ の数が書かれており、各数が書かれたカードはゲームを行うのに十分な枚数があります。 $i$ 回目のゲームでは、はじめに手札として 2 枚のカードが配られます。 それぞれのカードに書かれている数字は $x_i$ と $y_i$ です。 カードはルールに従って交換することができます。 $j$ 番目 $(1 \le j \le M)$ のルールでは、カード $a_j$ を手数料 $c_j$ 円で別のカード $b_j$ に交換することができます。 各ルールは何回でも用いることができます。また、手数料が足りなくて交換できない場合はありません。 最後に、手札のカードの数字を $R$ で割ったあまりが等しい時、報酬として $z_i$ 円受け取ります。 異なる場合報酬は $0$ 円です。 $Q$ 回ゲームを終えた時に増やすことのできるお金の最大値を求めてください。 制約 $1 \le N \le 10^5$ $0 \le M \le \min(2 \times 10^5, N\times(N-1))$ $2 \le R \le 10$ $1 \le Q \le 10^5$ $1 \le a_j, b_j \le N$ $a_j \neq b_j$ $0 \le c_j \le 10^5$ $1 \le x_i, y_i \le N$ $0 \le z_i \le 10^5$ 入力 入力は以下の形式で標準入力から与えられます。 $N \ M \ R \ Q$ $a_1 \ b_1 \ c_1$ $\vdots$ $a_M \ b_M \ c_M$ $x_1 \ y_1 \ z_1$ $\vdots$ $x_Q \ y_Q \ z_Q$ 出力 ゲームを $Q$ 回行ったときに増やすことのできるお金の最大値を 1 行で出力してください。また、末尾に改行も出力してください。 サンプル 入力例 1 4 4 2 2 1 2 1 2 3 1 3 4 5 4 1 7 1 4 5 2 4 3 出力例 1 7 1 回目のゲームではカード 1 を手数料 1 でカード 2 に交換すると手札のカードを 2 で割った時のあまりがどちらも 0 となり、得られる報酬は 5 となります。 2 回目のゲームではすでに 2 で割ったときのあまりがどちらも 0 であり、得られる報酬は 3 となります。 2 回のゲームの結果、増やせるお金は $5-1+3=7$ です。 入力例 2 4 4 2 1 1 2 1 2 3 1 3 4 5 4 1 7 3 4 2 出力例 2 0 カードの交換での手数料が報酬よりも多いので、交換を行わないのが最適です。
[ { "submission_id": "aoj_2810_10412963", "code_snippet": "// AOJ #2810 Card Game\n// 2025.4.23\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; int c = gc();\n\tdo n = 10*n + (c & 0xf), c = gc();...
aoj_2803_cpp
F: 風呂がオーバーフロー - Overflow of Furo - 物語 温泉宿・パロは自慢の温泉に全ての情熱を注いでおり、風呂のプロフェッショナルを集めている。 風呂のプロたちは主に温泉の配管を管理しており、複数の源泉から1つの大浴場へと繋がる、複雑に入り組んだ配管網を管理・調整している。 配管網の管理・調整業務もなかなかに大変なのだが、風呂のプロたちはその合間を縫って、さらに多くの湯を浴槽へと供給できるよう日々努力を積み重ねていた。 結果、風呂のプロたちは「1本だけ配管をオーバーフローさせることができる」という荒技を習得した。 すなわち、自由に配管1本を選び、その配管の湯量の制限をなくすことができるようになったのだ。 今まで最大湯量を実現する配管網の設定をあなたのプログラムに依存していた風呂のプロたちは、この技術を使ってさらに浴槽への供給湯量を増やすにはどうすればよいか、あなたに再びプログラムを書くよう依頼してきた。 問題 1 つの浴槽、 K 個の源泉、 N 個の結合点を含む配管網がある。 配管網は M 本の配管からなり、配管1つ1つは流すことのできる湯量の制限を持つ。 配管それぞれは湯を流す方向が決められていないので、自由に決めて使ってよい。 N 個の結合点では何本かの配管から流れてきた湯をそれ以外の何本かの配管へと自由な配分で流すことができる。 すべての源泉、および浴槽は一部の配管の端点になっており、源泉からの湯量、および結合点での湯量を調整することで源泉から浴槽へと湯を供給している。 M 個の配管のうち1本だけオーバーフローさせる、すなわち流せる湯量を無限に増やすことで浴槽に供給できるようになる最大湯量はいくらか。 ただし、最大供給湯量を無限に増やすことができる場合も考えられるが、その場合は風呂がオーバーフローしてしまうので、"overfuro"と出力すること。 入力形式 入力は以下の形式で与えられる。 K N M a_1 b_1 c_1 ... a_M b_M c_M 入力は全て整数からなる。 最初の行では源泉の数 K 、結合点の数 N 、配管の数 M が与えられる。 続く M 行のうち i 行目には、 i 番目の配管の情報を表す3つの整数 a_i , b_i , c_i が与えられる。 これは i 番目の配管の両端点がそれぞれ a_i と b_i であり、湯量の制限が c_i であることを示す。 ここで、配管の端点 x が 0 のときは大浴場、 1 から K までのときは x 番目の源泉、 K+1 から K+N までのときは x − K 番目の結合点であることを表す。 制約 1 ≤ K 0 ≤ N N+K ≤ 100 1 ≤ M ≤ (N+K+1)(N+K) / 2 0 ≤ a_i, b_i ≤ K+N a_i ≠ b_i 1 ≤ c_i ≤ 5{,}000 同じ2端点を持つ配管は2つ以上存在しないことが保証される。 与えられる配管網は、配管をオーバーフローさせることなく最低1以上の湯を源泉から浴槽に供給できることが保証される。 出力形式 1本だけ配管をオーバーフローさせて源泉から浴槽への供給湯量を最大化するときの最大供給湯量を1行に出力せよ。 ただし、最大湯量を無限に増やせるときは、"overfuro"と1行に出力せよ。 入力例1 2 2 4 1 3 4 2 4 2 0 3 3 4 0 5 出力例1 8 入力例2 2 3 7 1 0 8 2 0 9 3 0 3 0 4 5 5 0 2 1 3 2 2 4 9 出力例2 overfuro 入力例3 1 1 2 0 2 1 1 2 1 出力例3 1 入力例4 5 0 5 0 1 1 0 2 1 0 3 1 0 4 1 0 5 1 出力例4 overfuro
[ { "submission_id": "aoj_2803_8322658", "code_snippet": "#include <iostream>\n#include <queue>\n#include <vector>\nusing namespace std;\n\nstruct Edge {\n\tint to, cap, rev;\n};\n\nclass MaxFlow {\npublic:\n\tvector<Edge> G[109];\n\tint Dist[109];\n\tbool Used[109];\n\n\tvoid add_edge(int u, int v, int c) {\...
aoj_2802_cpp
E: 鬼畜ババ抜き - Unbalanced Old Maid - 物語 高坂さんと園田さんと南さんは子供の頃からの仲良し3人組だ。3人は沖縄に修学旅行に行くも、台風が来てしまい、海で遊ぶことができないのでババ抜きをすることにした。 園田さんは勝負事には強いが、ポーカーフェイスが苦手なため、自分のカードが引かれるときに無意識にかわいらしい顔芸を披露してしまう。園田さんを愛して止まない高坂さんと南さんは、園田さんの顔芸を見るだけで園田さんの手札が分かってしまうため、園田さんのカードを引くときは、自分が有利になるように引くことができる。 一方、高坂さんと南さんは特に顔芸をしたりはしないので、手札がばれることはない。 よって、高坂さんと南さんのカードを引く人はそれぞれ、高坂さんと南さんが持っているカードの中から等確率で1枚引く。 どう考えても不利な園田さんは、ババ抜きでなかなか勝てないことに違和感を覚えている。 使用するカードの種類数 n と初期の手札が与えられたとき、園田さんが負けとならない確率を求めてあげよう。 問題文 高坂さん、園田さん、南さんの3人は、以下の状況でババ抜きを行う。 使用するカードは、1から n までの整数が書かれた n 種類のカードそれぞれ4枚ずつとジョーカー1枚の合計 4n+1 枚である。 最初の手札が与えられたとき、3人はそれぞれ自分の手札に同一の整数が書かれたカードのペア(2枚組)があるならば、そのようなペアを全て捨てる。 3人は「高坂さんが南さんの手札からカードを引く」、「園田さんが高坂さんの手札からカードを引く」「南さんが園田さんの手札からカードを引く」という順番(ターン)で以下の操作を繰り返す。(このババ抜きでは、カードを引いた人が、次に引かれるというルールであることに注意せよ。) 一人がジョーカーのみを持ち、その人以外の2人の手札が空のとき、ババ抜きは終了し、ジョーカーを持っている人が負けとなる。 カードを引く順番の人の手札が空のとき、ターンを次の人とし、1.に戻る。 そうでなければ、カードを引く順番の人は決められた相手からカードを1枚引く。ただし、相手の手札が空のとき、まだ残っているもう1人からカードを引く。 引いたカードと同じ整数が書かれたカードがカードを引いた人の手札にあれば、その2枚を捨てる。 ターンを次の人とし、1.へ戻る。 ただし、園田さんのカードを引く人(南さんか高坂さん)は、以下の戦略でカード引く。 もし自分のカードと園田さんのカードで同じ整数が書かれたカードがあれば、それらのうち最小の整数が書かれたカードを引く そうでないとき、園田さんがジョーカーでないカードを持っていれば、それらのうち最小の整数が書かれたカードを引く そうでないなら、園田さんはジョーカーしか持っていないので、ジョーカーを引く 高坂さんと南さんのカードを引く人は、それぞれ高坂さんと南さんが持っているカードの中から等確率で1枚引く。 使用するカードの種類数 n と3人の初期の手札が与えられたとき、園田さんが負けとならない確率を求めてあげよう。 入力形式 入力は4行からなり、以下の形式で与えられる。 n m_1 c_{1,1} ... c_{1,m_1} m_2 c_{2,1} ... c_{2,m_2} m_3 c_{3,1} ... c_{3,m_3} 1行目にジョーカー以外のカードの種類数 n が与えられる。 続く入力は3行からなり、2行目に高坂さんの手札、3行目に園田さんの手札、4行目に南さんの手札の情報が与えられる。 i+1 (1 ≤ i ≤ 3) 行目では行頭に手持ちの枚数 m_i 、続けて手持ちのカードを表す m_i 個の整数 c_{i,j} (1 ≤ j ≤ m_i) が空白区切りで与えられる。 0 はジョーカー、 1 から n はカードに書かれた整数を表す。 制約 1 ≤ n ≤ 100 m_1+m_2+m_3 = 4n+1 3人の手札で0はちょうど1つ、 1 から n はちょうど4回ずつ現れる 0 ≤ c_{i,j} ≤ n ( 1 ≤ i ≤ 3 , 1 ≤ j ≤ m_i ) 出力形式 園田さんが負けとならない確率を1行に出力せよ。答えには 10^{−6} を超える絶対誤差があってはならない。 入力例1 1 1 1 3 0 1 1 1 1 出力例1 0.0 高坂さんが南さんのカード1を引き、高坂さんと南さんは手札が空になる。 園田さんはどうしても勝つことはできない。 入力例2 1 2 1 1 1 1 2 0 1 出力例2 0.5 はじめの高坂さんのターンは、高坂さんの手札が既に空なので何もしない。次の園田さんのターンでは、園田さんは南さんの手札のうちそれぞれ0.5の確率でカード1かジョーカーを引く。カード1を引いたとき、園田さんの手札は空となり勝利する。一方ジョーカーを引いたときは、次の南さんのターンで、南さんは確実にカード1を引くので園田さんが負けとなる。 入力例3 2 3 0 1 2 3 1 2 2 3 1 1 2 出力例3 0.5 入力例4 2 2 0 1 6 2 2 2 1 1 1 1 2 出力例4 0.6666666667
[ { "submission_id": "aoj_2802_8322461", "code_snippet": "#include <iostream>\n#include <tuple>\n#include <vector>\nusing namespace std;\n\nint N;\nint M[3], P[3][409];\nint Cnt[3][109];\ndouble prevs[109][109][109][3];\ndouble dp[109][109][109][3];\ndouble Answer = 0.0;\n\nint main() {\n\t// Step 1. Input\n\...
aoj_2809_cpp
E: 卒業式 背景 ある日 Y さんはプログラミングコンテストに参加するため,会場がある大学に向かいました. ところが大学に着くと人がたくさん!なんと今日は卒業式だったのです. 会場に向かおうにも人に押し流されてしまい,自分の進みたい方向に進むことすらできません. 人混みから抜け出すために,Y さんは今いる場所から少しでも遠くに行きたいと考えました. そこで Y さんは状況を以下のような問題として定式化し,競技プログラミングを役に立てることにしました. なお,以下の問題文では Y さんは点 $P$ としてモデル化されています. 問題 座標平面上の原点に点 $P$ が置かれている. 点 $P$ を動かし,できるだけ原点からのマンハッタン距離が遠い位置に移動させたい. はじめに,文字列 $S = s_1s_2 \cdots s_{|S|}$ ($|S|$ は $S$ の文字数) が与えられる. 点 $P$ の移動は,文字列 $S$ の先頭から文字を 1 文字ずつ読み込むことで行う. 文字列 $S$ は文字 'U', 'L', 'D', 'R' からなる. それぞれの文字を読み込んだとき,点 $P$ の移動前の座標を $(x, y)$ とすると, 移動後の点 $P$ の座標はそれぞれ $(x, y+1),\ (x-1, y),\ (x, y-1),\ (x+1, y)$ となる. 各文字を読み込む直前に,魔法をかけるか否かを選択することができる. 魔法には魔法 1 と魔法 2 の二種類がある.文字列 $S$ の $i$ 番目の文字を $s_i$ とすると, $s_i$ を読み込む直前に魔法をかけたときの変化は以下の通りである. 魔法 1 をかけたとき: 全ての $s_j \ (i \le j \le |S|)$ に対し,'U' を 'D' に,'D' を 'U' に置換する. 魔法 2 をかけたとき: 全ての $s_j \ (i \le j \le |S|)$ に対し,'L' を 'R' に,'R' を 'L' に置換する. 直感的には,魔法 1 ではその後の上下の扱いを反転させることができ,魔法 2 では左右の扱いを反転させることができる. ある文字を読み込む前にかける魔法の回数は複数回でも構わない.また,両方の魔法を続けてかけてもかまわない. ただし,文字列 $S$ の文字をすべて読み終えるまでに魔法をかけられる回数は,合計 $K$ 回までである. 詳細はサンプルを参照されたい. 文字列 $S$ の文字をすべて読み終えた後の点 $P$ の座標を $(x',y')$ とするとき,$|x'| + |y'|$ の最大値を求めよ. 制約 $1 \le |S| \le 2000$ $1 \le K \le 2000$ 入力 入力は以下の形式で標準入力から与えられる. $S$ $K$ 出力 $|x'| + |y'|$ の最大値を 1 行で出力せよ.また、末尾に改行も出力せよ. サンプル サンプル入力 1 RRLUDDD 2 サンプル出力 1 7 3 文字目を読み込む直前に魔法 2 をかけると,文字列 $S$ は "RRRUDDD" となる. 続いて 5 文字目を読み込む直前に魔法 1 をかけると,文字列 $S$ は "RRRUUUU" となる. すべての文字を読み終えた後の点 $P$ の座標は $(3, 4)$ となり, 原点からのマンハッタン距離は 7 である.これはこの例の最大値である. サンプル入力 2 LULLLUULLU 1984 サンプル出力 2 10 1 回も魔法をかけなかった場合,$x’ = -6, \ y’ = 4$ となり,$|x’| + |y’| = 10$ である. サンプル入力 3 DRDLUDD 1 サンプル出力 3 5 サンプル入力 4 LURRRLUDLL 1 サンプル出力 4 6
[ { "submission_id": "aoj_2809_9117771", "code_snippet": "#include<bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\n#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)\n#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)\n\ntemplate <typename T> bool chmin(T &a, const...
aoj_2813_cpp
I: Islands Survival 問題 $N$ 頂点 $M$ 辺の単純連結無向グラフがある. 頂点には $1, 2, \dots, N$ と番号がつけられている. 辺には $1, 2, \dots, M$ と番号がつけられており,辺 $i$ は頂点 $a_i$ と $b_i$ をつないでいる. また,辺 $i$ は時刻 $t_i$ に消える.どの辺も通過するために単位時間がかかる. あなたは最初,時刻 0 において頂点 1 にいる. あなたは最適に行動することで,時刻 $T$ までに得られるスコアを最大化したい. スコアは最初 0 であり,イベントは以下に従って起きる. 今の時刻 $t'$ が $t' \geq T$ を満たすならば終了する.そうでなければ 2. へ. $t_i = t'$ なるすべての辺 $i$ が消える. あなたがいる頂点を $v$ とするとき,頂点 $v$ を含む連結成分に含まれる頂点数を $x$ とすると,スコアに $x$ が加算される. あなたは頂点 $v$ と隣接している頂点のいずれかに移動するか,頂点 $v$ に留まる.ただし前者の場合,既に消えた辺を使うことはできない. $t' \gets t' + 1$ として 1. へ. 得られるスコアの最大値を求めよ. 制約 $2 \le N \le 10^5$ $N - 1 \le M \le \min(2 \times 10^5, N(N - 1) / 2)$ $1 \le T \le 10^5$ $1 \le a_i , b_i \le N$ $1 \le t_i \le T$ 与えられるグラフは単純かつ連結. 入力形式 入力は以下の形式で与えられる. $N \ M \ T$ $a_1 \ b_1 \ t_1$ $\vdots$ $a_M \ b_M \ t_M$ 出力 スコアの最大値を出力せよ.また,末尾に改行も出力せよ. サンプル サンプル入力1 5 4 2 1 2 2 2 3 1 1 4 2 4 5 1 サンプル出力1 8 時刻 0 に 5,時刻 1 に 3 のスコアを得られる. サンプル入力2 4 4 3 1 2 2 2 3 1 3 4 3 4 1 1 サンプル出力2 8 時刻 0 に 4,時刻 1 に 2,時刻 2 に 2 のスコアを得られる.
[ { "submission_id": "aoj_2813_10413041", "code_snippet": "// AOJ #2813 Islands Survival\n// 2025.4.23\n\n#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\n\n#define gc() getchar_unlocked()\n\nint Cin() {\n\tint n = 0; int c = gc();\n\tdo n = 10*n + (c & 0xf), c = gc(); while (c >= '0');\...
aoj_2811_cpp
G: 雨降りバス乗り換え 背景 今日は AOR イカちゃんにとって初となるデートの日だ。 AOR イカちゃんは駅からバスを乗り継ぎ、待ち合わせ場所のバス停に向かう予定である。 AOR イカちゃんが駅に着いた時、不幸にも雨が降ってきた。当初予定していた経路ではバスの待ち時間で濡れてしまい、せっかく整えた身だしなみが台無しになってしまう可能性がある。 そこで、バスの待ち時間が最も少なくなるような経路でバス停まで向かうことにした。 AOR イカちゃんは、待ち合わせ場所のバス停に着くまでにどの程度濡れるのか心配している。 問題 AOR イカちゃんは時刻 $0$ に $S$ 番目のバス停におり、そこから $G$ 番目のバス停まで行きたいと考えている。 バス停の個数 $N$ と、異なるバス停同士を繋ぐ $M$ 個の経路 (※) が与えられる。 バス停には、それぞれ $1, \dots, N$ の番号がふられている。 各経路は、出発地 $u$ 、目的地 $v$ 、出発時刻 $t$ 、所要時間 $c$ の $4$ つの値からなる。 時刻 $t$ に出発地 $u$ にいればバスに乗ることができ、時刻 $t + c$ に目的地 $v$ へ到着する。 バスに乗っていない間、 AOR イカちゃんは雨に濡れてしまう。 雨にぬれる時間が最小となる経路を通り $G$ 番目のバス停へ向かう時、時刻 $0$ から $G$ 番目のバス停に着くまでの間に雨に濡れた時間の合計を出力せよ。 (※) グラフ理論の用語における経路とは頂点と辺の列であるが,ここでは辺の意味で使われていることに注意してほしい。 制約 $2 \le N \le 10^5$ $1 \le M \le 2 \times 10^5$ $1 \le S , G \le N$ $1 \le u_i , v_i \le N$ $0 \le t_i \le 10^5$ $1 \le c_i \le 10^5$ $S \neq G$ $u_i \neq v_i$ 出発地 $u$ と目的地 $v$ が同じであるような経路は存在しない。 出発地 $u$ と目的地 $v$ を結ぶ経路は複数存在する場合がある。 バスの乗り降り、乗り換えに時間はかからないものとする。 $G$ 番目のバス停へ到着する時間は問わない。 $S$ 番目のバス停から $G$ 番目のバス停へ到着できることは保証されている。 入力 入力は以下の形式で標準入力から与えられる。 $N \ M \ S \ G$ $u_1 \ v_1 \ t_1 \ c_1$ $\vdots$ $u_M \ v_M \ t_M \ c_M$ 出力 雨に濡れた最小の時間を 1 行で出力せよ。また、末尾に改行も出力せよ。 サンプル 入力例 1 2 2 1 2 1 2 10 100 1 2 5 500 出力例 1 5 到着時間が遅くても、できるだけ雨に濡れない経路を選ぶ。 入力例 2 3 2 1 3 1 2 0 123 2 3 123 500 出力例 2 0 乗り換えに時間はかからない。
[ { "submission_id": "aoj_2811_9752318", "code_snippet": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nconst int M = 200001;\nint u[M], v[M], t[M], c[M];\nvector<int> start[M], dest[M], busy_time[M];\n\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n\n #ifdef BACKER\n ...
aoj_2812_cpp
H: ジャンプパーティ 問題 とあるダンスホールで $N$ 人の参加するダンスパーティーが行われる。 そのダンスホールは縦方向に $H$ 個、横方向に $W$ 個のグリッドに分けられており、 左上を $(0,0)$、上から $r$ マス、左から $c$ マス目のグリッドの座標を $(r,c)$ と表す。 $i$ 番目の参加者の初期位置は $(R_i, C_i)$ であり、$(i,j)$ のグリッドには $(r_{ij}, c_{ij})$ が書かれている。 各参加者は、無限に続く音楽に合わせて次のように同時に移動を行う。 その時にいる座標が $(i,j)$ のとき、$(r_{ij}, c_{ij})$ へジャンプで移動する。 それぞれのグリッドは狭く、2 人以上の参加者が同時に同じグリッドに移動すると衝突してしまう。ただし、空中で衝突することは無いとする。 これを聞いたあなたは、ジャンプ後に 2 人以上の参加者が衝突してしまわないかと心配になった。 そこで、衝突が起こる可能性があるか、あるならば何回目のジャンプの後に衝突が起こるかを求めることにした。 制約 $1 \le H,W \le 500$ $0 \le N \le H \times W$ $0 \le r_{ij} < H \ (0 \le i < H, 0 \le j < W)$ $0 \le c_{ij} < W \ (0 \le i < H, 0 \le j < W)$ $0 \le R_i < H \ (0 \le i < N)$ $0 \le C_i < W \ (0 \le i < N)$ 参加者の初期位置は相異なる 入力形式 入力は以下の形式で与えられる。 $H \ W \ N$ $r_{00} \ c_{00} \ \cdots \ r_{0 \ W-1} \ c_{0 \ W-1}$ $\vdots$ $r_{H-1 \ 0} \ c_{H-1 \ 0} \ \cdots \ r_{H-1 \ W-1} \ c_{H-1 \ W-1}$ $R_0 \ C_0$ $\vdots$ $R_{N-1} \ C_{N-1}$ この問題では入力ファイルが非常に大きくなることがあることに注意せよ。 C++ なら このページ を参考にすると良いかもしれない。 出力 衝突が起こる場合は何回目のジャンプの後に起こるかを 1 行で出力せよ。 そうでない場合は -1 を出力せよ。 また、末尾に改行を出力せよ。 サンプル サンプル入力 1 2 2 2 1 0 0 1 0 0 1 0 0 0 0 1 サンプル出力 1 -1 サンプル入力 2 2 2 2 1 0 0 1 0 0 1 0 0 0 1 1 サンプル出力 2 1
[ { "submission_id": "aoj_2812_10191609", "code_snippet": "// AOJ #2812\n// Jump Party 2025.2.5\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 n = 10*...
aoj_2817_cpp
B: だんすなうwww - Dance Now! - 物語 前回のラボライフ!ダイガクイン!! マスターアイドルたちが競う最大の大会「ラボライフ」の前哨戦ともいえるイベント「マスターアイドルワールド」で9位を取ってしまったドサンコスノー。 キレのあるダンスは「9位の舞」、渾身の決めポーズも「9位の構え」などと冷やかされてしまい、心に深い傷を負ってしまう。 来たるラボライフ予備予選では絶対に9位をとるわけにはいかない…… 決意を新たにしたドサンコスノーは、自分たちの特技「セルフコントロール」に磨きをかけ、決戦の地へと向かうのだった。 問題 N 組のユニットが競う大会「ラボライフ」が開かれる。 この大会では、スマイル・ピュア・クールの3つの部門で別々に勝負が行われ、それぞれの勝負で得た得点の合計が高い順に総合順位が決定される。 スマイル部門の順位はそれぞれのユニットが持つスマイル値の値が高い順に決定される。 ピュア・クール部門でも同様にピュア値・クール値が高い順に順位が決定される。 順位に応じた得点設定は全部門で共通しており、ある部門で i 位のユニットは、その部門では r_i 点を得る。 ここで、順位が i 位のユニットと同じ値を持つユニットが複数ある場合、それらは同率 i 位とみなし、等しく得点 r_i を得る。 より詳細には、 k ユニットが同率 i 位の場合には、 k ユニットが等しく得点 r_i を得て、 r_{i+1} から r_{i+k-1} までの得点を得るユニットはいない。 また、その次に値が大きいユニット (たち) は得点 r_{i+k} を得る。 具体的な例として、例えばそれぞれスマイル値が1, 3, 2, 3, 2の5つのユニットがあり、順位が高い順に10, 8, 6, 4, 2点が得られる場合を考える。 このとき、2番目と4番目のユニットが10点、3番目と5番目のユニットが6点、1番目のユニットが2点を得ることになる。 ラボライフ予備予選に参加するユニット・ドサンコスノーは「ラボライフは遊びじゃない」と考えているため、真っ先に大会にエントリーし、1番目のユニットとなった。 しかし、大会に参加する N 組すべてのスマイル値・ピュア値・クール値 (以降3値と呼ぶ) の情報を入手したところ、自分たちの総合順位が (同率) 9位であることがわかった。 ドサンコスノーは特技「セルフコントロール」により3値のうちいずれか1つの値を任意の値だけ上昇させることができるが、あまり上げすぎると疲れてしまい本戦に影響するので、できるだけ上昇値を小さくしたい。 ドサンコスノーはとにかく9位を脱したいので、同率8位以上になるようにセルフコントロールによって3値のいずれか1値を上げるとき、上げる必要のある最小の値を求めよ。 入力形式 入力は以下の形式で与えられる。 N r_1 ... r_N s_1 p_1 c_1 ... s_N p_N c_N 1行目はユニットの数を表す整数 N が1行で与えられる。 続く2行目は N 個の整数が空白区切りで与えられる。 i (1 \leq i \leq N) 番目の整数は各部門で順位が i 位だったときに貰える得点 r_i を表す。 続く N 行目のうち、 j 行目には3つの整数 s_j, p_j, c_j が与えられる。これらはそれぞれ j 番目のユニットのスマイル値 s_j 、ピュア値 p_j 、クール値 c_j を表す。 なおドサンコスノーは1番目のユニットであるとする。 制約 9 \leq N \leq 100 100 \geq r_1 > ... > r_N \geq 1 1 \leq s_j, p_j, c_j \leq 100 (1 \leq j \leq N) セルフコントロールする前、ドサンコスノーは9位 (同率9位の場合はあるが、同率8位以上であることはない) 出力形式 セルフコントロールによって3値のどれかを x だけ上げることでドサンコスノーが同率8位以上になるような最小の x を1行に出力せよ。ただし、セルフコントロールによってどの値をどれだけ上げても順位が上げられない場合は "Saiko" と出力せよ。 入力例1 9 9 8 7 6 5 4 3 2 1 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 出力例1 2 例えばセルフコントロールでスマイル値を2だけ上げると、ドサンコスノーの各部門における順位はそれぞれ同率7位、9位、9位となり、3+1+1 = 5点を得る。一方、2番目のユニットの各部門における順位はそれぞれ9位、8位、8位となり、1+2+2 = 5点を得る。他のユニットは6点以上を獲得するため、この2つのユニットが同率8位となり、条件を満たす。 入力例2 9 9 8 7 6 5 4 3 2 1 1 1 1 2 6 9 6 9 2 9 2 6 3 5 8 5 8 3 8 3 5 4 7 4 7 4 7 出力例2 Saiko どの値をどれだけ上げてもドサンコスノーは11点までしか得ることができないが、他のユニットは必ず14点以上を得ることができるため、ドサンコスノーは不動の9位である。
[ { "submission_id": "aoj_2817_10195234", "code_snippet": "// AOJ #2817\n// Dance Now! 2025.2.5\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; \n cin >> N;\n \n vector<int> r(N);\n for (int i = 0; i < N; i++) cin >...
aoj_2814_cpp
J: エナジードリンク 問題 ixmel、Pulmn 兄弟は $N$ 種類のエナジードリンクをそれぞれ 1 本ずつ持っています。 現在、ixmel と Pulmn のエネルギーはともに 0 で、時刻は午前 0 時です。 ixmel と Pulmn はエネルギーが 0 以下のまま正午になると「たいへんなこと」になります。 エネルギーが正の数では「たいへんなこと」にはなりません。 ixmel とPulmn は「たいへんなこと」にならないために、 毎日朝 6 時に 1 本のエナジードリンクを 2 人で分けて飲むことにしました。 i 番目のエナジードリンクを ixmel が飲むとエネルギーが $a_i$ 増えますが、副作用として 24 時間後にエネルギーが $b_i$ 減ってしまいます。 一方、Pulmn が飲むとエネルギーが $b_i$ 増え、24 時間後にエネルギーが $a_i$ 減ります。 また、ixmel と Pulmn は日が変わった直後、エネルギーは 0 になります。 ixmel と Pulmn は今持っているエナジードリンクだけで、どちらか一方、または両方が「たいへんなこと」になるまでの日数を少しでも長くしたいと思いました。 ixmel、Pulmn 兄弟のために最初にどちらか片方、または両方が「たいへんなこと」になるまでの日数の最大値を求めてください。なお、エナジードリンクを飲む時間は無視できるものとし、「たいへんなこと」が起こる日は求める日数に含まないものとします。 入力 入力は $1+n$ 行からなります。 1 行目には 1 個の整数 $N$ が与えられます。 続く $N$ 行の内、$i$ 行目は 2 個の整数 $a_i, b_i$ が与えられます。 制約 $1 \le N \le 10^5$ $1 \le a_i, b_i \le 10^9$ 出力 考えられる「たいへんなこと」になるまでの日数の最大値を出力しましょう。 また、末尾に改行を出力しましょう。 サンプル サンプル入力 1 5 3 5 7 4 11 9 3 7 7 8 サンプル出力 1 4 1 日目から 5 日目に飲むドリンクは、それぞれ 1, 2, 5, 3, 4 とするのが最適です。 各日のエナジードリンクを飲んだあとの (ixmel の体力, Pulmn の体力) は、それぞれ $(3, 5), (2, 1), (1, 3), (3, 2), (-6, -4)$ と変化し、 5 日目に「たいへんなこと」になるので答えは 4 です。 サンプル入力 2 2 1 1 1 1 サンプル出力 2 1 異なる種類で成分が同じエナジードリンクもあります。また、「たいへんなこと」が起こるのは、エネルギーの量が 0 以下の時であることに注意してください。 サンプル入力 2 3 5 9 3 2 7 4 サンプル出力 2 3 4 日目に飲むエナジードリンクはありません。
[ { "submission_id": "aoj_2814_9118078", "code_snippet": "#line 2 \"/Users/noya2/Desktop/Noya2_library/template/template.hpp\"\nusing namespace std;\n\n#include<bits/stdc++.h>\n#line 1 \"/Users/noya2/Desktop/Noya2_library/template/inout_old.hpp\"\nnamespace noya2 {\n\ntemplate <typename T, typename U>\nostrea...
aoj_2815_cpp
K: AOR イカちゃんの成績 問題 AOR イカちゃんは $N$ 回のレポートの点数のみで成績が決まる授業を受けている。 AOR イカちゃんは同じ授業を受けている友達が $M$ 人いて、自分が苦手なテーマのレポートは、そのテーマが得意な友達のレポートを写すことで、その友達と同じ点数を取ることができる。 ただし、他人のレポートを写していることを先生に気付かれてはいけないので、 $N$ 回のうち少なくとも $K$ 回は他人のレポートを写さず、自力でレポートを仕上げなくてはならない。 また、 AOR イカちゃんは友達に迷惑をかけたくないと思ったので友達 $i$ に対してレポートを写すのは $T_i$ 回以下にすることにした。 AOR イカちゃんが自力でレポートを仕上げたときの点数と、友達がレポートを仕上げた時の点数が与えられたときに、 AOR イカちゃんが取ることのできる合計点数の最大値を答えよ。 制約 $1 \le N \le 100$ $0 \le M \le 100$ $0 \le K \le N$ $0 \le a_i \le 100$ $0 \le b_{ij} \le 100$ $0 \le T_i \le N$ 入力 入力は以下の形式で標準入力から与えられる。 $N \ M \ K$ $a_1 \ \cdots \ a_N$ $b_{11} \ \cdots \ b_{1N}$ $\vdots$ $b_{M1} \ \cdots \ b_{MN}$ $T_1 \ \cdots \ T_M$ $a_i$ はAORイカちゃんが $i$ 番目のレポートを仕上げた時の点数を、$b_{ij}$ は友達 $i$ が $j$ 番目のレポートを仕上げた時の点数を表す。 出力 AOR イカちゃんが取ることのできる合計点数の最大値を出力せよ。また、末尾に改行も出力せよ。 サンプル 入力例 1 3 2 2 50 65 70 80 100 80 90 65 45 1 1 出力例 1 225 AOR イカちゃんは 1 回だけ他人のレポートを写すことができるので、友達 2 の 1 つめのレポートを写すことで、 AOR イカちゃんは最高点数を取ることができる。 入力例 2 3 0 3 0 0 0 出力例 2 0 AOR イカちゃんには友達がいないため、レポートを写すことはできない。
[ { "submission_id": "aoj_2815_5147669", "code_snippet": "#line 1 \"test/aizu-online-judge/2815.test.cpp\"\n#define PROBLEM \"https://onlinejudge.u-aizu.ac.jp/problems/2815\"\n\n#include <iostream>\n\n#line 2 \"src/graph/directed/flow/min_cost_flow.hpp\"\n\n/**\n * @file min_cost_flow.hpp\n * @brief Minimum C...