| [ | |
| { | |
| "id": 1, | |
| "name": "answer_A01", | |
| "Python": "N = int(input()) # 入力\nprint(N * N) # 出力\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\t\tint N = sc.nextInt(); // 入力部分\n\t\tSystem.out.println(N * N); // 出力部分\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 2, | |
| "name": "answer_A02", | |
| "Python": "# 入力\nN, X = map(int, input().split())\nA = list(map(int, input().split()))\nAnswer = False\n\n# 全探索(変数 Answer は「既に x が見つかったかどうか」を表す)\nfor i in range(N):\n\tif A[i] == X:\n\t\tAnswer = True\n\n# 出力\nif Answer == True:\n\tprint(\"Yes\")\nelse:\n\tprint(\"No\")\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tint X = sc.nextInt();\n\t\tint[] A = new int[N + 1];\n\t\tfor (int i = 1; i <= N; i++) A[i] = sc.nextInt();\n\n\t\t// 全探索(変数 Answer は「既に x が見つかったかどうか」を表す)\n\t\tboolean Answer = false;\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tif (A[i] == X) Answer = true;\n\t\t}\n\n\t\t// 出力\n\t\tif (Answer == true) System.out.println(\"Yes\");\n\t\telse System.out.println(\"No\");\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 3, | |
| "name": "answer_A03", | |
| "Python": "# 入力\nN, K = map(int, input().split())\nP = list(map(int, input().split()))\nQ = list(map(int, input().split()))\nAnswer = False\n\n# 全探索(Answer は「合計が K になる選び方が見つかったか」を示す)\nfor x in range(N):\n\tfor y in range(N):\n\t\tif P[x] + Q[y] == K:\n\t\t\tAnswer = True\n\n# 出力\nif Answer == True:\n\tprint(\"Yes\")\nelse:\n\tprint(\"No\")\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tint K = sc.nextInt();\n\t\tint[] P = new int[N + 1];\n\t\tint[] Q = new int[N + 1];\n\t\tfor (int i = 1; i <= N; i++) P[i] = sc.nextInt();\n\t\tfor (int i = 1; i <= N; i++) Q[i] = sc.nextInt();\n\n\t\t// 全探索(Answer は「合計が K になる選び方が見つかったか」を示す)\n\t\tboolean Answer = false;\n\t\tfor (int x = 1; x <= N; x++) {\n\t\t\tfor (int y = 1; y <= N; y++) {\n\t\t\t\tif (P[x] + Q[y] == K) Answer = true;\n\t\t\t}\n\t\t}\n\n\t\t// 出力\n\t\tif (Answer == true) System.out.println(\"Yes\");\n\t\telse System.out.println(\"No\");\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 4, | |
| "name": "answer_A04", | |
| "Python": "# 入力\nN = int(input())\n\n# 上の桁から順番に「2 進法に変換した値」を求める\nfor x in [9,8,7,6,5,4,3,2,1,0]:\n\twari = (2 ** x)\n\tprint((N // wari) % 2, end='')\n\n# 最後に改行する\nprint(\"\")\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\n\t\t// 上の桁から順番に「2 進法に変換した値」を求める\n\t\tfor (int x = 9; x >= 0; x--) {\n\t\t\tint wari = (1 << x); // 2 の x 乗\n\t\t\tSystem.out.print((N / wari) % 2); // 割り算の結果に応じて 0 または 1 の出力\n\t\t}\n\t\tSystem.out.println(); // 最後に改行する\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 5, | |
| "name": "answer_A05", | |
| "Python": "# 入力\nN, K = map(int, input().split())\nAnswer = 0\n\n# 全探索\nfor x in range(1, N+1):\n\tfor y in range(1, N+1):\n\t\tz = K - x - y\n\t\tif z >= 1 and z <= N:\n\t\t\tAnswer += 1\n\n# 出力\nprint(Answer)\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tint K = sc.nextInt();\n\n\t\t// 全探索\n\t\tint Answer = 0;\n\t\tfor (int x = 1; x <= N; x++) {\n\t\t\tfor (int y = 1; y <= N; y++) {\n\t\t\t\tint z = K - x - y; // 白いカードに書かれるべき整数\n\t\t\t\tif (z >= 1 && z <= N) Answer += 1;\n\t\t\t}\n\t\t}\n\n\t\t// 出力\n\t\tSystem.out.println(Answer);\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 6, | |
| "name": "answer_A06", | |
| "Python": "N, Q = map(int, input().split())\nA = list(map(int, input().split()))\nL = [ None ] * Q\nR = [ None ] * Q\nfor j in range(Q):\n\tL[j], R[j] = map(int, input().split())\n \nS = [ None ] * (N + 1)\nS[0] = 0\nfor i in range(N):\n\tS[i + 1] = S[i] + A[i]\n \nfor j in range(Q):\n\tprint(S[R[j]] - S[L[j] - 1])\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tint Q = sc.nextInt();\n\t\tint[] A = new int[N + 1];\n\t\tint[] S = new int[N + 1];\n\t\tint[] L = new int[Q + 1];\n\t\tint[] R = new int[Q + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tA[i] = sc.nextInt();\n\t\t}\n\t\tfor (int j = 1; j <= Q; j++) {\n\t\t\tL[j] = sc.nextInt();\n\t\t\tR[j] = sc.nextInt();\n\t\t}\n\n\t\t// 累積和の計算\n\t\tS[0] = 0;\n\t\tfor (int i = 1; i <= N; i++) S[i] = S[i - 1] + A[i];\n\n\t\t// 質問に答える\n\t\tfor (int j = 1; j <= Q; j++) {\n\t\t\tSystem.out.println(S[R[j]] - S[L[j] - 1]);\n\t\t}\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 7, | |
| "name": "answer_A07", | |
| "Python": "# 入力\nD = int(input())\nN = int(input())\nL = [ None ] * N\nR = [ None ] * N\nfor i in range(N):\n\tL[i], R[i] = map(int, input().split())\n\n# 前日比に加算\nB = [ 0 ] * (D+2)\nfor i in range(N):\n\tB[L[i]] += 1\n\tB[R[i]+1] -= 1\n\n# 累積和をとる\nAnswer = [ None ] * (D+2)\nAnswer[0] = 0\nfor d in range(1, D+1):\n\tAnswer[d] = Answer[d - 1] + B[d]\n\n# 出力\nfor d in range(1, D+1):\n\tprint(Answer[d])\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint D = sc.nextInt();\n\t\tint N = sc.nextInt();\n\t\tint[] B = new int[D + 2];\n\t\tint[] L = new int[N + 1];\n\t\tint[] R = new int[N + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tL[i] = sc.nextInt();\n\t\t\tR[i] = sc.nextInt();\n\t\t}\n\n\t\t// 前日比に加算\n\t\tfor (int d = 1; d <= D; d++) B[d] = 0;\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tB[L[i]] += 1;\n\t\t\tB[R[i] + 1] -= 1;\n\t\t}\n\n\t\t// 累積和をとる → 出力\n\t\tint[] Answer = new int[D + 2];\n\t\tAnswer[0] = 0;\n\t\tfor (int d = 1; d <= D; d++) Answer[d] = Answer[d - 1] + B[d];\n\t\tfor (int d = 1; d <= D; d++) System.out.println(Answer[d]);\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 8, | |
| "name": "answer_A08", | |
| "Python": "# 入力(前半)\nH, W = map(int, input().split())\nX = [ None ] * (H)\nZ = [ [ 0 ] * (W + 1) for i in range(H + 1) ]\nfor i in range(H):\n\tX[i] = list(map(int, input().split()))\n\n# 入力(後半)\nQ = int(input())\nA = [ None ] * Q\nB = [ None ] * Q\nC = [ None ] * Q\nD = [ None ] * Q\nfor i in range(Q):\n\tA[i], B[i], C[i], D[i] = map(int, input().split())\n\n# 配列 Z は既に初期化済み\n# 横方向に累積和をとる\n# X[i-1][j-1] などになっているのは、配列が 0 番目から始まるため\nfor i in range(1, H+1):\n\tfor j in range(1, W+1):\n\t\tZ[i][j] = Z[i][j-1] + X[i-1][j-1]\n\n# 縦方向に累積和をとる\nfor j in range(1, W+1):\n\tfor i in range(1, H+1):\n\t\tZ[i][j] = Z[i-1][j] + Z[i][j]\n\n# 答えを求める\nfor i in range(Q):\n\tprint(Z[C[i]][D[i]] + Z[A[i]-1][B[i]-1] - Z[A[i]-1][D[i]] - Z[C[i]][B[i]-1])\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力(前半部分)\n\t\tint H = sc.nextInt();\n\t\tint W = sc.nextInt();\n\t\tint[][] X = new int[H + 2][W + 2];\n\t\tint[][] Z = new int[H + 2][W + 2];\n\t\tfor (int i = 1; i <= H; i++) {\n\t\t\tfor (int j = 1; j <= W; j++) X[i][j] = sc.nextInt();\n\t\t}\n\n\t\t// 入力(後半部分)\n\t\tint Q = sc.nextInt();\n\t\tint[] A = new int[Q + 1];\n\t\tint[] B = new int[Q + 1];\n\t\tint[] C = new int[Q + 1];\n\t\tint[] D = new int[Q + 1];\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tA[i] = sc.nextInt();\n\t\t\tB[i] = sc.nextInt();\n\t\t\tC[i] = sc.nextInt();\n\t\t\tD[i] = sc.nextInt();\n\t\t}\n\n\t\t// 配列 Z の初期化\n\t\tfor (int i = 0; i <= H; i++) {\n\t\t\tfor (int j = 0; j <= W; j++) Z[i][j] = 0;\n\t\t}\n\n\t\t// 横方向に累積和をとる\n\t\tfor (int i = 1; i <= H; i++) {\n\t\t\tfor (int j = 1; j <= W; j++) Z[i][j] = Z[i][j - 1] + X[i][j];\n\t\t}\n\n\t\t// 縦方向に累積和をとる\n\t for (int j = 1; j <= W; j++) {\n\t\t\tfor (int i = 1; i <= H; i++) Z[i][j] = Z[i - 1][j] + Z[i][j];\n\t\t}\n\n\t\t// 答えを求める\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tSystem.out.println(Z[C[i]][D[i]] + Z[A[i]-1][B[i]-1] - Z[A[i]-1][D[i]] - Z[C[i]][B[i]-1]);\n\t\t}\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 9, | |
| "name": "answer_A09", | |
| "Python": "# 入力\nH, W, N = map(int, input().split())\nA = [ None ] * N\nB = [ None ] * N\nC = [ None ] * N\nD = [ None ] * N\nX = [ None ] * (W)\nfor t in range(N):\n\tA[t], B[t], C[t], D[t] = map(int, input().split())\n\n# 各日について加算\nX = [ [ 0 ] * (W + 2) for i in range(H + 2) ]\nZ = [ [ 0 ] * (W + 2) for i in range(H + 2) ]\nfor t in range(N):\n X[A[t]][B[t]] += 1\n X[A[t]][D[t]+1] -= 1\n X[C[t]+1][B[t]] -= 1\n X[C[t]+1][D[t]+1] += 1\n\n# 二次元累積和をとる\nfor i in range(1, H+1):\n\tfor j in range(1, W+1):\n\t\tZ[i][j] = Z[i][j-1] + X[i][j]\nfor j in range(1, W+1):\n\tfor i in range(1, H+1):\n\t\tZ[i][j] = Z[i-1][j] + Z[i][j]\n\n# 出力\nfor i in range(1, H+1):\n\tfor j in range(1, W+1):\n\t\tif j >= 2:\n\t\t\tprint(\" \", end=\"\")\n\t\tprint(Z[i][j], end=\"\")\n\tprint(\"\")\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint H = sc.nextInt();\n\t\tint W = sc.nextInt();\n\t\tint N = sc.nextInt();\n\t\tint[] A = new int[N + 1];\n\t\tint[] B = new int[N + 1];\n\t\tint[] C = new int[N + 1];\n\t\tint[] D = new int[N + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tA[i] = sc.nextInt();\n\t\t\tB[i] = sc.nextInt();\n\t\t\tC[i] = sc.nextInt();\n\t\t\tD[i] = sc.nextInt();\n\t\t}\n\n\t\t// 各日について加算\n\t\tint[][] X = new int[H + 2][W + 2];\n\t\tint[][] Z = new int[H + 2][W + 2];\n\t\tfor (int t = 1; t <= N; t++) {\n\t\t\tX[A[t]][B[t]] += 1;\n\t\t\tX[A[t]][D[t]+1] -= 1;\n\t\t\tX[C[t]+1][B[t]] -= 1;\n\t\t\tX[C[t]+1][D[t]+1] += 1;\n\t\t}\n\n\t\t// 二次元累積和を求める\n\t\tfor (int i = 0; i <= H; i++) {\n\t\t\tfor (int j = 0; j <= W; j++) Z[i][j] = 0;\n\t\t}\n\t\tfor (int i = 1; i <= H; i++) {\n\t\t\tfor (int j = 1; j <= W; j++) Z[i][j] = Z[i][j - 1] + X[i][j];\n\t\t}\n\t\tfor (int j = 1; j <= W; j++) {\n\t\t\tfor (int i = 1; i <= H; i++) Z[i][j] = Z[i - 1][j] + Z[i][j];\n\t\t}\n\n\t\t// 出力\n\t\tfor (int i = 1; i <= H; i++) {\n\t\t\tfor (int j = 1; j <= W; j++) {\n\t\t\t\tif (j >= 2) System.out.print(\" \");\n\t\t\t\tSystem.out.print(Z[i][j]);\n\t\t\t}\n\t\t\tSystem.out.println();\n\t\t}\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 10, | |
| "name": "answer_A10", | |
| "Python": "# 入力\nN = int(input())\nA = list(map(int, input().split()))\nD = int(input())\nL = [ None ] * D\nR = [ None ] * D\nfor d in range(D):\n\tL[d], R[d] = map(int, input().split())\n\n# P[i] を求める\n# P[0] などになっているのは、配列が 0 始まりであるため\nP = [ None ] * N\nP[0] = A[0]\nfor i in range(1, N):\n\tP[i] = max(P[i-1], A[i])\n\n# Q[i] を求める\n# Q[N-1] などになっているのは、配列が 0 始まりであるため\nQ = [ None ] * N\nQ[N-1] = A[N-1]\nfor i in reversed(range(0,N-1)):\n\tQ[i] = max(Q[i+1], A[i])\n\n# それぞれの日について答えを求める\n# (L[d]-1)-1 などになっているのは、配列が 0 始まりであるため\nfor d in range(D):\n\tprint(max(P[(L[d]-1)-1], Q[(R[d]+1)-1]))\n", | |
| "Java": "import java.util.*;\n \nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n \n\t\t// 入力(前半部分)\n\t\tint N = sc.nextInt();\n\t\tint[] A = new int[N + 1];\n\t\tfor (int i = 1; i <= N; i++) A[i] = sc.nextInt();\n \n\t\t// 入力(後半部分)\n\t\tint D = sc.nextInt();\n\t\tint[] L = new int[D + 1];\n\t\tint[] R = new int[D + 1];\n\t\tfor (int d = 1; d <= D; d++) {\n\t\t\tL[d] = sc.nextInt();\n\t\t\tR[d] = sc.nextInt();\n\t\t}\n \n\t\t// P[i] を求める\n\t\tint[] P = new int[N + 1];\n\t\tP[1] = A[1];\n\t\tfor (int i = 2; i <= N; i++) P[i] = Math.max(P[i - 1], A[i]);\n \n\t\t// Q[i] を求める\n\t\tint[] Q = new int[N + 1];\n\t\tQ[N] = A[N];\n\t\tfor (int i = N - 1; i >= 1; i--) Q[i] = Math.max(Q[i + 1], A[i]);\n \n\t\t// それぞれの日について答えを求める\n\t\tfor (int d = 1; d <= D; d++) {\n\t\t\tSystem.out.println(Math.max(P[L[d]-1], Q[R[d]+1]));\n\t\t}\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 11, | |
| "name": "answer_A11", | |
| "Python": "# 整数 x が何番目に存在するかを返す関数\ndef search(x, A):\n\tL = 0\n\tR = N-1\n\twhile L <= R:\n\t\tM = (L + R) // 2\n\t\tif x < A[M]:\n\t\t\tR = M - 1\n\t\tif x == A[M]:\n\t\t\treturn M\n\t\tif x > A[M]:\n\t\t\tL = M + 1\n\treturn -1 # 整数 x が存在しない(注:この問題の制約で -1 が返されることはない)\n\n\n# 入力(配列 X が 0 番目から始まることに注意)\nN, X = map(int, input().split())\nA = list(map(int, input().split()))\n\n# 二分探索を行う(配列が 0 番目から始まるので、答えに 1 を足している)\nAnswer = search(X, A)\nprint(Answer + 1)\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tN = sc.nextInt();\n\t\tX = sc.nextInt();\n\t\tA = new int[N + 1];\n\t\tfor (int i = 1; i <= N; i++) A[i] = sc.nextInt();\n\n\t\t// 二分探索を行う\n\t\tint Answer = search(X);\n\t\tSystem.out.println(Answer);\n\t}\n\n\tstatic int N, X;\n\tstatic int[] A;\n\t\n\t// 整数 x が何番目に存在するかを返す\n\tstatic int search(int x) {\n\t\tint L = 1, R = N;\n\t\twhile (L <= R) { // 探索範囲がなくなるまで、比較を続ける\n\t\t\tint M = (L + R) / 2;\n\t\t\tif (x < A[M]) R = M - 1;\n\t\t\tif (x == A[M]) return M;\n\t\t\tif (x > A[M]) L = M + 1;\n\t\t}\n\t\treturn -1; // 整数 x が存在しない(注:この問題の制約で -1 が返されることはない)\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 12, | |
| "name": "answer_A12", | |
| "Python": "# 答えが x 以下かを判定し、Yes であれば true、No であれば false を返す関数\ndef check(x, N, K, A):\n\tsum = 0\n\tfor i in range(N):\n\t\tsum += x // A[i] # 「x÷A[i]」の小数点以下切り捨て\n\n\tif sum >= K:\n\t\treturn True\n\treturn False\n\n# 入力\nN, K = map(int, input().split())\nA = list(map(int, input().split()))\n\n# 二分探索\n# Left は探索範囲の左端を、Right は探索範囲の右端を表す\nLeft = 1\nRight = 10 ** 9\nwhile Left < Right:\n\tMid = (Left + Right) // 2\n\tAnswer = check(Mid, N, K, A)\n\n\tif Answer == False:\n\t\tLeft = Mid + 1 # 答えが Mid+1 以上であることが分かる\n\tif Answer == True:\n\t\tRight = Mid # 答えが Mid 以下であることが分かる\n\n# 出力(このとき Left=Right になっている)\nprint(Left)\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tN = sc.nextInt();\n\t\tK = sc.nextInt();\n\t\tA = new int[N + 1];\n\t\tfor (int i = 1; i <= N; i++) A[i] = sc.nextInt();\n\n\t\t// 二分探索\n\t\t// Left は探索範囲の左端を、Right は探索範囲の右端を表す\n\t\tlong Left = 1, Right = 1000000000;\n\t\twhile (Left < Right) {\n\t\t\tlong Mid = (Left + Right) / 2;\n\t\t\tboolean Answer = check(Mid);\n\t\t\tif (Answer == false) Left = Mid + 1; // 答えが Mid+1 以上であることが分かる\n\t\t\tif (Answer == true) Right = Mid; // 答えが Mid 以下であることが分かる\n\t\t}\n\n\t\t// 出力(このとき Left=Right になっている)\n\t\tSystem.out.println(Left);\n\t}\n\n\tstatic int N, K;\n\tstatic int[] A;\n\n\t// 答えが x 以下かを判定し、Yes であれば true、No であれば false を返す\n\tstatic boolean check(long x) {\n\t\tlong sum = 0;\n\t\tfor (int i = 1; i <= N; i++) sum += x / (long)A[i]; //「x ÷ A[i]」の小数点以下切り捨て\n\t\tif (sum >= (long)K) return true;\n\t\treturn false;\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 13, | |
| "name": "answer_A13", | |
| "Python": "# 入力(A は 0 番目から始まることに注意)\nN, K = map(int, input().split())\nA = list(map(int, input().split()))\n\n# 配列の準備(R は 0 番目から始まることに注意)\nR = [ None ] * N\n\n# しゃくとり法\nfor i in range(0, N-1):\n\t# スタート地点を決める\n\tif i == 0:\n\t\tR[i] = 0\n\telse:\n\t\tR[i] = R[i - 1]\n\n\t# ギリギリまで増やしていく\n\twhile R[i] < N-1 and A[R[i]+1]-A[i] <= K:\n\t\tR[i] += 1\n\n# 出力\nAnswer = 0\nfor i in range(0, N-1):\n\tAnswer += (R[i] - i)\nprint(Answer)\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tint K = sc.nextInt();\n\t\tint[] A = new int[N + 1];\n\t\tfor (int i = 1; i <= N; i++) A[i] = sc.nextInt();\n\n\t\t// 配列 R の定義\n\t\tint[] R = new int[N + 1];\n\n\t\t// しゃくとり法\n\t\tfor (int i = 1; i <= N - 1; i++) {\n\t\t\t// スタート地点を決める\n\t\t\tif (i == 1) R[i] = 1;\n\t\t\telse R[i] = R[i - 1];\n\n\t\t\t// ギリギリまで増やしていく\n\t\t\twhile (R[i] < N && A[R[i] + 1] - A[i] <= K) {\n\t\t\t\tR[i] += 1;\n\t\t\t}\n\t\t}\n\n\t\t// 出力(答えは最大 50 億程度になるので long 型を使う必要があります)\n\t\tlong Answer = 0;\n\t\tfor (int i = 1; i <= N - 1; i++) Answer += (R[i] - i);\n\t\tSystem.out.println(Answer);\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 14, | |
| "name": "answer_A14", | |
| "Python": "import bisect\nimport sys\n\n# 入力\nN, K = map(int, input().split())\nA = list(map(int, input().split()))\nB = list(map(int, input().split()))\nC = list(map(int, input().split()))\nD = list(map(int, input().split()))\n\n# 配列 P を作成\nP = []\nfor x in range(N):\n\tfor y in range(N):\n\t\tP.append(A[x] + B[y])\n\n# 配列 Q を作成\nQ = []\nfor z in range(N):\n\tfor w in range(N):\n\t\tQ.append(C[z] + D[w])\n\n# 配列 Q を小さい順にソート\nQ.sort()\n\n# 二分探索\nfor i in range(len(P)):\n\tpos1 = bisect.bisect_left(Q, K-P[i])\n\tif pos1<N*N and Q[pos1]==K-P[i]:\n\t\tprint(\"Yes\")\n\t\tsys.exit(0)\n\n# 見つからなかった場合\nprint(\"No\")\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tint K = sc.nextInt();\n\t\tint[] A = new int[N + 1];\n\t\tint[] B = new int[N + 1];\n\t\tint[] C = new int[N + 1];\n\t\tint[] D = new int[N + 1];\n\t\tfor (int x = 1; x <= N; x++) A[x] = sc.nextInt();\n\t\tfor (int y = 1; y <= N; y++) B[y] = sc.nextInt();\n\t\tfor (int z = 1; z <= N; z++) C[z] = sc.nextInt();\n\t\tfor (int w = 1; w <= N; w++) D[w] = sc.nextInt();\n\n\t\t// 配列 P を作成\n\t\tArrayList<Integer> P = new ArrayList<Integer>();\n\t\tfor (int x = 1; x <= N; x++) {\n\t\t\tfor (int y = 1; y <= N; y++) P.add(A[x] + B[y]);\n\t\t}\n\n\t\t// 配列 Q を作成\n\t\tArrayList<Integer> Q = new ArrayList<Integer>();\n\t\tfor (int z = 1; z <= N; z++) {\n\t\t\tfor (int w = 1; w <= N; w++) Q.add(C[z] + D[w]);\n\t\t}\n\n\t\t// 配列 Q を小さい順にソート\n\t\tCollections.sort(Q);\n\n\t\t// 二分探索(配列 P, Q が 0 番目から始まることに注意!)\n\t\tfor (int i = 0; i < N * N; i++) {\n\t\t\tint pos1 = ~Collections.binarySearch(Q, K - P.get(i), (x, y) -> x.compareTo(y) >= 0 ? 1 : -1);\n\t\t\tif (pos1 < N * N && Q.get(pos1) == K - P.get(i)) {\n\t\t\t\tSystem.out.println(\"Yes\");\n\t\t\t\tSystem.exit(0);\n\t\t\t}\n\t\t}\n\n\t\t// 見つからなかった場合\n\t\tSystem.out.println(\"No\");\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 15, | |
| "name": "answer_A15", | |
| "Python": "import bisect\n\n# 入力\nN = int(input())\nA = list(map(int, input().split()))\n\n# 配列 T の作成(重複も消す)\nT = list(set(A))\nT.sort()\n\n# 答えを求める\nB = [ None ] * N\nfor i in range(N):\n\tB[i] = bisect.bisect_left(T, A[i])\n\tB[i] += 1\n\n# 答えを空白区切りで出力\nAnswer = [str(i) for i in B]\nprint(\" \".join(Answer))\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tint[] A = new int[N + 1];\n\t\tfor (int i = 1; i <= N; i++) A[i] = sc.nextInt();\n\n\t\t// 配列 T の作成・重複を消す\n\t\tHashSet<Integer> tmp = new HashSet<Integer>();\n\t\tfor (int i = 1; i <= N; i++) tmp.add(A[i]);\n\t\tArrayList<Integer> T = new ArrayList<>(tmp);\n\t\tCollections.sort(T);\n\n\t\t// 答えを求める\n\t\tint[] B = new int[N + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tB[i] = ~Collections.binarySearch(T, A[i], (x, y) -> x.compareTo(y) >= 0 ? 1 : -1);\n\t\t\tB[i] += 1;\n\t\t}\n\n\t\t// 答えを空白区切りで出力\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tif (i >= 2) System.out.print(\" \");\n\t\t\tSystem.out.print(B[i]);\n\t\t}\n\t\tSystem.out.println();\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 16, | |
| "name": "answer_A16", | |
| "Python": "# 入力(A, B が 0 番目から始まっていることに注意)\nN = int(input())\nA = list(map(int, input().split()))\nB = list(map(int, input().split()))\n\n# 動的計画法\ndp = [ None ] * (N+1)\ndp[1] = 0\ndp[2] = A[0]\nfor i in range(3, N+1):\n\tdp[i] = min(dp[i-1]+A[i-2], dp[i-2]+B[i-3])\n\n# 出力\nprint(dp[N])\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tint[] A = new int[N + 1];\n\t\tint[] B = new int[N + 1];\n\t\tfor (int i = 2; i <= N; i++) A[i] = sc.nextInt();\n\t\tfor (int i = 3; i <= N; i++) B[i] = sc.nextInt();\n\n\t\t// 動的計画法\n\t\tint[] dp = new int[N + 1];\n\t\tdp[1] = 0;\n\t\tdp[2] = A[2];\n\t\tfor (int i = 3; i <= N; i++) {\n\t\t\tdp[i] = Math.min(dp[i - 1] + A[i], dp[i - 2] + B[i]);\n\t\t}\n\n\t\t// 出力\n\t\tSystem.out.println(dp[N]);\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 17, | |
| "name": "answer_A17", | |
| "Python": "# 入力(A, B が 0 番目から始まっていることに注意)\nN = int(input())\nA = list(map(int, input().split()))\nB = list(map(int, input().split()))\n\n# 動的計画法\ndp = [ None ] * (N+1)\ndp[1] = 0\ndp[2] = A[0]\nfor i in range(3, N+1):\n\tdp[i] = min(dp[i-1]+A[i-2], dp[i-2]+B[i-3])\n\n# 答えの復元\n# 変数 Place は現在位置(ゴールから進んでいく)\n# たとえば入力例の場合、Place は 5 → 4 → 2 → 1 と変化していく\nAnswer = []\nPlace = N\nwhile True:\n\tAnswer.append(Place)\n\tif Place == 1:\n\t\tbreak\n\n\t# どこから部屋 Place に向かうのが最適かを求める\n\tif dp[Place-1] + A[Place-2] == dp[Place]:\n\t\tPlace = Place - 1\n\telse:\n\t\tPlace = Place - 2\nAnswer.reverse()\n\n# 答えを出力\nAnswer2 = [str(i) for i in Answer]\nprint(len(Answer))\nprint(\" \".join(Answer2))\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tint[] A = new int[N + 1];\n\t\tint[] B = new int[N + 1];\n\t\tfor (int i = 2; i <= N; i++) A[i] = sc.nextInt();\n\t\tfor (int i = 3; i <= N; i++) B[i] = sc.nextInt();\n\n\t\t// 動的計画法\n\t\tint[] dp = new int[N + 1];\n\t\tdp[1] = 0;\n\t\tdp[2] = A[2];\n\t\tfor (int i = 3; i <= N; i++) {\n\t\t\tdp[i] = Math.min(dp[i - 1] + A[i], dp[i - 2] + B[i]);\n\t\t}\n\n\t\t// 答えの復元\n\t\t// 変数 Place は現在位置(ゴールから進んでいく)\n\t\t// たとえば入力例の場合、Place は 5 → 4 → 2 → 1 と変化していく\n\t\tArrayList<Integer> Answer = new ArrayList<Integer>();\n\t\tint Place = N;\n\t\twhile (true) {\n\t\t\tAnswer.add(Place);\n\t\t\tif (Place == 1) break;\n\n\t\t\t// どこから部屋 Place に向かうのが最適かを求める\n\t\t\tif (dp[Place - 1] + A[Place] == dp[Place]) Place = Place - 1;\n\t\t\telse Place = Place - 2;\n\t\t}\n\n\t\t// 出力\n\t\tSystem.out.println(Answer.size());\n\t\tfor (int i = Answer.size() - 1; i >= 0; i--) {\n\t\t\tif (i < Answer.size() - 1) System.out.print(\" \");\n\t\t\tSystem.out.print(Answer.get(i));\n\t\t}\n\t\tSystem.out.println();\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 18, | |
| "name": "answer_A18", | |
| "Python": "# 入力\nN, S = map(int, input().split())\nA = list(map(int, input().split()))\n\n# 動的計画法(i=0)\ndp = [ [ None ] * (S + 1) for i in range(N + 1) ]\ndp[0][0] = True\nfor i in range(1, S+1):\n\tdp[0][i] = False\n\n# 動的計画法(i=1)\nfor i in range(1, N+1):\n\tfor j in range(0,S+1):\n\t\tif j < A[i-1]:\n\t\t\tif dp[i-1][j] == True:\n\t\t\t\tdp[i][j] = True\n\t\t\telse:\n\t\t\t\tdp[i][j] = False\n\n\t\tif j >= A[i-1]:\n\t\t\tif dp[i-1][j] == True or dp[i-1][j-A[i-1]] == True:\n\t\t\t\tdp[i][j] = True\n\t\t\telse:\n\t\t\t\tdp[i][j] = False\n\n# 出力\nif dp[N][S] == True:\n\tprint(\"Yes\")\nelse:\n\tprint(\"No\")\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tint S = sc.nextInt();\n\t\tint[] A = new int[N + 1];\n\t\tfor (int i = 1; i <= N; i++) A[i] = sc.nextInt();\n\n\t\t// 配列 dp の定義\n\t\tboolean[][] dp = new boolean[N + 1][S + 1];\n\n\t\t// 動的計画法(i = 0)\n\t\tdp[0][0] = true;\n\t\tfor (int i = 1; i <= S; i++) dp[0][i] = false;\n\n\t\t// 動的計画法(i >= 1)\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tfor (int j = 0; j <= S; j++) {\n\t\t\t\tif (j < A[i]) {\n\t\t\t\t\tif (dp[i - 1][j] == true) dp[i][j] = true;\n\t\t\t\t\telse dp[i][j] = false;\n\t\t\t\t}\n\t\t\t\tif (j >= A[i]) {\n\t\t\t\t\tif (dp[i - 1][j] == true || dp[i - 1][j - A[i]] == true) dp[i][j] = true;\n\t\t\t\t\telse dp[i][j] = false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// 出力\n\t\tif (dp[N][S] == true) System.out.println(\"Yes\");\n\t\telse System.out.println(\"No\");\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 19, | |
| "name": "answer_A19", | |
| "Python": "# 入力\nN, W = map(int, input().split())\nw = [ None ] * (N + 1)\nv = [ None ] * (N + 1)\nfor i in range(1, N+1):\n\tw[i], v[i] = map(int, input().split())\n\n# 動的計画法\ndp = [ [ -10 ** 15 ] * (W + 1) for i in range(N + 1) ]\ndp[0][0] = 0\nfor i in range(1, N+1):\n\tfor j in range(0,W+1):\n\t\tif j < w[i]:\n\t\t\tdp[i][j] = dp[i-1][j]\n\t\tif j >= w[i]:\n\t\t\tdp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i]]+v[i])\n\n# 出力\nprint(max(dp[N]))\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tint W = sc.nextInt();\n\t\tint[] w = new int[N + 1];\n\t\tint[] v = new int[N + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tw[i] = sc.nextInt();\n\t\t\tv[i] = sc.nextInt();\n\t\t}\n\n\t\t// 配列 dp の定義・初期化\n\t\tlong[][] dp = new long[N + 1][W + 1];\n\t\tfor (int i = 0; i <= N; i++) {\n\t\t\tfor (int j = 0; j <= W; j++) dp[i][j] = -1000000000000L;\n\t\t}\n\n\t\t// 動的計画法\n\t\tdp[0][0] = 0;\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tfor (int j = 0; j <= W; j++) {\n\t\t\t\tif (j < w[i]) dp[i][j] = dp[i - 1][j];\n\t\t\t\telse dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);\n\t\t\t}\n\t\t}\n\n\t\t// 出力\n\t\tlong Answer = 0;\n\t\tfor (int i = 0; i <= W; i++) Answer = Math.max(Answer, dp[N][i]);\n\t\tSystem.out.println(Answer);\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 20, | |
| "name": "answer_A20", | |
| "Python": "# 入力\nS = input()\nT = input()\nN = len(S)\nM = len(T)\n\n# 動的計画法\ndp = [ [ None ] * (M + 1) for i in range(N + 1) ]\ndp[0][0] = 0\nfor i in range(0, N+1):\n\tfor j in range(0,M+1):\n\t\tif i>=1 and j>=1 and S[i-1]==T[j-1]:\n\t\t\tdp[i][j] = max(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]+1)\n\t\telif i>=1 and j>=1:\n\t\t\tdp[i][j] = max(dp[i-1][j], dp[i][j-1])\n\t\telif i>=1:\n\t\t\tdp[i][j] = dp[i-1][j]\n\t\telif j>=1:\n\t\t\tdp[i][j] = dp[i][j-1]\n\n# 出力\nprint(dp[N][M])\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力(文字列は 0 文字目から始まることに注意!)\n\t\tString S = sc.next(); int N = S.length();\n\t\tString T = sc.next(); int M = T.length();\n\n\t\t// 配列 dp の定義\n\t\tint[][] dp = new int[N + 1][M + 1];\n\n\t\t// 動的計画法\n\t\tdp[0][0] = 0;\n\t\tfor (int i = 0; i <= N; i++) {\n\t\t\tfor (int j = 0; j <= M; j++) {\n\t\t\t\tif (i >= 1 && j >= 1 && S.charAt(i-1) == T.charAt(j-1)) {\n\t\t\t\t\tdp[i][j] = Math.max(dp[i - 1][j], Math.max(dp[i][j - 1], dp[i - 1][j - 1] + 1));\n\t\t\t\t}\n\t\t\t\telse if (i >= 1 && j >= 1) {\n\t\t\t\t\tdp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);\n\t\t\t\t}\n\t\t\t\telse if (i >= 1) {\n\t\t\t\t\tdp[i][j] = dp[i - 1][j];\n\t\t\t\t}\n\t\t\t\telse if (j >= 1) {\n\t\t\t\t\tdp[i][j] = dp[i][j - 1];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// 出力\n\t\tSystem.out.println(dp[N][M]);\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 21, | |
| "name": "answer_A21", | |
| "Python": "# 入力\nN = int(input())\nP = [ None ] * (N + 1)\nA = [ None ] * (N + 1)\nfor i in range(1, N+1):\n\tP[i], A[i] = map(int, input().split())\n\n# 動的計画法(LEN は r-l の値)\ndp = [ [ None ] * (N + 1) for i in range(N + 1) ]\ndp[1][N] = 0\nfor LEN in reversed(range(0, N-1)):\n\tfor l in range(1, N-LEN+1):\n\t\tr = l + LEN\n\n\t\t# score1 の値(l-1 番目のブロックを取り除くときの得点)を求める\n\t\tscore1 = 0\n\t\tif l>=2 and l<=P[l-1] and P[l-1]<=r:\n\t\t\tscore1 = A[l-1]\n\n\t\t# score2 の値(r+1 番目のブロックを取り除くときの得点)を求める\n\t\tscore2 = 0\n\t\tif r<=N-1 and l<=P[r+1] and P[r+1]<=r:\n\t\t\tscore2 = A[r+1]\n\n\t\t# dp[l][r] を求める\n\t\tif l==1:\n\t\t\tdp[l][r] = dp[l][r+1] + score2\n\t\telif r==N:\n\t\t\tdp[l][r] = dp[l-1][r] + score1\n\t\telse:\n\t\t\tdp[l][r] = max(dp[l-1][r] + score1, dp[l][r+1] + score2)\n\n# 出力\nAnswer = 0\nfor i in range(1, N+1):\n\tAnswer = max(Answer, dp[i][i])\nprint(Answer)\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tint[] P = new int[N + 1];\n\t\tint[] A = new int[N + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tP[i] = sc.nextInt();\n\t\t\tA[i] = sc.nextInt();\n\t\t}\n\n\t\t// 動的計画法(LEN は r-l の値)\n\t\tint[][] dp = new int[N + 1][N + 1];\n\t\tdp[1][N] = 0;\n\t\tfor (int LEN = N - 2; LEN >= 0; LEN--) {\n\t\t\tfor (int l = 1; l <= N - LEN; l++) {\n\t\t\t\tint r = l + LEN;\n\n\t\t\t\t// score1 の値(l-1 番目のブロックを取り除くときの得点)を求める\n\t\t\t\tint score1 = 0;\n\t\t\t\tif (l >= 2 && l <= P[l - 1] && P[l - 1] <= r) score1 = A[l - 1];\n\n\t\t\t\t// score2 の値(r+1 番目のブロックを取り除くときの得点)を求める\n\t\t\t\tint score2 = 0;\n\t\t\t\tif (r < N && l <= P[r + 1] && P[r + 1] <= r) score2 = A[r + 1];\n\n\t\t\t\t// dp[l][r] を求める\n\t\t\t\tif (l == 1) {\n\t\t\t\t\tdp[l][r] = dp[l][r + 1] + score2;\n\t\t\t\t}\n\t\t\t\telse if (r == N) {\n\t\t\t\t\tdp[l][r] = dp[l - 1][r] + score1;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tdp[l][r] = Math.max(dp[l - 1][r] + score1, dp[l][r + 1] + score2);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// 出力\n\t\tint Answer = 0;\n\t\tfor (int i = 1; i <= N; i++) Answer = Math.max(Answer, dp[i][i]);\n\t\tSystem.out.println(Answer);\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 22, | |
| "name": "answer_A22", | |
| "Python": "# 入力(配列 A, B は 0 番目から始まることに注意!)\nN = int(input())\nA = list(map(int, input().split()))\nB = list(map(int, input().split()))\n\n# 配列の初期化\ndp = [ -(10 ** 9) ] * (N + 1)\ndp[1] = 0\n\n# 動的計画法\nfor i in range(1, N):\n\tdp[A[i-1]] = max(dp[A[i-1]], dp[i] + 100)\n\tdp[B[i-1]] = max(dp[B[i-1]], dp[i] + 150)\n\n# 出力\nprint(dp[N])\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tint[] A = new int[N + 1];\n\t\tint[] B = new int[N + 1];\n\t\tfor (int i = 1; i <= N - 1; i++) A[i] = sc.nextInt();\n\t\tfor (int i = 1; i <= N - 1; i++) B[i] = sc.nextInt();\n\n\t\t// 配列の初期化\n\t\tint[] dp = new int[N + 1];\n\t\tdp[1] = 0;\n\t\tfor (int i = 2; i <= N; i++) dp[i] = -1000000000;\n\n\t\t// 動的計画法\n\t\tfor (int i = 1; i <= N - 1; i++) {\n\t\t\tdp[A[i]] = Math.max(dp[A[i]], dp[i] + 100);\n\t\t\tdp[B[i]] = Math.max(dp[B[i]], dp[i] + 150);\n\t\t}\n\n\t\t// 出力\n\t\tSystem.out.println(dp[N]);\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 23, | |
| "name": "answer_A23", | |
| "Python": "# 入力(配列 A は 0 番目から始まることに注意!)\nN, M = map(int, input().split())\nA = [ None ] * M\nfor i in range(0, M):\n\tA[i] = list(map(int, input().split()))\n\n# 配列の初期化\ndp = [ [ 10 ** 9 ] * (2 ** N) for i in range(M + 1) ]\n\n# 動的計画法\ndp[0][0] = 0\nfor i in range(1, M+1):\n\tfor j in range(0, 2 ** N):\n\t\t# already[k] = 1 のとき、品物 k は既に無料になっている\n\t\talready = [ None ] * N\n\t\tfor k in range(0, N):\n\t\t\tif (j // (2 ** k)) % 2 == 0:\n\t\t\t\talready[k] = 0\n\t\t\telse:\n\t\t\t\talready[k] = 1\n\n\t\t# クーポン券 i を選んだ場合の整数表現 v を計算する\n\t\tv = 0\n\t\tfor k in range(0, N):\n\t\t\tif already[k] == 1 or A[i-1][k] == 1:\n\t\t\t\tv += (2 ** k)\n\n\t\t# 遷移を行う\n\t\tdp[i][j] = min(dp[i][j], dp[i-1][j])\n\t\tdp[i][v] = min(dp[i][v], dp[i-1][j] + 1)\n\n# 出力\nif dp[M][2 ** N - 1] == 1000000000:\n\tprint(\"-1\")\nelse:\n\tprint(dp[M][2 ** N - 1])\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tint M = sc.nextInt();\n\t\tint[][] A = new int[M + 1][N + 1];\n\t\tfor (int i = 1; i <= M; i++) {\n\t\t\tfor (int j = 1; j <= N; j++) A[i][j] = sc.nextInt();\n\t\t}\n\n\t\t// 配列の初期化\n\t\tint[][] dp = new int[M + 1][1 << N];\n\t\tfor (int i = 0; i <= M; i++) {\n\t\t\tfor (int j = 0; j < (1 << N); j++) dp[i][j] = 1000000000;\n\t\t}\n\n\t\t// 動的計画法\n\t\tdp[0][0] = 0;\n\t\tfor (int i = 1; i <= M; i++) {\n\t\t\tfor (int j = 0; j < (1 << N); j++) {\n\t\t\t\t// already[k] = 1 のとき、品物 k は既に無料になっている\n\t\t\t\tint[] already = new int[N + 1];\n\t\t\t\tfor (int k = 1; k <= N; k++) {\n\t\t\t\t\tif ((j / (1 << (k - 1)) % 2) == 0) already[k] = 0;\n\t\t\t\t\telse already[k] = 1;\n\t\t\t\t}\n\n\t\t\t\t// クーポン券 i を選んだ場合の整数表現 v を計算する\n\t\t\t\tint v = 0;\n\t\t\t\tfor (int k = 1; k <= N; k++) {\n\t\t\t\t\tif (already[k] == 1 || A[i][k] == 1) v += (1 << (k - 1));\n\t\t\t\t}\n\n\t\t\t\t// 遷移を行う\n\t\t\t\tdp[i][j] = Math.min(dp[i][j], dp[i - 1][j]);\n\t\t\t\tdp[i][v] = Math.min(dp[i][v], dp[i - 1][j] + 1);\n\t\t\t}\n\t\t}\n\n\t\t// 出力(すべて選んだ場合の整数表現は 2^N-1)\n\t\tif (dp[M][(1 << N) - 1] == 1000000000) System.out.println(\"-1\");\n\t\telse System.out.println(dp[M][(1 << N) - 1]);\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 24, | |
| "name": "answer_A24", | |
| "Python": "import bisect\n\n# 入力(A は 0 番目から始まることに注意!)\nN = int(input())\nA = list(map(int, input().split()))\n\n# 動的計画法の準備\nLEN = 0 # LEN は L の長さ(例:L[3] まで書き込まれている場合 LEN=4)\nL = [] # 0 番目から始まることに注意\ndp = [ None ] * N # 0 番目から始まることに注意\n\n# 動的計画法(配列 dp を使った実装)\nfor i in range(N):\n\tpos = bisect.bisect_left(L, A[i])\n\tdp[i] = pos\n\n\t# 配列 L を更新\n\tif dp[i] >= LEN:\n\t\tL.append(A[i])\n\t\tLEN += 1\n\telse:\n\t\tL[dp[i]] = A[i]\n\n# 答えを出力\nprint(LEN)\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tint[] A = new int[N + 1];\n\t\tfor (int i = 1; i <= N; i++) A[i] = sc.nextInt();\n\n\t\t// 配列などの定義\n\t\tint LEN = 0; // LEN は L の長さ(例:L[4] まで書き込まれている場合 LEN=4)\n\t\tArrayList<Integer> L = new ArrayList<Integer>();\n\n\t\t// 動的計画法(配列 dp を使わない実装)\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tint pos = ~Collections.binarySearch(L, A[i], (x, y) -> x.compareTo(y) >= 0 ? 1 : -1);\n\n\t\t\t// L の最大値より A[i] の方が大きかった場合\n\t\t\tif (pos >= LEN) {\n\t\t\t\tLEN += 1;\n\t\t\t\tL.add(A[i]);\n\t\t\t}\n\t\t\t// そうでない場合\n\t\t\telse {\n\t\t\t\tL.set(pos, A[i]);\n\t\t\t}\n\t\t}\n\n\t\t// 答えを出力\n\t\tSystem.out.println(LEN);\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 25, | |
| "name": "answer_A25", | |
| "Python": "# 入力(配列 c が 0 番目から始まることに注意)\nH, W = map(int, input().split())\nc = [ None ] * H\nfor i in range(H):\n\tc[i] = input()\n\n# 動的計画法\ndp = [ [ 0 ] * (W + 1) for i in range(H + 1) ]\nfor i in range(H):\n\tfor j in range(W):\n\t\tif i==0 and j==0:\n\t\t\tdp[i][j] = 1\n\t\telse:\n\t\t\tdp[i][j] = 0\n\t\t\tif i>=1 and c[i-1][j]=='.':\n\t\t\t\tdp[i][j] += dp[i-1][j]\n\t\t\tif j>=1 and c[i][j-1]=='.':\n\t\t\t\tdp[i][j] += dp[i][j-1]\n\n# 出力\nprint(dp[H-1][W-1])\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力(配列 c は 0 番目から始まることに注意!)\n\t\tint H = sc.nextInt();\n\t\tint W = sc.nextInt();\n\t\tString[] c = new String[H];\n\t\tfor (int i = 0; i < H; i++) c[i] = sc.next();\n\n\t\t// 動的計画法\n\t\tlong[][] dp = new long[H][W];\n\t\tfor (int i = 0; i < H; i++) {\n\t\t\tfor (int j = 0; j < W; j++) {\n\t\t\t\tif (i == 0 && j == 0) {\n\t\t\t\t\tdp[i][j] = 1;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tdp[i][j] = 0;\n\t\t\t\t\tif (i >= 1 && c[i - 1].charAt(j) == '.') dp[i][j] += dp[i - 1][j];\n\t\t\t\t\tif (j >= 1 && c[i].charAt(j - 1) == '.') dp[i][j] += dp[i][j - 1];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// 出力\n\t\tSystem.out.println(dp[H-1][W-1]);\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 26, | |
| "name": "answer_A26", | |
| "Python": "# x が素数のとき true を、素数ではないとき false を返す\ndef isPrime(N):\n\tLIMIT = int(N ** 0.5)\n\tfor i in range(2, LIMIT + 1):\n\t\tif N % i == 0:\n\t\t\treturn False\n\treturn True\n\n# 入力\nQ = int(input())\nX = [ None ] * Q\nfor i in range(Q):\n\tX[i] = int(input())\n\n# 出力\nfor i in range(Q):\n\tAnswer = isPrime(X[i])\n\tif Answer == True:\n\t\tprint(\"Yes\")\n\telse:\n\t\tprint(\"No\")\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint Q = sc.nextInt();\n\t\tint[] X = new int[Q + 1];\n\t\tfor (int i = 1; i <= Q; i++) X[i] = sc.nextInt();\n\n\t\t// 出力\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tboolean Answer = isPrime(X[i]);\n\t\t\tif (Answer == true) System.out.println(\"Yes\");\n\t\t\tif (Answer == false) System.out.println(\"No\");\n\t\t}\n\t}\n\n\t// x が素数のとき true を、素数ではないとき false を返す関数\n\tstatic boolean isPrime(int x) {\n\t\tfor (int i = 2; i * i <= x; i++) {\n\t\t\tif (x % i == 0) return false;\n\t\t}\n\t\treturn true;\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 27, | |
| "name": "answer_A27", | |
| "Python": "def GCD(A, B):\n\twhile A >= 1 and B >= 1:\n\t\tif A >= B:\n\t\t\tA = A % B # A の値を変更する場合\n\t\telse:\n\t\t\tB = B % A # B の値を変更する場合\n\tif A >= 1:\n\t\treturn A\n\treturn B\n\n# 入力\nA, B = map(int, input().split())\nprint(GCD(A, B))\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint A = sc.nextInt();\n\t\tint B = sc.nextInt();\n\n\t\t// 出力\n\t\tSystem.out.println(GCD(A, B));\n\t}\n\n\t// 正の整数 A と B の最大公約数を返す関数\n\t// GCD は Greatest Common Divisor(最大公約数)の略\n\tstatic int GCD(int A, int B) {\n\t\twhile (A >= 1 && B >= 1) {\n\t\t\tif (A >= B) {\n\t\t\t\tA %= B; // A の値を変更する場合\n\t\t\t}\n\t\t\telse {\n\t\t\t\tB %= A; // B の値を変更する場合\n\t\t\t}\n\t\t}\n\t\tif (A >= 1) {\n\t\t\treturn A;\n\t\t}\n\t\treturn B;\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 28, | |
| "name": "answer_A28", | |
| "Python": "# 入力\nN = int(input())\nT = [ None ] * N\nA = [ None ] * N\nfor i in range(N):\n\tT[i], A[i] = input().split()\n\tA[i] = int(A[i])\n\n# 出力(Answer は現在の黒板の数)\nAnswer = 0\nfor i in range(N):\n\tif T[i] == '+':\n\t\tAnswer += A[i]\n\tif T[i] == '-':\n\t\tAnswer -= A[i]\n\tif T[i] == '*':\n\t\tAnswer *= A[i]\n\n\t# 引き算で答えが 0 未満になった場合\n\tif Answer < 0:\n\t\tAnswer += 10000\n\n\t# ここで余りをとっている!\n\tAnswer %= 10000\n\tprint(Answer)\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tchar[] T = new char[N + 1];\n\t\tlong[] A = new long[N + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tT[i] = sc.next().charAt(0);\n\t\t\tA[i] = sc.nextInt();\n\t\t}\n\n\t\t// 出力(Answer は現在の黒板の数)\n\t\tlong Answer = 0;\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tif (T[i] == '+') Answer += A[i];\n\t\t\tif (T[i] == '-') Answer -= A[i];\n\t\t\tif (T[i] == '*') Answer *= A[i];\n\n\t\t\t// 引き算で答えが 0 未満になった場合\n\t\t\tif (Answer < 0) Answer += 10000;\n\n\t\t\t// ここで余りをとっている!\n\t\t\tAnswer %= 10000;\n\t\t\tSystem.out.println(Answer);\n\t\t}\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 29, | |
| "name": "answer_A29", | |
| "Python": "# a の b 乗を m で割った余りを返す関数\ndef Power(a, b, m):\n\tp = a\n\tAnswer = 1\n\tfor i in range(30):\n\t\twari = 2 ** i\n\t\tif (b // wari) % 2 == 1:\n\t\t\tAnswer = (Answer * p) % m # a の 2^i 乗が掛けられるとき\n\t\tp = (p * p) % m\n\treturn Answer\n\n# 入力\na, b = map(int, input().split())\n\n# 出力\nprint(Power(a, b, 1000000007))\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tlong a = sc.nextLong();\n\t\tlong b = sc.nextLong();\n\n\t\t// 出力(Answer は現在の黒板の数)\n\t\tSystem.out.println(Power(a, b, 1000000007));\n\t}\n\n\t// a の b 乗を m で割った余りを返す関数\n\t// 変数 a は a^1 → a^2 → a^4 → a^8 → a^16 → ・・・ と変化\n\tstatic long Power(long a, long b, long m) {\n\t\tlong p = a, Answer = 1;\n\t\tfor (int i = 0; i < 30; i++) {\n\t\t\tint wari = (1 << i);\n\t\t\tif ((b / wari) % 2 == 1) {\n\t\t\t\tAnswer = (Answer * p) % m; // 「a の 2i 乗」が掛けられるとき\n\t\t\t}\n\t\t\tp = (p * p) % m;\n\t\t}\n\t\treturn Answer;\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 30, | |
| "name": "answer_A30", | |
| "Python": "# a の b 乗を m で割った余りを返す関数\ndef Power(a, b, m):\n\tp = a\n\tAnswer = 1\n\tfor i in range(30):\n\t\twari = 2 ** i\n\t\tif (b // wari) % 2 == 1:\n\t\t\tAnswer = (Answer * p) % m # a の 2^i 乗が掛けられるとき\n\t\tp = (p * p) % m\n\treturn Answer\n\n# a÷b を m で割った余りを返す関数\ndef Division(a, b, m):\n\treturn (a * Power(b, m - 2, m)) % m\n\n# 入力など\nn, r = map(int, input().split())\nM = 1000000007\n\n# 手順 1:分子を求める\na = 1\nfor i in range(1, n + 1):\n\ta = (a * i) % M\n\n# 手順 2:分母を求める\nb = 1\nfor i in range(1, r+1):\n\tb = (b * i) % M\nfor i in range(1, n-r+1):\n\tb = (b * i) % M\n\n# 手順 3:答えを求める\nprint(Division(a, b, M))\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tlong n = sc.nextLong();\n\t\tlong r = sc.nextLong();\n\n\t\t// 手順 1: 分子 a を求める\n\t\tlong M = 1000000007;\n\t\tlong a = 1;\n\t\tfor (int i = 1; i <= n; i++) a = (a * i) % M;\n\n\t\t// 手順 2: 分母 b を求める\n\t\tlong b = 1;\n\t\tfor (int i = 1; i <= r; i++) b = (b * i) % M;\n\t\tfor (int i = 1; i <= n - r; i++) b = (b * i) % M;\n\n\t\t// 手順 3: 答えを求める\n\t\tSystem.out.println(Division(a, b, M));\n\t}\n\n\t// a の b 乗を m で割った余りを返す関数\n\tstatic long Power(long a, long b, long m) {\n\t\tlong p = a, Answer = 1;\n\t\tfor (int i = 0; i < 30; i++) {\n\t\t\tint wari = (1 << i);\n\t\t\tif ((b / wari) % 2 == 1) {\n\t\t\t\tAnswer = (Answer * p) % m;\n\t\t\t}\n\t\t\tp = (p * p) % m;\n\t\t}\n\t\treturn Answer;\n\t}\n\n\t// a ÷ b を m で割った余りを返す関数\n\tstatic long Division(long a, long b, long m) {\n\t\treturn (a * Power(b, m - 2, m)) % m;\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 31, | |
| "name": "answer_A31", | |
| "Python": "N = int(input())\nA1 = (N // 3) # 3 で割り切れるものの個数\nA2 = (N // 5) # 5 で割り切れるものの個数\nA3 = (N // 15) # 3, 5 両方で割り切れるもの(= 15 の倍数)の個数\nprint(A1 + A2 - A3)\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\tlong N = sc.nextLong();\n\t\tlong A1 = N / 3; // 3 で割り切れるものの個数\n\t\tlong A2 = N / 5; // 5 で割り切れるものの個数\n\t\tlong A3 = N / 15; // 3, 5 両方で割り切れるもの(= 15 の倍数)の個数\n\t\tSystem.out.println(A1 + A2 - A3);\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 32, | |
| "name": "answer_A32", | |
| "Python": "# 入力\nN, A, B = map(int, input().split())\n\n# 配列 dp を定義\n# dp[x]=True のとき勝ちの状態、dp[x]=False のとき負けの状態\ndp = [ None ] * (N+1)\n\n# 勝者を計算する\nfor i in range(N+1):\n\tif i >= A and dp[i-A] == False:\n\t\tdp[i] = True\n\telif i >= B and dp[i-B] == False:\n\t\tdp[i] = True\n\telse:\n\t\tdp[i] = False\n\n# 出力\nif dp[N] == True:\n\tprint(\"First\")\nelse:\n\tprint(\"Second\")\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tint A = sc.nextInt();\n\t\tint B = sc.nextInt();\n\n\t\t// 勝者を計算する\n\t\tboolean[] dp = new boolean[N + 1];\n\t\tfor (int i = 0; i <= N; i++) {\n\t\t\tif (i >= A && dp[i-A] == false) dp[i] = true; // 勝ちの状態\n\t\t\telse if (i >= B && dp[i-B] == false) dp[i] = true; // 勝ちの状態\n\t\t\telse dp[i] = false; // 負けの状態\n\t\t}\n\n\t\t// 出力\n\t\tif (dp[N] == true) System.out.println(\"First\");\n\t\telse System.out.println(\"Second\");\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 33, | |
| "name": "answer_A33", | |
| "Python": "# 入力\nN = int(input())\nA = list(map(int, input().split()))\n\n# 全部 XOR した値(ニム和)を求める\nXOR_Sum = A[0]\nfor i in range(1,N):\n\tXOR_Sum = (XOR_Sum ^ A[i])\n\n# 出力\nif XOR_Sum >= 1:\n\tprint(\"First\")\nelse:\n\tprint(\"Second\")\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tint[] A = new int[N + 1];\n\t\tfor (int i = 1; i <= N; i++) A[i] = sc.nextInt();\n\n\t\t// 全部 XOR した値(ニム和)を求める\n\t\tint XOR_Sum = A[1];\n\t\tfor (int i = 2; i <= N; i++) XOR_Sum = (XOR_Sum ^ A[i]);\n\n\t\t// 出力\n\t\tif (XOR_Sum != 0) System.out.println(\"First\");\n\t\tif (XOR_Sum == 0) System.out.println(\"Second\");\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 34, | |
| "name": "answer_A34", | |
| "Python": "# 入力\nN, X, Y = map(int, input().split())\nA = list(map(int, input().split()))\n\n# Grundy 数を求める\n# 変数 grundy[i] : 石が i 個の時の Grundy 数\n# 変数 Transit[i]: Grundy 数が i となるような遷移ができるか\ngrundy = [ None ] * 100001\nfor i in range(100001):\n\tTransit = [False, False, False]\n\tif i >= X:\n\t\tTransit[grundy[i-X]] = True\n\tif i >= Y:\n\t\tTransit[grundy[i-Y]] = True\n\n\tif Transit[0] == False:\n\t\tgrundy[i] = 0\n\telif Transit[1] == False:\n\t\tgrundy[i] = 1\n\telse:\n\t\tgrundy[i] = 2\n\n# 出力\nXOR_Sum = 0\nfor i in range(N):\n\tXOR_Sum = (XOR_Sum ^ grundy[A[i]])\nif XOR_Sum >= 1:\n\tprint(\"First\")\nelse:\n\tprint(\"Second\")\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tint X = sc.nextInt();\n\t\tint Y = sc.nextInt();\n\t\tint[] A = new int[N + 1];\n\t\tfor (int i = 1; i <= N; i++) A[i] = sc.nextInt();\n\n\t\t// Grundy 数を求める\n\t\t// 変数 grundy[i] : 石が i 個の時の Grundy 数\n\t\t// 変数 Transit[i]: Grundy 数が i となるような遷移ができるか\n\t\tint[] grundy = new int[100001];\n\t\tfor (int i = 0; i <= 100000; i++) {\n\t\t\tboolean[] Transit = {false, false, false};\n\t\t\tif (i >= X) Transit[grundy[i-X]] = true;\n\t\t\tif (i >= Y) Transit[grundy[i-Y]] = true;\n\t\t\tif (Transit[0] == false) grundy[i] = 0;\n\t\t\telse if (Transit[1] == false) grundy[i] = 1;\n\t\t\telse grundy[i] = 2;\n\t }\n\t\t\n\t\t// 出力\n\t\tint XOR_Sum = 0;\n\t\tfor (int i = 1; i <= N; i++) XOR_Sum = (XOR_Sum ^ grundy[A[i]]);\n\t\tif (XOR_Sum != 0) System.out.println(\"First\");\n\t\tif (XOR_Sum == 0) System.out.println(\"Second\");\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 35, | |
| "name": "answer_A35", | |
| "Python": "# 入力\nN = int(input())\nA = list(map(int, input().split()))\n\n# 配列 dp を定義\ndp = [ [ None ] * (N+1) for i in range(N+1) ]\n\n# 動的計画法 [N 段目]\nfor j in range(1, N+1):\n\tdp[N][j] = A[j-1]\n\n# 動的計画法 [1 ~ N-1 段目]\nfor i in reversed(range(1, N)):\n\tfor j in range(1, i+1):\n\t\tif i % 2 == 1:\n\t\t\tdp[i][j] = max(dp[i+1][j], dp[i+1][j+1])\n\t\tif i % 2 == 0:\n\t\t\tdp[i][j] = min(dp[i+1][j], dp[i+1][j+1])\n\n# 出力\nprint(dp[1][1])\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tint[] A = new int[N + 1];\n\t\tfor (int i = 1; i <= N; i++) A[i] = sc.nextInt();\n\n\t\t// 動的計画法(N 段目)\n\t\tint[][] dp = new int[N + 1][N + 1];\n\t\tfor (int j = 1; j <= N; j++) dp[N][j] = A[j];\n\n\t\t// 動的計画法 [1 ~ N-1 段目 ]\n\t\tfor (int i = N - 1; i >= 1; i--) {\n\t\t\tfor (int j = 1; j <= i; j++) {\n\t\t\t\tif (i % 2 == 1) dp[i][j] = Math.max(dp[i+1][j], dp[i+1][j+1]);\n\t\t\t\tif (i % 2 == 0) dp[i][j] = Math.min(dp[i+1][j], dp[i+1][j+1]);\n\t\t\t}\n\t\t}\n\n\t\t// 出力\n\t\tSystem.out.println(dp[1][1]);\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 36, | |
| "name": "answer_A36", | |
| "Python": "# 入力\nN, K = map(int, input().split())\n\n# 出力\nif K >= 2*N-2 and K%2 == 0:\n\tprint(\"Yes\")\nelse:\n\tprint(\"No\")\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tint K = sc.nextInt();\n\n\t\t// 出力\n\t\tif (K >= 2*N-2 && K%2 == 0) System.out.println(\"Yes\");\n\t\telse System.out.println(\"No\");\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 37, | |
| "name": "answer_A37", | |
| "Python": "# 入力\nN, M, B = map(int, input().split())\nA = list(map(int, input().split()))\nC = list(map(int, input().split()))\n\n# 答えの計算\nAnswer = 0\nfor i in range(N):\n\tAnswer += A[i] * M\nAnswer += B * N * M\nfor j in range(M):\n\tAnswer += C[j] * N\n\n# 出力\nprint(Answer)\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tint M = sc.nextInt();\n\t\tlong B = sc.nextLong();\n\t\tlong[] A = new long[N + 1];\n\t\tlong[] C = new long[M + 1];\n\t\tfor (int i = 1; i <= N; i++) A[i] = sc.nextLong();\n\t\tfor (int j = 1; j <= M; j++) C[j] = sc.nextLong();\n\n\t\t// 答えの計算\n\t\tlong Answer = 0;\n\t\tfor (int i = 1; i <= N; i++) Answer += A[i] * M;\n\t\tAnswer += B * N * M;\n\t\tfor (int j = 1; j <= M; j++) Answer += C[j] * N;\n\n\t\t// 出力\n\t\tSystem.out.println(Answer);\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 38, | |
| "name": "answer_A38", | |
| "Python": "# 入力\nD, N = map(int, input().split())\nL = [ None ] * N\nR = [ None ] * N\nH = [ None ] * N\nfor i in range(N):\n\tL[i], R[i], H[i] = map(int, input().split())\n\n# 配列の初期化(1 日は 24 時間)\nLIM = [ 24 ] * (D + 1)\n\n# 上限値を求める\nfor i in range(N):\n\tfor j in range(L[i], R[i]+1):\n\t\tLIM[j] = min(LIM[j], H[i])\n\n# 答えを出力\nAnswer = 0\nfor i in range(1, D+1):\n\tAnswer += LIM[i]\nprint(Answer)\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint D = sc.nextInt();\n\t\tint N = sc.nextInt();\n\t\tint[] L = new int[N + 1];\n\t\tint[] R = new int[N + 1];\n\t\tint[] H = new int[N + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tL[i] = sc.nextInt();\n\t\t\tR[i] = sc.nextInt();\n\t\t\tH[i] = sc.nextInt();\n\t\t}\n\n\t\t// 配列の初期化(1 日は 24 時間)\n\t\tint[] LIM = new int[D + 1];\n\t\tfor (int i = 1; i <= D; i++) LIM[i] = 24;\n\n\t\t// 上限値を求める\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tfor (int j = L[i]; j <= R[i]; j++) LIM[j] = Math.min(LIM[j], H[i]);\n\t }\n\n\t\t// 出力\n\t\tint Answer = 0;\n\t\tfor (int i = 1; i <= D; i++) Answer += LIM[i];\n\t\tSystem.out.println(Answer);\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 39, | |
| "name": "answer_A39", | |
| "Python": "# 入力\n# A は (終了時刻, 開始時刻) になっていることに注意 [終了時刻の昇順にソートするため]\nN = int(input())\nA = []\nfor i in range(N):\n\tl, r = map(int, input().split())\n\tA.append([r, l])\n\n# ソート\nA.sort()\n\n# 終了時刻の早いものから貪欲に取っていく(CurrentTime は現在時刻)\nCurrentTime = 0\nAnswer = 0\nfor i in range(N):\n\tif CurrentTime <= A[i][1]:\n\t\tCurrentTime = A[i][0]\n\t\tAnswer += 1\n\n# 出力\nprint(Answer)\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tint[][] A = new int[N][2];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tA[i - 1][0] = sc.nextInt();\n\t\t\tA[i - 1][1] = sc.nextInt();\n\t\t}\n\n\t\t// 配列 A を終了時刻(A[x][1])の昇順にソート\n\t\tArrays.sort(A, (a1, a2) -> Integer.compare(a1[1], a2[1]));\n\n\t\t// 終了時刻の早いものから貪欲に取っていく(CurrentTime は現在時刻)\n\t\tint CurrentTime = 0;\n\t\tint Answer = 0;\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tif (CurrentTime <= A[i - 1][0]) {\n\t\t\t\tCurrentTime = A[i - 1][1];\n\t\t\t\tAnswer += 1;\n\t\t\t}\n\t\t}\n\n\t\t// 出力\n\t\tSystem.out.println(Answer);\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 40, | |
| "name": "answer_A40", | |
| "Python": "# 入力\nN = int(input())\nA = list(map(int, input().split()))\n\n# 個数を数える\ncnt = [ 0 ] * 101\nAnswer = 0\nfor i in range(N):\n\tcnt[A[i]] += 1\n\n# 答えを求める\nfor i in range(1, 101):\n\tAnswer += cnt[i] * (cnt[i]-1) * (cnt[i]-2) // 6;\nprint(Answer)\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tint[] A = new int[N + 1];\n\t\tfor (int i = 1; i <= N; i++) A[i] = sc.nextInt();\n\n\t\t// 個数を数える\n\t\tlong[] cnt = new long[101];\n\t\tfor (int i = 1; i <= 100; i++) cnt[i] = 0;\n\t\tfor (int i = 1; i <= N; i++) cnt[A[i]] += 1;\n\n\t\t// 答えを求める\n\t\t// nC3 = n * (n-1) * (n-2) / 6 を使っている\n\t\tlong Answer = 0;\n\t\tfor (int i = 1; i <= 100; i++) {\n\t\t\tAnswer += cnt[i] * (cnt[i]-1) * (cnt[i]-2) / 6;\n\t\t}\n\t\tSystem.out.println(Answer);\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 41, | |
| "name": "answer_A41", | |
| "Python": "# 入力\nN = int(input())\nS = input()\n\n# 答えを求める\nAnswer = False\nfor i in range(0, N-2):\n\tif S[i] == 'R' and S[i+1] == 'R' and S[i+2] == 'R':\n\t\tAnswer = True\n\tif S[i] == 'B' and S[i+1] == 'B' and S[i+2] == 'B':\n\t\tAnswer = True\n\n# 出力\nif Answer == True:\n\tprint(\"Yes\")\nelse:\n\tprint(\"No\")\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tString S = sc.next();\n\n\t\t// 答えを求める\n\t\tboolean Answer = false;\n\t\tfor (int i = 0; i <= N - 3; i++) {\n\t\t\tif (S.charAt(i)=='R' && S.charAt(i+1)=='R' && S.charAt(i+2)=='R') Answer = true;\n\t\t\tif (S.charAt(i)=='B' && S.charAt(i+1)=='B' && S.charAt(i+2)=='B') Answer = true;\n\t\t}\n\n\t\t// 出力\n\t\tif (Answer == true) System.out.println(\"Yes\");\n\t\telse System.out.println(\"No\");\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 42, | |
| "name": "answer_A42", | |
| "Python": "# 整数の組 (a, b) が決まったときの、参加可能な生徒数を返す関数\ndef GetScore(a, b, A, B, K):\n\tcnt = 0\n\tfor i in range(N):\n\t\tif a <= A[i] and A[i] <= a + K and b <= B[i] and B[i] <= b+K:\n\t\t\tcnt += 1\n\treturn cnt\n\n# 入力\nN, K = map(int, input().split())\nA = [ None ] * N\nB = [ None ] * N\nfor i in range(N):\n\tA[i], B[i] = map(int, input().split())\n\n# (a, b) の組を全探索\nAnswer = 0\nfor a in range(1, 101):\n\tfor b in range(1, 101):\n\t\tScore = GetScore(a, b, A, B, K)\n\t\tAnswer = max(Answer, Score)\n\n# 出力\nprint(Answer)\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tint K = sc.nextInt();\n\t\tint[] A = new int[N + 1];\n\t\tint[] B = new int[N + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tA[i] = sc.nextInt();\n\t\t\tB[i] = sc.nextInt();\n\t\t}\n\n\t\t// (a, b) の組を全探索\n\t\tint Answer = 0;\n\t\tfor (int a = 1; a <= 100; a++) {\n\t\t\tfor (int b = 1; b <= 100; b++) {\n\t\t\t\tint Score = GetScore(a, b, N, K, A, B);\n\t\t\t\tAnswer = Math.max(Answer, Score);\n\t\t\t}\n\t\t}\n\n\t\t// 出力\n\t\tSystem.out.println(Answer);\n\t}\n\n\t// 整数の組 (a, b) が決まったときの、参加可能な生徒数を返す関数\n\tstatic int GetScore(int a, int b, int N, int K, int[] A, int[] B) {\n\t\tint cnt = 0;\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tif (a<=A[i] && A[i]<=a+K && b<=B[i] && B[i]<=b+K) {\n\t\t\t\tcnt += 1;\n\t\t\t}\n\t\t}\n\t\treturn cnt;\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 43, | |
| "name": "answer_A43", | |
| "Python": "# 入力\nN, L = map(int, input().split())\nA = [ None ] * N\nB = [ None ] * N\nfor i in range(N):\n\tA[i], B[i] = input().split()\n\tA[i] = int(A[i])\n\n# 答えを求める\nAnswer = 0\nfor i in range(N):\n\tif B[i] == 'E':\n\t\tAnswer = max(Answer, L - A[i])\n\tif B[i] == 'W':\n\t\tAnswer = max(Answer, A[i])\n\n# 出力\nprint(Answer)\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tint L = sc.nextInt();\n\t\tint[] A = new int[N + 1];\n\t\tchar[] B = new char[N + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tA[i] = sc.nextInt();\n\t\t\tB[i] = sc.next().charAt(0);\n\t\t}\n\n\t\t// 答えを求める\n\t\tint Answer = 0;\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tif (B[i] == 'E') Answer = Math.max(Answer, L - A[i]);\n\t\t\tif (B[i] == 'W') Answer = Math.max(Answer, A[i]);\n\t\t}\n\t\tSystem.out.println(Answer);\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 44, | |
| "name": "answer_A44", | |
| "Python": "# 入力・配列の準備\nN, Q = map(int, input().split())\nState = 1\nE = [ None ] * (N+2)\nfor i in range(1, N+1):\n\tE[i] = i\n\n# クエリの処理\nfor i in range(Q):\n\tQuery = input().split()\n\n\t# [1] 変更操作\n\tif int(Query[0]) == 1:\n\t\tx = int(Query[1])\n\t\ty = int(Query[2])\n\t\tif State == 1:\n\t\t\tE[x] = y\n\t\tif State == 2:\n\t\t\tE[N+1-x] = y\n\n\t# [2] 反転操作\n\tif int(Query[0]) == 2:\n\t\tif State == 1:\n\t\t\tState = 2\n\t\telse:\n\t\t\tState = 1\n\n\t# [3] 取得操作\n\tif int(Query[0]) == 3:\n\t\tx = int(Query[1])\n\t\tif State == 1:\n\t\t\tprint(E[x])\n\t\tif State == 2:\n\t\t\tprint(E[N+1-x])\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tint Q = sc.nextInt();\n\n\t\t// 配列の初期化\n\t\tint State = 1;\n\t\tint[] E = new int[N + 1];\n\t\tfor (int i = 1; i <= N; i++) E[i] = i;\n\n\t\t// クエリの処理\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tint Type, x, y;\n\t\t\tType = sc.nextInt();\n\n\t\t\t// [1] 変更操作\n\t\t\tif (Type == 1) {\n\t\t\t\tx = sc.nextInt();\n\t\t\t\ty = sc.nextInt();\n\t\t\t\tif (State == 1) E[x] = y;\n\t\t\t\tif (State == 2) E[N+1-x] = y;\n\t\t\t}\n\n\t\t\t// [2] 反転操作\n\t\t\tif (Type == 2) {\n\t\t\t\tif (State == 1) State = 2;\n\t\t\t\telse State = 1;\n\t\t\t}\n\n\t\t\t// [3] 取得操作\n\t\t\tif (Type == 3) {\n\t\t\t\tx = sc.nextInt();\n\t\t\t\tif (State == 1) System.out.println(E[x]);\n\t\t\t\tif (State == 2) System.out.println(E[N + 1 - x]);\n\t\t\t}\n\t\t}\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 45, | |
| "name": "answer_A45", | |
| "Python": "# 入力\nN, C = input().split()\nA = input()\nN = int(N)\n\n# スコアの計算\nscore = 0\nfor i in range(N):\n\tif A[i] == 'W':\n\t\tscore += 0\n\tif A[i] == 'B':\n\t\tscore += 1\n\tif A[i] == 'R':\n\t\tscore += 2\n\n# 出力\nif score%3 == 0 and C == 'W':\n\tprint(\"Yes\")\nelif score%3 == 1 and C == 'B':\n\tprint(\"Yes\")\nelif score%3 == 2 and C == 'R':\n\tprint(\"Yes\")\nelse:\n\tprint(\"No\")\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = sc.nextInt();\n\t\tchar C = sc.next().charAt(0);\n\t\tString A = sc.next();\n\n\t\t// スコアの計算\n\t\tint score = 0;\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tif (A.charAt(i) == 'W') score += 0;\n\t\t\tif (A.charAt(i) == 'B') score += 1;\n\t\t\tif (A.charAt(i) == 'R') score += 2;\n\t\t}\n\n\t\t// 出力\n\t\tif (score % 3 == 0 && C == 'W') System.out.println(\"Yes\");\n\t\telse if (score % 3 == 1 && C == 'B') System.out.println(\"Yes\");\n\t\telse if (score % 3 == 2 && C == 'R') System.out.println(\"Yes\");\n\t\telse System.out.println(\"No\");\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 46, | |
| "name": "answer_A46", | |
| "Python": "# 二次元の点を扱うクラス\nclass point2d:\n\tdef __init__(self, x, y):\n\t\tself.x = x\n\t\tself.y = y\n\t# 2 点間の距離を求める関数\n\tdef dist(self, p):\n\t\treturn ((self.x - p.x) ** 2 + (self.y - p.y) ** 2) ** 0.5\n\n# 貪欲法によって答えを求める関数\n# (ここでは都市の番号を 0-indexed で扱っていることに注意)\ndef play_greedy(n, points):\n\t# 配列などの初期化\n\tcurrent_place = 0\n\tvisited = [ False ] * n\n\tvisited[0] = True\n\tP = [ 0 ]\n\t# 貪欲法スタート\n\tfor i in range(1, n):\n\t\tmindist = 10 ** 10 # 現時点での距離の最小\n\t\tmin_id = -1 # 次はどの都市に移動すればよいか\n\t\t# 距離が最小となる都市を探す\n\t\tfor j in range(n):\n\t\t\tif not visited[j] and mindist > points[current_place].dist(points[j]):\n\t\t\t\tmindist = points[current_place].dist(points[j])\n\t\t\t\tmin_id = j\n\t\t# 現在位置の更新\n\t\tvisited[min_id] = True\n\t\tP.append(min_id)\n\t\tcurrent_place = min_id\n\t# 最後に訪問する都市\n\tP.append(0)\n\treturn P\n\n# 入力\nN = int(input())\npoints = [ None ] * N\nfor i in range(N):\n\tx, y = map(int, input().split())\n\tpoints[i] = point2d(x, y)\n\n# 貪欲法\nanswer = play_greedy(N, points)\n\n# 答えを出力(配列 answer の要素は 0-indexed になっていることに注意)\nfor i in answer:\n\tprint(i + 1)", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\t// 入力\n\t\tScanner sc = new Scanner(System.in);\n\t\tint N = sc.nextInt();\n\t\tPoint2D[] points = new Point2D[N + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tpoints[i] = new Point2D(sc.nextInt(), sc.nextInt());\n\t\t}\n\n\t\t// 貪欲法\n\t\tint[] answer = playGreedy(N, points);\n\n\t\t// 答えを出力\n\t\tfor (int i = 1; i <= N + 1; i++) {\n\t\t\tSystem.out.println(answer[i]);\n\t\t}\n\t}\n\n\t// 貪欲法によって答えを求める関数\n\tstatic int[] playGreedy(int N, Point2D[] points) {\n\t\t// 配列の初期化\n\t\tint currentPlace = 1;\n\t\tint[] P = new int[N + 2];\n\t\tboolean[] visited = new boolean[N + 1]; // Java では new で初期化した配列の要素は false になることに注意\n\t\tP[1] = 1;\n\t\tvisited[1] = true;\n\n\t\t// 貪欲法スタート\n\t\tfor (int i = 2; i <= N; i++) {\n\t\t\tdouble minDist = 1.0e+99; // 現時点での距離の最小\n\t\t\tint minID = -1; // 次はどの都市に移動すればよいか\n\t\t\t// 距離が最小となる都市を探す\n\t\t\tfor (int j = 1; j <= N; j++) {\n\t\t\t\tif (visited[j]) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tdouble newDist = points[currentPlace].dist(points[j]);\n\t\t\t\tif (minDist > newDist) {\n\t\t\t\t\tminDist = newDist;\n\t\t\t\t\tminID = j;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// 現在位置の更新\n\t\t\tvisited[minID] = true;\n\t\t\tP[i] = minID;\n\t\t\tcurrentPlace = minID;\n\t\t}\n\n\t\t// 最後に訪問する都市\n\t\tP[N + 1] = 1;\n\n\t\treturn P;\n\t}\n\n\t// 二次元の点を扱うクラス Point2D\n\tstatic class Point2D {\n\t\tint x, y;\n\t\tpublic Point2D(int x, int y) {\n\t\t\tthis.x = x;\n\t\t\tthis.y = y;\n\t\t}\n\t\t// 2 点間の距離を求める関数\n\t\tdouble dist(Point2D p) {\n\t\t\treturn Math.sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));\n\t\t}\n\t}\n}" | |
| }, | |
| { | |
| "id": 47, | |
| "name": "answer_A47", | |
| "Python": "import random\n\n# 二次元の点を扱うクラス\nclass point2d:\n\tdef __init__(self, x, y):\n\t\tself.x = x\n\t\tself.y = y\n\t# 2 点間の距離を求める関数\n\tdef dist(self, p):\n\t\treturn ((self.x - p.x) ** 2 + (self.y - p.y) ** 2) ** 0.5\n\n# 合計距離を計算する関数\ndef get_score(n, points, P):\n\tscore = 0\n\tfor i in range(n):\n\t\tscore += points[P[i]].dist(points[P[i + 1]])\n\treturn score\n\n# 山登り法によって答えを求める関数\n# (ここでは都市の番号を 0-indexed で扱っていることに注意)\ndef hill_climbing(n, points):\n\t# 初期解生成\n\tP = [ i % n for i in range(n + 1) ]\n\tcurrent_score = get_score(n, points, P)\n\t# 山登り法\n\tNUM_LOOPS = 40000\n\tfor t in range(NUM_LOOPS):\n\t\t# 反転させる区間 [L, R] を選ぶ\n\t\tl = random.randint(1, n - 1) # 1 以上 n-1 以下のランダムな整数\n\t\tr = random.randint(1, n - 1) # 1 以上 n-1 以下のランダムな整数\n\t\tif l > r:\n\t\t\tl, r = r, l\n\t\t# P[l], P[l+1], ..., P[r] を逆順にする\n\t\tP[l:r+1] = reversed(P[l:r+1])\n\t\tnew_score = get_score(n, points, P)\n\t\t# 改善すればスコアを更新、悪化すれば元に戻す\n\t\tif current_score >= new_score:\n\t\t\tcurrent_score = new_score\n\t\telse:\n\t\t\tP[l:r+1] = reversed(P[l:r+1])\n\treturn P\n\n# 入力\nN = int(input())\npoints = [ None ] * N\nfor i in range(N):\n\tx, y = map(int, input().split())\n\tpoints[i] = point2d(x, y)\n\n# 山登り法\nanswer = hill_climbing(N, points)\n\n# 答えを出力(配列 answer の要素は 0-indexed になっていることに注意)\nfor i in answer:\n\tprint(i + 1)\n\n# 注意 1:このプログラムを Python で提出すると、山登り法のループを 7,000 回程度しか回せませんが、\n# PyPy3 で提出するとより高速に動作し、山登り法のループを 40,000 回程度回すことができます。", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\t// 入力\n\t\tScanner sc = new Scanner(System.in);\n\t\tint N = sc.nextInt();\n\t\tPoint2D[] points = new Point2D[N + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tpoints[i] = new Point2D(sc.nextInt(), sc.nextInt());\n\t\t}\n\n\t\t// 山登り法\n\t\tint[] answer = hillClimbing(N, points);\n\n\t\t// 答えを出力\n\t\tfor (int i = 1; i <= N + 1; i++) {\n\t\t\tSystem.out.println(answer[i]);\n\t\t}\n\t}\n\n\t// 合計距離を計算する関数\n\tstatic double getScore(int N, Point2D[] points, int[] P) {\n\t\tdouble score = 0.0;\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tscore += points[P[i]].dist(points[P[i + 1]]);\n\t\t}\n\t\treturn score;\n\t}\n\n\t// 山登り法によって答えを求める関数\n\tstatic int[] hillClimbing(int N, Point2D[] points) {\n\t\t// 初期解生成\n\t\tint[] P = new int[N + 2];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tP[i] = i;\n\t\t}\n\t\tP[N + 1] = 1;\n\t\t\n\t\t// 山登り法\n\t\tfinal int NUM_LOOPS = 200000;\n\t\tRandom rnd = new Random();\n\t\tdouble currentScore = getScore(N, points, P);\n\t\tfor (int t = 1; t <= NUM_LOOPS; t++) {\n\t\t\t// ランダムに反転させる区間 [L, R] を選ぶ\n\t\t\tint L = 2 + rnd.nextInt(N - 1); // 2 以上 N 以下のランダムな整数\n\t\t\tint R = 2 + rnd.nextInt(N - 1); // 2 以上 N 以下のランダムな整数\n\t\t\tif (L > R) {\n\t\t\t\t// L と R を交換\n\t\t\t\tint z = L; L = R; R = z;\n\t\t\t}\n\t\t\t// P[L], P[L+1], ..., P[R] の順序を逆転させる\n\t\t\tfor (int i = L, j = R; i < j; i++, j--) {\n\t\t\t\t// P[i] と P[j] を交換\n\t\t\t\tint z = P[i]; P[i] = P[j]; P[j] = z;\n\t\t\t}\n\t\t\tdouble newScore = getScore(N, points, P);\n\t\t\t// 改善すればスコアを更新、悪化すれば元に戻す\n\t\t\tif (currentScore >= newScore) {\n\t\t\t\tcurrentScore = newScore;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tfor (int i = L, j = R; i < j; i++, j--) {\n\t\t\t\t\t// P[i] と P[j] を交換\n\t\t\t\t\tint z = P[i]; P[i] = P[j]; P[j] = z;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn P;\n\t}\n\n\t// 二次元の点を扱うクラス Point2D\n\tstatic class Point2D {\n\t\tint x, y;\n\t\tpublic Point2D(int x, int y) {\n\t\t\tthis.x = x;\n\t\t\tthis.y = y;\n\t\t}\n\t\t// 2 点間の距離を求める関数\n\t\tdouble dist(Point2D p) {\n\t\t\treturn Math.sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));\n\t\t}\n\t}\n}" | |
| }, | |
| { | |
| "id": 48, | |
| "name": "answer_A48", | |
| "Python": "import math\nimport random\n\n# 二次元の点を扱うクラス\nclass point2d:\n\tdef __init__(self, x, y):\n\t\tself.x = x\n\t\tself.y = y\n\t# 2 点間の距離を求める関数\n\tdef dist(self, p):\n\t\treturn ((self.x - p.x) ** 2 + (self.y - p.y) ** 2) ** 0.5\n\n# 合計距離を計算する関数\ndef get_score(n, points, P):\n\tscore = 0\n\tfor i in range(n):\n\t\tscore += points[P[i]].dist(points[P[i + 1]])\n\treturn score\n\n# 焼きなまし法によって答えを求める関数\n# (ここでは都市の番号を 0-indexed で扱っていることに注意)\ndef simulated_annealing(n, points):\n\t# 初期解生成\n\tP = [ i % n for i in range(n + 1) ]\n\tcurrent_score = get_score(n, points, P)\n\t# 焼きなまし法\n\tNUM_LOOPS = 40000\n\tfor t in range(NUM_LOOPS):\n\t\t# 反転させる区間 [L, R] を選ぶ\n\t\tl = random.randint(1, n - 1) # 1 以上 n-1 以下のランダムな整数\n\t\tr = random.randint(1, n - 1) # 1 以上 n-1 以下のランダムな整数\n\t\tif l > r:\n\t\t\tl, r = r, l\n\t\t# P[l], P[l+1], ..., P[r] を逆順にする\n\t\tP[l:r+1] = reversed(P[l:r+1])\n\t\tnew_score = get_score(n, points, P)\n\t\t# 7.2 節の解答例から変更した唯一の部分(probability は採用確率)\n\t\t# (random.random() で 0 以上 1 未満のランダムな実数を生成)\n\t\tT = 30 - 28 * (t / NUM_LOOPS)\n\t\tprobability = math.exp(min((current_score - new_score) / T, 0))\n\t\tif random.random() < probability:\n\t\t\tcurrent_score = new_score\n\t\telse:\n\t\t\tP[l:r+1] = reversed(P[l:r+1])\n\treturn P\n\n# 入力\nN = int(input())\npoints = [ None ] * N\nfor i in range(N):\n\tx, y = map(int, input().split())\n\tpoints[i] = point2d(x, y)\n\n# 焼きなまし法\nanswer = simulated_annealing(N, points)\n\n# 答えを出力(配列 answer の要素は 0-indexed になっていることに注意)\nfor i in answer:\n\tprint(i + 1)\n\n# 注意 1:このプログラムを Python で提出すると、山登り法のループを 7,000 回程度しか回せませんが、\n# PyPy3 で提出するとより高速に動作し、山登り法のループを 40,000 回程度回すことができます。\n# 注意 2:さらにひと工夫すると、PyPy3 でループを 150,000 回程度回せるようになり、スコア 5000 以上も期待できるようになrみあす。", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\t// 入力\n\t\tScanner sc = new Scanner(System.in);\n\t\tint N = sc.nextInt();\n\t\tPoint2D[] points = new Point2D[N + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tpoints[i] = new Point2D(sc.nextInt(), sc.nextInt());\n\t\t}\n\n\t\t// 焼きなまし法\n\t\tint[] answer = simulatedAnnealing(N, points);\n\n\t\t// 答えを出力\n\t\tfor (int i = 1; i <= N + 1; i++) {\n\t\t\tSystem.out.println(answer[i]);\n\t\t}\n\t}\n\n\t// 合計距離を計算する関数\n\tstatic double getScore(int N, Point2D[] points, int[] P) {\n\t\tdouble score = 0.0;\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tscore += points[P[i]].dist(points[P[i + 1]]);\n\t\t}\n\t\treturn score;\n\t}\n\n\t// 焼きなまし法によって答えを求める関数\n\tstatic int[] simulatedAnnealing(int N, Point2D[] points) {\n\t\t// 初期解生成\n\t\tint[] P = new int[N + 2];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tP[i] = i;\n\t\t}\n\t\tP[N + 1] = 1;\n\t\t\n\t\t// 山登り法\n\t\tfinal int NUM_LOOPS = 200000;\n\t\tRandom rnd = new Random();\n\t\tdouble currentScore = getScore(N, points, P);\n\t\tfor (int t = 1; t <= NUM_LOOPS; t++) {\n\t\t\t// ランダムに反転させる区間 [L, R] を選ぶ\n\t\t\tint L = 2 + rnd.nextInt(N - 1); // 2 以上 N 以下のランダムな整数\n\t\t\tint R = 2 + rnd.nextInt(N - 1); // 2 以上 N 以下のランダムな整数\n\t\t\tif (L > R) {\n\t\t\t\t// L と R を交換\n\t\t\t\tint z = L; L = R; R = z;\n\t\t\t}\n\t\t\t// P[L], P[L+1], ..., P[R] の順序を逆転させる\n\t\t\tfor (int i = L, j = R; i < j; i++, j--) {\n\t\t\t\t// P[i] と P[j] を交換\n\t\t\t\tint z = P[i]; P[i] = P[j]; P[j] = z;\n\t\t\t}\n\t\t\tdouble newScore = getScore(N, points, P);\n\t\t\t// 7.2 節の解答例から変更した唯一の部分(probability は採用確率)\n\t\t\t// (rnd.nextDouble() で 0 以上 1 未満のランダムな実数を生成)\n\t\t\tdouble T = 30.0 - 28.0 * t / NUM_LOOPS;\n\t\t\tdouble probability = Math.exp(Math.min((currentScore - newScore) / T, 0.0));\n\t\t\tif (rnd.nextDouble() < probability) {\n\t\t\t\tcurrentScore = newScore;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tfor (int i = L, j = R; i < j; i++, j--) {\n\t\t\t\t\t// P[i] と P[j] を交換\n\t\t\t\t\tint z = P[i]; P[i] = P[j]; P[j] = z;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn P;\n\t}\n\n\t// 二次元の点を扱うクラス Point2D\n\tstatic class Point2D {\n\t\tint x, y;\n\t\tpublic Point2D(int x, int y) {\n\t\t\tthis.x = x;\n\t\t\tthis.y = y;\n\t\t}\n\t\t// 2 点間の距離を求める関数\n\t\tdouble dist(Point2D p) {\n\t\t\treturn Math.sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));\n\t\t}\n\t}\n}" | |
| }, | |
| { | |
| "id": 49, | |
| "name": "answer_A49", | |
| "Python": "import copy\n\n# 1 回の操作 (P[i], Q[i], R[i]) を表す構造体\nclass round:\n\tdef __init__(self, p, q, r):\n\t\tself.p = p\n\t\tself.q = q\n\t\tself.r = r\n\n# 盤面の状態を表す構造体\nclass state:\n\t# 盤面の状態の初期化\n\tdef __init__(self, n):\n\t\tself.score = 0 # 暫定スコア\n\t\tself.x = [ 0 ] * n # 現在の配列 X の値\n\t\tself.lastmove = '?' # 最後の動き('A' または 'B'、ただし初期状態では '?' としておく)\n\t\tself.lastpos = -1 # Beam[i-1][どこ] から遷移したか(ただし初期状態では -1 としておく)\n\n# ビームサーチを行う関数\ndef beam_search(N, T, rounds):\n\t# 2 次元配列 beam を用意し、0 手目の状態を設定\n\tWIDTH = 10000\n\tbeam = [ list() for i in range(T + 1) ]\n\tbeam[0].append(state(N))\n\n\t# ビームサーチ\n\tfor i in range(T):\n\t\tcandidate = list()\n\t\tfor j in range(len(beam[i])):\n\t\t\t# 操作 A の場合\n\t\t\tsousa_a = copy.deepcopy(beam[i][j])\n\t\t\tsousa_a.lastmove = 'A'\n\t\t\tsousa_a.lastpos = j\n\t\t\tsousa_a.x[rounds[i].p] += 1\n\t\t\tsousa_a.x[rounds[i].q] += 1\n\t\t\tsousa_a.x[rounds[i].r] += 1\n\t\t\tsousa_a.score += sousa_a.x.count(0) # スコアに「sousa_a.x に含まれる 0 の個数」を加算\n\t\t\t# 操作 B の場合\n\t\t\tsousa_b = copy.deepcopy(beam[i][j])\n\t\t\tsousa_b.lastmove = 'B'\n\t\t\tsousa_b.lastpos = j\n\t\t\tsousa_b.x[rounds[i].p] -= 1\n\t\t\tsousa_b.x[rounds[i].q] -= 1\n\t\t\tsousa_b.x[rounds[i].r] -= 1\n\t\t\tsousa_b.score += sousa_b.x.count(0) # スコアに「sousa_a.x に含まれる 0 の個数」を加算\n\t\t\t# 候補に追加\n\t\t\tcandidate.append(sousa_a)\n\t\t\tcandidate.append(sousa_b)\n\t\t# ソートして beam[i+1] の結果を計算する\n\t\tcandidate.sort(key = lambda s: -s.score)\n\t\tbeam[i + 1] = copy.deepcopy(candidate[:WIDTH]) # 多くとも candidate の上位 WIDTH 件しか記録しない\n\t\n\t# ビームサーチの復元\n\tcurrent_place = 0\n\tanswer = [ None ] * T\n\tfor i in range(T, 0, -1):\n\t\tanswer[i - 1] = beam[i][current_place].lastmove\n\t\tcurrent_place = beam[i][current_place].lastpos\n\treturn answer\n\n\n# 入力\nT = int(input())\nrounds = [ None ] * T\nfor i in range(T):\n\tp, q, r = map(int, input().split())\n\trounds[i] = round(p - 1, q - 1, r - 1) # 書籍とは異なり、ここでは 0-indexed にするために 1 を引いています\n\n# ビームサーチを行って答えを出す(書籍とは異なり、ビームサーチの復元は関数の中で行う)\nanswer = beam_search(20, T, rounds)\n\n# 出力\nfor c in answer:\n\tprint(c)\n\n# 注意 1:このプログラムは、Python で提出するとビーム幅 200 程度、PyPy3 で提出するとビーム幅 350 程度で、実行時間制限の 1 秒ギリギリになります。\n# 注意 2:ここでは deepcopy 関数を使っていますが、これが実行速度を遅くする原因となっています。\n# これを使わずに実装すると、プログラムがより高速になり、ビーム幅をより大きくすることができます。考えてみましょう。", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\t// 入力\n\t\tScanner sc = new Scanner(System.in);\n\t\tfinal int N = 20;\n\t\tint T = sc.nextInt();\n\t\tint[] P = new int[T + 1];\n\t\tint[] Q = new int[T + 1];\n\t\tint[] R = new int[T + 1];\n\t\tfor (int i = 1; i <= T; i++) {\n\t\t\tP[i] = sc.nextInt();\n\t\t\tQ[i] = sc.nextInt();\n\t\t\tR[i] = sc.nextInt();\n\t\t}\n\t\t\n\t\t// ビームサーチ(書籍とは異なり、ビームサーチの復元は関数の中で行う)\n\t\tchar[] answer = beamSearch(N, T, P, Q, R);\n\n\t\t// 答えの出力\n\t\tfor (int i = 1; i <= T; i++) {\n\t\t\tSystem.out.println(answer[i]);\n\t\t}\n\t}\n\n\t// ビームサーチを行う関数\n\tstatic char[] beamSearch(int N, int T, int[] P, int[] Q, int[] R) {\n\t\t// 2 次元配列 beam を用意し、0 手目の状態を設定\n\t\tfinal int WIDTH = 7000; // WIDTH はビーム幅\n\t\tArrayList<State>[] beam = new ArrayList[T + 1];\n\t\tbeam[0] = new ArrayList<>();\n\t\tbeam[0].add(new State(N));\n\n\t\t// ビームサーチ\n\t\tfor (int i = 1; i <= T; i++) {\n\t\t\tArrayList<State> candidate = new ArrayList<>();\n\t\t\tfor (int j = 0; j < beam[i - 1].size(); j++) {\n\t\t\t\t// 操作 A の場合\n\t\t\t\tState sousaA = new State(beam[i - 1].get(j));\n\t\t\t\tsousaA.lastMove = 'A';\n\t\t\t\tsousaA.lastPos = j;\n\t\t\t\tsousaA.x[P[i]] += 1;\n\t\t\t\tsousaA.x[Q[i]] += 1;\n\t\t\t\tsousaA.x[R[i]] += 1;\n\t\t\t\tfor (int k = 1; k <= N; k++) {\n\t\t\t\t\tif (sousaA.x[k] == 0) {\n\t\t\t\t\t\tsousaA.score += 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t// 操作 B の場合\n\t\t\t\tState sousaB = new State(beam[i - 1].get(j));\n\t\t\t\tsousaB.lastMove = 'B';\n\t\t\t\tsousaB.lastPos = j;\n\t\t\t\tsousaB.x[P[i]] -= 1;\n\t\t\t\tsousaB.x[Q[i]] -= 1;\n\t\t\t\tsousaB.x[R[i]] -= 1;\n\t\t\t\tfor (int k = 1; k <= N; k++) {\n\t\t\t\t\tif (sousaB.x[k] == 0) {\n\t\t\t\t\t\tsousaB.score += 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t// 候補に追加\n\t\t\t\tcandidate.add(sousaA);\n\t\t\t\tcandidate.add(sousaB);\n\t\t\t}\n\t\t\t// ソートして beam[i] の結果を計算する\n\t\t\tCollections.sort(candidate);\n\t\t\tbeam[i] = new ArrayList<State>(candidate.subList(0, Math.min(candidate.size(), WIDTH)));\n\t\t}\n\n\t\t// ビームサーチの復元(currentPlace は配列 beam のどの位置を見ているかを表す)\n\t\tchar[] answer = new char[T + 1];\n\t\tint currentPlace = 0;\n\t\tfor (int i = T; i >= 1; i--) {\n\t\t\tanswer[i] = beam[i].get(currentPlace).lastMove;\n\t\t\tcurrentPlace = beam[i].get(currentPlace).lastPos;\n\t\t}\n\n\t\treturn answer;\n\t}\n\n\t// 盤面の状態を表す構造体 State\n\tstatic class State implements Comparable<State> {\n\t\tint score; // 暫定スコア\n\t\tint[] x; // 現在の配列 X の値\n\t\tchar lastMove; // 最後の動き('A' または 'B')\n\t\tint lastPos; // Beam[i-1][どこ] から遷移したか\n\t\t// 盤面の状態の初期化\n\t\tpublic State(int N) {\n\t\t\tscore = 0;\n\t\t\tx = new int[N + 1];\n\t\t\tlastMove = '?';\n\t\t\tlastPos = -1;\n\t\t}\n\t\t// コピーコンストラクタ\n\t\tpublic State(State s) {\n\t\t\tscore = s.score;\n\t\t\tx = s.x.clone();\n\t\t\tlastMove = s.lastMove;\n\t\t\tlastPos = s.lastPos;\n\t\t}\n\t\t@Override public int compareTo(State s) {\n\t\t\treturn s.score - score; // ソートの際は、self.score > s.score のとき self が s よりも前に来るようにする\n\t\t}\n\t}\n}" | |
| }, | |
| { | |
| "id": 50, | |
| "name": "answer_A49_greedy1_37454pts", | |
| "Python": "################################################\n# 本の 266 ページ前半の貪欲法を用いた実装です\n################################################\n\n# 入力\nT = int(input())\nP = [ None ] * T\nQ = [ None ] * T\nR = [ None ] * T\nfor i in range(T):\n\tP[i], Q[i], R[i] = map(int, input().split())\n\tP[i] -= 1 # 0 始まりに直す\n\tQ[i] -= 1\n\tR[i] -= 1\n\n# 配列 A の初期化\nA = [ 0 ] * 20\n\n# 貪欲法\nCurrentScore = 0\nfor i in range(T):\n\t# パターン A の場合のスコアを求める\n\tScoreA = CurrentScore\n\tPatA = [ 0 ] * 20\n\tfor j in range(20):\n\t\tPatA[j] = A[j]\n\tPatA[P[i]] += 1\n\tPatA[Q[i]] += 1\n\tPatA[R[i]] += 1\n\tfor j in range(20):\n\t\tif PatA[j] == 0:\n\t\t\tScoreA += 1\n\n\t# パターン B の場合のスコアを求める\n\tScoreB = CurrentScore\n\tPatB = [ 0 ] * 20\n\tfor j in range(20):\n\t\tPatB[j] = A[j]\n\tPatB[P[i]] -= 1\n\tPatB[Q[i]] -= 1\n\tPatB[R[i]] -= 1\n\tfor j in range(20):\n\t\tif PatB[j] == 0:\n\t\t\tScoreB += 1\n\n\t# スコアの大きい方を採用\n\tif ScoreA >= ScoreB:\n\t\tprint(\"A\")\n\t\tCurrentScore = ScoreA\n\t\tfor j in range(20):\n\t\t\tA[j] = PatA[j]\n\telse:\n\t\tprint(\"B\")\n\t\tCurrentScore = ScoreB\n\t\tfor j in range(20):\n\t\t\tA[j] = PatB[j]\n", | |
| "Java": "// ###############################################\n// # 本の 266 ページ前半の貪欲法を用いた実装です\n// ###############################################\n \nimport java.util.*;\n \nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n \n\t\t// 入力\n\t\tint N = 20;\n\t\tint T = sc.nextInt();\n\t\tint[] P = new int[T + 1];\n\t\tint[] Q = new int[T + 1];\n\t\tint[] R = new int[T + 1];\n\t\tfor (int i = 1; i <= T; i++) {\n\t\t\tP[i] = sc.nextInt();\n\t\t\tQ[i] = sc.nextInt();\n\t\t\tR[i] = sc.nextInt();\n\t\t}\n \n\t\t// 配列 A の初期化\n\t\tint[] A = new int[21];\n\t\tint[] PatA = new int[21];\n\t\tint[] PatB = new int[21];\n\t\tfor (int i = 1; i <= 20; i++) A[i] = 0;\n \n\t\t// 貪欲法\n\t\tint CurrentScore = 0;\n\t\tfor (int i = 1; i <= T; i++) {\n\t\t\t// パターン A の場合のスコアを求める\n\t\t\tint ScoreA = CurrentScore;\n\t\t\tfor (int j = 1; j <= 20; j++) PatA[j] = A[j];\n\t\t\tPatA[P[i]] += 1;\n\t\t\tPatA[Q[i]] += 1;\n\t\t\tPatA[R[i]] += 1;\n\t\t\tfor (int j = 1; j <= 20; j++) {\n\t\t\t\tif (PatA[j] == 0) ScoreA += 1;\n\t\t\t}\n \n\t\t\t// パターン B の場合のスコアを求める\n\t\t\tint ScoreB = CurrentScore;\n\t\t\tfor (int j = 1; j <= 20; j++) PatB[j] = A[j];\n\t\t\tPatB[P[i]] -= 1;\n\t\t\tPatB[Q[i]] -= 1;\n\t\t\tPatB[R[i]] -= 1;\n\t\t\tfor (int j = 1; j <= 20; j++) {\n\t\t\t\tif (PatB[j] == 0) ScoreB += 1;\n\t\t\t}\n \n\t\t\t// スコアの大きい方を採用\n\t\t\tif (ScoreA >= ScoreB) {\n\t\t\t\tSystem.out.println(\"A\");\n\t\t\t\tCurrentScore = ScoreA;\n\t\t\t\tfor (int j = 1; j <= 20; j++) A[j] = PatA[j];\n\t\t\t}\n\t\t\telse {\n\t\t\t\tSystem.out.println(\"B\");\n\t\t\t\tCurrentScore = ScoreB;\n\t\t\t\tfor (int j = 1; j <= 20; j++) A[j] = PatB[j];\n\t\t\t}\n\t\t}\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 51, | |
| "name": "answer_A49_greedy2_40978pts", | |
| "Python": "################################################\n# 本の 272 ページ後半の評価関数を用いた実装です\n################################################\n\n# 入力\nT = int(input())\nP = [ None ] * T\nQ = [ None ] * T\nR = [ None ] * T\nfor i in range(T):\n\tP[i], Q[i], R[i] = map(int, input().split())\n\tP[i] -= 1 # 0 始まりに直す\n\tQ[i] -= 1\n\tR[i] -= 1\n\n# 配列 A の初期化\nA = [ 0 ] * 20\n\n# 貪欲法\nfor i in range(T):\n\t# パターン A の場合のスコアを求める\n\tScoreA = 0\n\tPatA = [ 0 ] * 20\n\tfor j in range(20):\n\t\tPatA[j] = A[j]\n\tPatA[P[i]] += 1\n\tPatA[Q[i]] += 1\n\tPatA[R[i]] += 1\n\tfor j in range(20):\n\t\tScoreA += abs(PatA[j])\n\n\t# パターン B の場合のスコアを求める\n\tScoreB = 0\n\tPatB = [ 0 ] * 20\n\tfor j in range(20):\n\t\tPatB[j] = A[j]\n\tPatB[P[i]] -= 1\n\tPatB[Q[i]] -= 1\n\tPatB[R[i]] -= 1\n\tfor j in range(20):\n\t\tScoreB += abs(PatB[j])\n\n\t# スコアの小さい方を採用\n\tif ScoreA <= ScoreB:\n\t\tprint(\"A\")\n\t\tCurrentScore = ScoreA\n\t\tfor j in range(20):\n\t\t\tA[j] = PatA[j]\n\telse:\n\t\tprint(\"B\")\n\t\tCurrentScore = ScoreB\n\t\tfor j in range(20):\n\t\t\tA[j] = PatB[j]\n", | |
| "Java": "// ###############################################\n// # 本の 272 ページ後半の評価関数を用いた実装です\n// ###############################################\n\nimport java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\n\t\t// 入力\n\t\tint N = 20;\n\t\tint T = sc.nextInt();\n\t\tint[] P = new int[T + 1];\n\t\tint[] Q = new int[T + 1];\n\t\tint[] R = new int[T + 1];\n\t\tfor (int i = 1; i <= T; i++) {\n\t\t\tP[i] = sc.nextInt();\n\t\t\tQ[i] = sc.nextInt();\n\t\t\tR[i] = sc.nextInt();\n\t\t}\n\n\t\t// 配列 A の初期化\n\t\tint[] A = new int[21];\n\t\tint[] PatA = new int[21];\n\t\tint[] PatB = new int[21];\n\t\tfor (int i = 1; i <= 20; i++) A[i] = 0;\n\n\t\t// 貪欲法\n\t\tfor (int i = 1; i <= T; i++) {\n\t\t\t// パターン A の場合のスコアを求める\n\t\t\tint ScoreA = 0;\n\t\t\tfor (int j = 1; j <= 20; j++) PatA[j] = A[j];\n\t\t\tPatA[P[i]] += 1;\n\t\t\tPatA[Q[i]] += 1;\n\t\t\tPatA[R[i]] += 1;\n\t\t\tfor (int j = 1; j <= 20; j++) ScoreA += Math.abs(PatA[j]);\n\n\t\t\t// パターン B の場合のスコアを求める\n\t\t\tint ScoreB = 0;\n\t\t\tfor (int j = 1; j <= 20; j++) PatB[j] = A[j];\n\t\t\tPatB[P[i]] -= 1;\n\t\t\tPatB[Q[i]] -= 1;\n\t\t\tPatB[R[i]] -= 1;\n\t\t\tfor (int j = 1; j <= 20; j++) ScoreB += Math.abs(PatB[j]);\n\n\t\t\t// スコアの小さい方を採用\n\t\t\tif (ScoreA <= ScoreB) {\n\t\t\t\tSystem.out.println(\"A\");\n\t\t\t\tfor (int j = 1; j <= 20; j++) A[j] = PatA[j];\n\t\t\t}\n\t\t\telse {\n\t\t\t\tSystem.out.println(\"B\");\n\t\t\t\tfor (int j = 1; j <= 20; j++) A[j] = PatB[j];\n\t\t\t}\n\t\t}\n\t}\n};\n" | |
| }, | |
| { | |
| "id": 52, | |
| "name": "answer_A50", | |
| "Python": "# 本プログラムでは、プログラムの実行速度を上げるため、numpy を使って 2 次元配列を計算しています。\n# numpy で計算しやすくするために、B[i][j] は 100×100 の配列ではなく、左右に広げた 300×300 の配列を使って計算しています。\n# プログラムは https://atcoder.jp/contests/future-contest-2018-qual/submissions/4216898 を参考に、本書籍のコードに基づいて実装しています。\n# ※ 期待されるスコアは、およそ 99.940 億点です。\n# ※ 焼きなまし法の実装は answer_A50_extra.py に載せています。\n\nimport numpy as np\nimport random\nimport time\nimport sys\n\n# 定数の設定・入力\nN = 100\nQ = 1000\nA = np.array([ list(map(int, input().split())) for i in range(N) ])\n\n# 初期解を生成\nX = [ random.randint(0, N - 1) for i in range(Q) ]\nY = [ random.randint(0, N - 1) for i in range(Q) ]\nH = [ 1 ] * Q\nB = np.zeros((3 * N, 3 * N))\nfor i in range(Q):\n\tB[Y[i]][X[i]] += 1\n\n# H = 1, 2, ..., N に設定された場合の「増減分」を持った numpy 配列を作る\ndelta = [ None ] * (N + 1)\nfor i in range(1, N + 1):\n\tdelta[i] = np.array([ [ max(i - abs(y) - abs(x), 0) for x in range(-i + 1, i) ] for y in range(-i + 1, i) ])\n\n# 現在のスコアを取得する関数\ndef get_score():\n\treturn 200000000 - np.absolute(A - B[N:2*N, N:2*N]).sum()\n\n# 山登り法の設定\n# (現在のスコアを np.absolute(A - B[N:2*N, N:2*N]).sum() として、これを最大化するという方法で実装する)\nTIME_LIMIT = 5.4\ncurrent_score = get_score()\nti = time.time()\n\n# 山登り法スタート\nloops = 0\nwhile time.time() - ti < TIME_LIMIT:\n\t# 「小さな変更」をランダムに選ぶ\n\tt = random.randint(0, Q - 1)\n\told_x, new_x = X[t], X[t] + random.randint(-9, +9)\n\told_y, new_y = Y[t], Y[t] + random.randint(-9, +9)\n\told_h, new_h = H[t], H[t] + random.randint(-19, +19)\n\tif new_x < 0 or new_x >= N or new_y < 0 or new_y >= N or new_h <= 0 or new_h > N:\n\t\tcontinue\n\t\n\t# X[t] = new_x, Y[t] = new_y, H[t] = new_h に変更(書籍中の Change(t, new_x, new_y, new_h) の呼び出しに対応)\n\tB[N+Y[t]-H[t]+1:N+Y[t]+H[t], N+X[t]-H[t]+1:N+X[t]+H[t]] -= delta[H[t]]\n\tX[t], Y[t], H[t] = new_x, new_y, new_h\n\tB[N+Y[t]-H[t]+1:N+Y[t]+H[t], N+X[t]-H[t]+1:N+X[t]+H[t]] += delta[H[t]]\n\n\t# スコアを計算\n\tnew_score = get_score()\n\n\t# スコアに応じて採用/不採用を決める\n\tif current_score < new_score:\n\t\t# 採用の場合\n\t\tcurrent_score = new_score\n\telse:\n\t\t# 不採用の場合:X[t] = old_x, Y[t] = old_y, H[t] = old_h に戻す(書籍中の Change(t, old_x, old_y, old_h) の呼び出しに対応)\n\t\tB[N+Y[t]-H[t]+1:N+Y[t]+H[t], N+X[t]-H[t]+1:N+X[t]+H[t]] -= delta[H[t]]\n\t\tX[t], Y[t], H[t] = old_x, old_y, old_h\n\t\tB[N+Y[t]-H[t]+1:N+Y[t]+H[t], N+X[t]-H[t]+1:N+X[t]+H[t]] += delta[H[t]]\n\t\n\tloops += 1\n\n# 出力\nprint(Q)\nfor i in range(Q):\n\tprint(X[i], Y[i], H[i])\nprint(\"score =\", current_score, file = sys.stderr)\nprint(\"loops =\", loops, file = sys.stderr)\n", | |
| "Java": "// 本プログラムは、山登り法を書籍内のコードに基づいて実装したものになります。\n// 書籍 p.283「さらなる高みへ」を実装した焼きなまし法のプログラムについては、answer_A50_extra.java をご覧ください。\n// ※ 期待されるスコアは、およそ 99.948 億点です。\n\nimport java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\t// 入力\n\t\tScanner sc = new Scanner(System.in);\n\t\tfinal int N = 100;\n\t\tfinal int Q = 1000;\n\t\tint[][] A = new int[N][N];\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tfor (int j = 0; j < N; j++) {\n\t\t\t\tA[i][j] = sc.nextInt();\n\t\t\t}\n\t\t}\n\n\t\t// 初期解を生成\n\t\tRandom rnd = new Random();\n\t\tAnswer ans = new Answer(N, Q, A, rnd);\n\n\t\t// 山登り法:変数の設定(5.4 秒ループを回す)\n\t\tfinal double TIME_LIMIT = 5.4;\n\t\tlong ti = System.currentTimeMillis();\n\t\tint currentScore = ans.getScore();\n\n\t\t// 山登り法スタート\n\t\tint loops = 0;\n\t\twhile ((System.currentTimeMillis() - ti) / 1000.0 < TIME_LIMIT) {\n\t\t\t// 「小さな変更」をランダムに選ぶ\n\t\t\tint t = rnd.nextInt(Q);\n\t\t\tint old_x = ans.X[t], new_x = ans.X[t] + (rnd.nextInt(19) - 9); // X[t] を -9 から +9 まで増減\n\t\t\tint old_y = ans.Y[t], new_y = ans.Y[t] + (rnd.nextInt(19) - 9); // Y[t] を -9 から +9 まで増減\n\t\t\tint old_z = ans.H[t], new_z = ans.H[t] + (rnd.nextInt(39) - 19); // H[t] を -19 から +19 まで増減\n\t\t\tif (new_x < 0 || new_x >= N || new_y < 0 || new_y >= N || new_z <= 0 || new_z > N) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t// とりあえず変更し、スコアを評価する\n\t\t\tans.change(t, new_x, new_y, new_z);\n\t\t\tint newScore = ans.getScore();\n\t\t\t// スコアに応じて採用/不採用を決める\n\t\t\tif (currentScore < newScore) {\n\t\t\t\tcurrentScore = newScore;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tans.change(t, old_x, old_y, old_z);\n\t\t\t}\n\t\t\tloops += 1;\n\t\t}\n\n\t\t// 出力\n\t\tSystem.out.println(Q);\n\t\tfor (int i = 0; i < Q; i++) {\n\t\t\tSystem.out.println(ans.X[i] + \" \" + ans.Y[i] + \" \" + ans.H[i]);\n\t\t}\n\t\tSystem.err.println(\"loops = \" + loops);\n\t\tSystem.err.println(\"score = \" + currentScore);\n\t}\n\n\t// 操作手順や盤面を記録した構造体 Answer\n\t// (ここでは書籍内のコードとは異なり、操作の番号は 1-indexed ではなく 0-indexed で実装しています(つまり番号が 0 から 999 になります))\n\tstatic class Answer {\n\t\tint N; // 盤面のサイズ\n\t\tint Q; // 操作の回数\n\t\tint[] X;\n\t\tint[] Y;\n\t\tint[] H;\n\t\tint[][] A;\n\t\tint[][] B;\n\t\t// 初期解を生成\n\t\tpublic Answer(int N, int Q, int[][] A, Random rnd) {\n\t\t\tthis.N = N;\n\t\t\tthis.Q = Q;\n\t\t\tthis.A = new int[N][N];\n\t\t\tfor (int i = 0; i < N; i++) {\n\t\t\t\tfor (int j = 0; j < N; j++) {\n\t\t\t\t\tthis.A[i][j] = A[i][j];\n\t\t\t\t}\n\t\t\t}\n\t\t\tX = new int[Q];\n\t\t\tY = new int[Q];\n\t\t\tH = new int[Q];\n\t\t\tB = new int[N][N];\n\t\t\tfor (int i = 0; i < Q; i++) {\n\t\t\t\tX[i] = rnd.nextInt(N); // 0 以上 N-1 以下のランダムな整数\n\t\t\t\tY[i] = rnd.nextInt(N); // 0 以上 N-1 以下のランダムな整数\n\t\t\t\tH[i] = 1;\n\t\t\t\tB[Y[i]][X[i]] += 1;\n\t\t\t}\n\t\t}\n\t\t// X[t] = x, Y[t] = y, H[t] = h に変更する関数\n\t\tvoid change(int t, int x, int y, int h) {\n\t\t\tfor (int i = 0; i < N; i++) {\n\t\t\t\tfor (int j = 0; j < N; j++) {\n\t\t\t\t\tB[i][j] -= Math.max(H[t] - Math.abs(Y[t] - i) - Math.abs(X[t] - j), 0);\n\t\t\t\t}\n\t\t\t}\n\t\t\tX[t] = x;\n\t\t\tY[t] = y;\n\t\t\tH[t] = h;\n\t\t\tfor (int i = 0; i < N; i++) {\n\t\t\t\tfor (int j = 0; j < N; j++) {\n\t\t\t\t\tB[i][j] += Math.max(H[t] - Math.abs(Y[t] - i) - Math.abs(X[t] - j), 0);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t// 現在のスコアを返す関数\n\t\tint getScore() {\n\t\t\tint sum = 0;\n\t\t\tfor (int i = 0; i < N; i++) {\n\t\t\t\tfor (int j = 0; j < N; j++) {\n\t\t\t\t\tsum += Math.abs(A[i][j] - B[i][j]);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn 200000000 - sum;\n\t\t}\n\t}\n}" | |
| }, | |
| { | |
| "id": 53, | |
| "name": "answer_A50_extra", | |
| "Python": "# 本プログラムは、書籍 p.283「さらなる高みへ」を実装した、焼きなまし法のコードとなります。\n# ベースとなる山登り法の実装については、answer_A50.py をご覧ください。\n# ※ 期待されるスコアは、およそ 99.962 億点です。\n\nimport math\nimport numpy as np\nimport random\nimport time\nimport sys\n\n# 定数の設定・入力\nN = 100\nQ = 1000\nA = np.array([ list(map(int, input().split())) for i in range(N) ])\n\n# 初期解を生成\nX = [ random.randint(0, N - 1) for i in range(Q) ]\nY = [ random.randint(0, N - 1) for i in range(Q) ]\nH = [ 1 ] * Q\nB = np.zeros((3 * N, 3 * N))\nfor i in range(Q):\n\tB[Y[i]][X[i]] += 1\n\n# H = 1, 2, ..., N に設定された場合の「増減分」を持った numpy 配列を作る\ndelta = [ None ] * (N + 1)\nfor i in range(1, N + 1):\n\tdelta[i] = np.array([ [ max(i - abs(y) - abs(x), 0) for x in range(-i + 1, i) ] for y in range(-i + 1, i) ])\n\n# 現在のスコアを取得する関数\ndef get_score():\n\treturn 200000000 - np.absolute(A - B[N:2*N, N:2*N]).sum()\n\n# 山登り法の設定\n# (現在のスコアを np.absolute(A - B[N:2*N, N:2*N]).sum() として、これを最大化するという方法で実装する)\nTIME_LIMIT = 5.4\ncurrent_score = get_score()\nti = time.time()\n\n# 山登り法スタート\nloops = 0\nwhile time.time() - ti < TIME_LIMIT:\n\t# 「小さな変更」をランダムに選ぶ\n\tt = random.randint(0, Q - 1)\n\th_limit = 14\n\tif current_score >= 199900000:\n\t\th_limit = 1\n\telif current_score >= 199500000:\n\t\th_limit = 7\n\told_x, new_x = X[t], X[t] + random.randint(-1, +1)\n\told_y, new_y = Y[t], Y[t] + random.randint(-1, +1)\n\told_h, new_h = H[t], H[t] + random.randint(-h_limit, +h_limit)\n\tif new_x < 0 or new_x >= N or new_y < 0 or new_y >= N or new_h <= 0 or new_h > N:\n\t\tcontinue\n\t\n\t# X[t] = new_x, Y[t] = new_y, H[t] = new_h に変更(書籍中の Change(t, new_x, new_y, new_h) の呼び出しに対応)\n\tB[N+Y[t]-H[t]+1:N+Y[t]+H[t], N+X[t]-H[t]+1:N+X[t]+H[t]] -= delta[H[t]]\n\tX[t], Y[t], H[t] = new_x, new_y, new_h\n\tB[N+Y[t]-H[t]+1:N+Y[t]+H[t], N+X[t]-H[t]+1:N+X[t]+H[t]] += delta[H[t]]\n\n\t# スコアを計算\n\tnew_score = get_score()\n\n\t# スコアに応じて採用/不採用を決める\n\ttemperature = 180.0 - 179.0 * (time.time() - ti) / TIME_LIMIT\n\tprobability = math.exp(min((new_score - current_score) / temperature, 0))\n\tif random.random() < probability:\n\t\t# 採用の場合\n\t\tcurrent_score = new_score\n\telse:\n\t\t# 不採用の場合:X[t] = old_x, Y[t] = old_y, H[t] = old_h に戻す(書籍中の Change(t, old_x, old_y, old_h) の呼び出しに対応)\n\t\tB[N+Y[t]-H[t]+1:N+Y[t]+H[t], N+X[t]-H[t]+1:N+X[t]+H[t]] -= delta[H[t]]\n\t\tX[t], Y[t], H[t] = old_x, old_y, old_h\n\t\tB[N+Y[t]-H[t]+1:N+Y[t]+H[t], N+X[t]-H[t]+1:N+X[t]+H[t]] += delta[H[t]]\n\t\n\tloops += 1\n\n# 出力\nprint(Q)\nfor i in range(Q):\n\tprint(X[i], Y[i], H[i])\nprint(\"score =\", current_score, file = sys.stderr)\nprint(\"loops =\", loops, file = sys.stderr)\n", | |
| "Java": "// 本プログラムは、書籍 p.283「さらなる高みへ」を実装した、焼きなまし法のコードとなります。\n// ベースとなる山登り法の実装については、answer_A50.java をご覧ください。\n// ※ 期待されるスコアは、およそ 99.977 億点です。\n\nimport java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\t// 入力\n\t\tScanner sc = new Scanner(System.in);\n\t\tfinal int N = 100;\n\t\tfinal int Q = 1000;\n\t\tint[][] A = new int[N][N];\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tfor (int j = 0; j < N; j++) {\n\t\t\t\tA[i][j] = sc.nextInt();\n\t\t\t}\n\t\t}\n\n\t\t// 初期解を生成\n\t\tRandom rnd = new Random();\n\t\tAnswer ans = new Answer(N, Q, A, rnd);\n\n\t\t// 山登り法:変数の設定(5.4 秒ループを回す)\n\t\tfinal double TIME_LIMIT = 5.4;\n\t\tlong ti = System.currentTimeMillis();\n\t\tint currentScore = ans.getScore();\n\n\t\t// 山登り法スタート\n\t\tint loops = 0;\n\t\twhile ((System.currentTimeMillis() - ti) / 1000.0 < TIME_LIMIT) {\n\t\t\t// 「小さな変更」をランダムに選ぶ\n\t\t\tint t = rnd.nextInt(Q);\n\t\t\tint h_limit = 14;\n\t\t\tif (currentScore >= 199900000) {\n\t\t\t\th_limit = 1;\n\t\t\t}\n\t\t\telse if (currentScore >= 199500000) {\n\t\t\t\th_limit = 7;\n\t\t\t}\n\t\t\tint old_x = ans.X[t], new_x = ans.X[t] + (rnd.nextInt(3) - 1); // X[t] を -1 から +1 まで増減\n\t\t\tint old_y = ans.Y[t], new_y = ans.Y[t] + (rnd.nextInt(3) - 1); // Y[t] を -1 から +1 まで増減\n\t\t\tint old_z = ans.H[t], new_z = ans.H[t] + (rnd.nextInt(h_limit * 2 + 1) - h_limit); // H[t] を -h_limit から +h_limit まで増減\n\t\t\tif (new_x < 0 || new_x >= N || new_y < 0 || new_y >= N || new_z <= 0 || new_z > N) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t// とりあえず変更し、スコアを評価する\n\t\t\tans.change(t, new_x, new_y, new_z);\n\t\t\tint newScore = ans.getScore();\n\t\t\t// スコアに応じて採用/不採用を決める\n\t\t\tdouble temperature = 180.0 - 179.0 * ((System.currentTimeMillis() - ti) / 1000.0 / TIME_LIMIT);\n\t\t\tdouble probability = Math.exp(Math.min((newScore - currentScore) / temperature, 0.0));\n\t\t\tif (rnd.nextDouble() < probability) {\n\t\t\t\tcurrentScore = newScore;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tans.change(t, old_x, old_y, old_z);\n\t\t\t}\n\t\t\tloops += 1;\n\t\t}\n\n\t\t// 出力\n\t\tSystem.out.println(Q);\n\t\tfor (int i = 0; i < Q; i++) {\n\t\t\tSystem.out.println(ans.X[i] + \" \" + ans.Y[i] + \" \" + ans.H[i]);\n\t\t}\n\t\tSystem.err.println(\"loops = \" + loops);\n\t\tSystem.err.println(\"score = \" + currentScore);\n\t}\n\n\t// 操作手順や盤面を記録した構造体 Answer\n\t// (ここでは書籍内のコードとは異なり、操作の番号は 1-indexed ではなく 0-indexed で実装しています(つまり番号が 0 から 999 になります))\n\tstatic class Answer {\n\t\tint N; // 盤面のサイズ\n\t\tint Q; // 操作の回数\n\t\tint[] X;\n\t\tint[] Y;\n\t\tint[] H;\n\t\tint[][] A;\n\t\tint[][] B;\n\t\t// 初期解を生成\n\t\tpublic Answer(int N, int Q, int[][] A, Random rnd) {\n\t\t\tthis.N = N;\n\t\t\tthis.Q = Q;\n\t\t\tthis.A = new int[N][N];\n\t\t\tfor (int i = 0; i < N; i++) {\n\t\t\t\tfor (int j = 0; j < N; j++) {\n\t\t\t\t\tthis.A[i][j] = A[i][j];\n\t\t\t\t}\n\t\t\t}\n\t\t\tX = new int[Q];\n\t\t\tY = new int[Q];\n\t\t\tH = new int[Q];\n\t\t\tB = new int[N][N];\n\t\t\tfor (int i = 0; i < Q; i++) {\n\t\t\t\tX[i] = rnd.nextInt(N); // 0 以上 N-1 以下のランダムな整数\n\t\t\t\tY[i] = rnd.nextInt(N); // 0 以上 N-1 以下のランダムな整数\n\t\t\t\tH[i] = 1;\n\t\t\t\tB[Y[i]][X[i]] += 1;\n\t\t\t}\n\t\t}\n\t\t// X[t] = x, Y[t] = y, H[t] = h に変更する関数\n\t\tvoid change(int t, int x, int y, int h) {\n\t\t\tfor (int i = 0; i < N; i++) {\n\t\t\t\tfor (int j = 0; j < N; j++) {\n\t\t\t\t\tB[i][j] -= Math.max(H[t] - Math.abs(Y[t] - i) - Math.abs(X[t] - j), 0);\n\t\t\t\t}\n\t\t\t}\n\t\t\tX[t] = x;\n\t\t\tY[t] = y;\n\t\t\tH[t] = h;\n\t\t\tfor (int i = 0; i < N; i++) {\n\t\t\t\tfor (int j = 0; j < N; j++) {\n\t\t\t\t\tB[i][j] += Math.max(H[t] - Math.abs(Y[t] - i) - Math.abs(X[t] - j), 0);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t// 現在のスコアを返す関数\n\t\tint getScore() {\n\t\t\tint sum = 0;\n\t\t\tfor (int i = 0; i < N; i++) {\n\t\t\t\tfor (int j = 0; j < N; j++) {\n\t\t\t\t\tsum += Math.abs(A[i][j] - B[i][j]);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn 200000000 - sum;\n\t\t}\n\t}\n}" | |
| }, | |
| { | |
| "id": 54, | |
| "name": "answer_A51", | |
| "Python": "from collections import deque\n\n# 入力\nQ = int(input()) # クエリ数\nqueries = [ input().split() for i in range(Q) ] # クエリの情報(各要素は [\"1\", 題名を表す文字列] or [\"2\"] or [\"3\"])\n\n# クエリの処理\nS = deque()\nfor q in queries:\n\tif q[0] == \"1\":\n\t\tS.append(q[1])\n\tif q[0] == \"2\":\n\t\tprint(S[-1])\n\tif q[0] == \"3\":\n\t\tS.pop()", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\t// 入力\n\t\tScanner sc = new Scanner(System.in);\n\t\tint Q = sc.nextInt();\n\t\tint[] queryType = new int[Q + 1];\n\t\tString[] x = new String[Q + 1];\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tqueryType[i] = sc.nextInt();\n\t\t\tif (queryType[i] == 1) {\n\t\t\t\tx[i] = sc.next();\n\t\t\t}\n\t\t}\n\n\t\t// クエリの処理\n\t\tStack<String> S = new Stack<>();\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tif (queryType[i] == 1) {\n\t\t\t\tS.push(x[i]);\n\t\t\t}\n\t\t\tif (queryType[i] == 2) {\n\t\t\t\tSystem.out.println(S.peek());\n\t\t\t}\n\t\t\tif (queryType[i] == 3) {\n\t\t\t\tS.pop();\n\t\t\t}\n\t\t}\n\t}\n}" | |
| }, | |
| { | |
| "id": 55, | |
| "name": "answer_A52", | |
| "Python": "from collections import deque\n\n# 入力\nQ = int(input()) # クエリ数\nqueries = [ input().split() for i in range(Q) ] # クエリの情報(各要素は [\"1\", 名前を表す文字列] or [\"2\"] or [\"3\"])\n\n# クエリの処理\nT = deque()\nfor q in queries:\n\tif q[0] == \"1\":\n\t\tT.append(q[1])\n\tif q[0] == \"2\":\n\t\tprint(T[0])\n\tif q[0] == \"3\":\n\t\tT.popleft()", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\t// 入力\n\t\tScanner sc = new Scanner(System.in);\n\t\tint Q = sc.nextInt();\n\t\tint[] queryType = new int[Q + 1];\n\t\tString[] x = new String[Q + 1];\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tqueryType[i] = sc.nextInt();\n\t\t\tif (queryType[i] == 1) {\n\t\t\t\tx[i] = sc.next();\n\t\t\t}\n\t\t}\n\n\t\t// クエリの処理\n\t\tQueue<String> T = new LinkedList<>();\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tif (queryType[i] == 1) {\n\t\t\t\tT.add(x[i]);\n\t\t\t}\n\t\t\tif (queryType[i] == 2) {\n\t\t\t\tSystem.out.println(T.peek());\n\t\t\t}\n\t\t\tif (queryType[i] == 3) {\n\t\t\t\tT.remove();\n\t\t\t}\n\t\t}\n\t}\n}" | |
| }, | |
| { | |
| "id": 56, | |
| "name": "answer_A53", | |
| "Python": "import heapq\n\n# 入力\nQ = int(input()) # クエリ数\nqueries = [ input().split() for i in range(Q) ] # クエリの情報(各要素は [\"1\", 値段を表す文字列] or [\"2\"] or [\"3\"])\n\n# クエリの処理\nT = []\nfor q in queries:\n\tif q[0] == \"1\":\n\t\theapq.heappush(T, int(q[1]))\n\tif q[0] == \"2\":\n\t\tprint(T[0]) # T[0] は「優先度付きキューの中の最小要素」になっている\n\tif q[0] == \"3\":\n\t\theapq.heappop(T)\n\n# 注意 1:Python の heapq モジュールは、deque モジュールとは異なり、関数を使って list を操作するという形式になっています。\n# 注意 2:優先度付きキューの最小要素は T[0] で取り出せますが、例えば小さいほうから 2 番目の要素が T[1] で取り出せるとは限らないことに注意してください。", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\t// 入力\n\t\tScanner sc = new Scanner(System.in);\n\t\tint Q = sc.nextInt();\n\t\tint[] queryType = new int[Q + 1];\n\t\tint[] x = new int[Q + 1];\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tqueryType[i] = sc.nextInt();\n\t\t\tif (queryType[i] == 1) {\n\t\t\t\tx[i] = sc.nextInt();\n\t\t\t}\n\t\t}\n\n\t\t// クエリの処理\n\t\tQueue<Integer> T = new PriorityQueue<>();\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tif (queryType[i] == 1) {\n\t\t\t\tT.add(x[i]);\n\t\t\t}\n\t\t\tif (queryType[i] == 2) {\n\t\t\t\tSystem.out.println(T.peek());\n\t\t\t}\n\t\t\tif (queryType[i] == 3) {\n\t\t\t\tT.remove();\n\t\t\t}\n\t\t}\n\t}\n}" | |
| }, | |
| { | |
| "id": 57, | |
| "name": "answer_A54", | |
| "Python": "# 入力\nQ = int(input()) # クエリ数\nqueries = [ input().split() for i in range(Q) ] # クエリの情報(各要素は [\"1\", 名前を表す文字列, 得点を表す文字列] or [\"2\", 名前を表す文字列])\n\n# クエリの処理\nMap = {}\nfor q in queries:\n\tif q[0] == \"1\":\n\t\tMap[q[1]] = q[2]\n\tif q[0] == \"2\":\n\t\tprint(Map[q[1]])", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\t// 入力\n\t\tScanner sc = new Scanner(System.in);\n\t\tint Q = sc.nextInt();\n\t\tint[] queryType = new int[Q + 1];\n\t\tString[] x = new String[Q + 1];\n\t\tint[] y = new int[Q + 1];\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tqueryType[i] = sc.nextInt();\n\t\t\tif (queryType[i] == 1) {\n\t\t\t\tx[i] = sc.next();\n\t\t\t\ty[i] = sc.nextInt();\n\t\t\t}\n\t\t\tif (queryType[i] == 2) {\n\t\t\t\tx[i] = sc.next();\n\t\t\t}\n\t\t}\n\n\t\t// クエリの処理\n\t\tMap<String, Integer> scoreMap = new HashMap<>();\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tif (queryType[i] == 1) {\n\t\t\t\tscoreMap.put(x[i], y[i]);\n\t\t\t}\n\t\t\tif (queryType[i] == 2) {\n\t\t\t\tSystem.out.println(scoreMap.get(x[i]));\n\t\t\t}\n\t\t}\n\t}\n}" | |
| }, | |
| { | |
| "id": 58, | |
| "name": "answer_A56", | |
| "Python": "# 入力\nN, Q = map(int, input().split())\nS = input()\nqueries = [ list(map(int, input().split())) for i in range(Q) ]\n\n# 文字を数値に変換(ここでは書籍とは異なり、0-indexed で実装しています)\n# ord(c) で文字 c の文字コード(ASCII コード)を取得\nT = list(map(lambda c: ord(c) - ord('a') + 1, S))\n\n# 100 の n 乗を前計算\nMOD = 2147483647\npower100 = [ None ] * (N + 1)\npower100[0] = 1\nfor i in range(N):\n\tpower100[i + 1] = power100[i] * 100 % MOD\n\n# H[1], H[2], ..., H[N] を計算する\nH = [ None ] * (N + 1)\nH[0] = 0\nfor i in range(N):\n\tH[i + 1] = (H[i] * 100 + T[i]) % MOD\n\n# ハッシュ値を求める関数\n# S[l-1:r] のハッシュ値は (H[r] - H[l - 1] * power100[r - l + 1]) % MOD で計算\n# C++ とは異なり、(負の値)% M (M >= 1) も 0 以上 M-1 以下になることに注意\ndef hash_value(l, r):\n\treturn (H[r] - H[l - 1] * power100[r - l + 1]) % MOD\n\n# クエリに答える\nfor a, b, c, d in queries:\n\thash1 = hash_value(a, b)\n\thash2 = hash_value(c, d)\n\tif hash1 == hash2:\n\t\tprint(\"Yes\")\n\telse:\n\t\tprint(\"No\")\n", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\t// 入力\n\t\tScanner sc = new Scanner(System.in);\n\t\tint N = sc.nextInt();\n\t\tint Q = sc.nextInt();\n\t\tString S = sc.next();\n\t\tint[] a = new int[Q + 1];\n\t\tint[] b = new int[Q + 1];\n\t\tint[] c = new int[Q + 1];\n\t\tint[] d = new int[Q + 1];\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\ta[i] = sc.nextInt();\n\t\t\tb[i] = sc.nextInt();\n\t\t\tc[i] = sc.nextInt();\n\t\t\td[i] = sc.nextInt();\n\t\t}\n\n\t\t// 文字列のハッシュの準備\n\t\tStringHash Z = new StringHash(S);\n\n\t\t// クエリに答える\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tlong hash1 = Z.hashValue(a[i], b[i]);\n\t\t\tlong hash2 = Z.hashValue(c[i], d[i]);\n\t\t\tif (hash1 == hash2) {\n\t\t\t\tSystem.out.println(\"Yes\");\n\t\t\t}\n\t\t\telse {\n\t\t\t\tSystem.out.println(\"No\");\n\t\t\t}\n\t\t}\n\t}\n\n\t// 文字列のハッシュを実装したクラス StringHash\n\tstatic class StringHash {\n\t\tstatic final int MOD = 2147483647;\n\t\tlong[] power100;\n\t\tlong[] h;\n\t\tStringHash(String S) {\n\t\t\tint N = S.length();\n\t\t\t// 文字列を数値に変換\n\t\t\tint[] T = new int[N + 1];\n\t\t\tfor (int i = 1; i <= N; i++) {\n\t\t\t\tT[i] = (int)(S.charAt(i - 1) - 'a') + 1;\n\t\t\t}\n\t\t\t// 100 の n 乗(本文中の B^0, B^1, ... に対応)を前計算する\n\t\t\tpower100 = new long[N + 1];\n\t\t\tpower100[0] = 1;\n\t\t\tfor (int i = 1; i <= N; i++) {\n\t\t\t\tpower100[i] = power100[i - 1] * 100 % MOD;\n\t\t\t}\n\t\t\t// H[1], H[2], ..., H[N] を前計算する\n\t\t\th = new long[N + 1];\n\t\t\th[0] = 1;\n\t\t\tfor (int i = 1; i <= N; i++) {\n\t\t\t\th[i] = (h[i - 1] * 100 + T[i]) % MOD;\n\t\t\t}\n\t\t}\n\t\t// S[l, r] のハッシュ値を返す関数\n\t\t// 余りの計算に注意!(5.3 節)\n\t\tlong hashValue(int l, int r) {\n\t\t\tlong val = h[r] - h[l - 1] * power100[r - l + 1] % MOD;\n\t\t\tif (val < 0) {\n\t\t\t\tval += MOD;\n\t\t\t}\n\t\t\treturn val;\n\t\t}\n\t}\n}" | |
| }, | |
| { | |
| "id": 59, | |
| "name": "answer_A57", | |
| "Python": "# 入力\nN, Q = map(int, input().split())\nA = list(map(int, input().split()))\nqueries = [ list(map(int, input().split())) for i in range(Q) ]\n\n# 前計算(ここでは書籍とは異なり、0-indexed で実装しています)\nLEVELS = 30\ndp = [ [ None ] * N for i in range(LEVELS) ]\nfor i in range(0, N):\n\tdp[0][i] = A[i] - 1\nfor d in range(1, LEVELS):\n\tfor i in range(0, N):\n\t\tdp[d][i] = dp[d - 1][dp[d - 1][i]]\n\n# クエリの処理(ここでは書籍とは異なり、0-indexed で実装しています)\nfor X, Y in queries:\n\tcurrent_place = X - 1\n\tfor d in range(29, -1, -1):\n\t\tif ((Y >> d) & 1) == 1:\n\t\t\tcurrent_place = dp[d][current_place]\n\tprint(current_place + 1) # current_place は 0-indexed で計算したので、1 を足して出力する\n\n# 注意 1:書籍の C++ プログラムにおいて「Y の 2^d の位を取り出す」は C++ では (Y / (1 << d)) % 2 と実装されていました。\n# Python でも 19 行目は (Y // (2 ** d)) % 2 と実装できますが、((Y >> d) & 1) と計算すると、定数倍の面でより高速です。\n# 注意 2:このプログラムの平均的な実行時間は 2 秒にギリギリ入るくらいであり、提出によっては TLE となる場合があります。\n# 同じプログラムを PyPy3 で提出すると、0.5 秒程度の実行時間で AC することができます。", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\t// 入力\n\t\tScanner sc = new Scanner(System.in);\n\t\tint N = sc.nextInt();\n\t\tint Q = sc.nextInt();\n\t\tint[] A = new int[N + 1];\n\t\tint[] X = new int[Q + 1];\n\t\tint[] Y = new int[Q + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tA[i] = sc.nextInt();\n\t\t}\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tX[i] = sc.nextInt();\n\t\t\tY[i] = sc.nextInt();\n\t\t}\n\n\t\t// 前計算\n\t\tfinal int LEVELS = 30;\n\t\tint[][] dp = new int[LEVELS][N + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tdp[0][i] = A[i];\n\t\t}\n\t\tfor (int d = 1; d <= 29; d++) {\n\t\t\tfor (int i = 1; i <= N; i++) {\n\t\t\t\tdp[d][i] = dp[d - 1][dp[d - 1][i]];\n\t\t\t}\n\t\t}\n\n\t\t// クエリの処理\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tint currentPlace = X[i];\n\t\t\tfor (int d = 29; d >= 0; d--) {\n\t\t\t\tif ((Y[i] / (1 << d)) % 2 == 1) {\n\t\t\t\t\tcurrentPlace = dp[d][currentPlace];\n\t\t\t\t}\n\t\t\t}\n\t\t\tSystem.out.println(currentPlace);\n\t\t}\n\t}\n}" | |
| }, | |
| { | |
| "id": 60, | |
| "name": "answer_A58", | |
| "Python": "# セグメント木(ここでは書籍とは異なり、pos が 0-indexed になるように実装しています)\nclass segtree:\n\t# 要素 dat の初期化を行う(最初は全部ゼロ)\n\tdef __init__(self, n):\n\t\tself.size = 1\n\t\twhile self.size < n:\n\t\t\tself.size *= 2\n\t\tself.dat = [ 0 ] * (self.size * 2)\n\t\n\t# クエリ 1 に対する処理\n\tdef update(self, pos, x):\n\t\tpos += self.size # pos は 0-indexed なので、A[pos] のみに対応するセルの番号は pos + size\n\t\tself.dat[pos] = x\n\t\twhile pos >= 2:\n\t\t\tpos //= 2\n\t\t\tself.dat[pos] = max(self.dat[pos * 2], self.dat[pos * 2 + 1])\n\t\n\t# クエリ 2 に対する処理\n\t# u は現在のセル番号、[a, b) はセルに対応する半開区間、[l, r) は求めたい半開区間\n\tdef query(self, l, r, a, b, u):\n\t\tif r <= a or b <= l:\n\t\t\treturn -1000000000 # 一切含まれない場合\n\t\tif l <= a and b <= r:\n\t\t\treturn self.dat[u] # 完全に含まれる場合\n\t\tm = (a + b) // 2\n\t\tanswerl = self.query(l, r, a, m, u * 2)\n\t\tanswerr = self.query(l, r, m, b, u * 2 + 1)\n\t\treturn max(answerl, answerr)\n\n# 入力\nN, Q = map(int, input().split())\nqueries = [ list(map(int, input().split())) for i in range(Q) ]\n\n# クエリの処理\nZ = segtree(N)\nfor q in queries:\n\ttp, *cont = q\n\tif tp == 1:\n\t\tpos, x = cont\n\t\tZ.update(pos - 1, x) # pos は 1-indexed で入力されるので、update 関数の引数は pos - 1 にします\n\tif tp == 2:\n\t\tl, r = cont\n\t\tanswer = Z.query(l - 1, r - 1, 0, Z.size, 1) # 0-indexed の実装では、最初のセルに対応する半開区間は [0, size) です\n\t\tprint(answer)", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\t// 入力\n\t\tScanner sc = new Scanner(System.in);\n\t\tint N = sc.nextInt();\n\t\tint Q = sc.nextInt();\n\t\tint[] queryType = new int[Q + 1];\n\t\tint[] pos = new int[Q + 1];\n\t\tint[] x = new int[Q + 1];\n\t\tint[] l = new int[Q + 1];\n\t\tint[] r = new int[Q + 1];\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tqueryType[i] = sc.nextInt();\n\t\t\tif (queryType[i] == 1) {\n\t\t\t\tpos[i] = sc.nextInt();\n\t\t\t\tx[i] = sc.nextInt();\n\t\t\t}\n\t\t\tif (queryType[i] == 2) {\n\t\t\t\tl[i] = sc.nextInt();\n\t\t\t\tr[i] = sc.nextInt();\n\t\t\t}\n\t\t}\n\n\t\t// クエリの処理\n\t\tSegmentTree Z = new SegmentTree(N);\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tif (queryType[i] == 1) {\n\t\t\t\tZ.update(pos[i], x[i]);\n\t\t\t}\n\t\t\tif (queryType[i] == 2) {\n\t\t\t\t// 最初のセルに対応する半開区間は [1, size + 1)\n\t\t\t\tSystem.out.println(Z.query(l[i], r[i], 1, Z.size + 1, 1));\n\t\t\t}\n\t\t}\n\t}\n\n\t// セグメント木を実装したクラス SegmentTree\n\tstatic class SegmentTree {\n\t\tint size;\n\t\tint[] dat;\n\t\t// 要素 dat の初期化を行う(最初は全部ゼロ)\n\t\tSegmentTree(int n) {\n\t\t\tsize = 1;\n\t\t\twhile (size < n) {\n\t\t\t\tsize *= 2;\n\t\t\t}\n\t\t\tdat = new int[size * 2]; // Java では new で初期化した配列の要素は 0 になることに注意\n\t\t}\n\t\t// クエリ 1 に対する処理\n\t\tvoid update(int pos, int x) {\n\t\t\tpos += size - 1;\n\t\t\tdat[pos] = x;\n\t\t\twhile (pos >= 2) {\n\t\t\t\tpos /= 2;\n\t\t\t\tdat[pos] = Math.max(dat[pos * 2], dat[pos * 2 + 1]);\n\t\t\t}\n\t\t}\n\t\t// クエリ 2 に対する処理\n\t\t// u は現在のセル番号、[a, b) はセルに対応する半開区間、[l, r) は求めたい半開区間\n\t\tint query(int l, int r, int a, int b, int u) {\n\t\t\tif (r <= a || b <= l) {\n\t\t\t\treturn -1000000000; // 一切含まれない場合\n\t\t\t}\n\t\t\tif (l <= a && b <= r) {\n\t\t\t\treturn dat[u]; // 完全に含まれる場合\n\t\t\t}\n\t\t\tint m = (a + b) / 2;\n\t\t\tint answerL = query(l, r, a, m, u * 2);\n\t\t\tint answerR = query(l, r, m, b, u * 2 + 1);\n\t\t\treturn Math.max(answerL, answerR);\n\t\t}\n\t}\n}" | |
| }, | |
| { | |
| "id": 61, | |
| "name": "answer_A59", | |
| "Python": "# セグメント木(ここでは書籍とは異なり、pos が 0-indexed になるように実装しています)\nclass segtree:\n\t# 要素 dat の初期化を行う(最初は全部ゼロ)\n\tdef __init__(self, n):\n\t\tself.size = 1\n\t\twhile self.size < n:\n\t\t\tself.size *= 2\n\t\tself.dat = [ 0 ] * (self.size * 2)\n\t\n\t# クエリ 1 に対する処理\n\tdef update(self, pos, x):\n\t\tpos += self.size # pos は 0-indexed なので、A[pos] のみに対応するセルの番号は pos + size\n\t\tself.dat[pos] = x\n\t\twhile pos >= 2:\n\t\t\tpos //= 2\n\t\t\tself.dat[pos] = self.dat[pos * 2] + self.dat[pos * 2 + 1] # 8.8 節から変更した部分\n\t\n\t# クエリ 2 に対する処理\n\t# u は現在のセル番号、[a, b) はセルに対応する半開区間、[l, r) は求めたい半開区間\n\tdef query(self, l, r, a, b, u):\n\t\tif r <= a or b <= l:\n\t\t\treturn 0 # 8.8 節から変更した部分\n\t\tif l <= a and b <= r:\n\t\t\treturn self.dat[u]\n\t\tm = (a + b) // 2\n\t\tanswerl = self.query(l, r, a, m, u * 2)\n\t\tanswerr = self.query(l, r, m, b, u * 2 + 1)\n\t\treturn answerl + answerr # 8.8 節から変更した部分\n\n# 入力\nN, Q = map(int, input().split())\nqueries = [ list(map(int, input().split())) for i in range(Q) ]\n\n# クエリの処理\nZ = segtree(N)\nfor q in queries:\n\ttp, *cont = q\n\tif tp == 1:\n\t\tpos, x = cont\n\t\tZ.update(pos - 1, x) # pos は 1-indexed で入力されるので、update 関数の引数は pos - 1 にします\n\tif tp == 2:\n\t\tl, r = cont\n\t\tanswer = Z.query(l - 1, r - 1, 0, Z.size, 1) # 0-indexed の実装では、最初のセルに対応する半開区間は [0, size) です\n\t\tprint(answer)", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\t// 入力\n\t\tScanner sc = new Scanner(System.in);\n\t\tint N = sc.nextInt();\n\t\tint Q = sc.nextInt();\n\t\tint[] queryType = new int[Q + 1];\n\t\tint[] pos = new int[Q + 1];\n\t\tint[] x = new int[Q + 1];\n\t\tint[] l = new int[Q + 1];\n\t\tint[] r = new int[Q + 1];\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tqueryType[i] = sc.nextInt();\n\t\t\tif (queryType[i] == 1) {\n\t\t\t\tpos[i] = sc.nextInt();\n\t\t\t\tx[i] = sc.nextInt();\n\t\t\t}\n\t\t\tif (queryType[i] == 2) {\n\t\t\t\tl[i] = sc.nextInt();\n\t\t\t\tr[i] = sc.nextInt();\n\t\t\t}\n\t\t}\n\n\t\t// クエリの処理\n\t\tSegmentTree Z = new SegmentTree(N);\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tif (queryType[i] == 1) {\n\t\t\t\tZ.update(pos[i], x[i]);\n\t\t\t}\n\t\t\tif (queryType[i] == 2) {\n\t\t\t\t// 最初のセルに対応する半開区間は [1, size + 1)\n\t\t\t\tSystem.out.println(Z.query(l[i], r[i], 1, Z.size + 1, 1));\n\t\t\t}\n\t\t}\n\t}\n\n\t// セグメント木を実装したクラス SegmentTree\n\tstatic class SegmentTree {\n\t\tint size;\n\t\tint[] dat;\n\t\t// 要素 dat の初期化を行う(最初は全部ゼロ)\n\t\tSegmentTree(int n) {\n\t\t\tsize = 1;\n\t\t\twhile (size < n) {\n\t\t\t\tsize *= 2;\n\t\t\t}\n\t\t\tdat = new int[size * 2]; // Java では new で初期化した配列の要素は 0 になることに注意\n\t\t}\n\t\t// クエリ 1 に対する処理\n\t\tvoid update(int pos, int x) {\n\t\t\tpos += size - 1;\n\t\t\tdat[pos] = x;\n\t\t\twhile (pos >= 2) {\n\t\t\t\tpos /= 2;\n\t\t\t\tdat[pos] = dat[pos * 2] + dat[pos * 2 + 1]; // 8.8 節から変更した部分\n\t\t\t}\n\t\t}\n\t\t// クエリ 2 に対する処理\n\t\t// u は現在のセル番号、[a, b) はセルに対応する半開区間、[l, r) は求めたい半開区間\n\t\tint query(int l, int r, int a, int b, int u) {\n\t\t\tif (r <= a || b <= l) {\n\t\t\t\treturn 0; // 8.8 節から変更した部分\n\t\t\t}\n\t\t\tif (l <= a && b <= r) {\n\t\t\t\treturn dat[u];\n\t\t\t}\n\t\t\tint m = (a + b) / 2;\n\t\t\tint answerL = query(l, r, a, m, u * 2);\n\t\t\tint answerR = query(l, r, m, b, u * 2 + 1);\n\t\t\treturn answerL + answerR; // 8.8 節から変更した部分\n\t\t}\n\t}\n}" | |
| }, | |
| { | |
| "id": 62, | |
| "name": "answer_A60", | |
| "Python": "from collections import deque\n\n# 入力\nN = int(input())\nA = list(map(int, input().split()))\n\n# スタックの変化の再現\n# (スタックには (日付, 株価) のタプルを記録する)\nanswer = [ None ] * N\nlevel2 = deque()\nfor i in range(N):\n\tif i >= 1:\n\t\tlevel2.append((i, A[i - 1]))\n\t\twhile len(level2) >= 1:\n\t\t\tkabuka = level2[-1][1] # 株価はタプルの 2 番目の要素\n\t\t\tif kabuka <= A[i]:\n\t\t\t\tlevel2.pop()\n\t\t\telse:\n\t\t\t\tbreak\n\tif len(level2) >= 1:\n\t\tanswer[i] = level2[-1][0] # 日付はタプルの 1 番目の要素\n\telse:\n\t\tanswer[i] = -1\n\n# answer を空白区切りで出力\nprint(*answer)", | |
| "Java": "import java.util.*;\nimport java.io.*;\n\nclass Main {\n\tpublic static void main(String[] args) throws IOException {\n\t\t// 入力(高速な入力のため、Scanner の代わりに BufferedReader を使っています)\n\t\tBufferedReader buff = new BufferedReader(new InputStreamReader(System.in));\n\t\tStringTokenizer st;\n\t\tst = new StringTokenizer(buff.readLine());\n\t\tint N = Integer.parseInt(st.nextToken());\n\t\tint[] A = new int[N + 1];\n\t\tst = new StringTokenizer(buff.readLine());\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tA[i] = Integer.parseInt(st.nextToken());\n\t\t}\n\n\t\t// スタックの変化の再現\n\t\tint[] answer = new int[N + 1];\n\t\tStack<PairInt> level2 = new Stack<>();\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tif (i >= 2) {\n\t\t\t\tlevel2.push(new PairInt(i - 1, A[i - 1]));\n\t\t\t\twhile (!level2.empty()) {\n\t\t\t\t\tint kabuka = level2.peek().second;\n\t\t\t\t\tif (kabuka <= A[i]) {\n\t\t\t\t\t\tlevel2.pop();\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\t// 起算日の特定\n\t\t\tif (!level2.empty()) {\n\t\t\t\tanswer[i] = level2.peek().first;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tanswer[i] = -1;\n\t\t\t}\n\t\t}\n\n\t\t// 出力(高速な出力のため、System.out.println ではなく PrintWriter を使っています)\n\t\tPrintWriter output = new PrintWriter(System.out);\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tif (i >= 2) {\n\t\t\t\toutput.print(\" \");\n\t\t\t}\n\t\t\toutput.print(answer[i]);\n\t\t}\n\t\toutput.println();\n\t\toutput.flush();\n\t}\n\t\n\t// int 型のペアのクラス PairInt\n\tstatic class PairInt {\n\t\tint first, second;\n\t\tpublic PairInt(int first, int second) {\n\t\t\tthis.first = first;\n\t\t\tthis.second = second;\n\t\t}\n\t}\n}" | |
| }, | |
| { | |
| "id": 63, | |
| "name": "answer_A61", | |
| "Python": "# 入力\nN, M = map(int, input().split())\nedges = [ list(map(int, input().split())) for i in range(M) ]\n\n# 隣接リストの作成\nG = [ list() for i in range(N + 1) ] # G[i] は頂点 i に隣接する頂点のリスト\nfor a, b in edges:\n\tG[a].append(b) # 頂点 a に隣接する頂点として b を追加\n\tG[b].append(a) # 頂点 b に隣接する頂点として a を追加\n\n# 出力\nfor i in range(1, N + 1):\n\toutput = ''\n\toutput += str(i)\n\toutput += ': {'\n\toutput += ', '.join(map(str, G[i])) # 例えば G[i] = { 2, 7, 5 } のとき、ここで \"2, 7, 5\" という文字列が output の後ろに連結される\n\toutput += '}'\n\tprint(output)", | |
| "Java": "import java.util.*;\nimport java.io.*;\n\nclass Main {\n\tpublic static void main(String[] args) throws IOException {\n\t\t// 入力(高速な入力のため、Scanner の代わりに BufferedReader を使っています)\n\t\tBufferedReader buff = new BufferedReader(new InputStreamReader(System.in));\n\t\tStringTokenizer st;\n\t\tst = new StringTokenizer(buff.readLine());\n\t\tint N = Integer.parseInt(st.nextToken());\n\t\tint M = Integer.parseInt(st.nextToken());\n\t\tint[] A = new int[M + 1];\n\t\tint[] B = new int[M + 1];\n\t\tfor (int i = 1; i <= M; i++) {\n\t\t\tst = new StringTokenizer(buff.readLine());\n\t\t\tA[i] = Integer.parseInt(st.nextToken());\n\t\t\tB[i] = Integer.parseInt(st.nextToken());\n\t\t}\n\t\t\n\t\t// 隣接リストの作成\n\t\tArrayList<Integer>[] G = new ArrayList[N + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tG[i] = new ArrayList<Integer>(); // G[i] は頂点 i に隣接する頂点のリスト\n\t\t}\n\t\tfor (int i = 1; i <= M; i++) {\n\t\t\tG[A[i]].add(B[i]); // 頂点 A[i] に隣接する頂点として B[i] を追加\n\t\t\tG[B[i]].add(A[i]); // 頂点 B[i] に隣接する頂点として A[i] を追加\n\t\t}\n\t\t\n\t\t// 出力(G[i].size() は頂点 i に隣接する頂点のリストの大きさ = 次数)\n\t\t//(高速な出力のため、System.out.println ではなく PrintWriter を使っています)\n\t\tPrintWriter output = new PrintWriter(System.out);\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\toutput.print(i + \": {\");\n\t\t\tfor (int j = 0; j < G[i].size(); j++) {\n\t\t\t\tif (j >= 1) {\n\t\t\t\t\toutput.print(\", \");\n\t\t\t\t}\n\t\t\t\toutput.print(G[i].get(j)); // G[i].get(j) は頂点 i に隣接する頂点のうち j+1 番目のもの\n\t\t\t}\n\t\t\toutput.println(\"}\");\n\t\t}\n\t\toutput.flush();\n\t}\n}" | |
| }, | |
| { | |
| "id": 64, | |
| "name": "answer_A62", | |
| "Python": "import sys\n\n# 再帰呼び出しの深さの上限を 120000 に設定\nsys.setrecursionlimit(120000)\n\n# 深さ優先探索を行う関数(pos は現在位置、visited[x] は頂点 x が青色かどうかを表す真偽値)\ndef dfs(pos, G, visited):\n\tvisited[pos] = True\n\tfor i in G[pos]:\n\t\tif visited[i] == False:\n\t\t\tdfs(i, G, visited)\n\n# 入力\nN, M = map(int, input().split())\nedges = [ list(map(int, input().split())) for i in range(M) ]\n\n# 隣接リストの作成\nG = [ list() for i in range(N + 1) ] # G[i] は頂点 i に隣接する頂点のリスト\nfor a, b in edges:\n\tG[a].append(b) # 頂点 a に隣接する頂点として b を追加\n\tG[b].append(a) # 頂点 b に隣接する頂点として a を追加\n\n# 深さ優先探索\nvisited = [ False ] * (N + 1)\ndfs(1, G, visited)\n\n# 連結かどうかの判定(answer = True のとき連結)\nanswer = True\nfor i in range(1, N + 1):\n\tif visited[i] == False:\n\t\tanswer = False\n\n# 答えの出力\nif answer == True:\n\tprint(\"The graph is connected.\")\nelse:\n\tprint(\"The graph is not connected.\")", | |
| "Java": "import java.util.*;\nimport java.io.*;\n\nclass Main {\n\tpublic static void main(String[] args) throws IOException {\n\t\t// 入力(高速な入出力のため、Scanner の代わりに BufferedReader を使っています)\n\t\tBufferedReader buff = new BufferedReader(new InputStreamReader(System.in));\n\t\tStringTokenizer st;\n\t\tst = new StringTokenizer(buff.readLine());\n\t\tint N = Integer.parseInt(st.nextToken());\n\t\tint M = Integer.parseInt(st.nextToken());\n\t\tint[] A = new int[M + 1];\n\t\tint[] B = new int[M + 1];\n\t\tfor (int i = 1; i <= M; i++) {\n\t\t\tst = new StringTokenizer(buff.readLine());\n\t\t\tA[i] = Integer.parseInt(st.nextToken());\n\t\t\tB[i] = Integer.parseInt(st.nextToken());\n\t\t}\n\t\t\n\t\t// 隣接リストの作成\n\t\tG = new ArrayList[N + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tG[i] = new ArrayList<Integer>();\n\t\t}\n\t\tfor (int i = 1; i <= M; i++) {\n\t\t\tG[A[i]].add(B[i]);\n\t\t\tG[B[i]].add(A[i]);\n\t\t}\n\t\t\n\t\t// 深さ優先探索\n\t\tvisited = new boolean[N + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tvisited[i] = false;\n\t\t}\n\t\tdfs(1);\n\n\t\t// 連結かどうかの判定(answer = true のとき連結)\n\t\tboolean answer = true;\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tif (visited[i] == false) {\n\t\t\t\tanswer = false;\n\t\t\t}\n\t\t}\n\n\t\t// 答えの出力\n\t\tif (answer == true) {\n\t\t\tSystem.out.println(\"The graph is connected.\");\n\t\t}\n\t\telse {\n\t\t\tSystem.out.println(\"The graph is not connected.\");\n\t\t}\n\t}\n\t\n\tstatic boolean[] visited; // 頂点 x が青色の場合、visited[x] = true\n\tstatic ArrayList<Integer>[] G;\n\tstatic void dfs(int pos) { // pos は現在位置\n\t\tvisited[pos] = true;\n\t\tfor (int i : G[pos]) {\n\t\t\tif (visited[i] == false) {\n\t\t\t\tdfs(i);\n\t\t\t}\n\t\t}\n\t}\n}" | |
| }, | |
| { | |
| "id": 65, | |
| "name": "answer_A63", | |
| "Python": "from collections import deque\n\n# 入力\nN, M = map(int, input().split())\nedges = [ list(map(int, input().split())) for i in range(M) ]\n\n# 隣接リストの作成\nG = [ list() for i in range(N + 1) ]\nfor a, b in edges:\n\tG[a].append(b)\n\tG[b].append(a)\n\n# 幅優先探索の初期化(dist[i] = ? ではなく dist[i] = -1 で初期化していることに注意)\ndist = [ -1 ] * (N + 1)\ndist[1] = 0\nQ = deque()\nQ.append(1)\n\n# 幅優先探索\nwhile len(Q) >= 1:\n\tpos = Q.popleft() # キュー Q の先頭要素を取り除き、その値を pos に代入する\n\tfor nex in G[pos]:\n\t\tif dist[nex] == -1:\n\t\t\tdist[nex] = dist[pos] + 1\n\t\t\tQ.append(nex)\n\n# 頂点 1 から各頂点までの最短距離を出力\nfor i in range(1, N + 1):\n\tprint(dist[i])", | |
| "Java": "import java.util.*;\nimport java.io.*;\n\nclass Main {\n\tpublic static void main(String[] args) throws IOException {\n\t\t// 入力(高速な入出力のため、Scanner の代わりに BufferedReader を使っています)\n\t\tBufferedReader buff = new BufferedReader(new InputStreamReader(System.in));\n\t\tStringTokenizer st;\n\t\tst = new StringTokenizer(buff.readLine());\n\t\tint N = Integer.parseInt(st.nextToken());\n\t\tint M = Integer.parseInt(st.nextToken());\n\t\tint[] A = new int[M + 1];\n\t\tint[] B = new int[M + 1];\n\t\tfor (int i = 1; i <= M; i++) {\n\t\t\tst = new StringTokenizer(buff.readLine());\n\t\t\tA[i] = Integer.parseInt(st.nextToken());\n\t\t\tB[i] = Integer.parseInt(st.nextToken());\n\t\t}\n\t\t\n\t\t// 隣接リストの作成\n\t\tArrayList<Integer>[] G = new ArrayList[N + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tG[i] = new ArrayList<Integer>();\n\t\t}\n\t\tfor (int i = 1; i <= M; i++) {\n\t\t\tG[A[i]].add(B[i]);\n\t\t\tG[B[i]].add(A[i]);\n\t\t}\n\t\t\n\t\t// 幅優先探索の初期化(dist[i]=? ではなく dist[i]=-1 で初期化していることに注意)\n\t\tint[] dist = new int[N + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tdist[i] = -1;\n\t\t}\n\t\tdist[1] = 0;\n\t\tQueue<Integer> Q = new LinkedList<>(); // キュー Q を定義する\n\t\tQ.add(1);\n\n\t\t// 幅優先探索\n\t\twhile (Q.size() >= 1) {\n\t\t\tint pos = Q.remove(); // キュー Q の先頭要素を取り除き、その値を pos に代入する\n\t\t\tfor (int i = 0; i < G[pos].size(); i++) {\n\t\t\t\tint nex = G[pos].get(i);\n\t\t\t\tif (dist[nex] == -1) {\n\t\t\t\t\tdist[nex] = dist[pos] + 1;\n\t\t\t\t\tQ.add(nex);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\t// 頂点 1 から各頂点までの最短距離を出力(高速な出力のため、System.out.println ではなく PrintWriter を使っています)\n\t\tPrintWriter output = new PrintWriter(System.out);\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\toutput.println(dist[i]);\n\t\t}\n\t\toutput.flush();\n\t}\n}" | |
| }, | |
| { | |
| "id": 66, | |
| "name": "answer_A64", | |
| "Python": "import heapq\n\n# 入力\nN, M = map(int, input().split())\nedges = [ list(map(int, input().split())) for i in range(M) ]\n\n# 隣接リストの作成(重み付きグラフなので、各辺について (隣接頂点, 重み) のタプルを記録する)\nG = [ list() for i in range(N + 1) ]\nfor a, b, c in edges:\n\tG[a].append((b, c))\n\tG[b].append((a, c))\n\n# 配列・キューの初期化(キューには (距離, 頂点番号) のタプルを記録する)\nINF = 10 ** 10\nkakutei = [ False ] * (N + 1)\ncur = [ INF ] * (N + 1)\ncur[1] = 0\nQ = []\nheapq.heappush(Q, (cur[1], 1))\n\n# ダイクストラ法\nwhile len(Q) >= 1:\n\t# 次に確定させるべき頂点を求める\n\t# (ここでは、優先度付きキュー Q の最小要素を取り除き、その要素の 2 番目の値(頂点番号)を pos に代入する)\n\tpos = heapq.heappop(Q)[1]\n\n\t# Q の最小要素が「既に確定した頂点」の場合\n\tif kakutei[pos] == True:\n\t\tcontinue\n\t\n\t# cur[x] の値を更新する\n\tkakutei[pos] = True\n\tfor e in G[pos]:\n\t\t# 書籍内のコードとは pos = e[0], cost = e[1] で対応している\n\t\tif cur[e[0]] > cur[pos] + e[1]:\n\t\t\tcur[e[0]] = cur[pos] + e[1]\n\t\t\theapq.heappush(Q, (cur[e[0]], e[0]))\n\n# 答えを出力\nfor i in range(1, N + 1):\n\tif cur[i] != INF:\n\t\tprint(cur[i])\n\telse:\n\t\tprint(\"-1\")", | |
| "Java": "import java.util.*;\nimport java.io.*;\n\nclass Main {\n\tpublic static void main(String[] args) throws IOException {\n\t\t// 入力(高速な入出力のため、Scanner の代わりに BufferedReader を使っています)\n\t\tBufferedReader buff = new BufferedReader(new InputStreamReader(System.in));\n\t\tStringTokenizer st;\n\t\tst = new StringTokenizer(buff.readLine());\n\t\tint N = Integer.parseInt(st.nextToken());\n\t\tint M = Integer.parseInt(st.nextToken());\n\t\tint[] A = new int[M + 1];\n\t\tint[] B = new int[M + 1];\n\t\tint[] C = new int[M + 1];\n\t\tfor (int i = 1; i <= M; i++) {\n\t\t\tst = new StringTokenizer(buff.readLine());\n\t\t\tA[i] = Integer.parseInt(st.nextToken());\n\t\t\tB[i] = Integer.parseInt(st.nextToken());\n\t\t\tC[i] = Integer.parseInt(st.nextToken());\n\t\t}\n\t\t\n\t\t// 隣接リストの作成\n\t\tArrayList<Edge>[] G = new ArrayList[N + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tG[i] = new ArrayList<Edge>();\n\t\t}\n\t\tfor (int i = 1; i <= M; i++) {\n\t\t\tG[A[i]].add(new Edge(B[i], C[i]));\n\t\t\tG[B[i]].add(new Edge(A[i], C[i]));\n\t\t}\n\t\t\n\t\t// 配列の初期化\n\t\tfinal int INF = 2000000000;\n\t\tboolean[] kakutei = new boolean[N + 1]; // Java では new で初期化した配列の要素は false になることに注意\n\t\tint[] cur = new int[N + 1];\n\t\tArrays.fill(cur, INF);\n\n\t\t// スタート地点をキューに追加\n\t\tcur[1] = 0;\n\t\tQueue<State> Q = new PriorityQueue<>();\n\t\tQ.add(new State(cur[1], 1));\n\n\t\t// ダイクストラ法\n\t\twhile (Q.size() >= 1) {\n\t\t\t// 次に確定させるべき頂点を求める\n\t\t\t// (ここでは、優先度付きキュー Q の最小要素を取り出し、これを Q から削除する)\n\t\t\tint pos = Q.remove().pos;\n\n\t\t\t// Q の最小要素が「既に確定した頂点」の場合\n\t\t\tif (kakutei[pos]) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// cur[x] の値を更新する\n\t\t\tkakutei[pos] = true;\n\t\t\tfor (int i = 0; i < G[pos].size(); i++) {\n\t\t\t\tEdge e = G[pos].get(i);\n\t\t\t\tif (cur[e.to] > cur[pos] + e.cost) {\n\t\t\t\t\tcur[e.to] = cur[pos] + e.cost;\n\t\t\t\t\tQ.add(new State(cur[e.to], e.to));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\t// 答えを出力(高速な出力のため、System.out.println ではなく PrintWriter を使っています)\n\t\tPrintWriter output = new PrintWriter(System.out);\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tif (cur[i] != INF) {\n\t\t\t\toutput.println(cur[i]);\n\t\t\t}\n\t\t\telse {\n\t\t\t\toutput.println(\"-1\");\n\t\t\t}\n\t\t}\n\t\toutput.flush();\n\t}\n\n\t// 重み付きグラフの辺のクラス Edge\n\tstatic class Edge {\n\t\tint to, cost; // 行き先 to、長さ cost\n\t\tpublic Edge(int to, int cost) {\n\t\t\tthis.to = to;\n\t\t\tthis.cost = cost;\n\t\t}\n\t}\n\n\t// ダイクストラ法の (cur[x], x) を管理するクラス(cur[x] = dist, x = pos に対応)\n\tstatic class State implements Comparable<State> {\n\t\tint dist, pos;\n\t\tpublic State(int dist, int pos) {\n\t\t\tthis.dist = dist;\n\t\t\tthis.pos = pos;\n\t\t}\n\t\t@Override public int compareTo(State s) {\n\t\t\t// State 型同士の比較をする関数\n\t\t\tif (this.dist < s.dist) {\n\t\t\t\treturn -1;\n\t\t\t}\n\t\t\tif (this.dist > s.dist) {\n\t\t\t\treturn 1;\n\t\t\t}\n\t\t\treturn 0;\n\t\t}\n\t}\n}\n" | |
| }, | |
| { | |
| "id": 67, | |
| "name": "answer_A65", | |
| "Python": "# 入力(A[2], ..., A[N] が入力される値になるようにする)\nN = int(input())\nA = [ 0 ] * 2 + list(map(int, input().split()))\n\n# 隣接リストの作成\nG = [ list() for i in range(N + 1) ]\nfor i in range(2, N + 1):\n\tG[A[i]].append(i) # 上司 → 部下の方向に方向に辺を追加\n\n# 動的計画法(dp[x] は社員 x の部下の数)\ndp = [ 0 ] * (N + 1)\nfor i in range(N, 0, -1):\n\tfor j in G[i]:\n\t\tdp[i] += (dp[j] + 1)\n\n# 答え (dp[1], dp[2], ..., dp[N]) を空白区切りで出力\nprint(*dp[1:])", | |
| "Java": "import java.util.*;\nimport java.io.*;\n\nclass Main {\n\tpublic static void main(String[] args) throws IOException {\n\t\t// 入力(高速な入出力のため、Scanner の代わりに BufferedReader を使っています)\n\t\tBufferedReader buff = new BufferedReader(new InputStreamReader(System.in));\n\t\tStringTokenizer st;\n\t\tst = new StringTokenizer(buff.readLine());\n\t\tint N = Integer.parseInt(st.nextToken());\n\t\tint[] A = new int[N + 1];\n\t\tst = new StringTokenizer(buff.readLine());\n\t\tfor (int i = 2; i <= N; i++) {\n\t\t\tA[i] = Integer.parseInt(st.nextToken());\n\t\t}\n\t\t\n\t\t// 隣接リストの作成\n\t\tArrayList<Integer>[] G = new ArrayList[N + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tG[i] = new ArrayList<Integer>();\n\t\t}\n\t\tfor (int i = 2; i <= N; i++) {\n\t\t\tG[A[i]].add(i); // 「上司→部下」の方向に辺を追加\n\t\t}\n\n\t\t// 動的計画法(dp[x] は社員 x の部下の数)\n\t\tint[] dp = new int[N + 1]; // Java では new で初期化した配列の要素は 0 になることに注意\n\t\tfor (int i = N; i >= 1; i--) {\n\t\t\tfor (int j = 0; j < G[i].size(); j++) {\n\t\t\t\tdp[i] += (dp[G[i].get(j)] + 1);\n\t\t\t}\n\t\t}\n\t\t\n\t\t// 答えを空白区切りで出力(高速な出力のため、System.out.println ではなく PrintWriter を使っています)\n\t\tPrintWriter output = new PrintWriter(System.out);\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tif (i >= 2) {\n\t\t\t\toutput.print(\" \");\n\t\t\t}\n\t\t\toutput.print(dp[i]);\n\t\t}\n\t\toutput.println();\n\t\toutput.flush();\n\t}\n}" | |
| }, | |
| { | |
| "id": 68, | |
| "name": "answer_A66", | |
| "Python": "# Union-Find 木\nclass unionfind:\n\t# n 頂点の Union-Find 木を作成\n\t# (ここでは頂点番号が 1-indexed になるように実装しているが、0-indexed の場合は par, size のサイズは n でよい)\n\tdef __init__(self, n):\n\t\tself.n = n\n\t\tself.par = [ -1 ] * (n + 1) # 最初は親が無い\n\t\tself.size = [ 1 ] * (n + 1) # 最初はグループの頂点数が 1\n\t\n\t# 頂点 x の根を返す関数\n\tdef root(self, x):\n\t\t# 1 個先(親)がなくなる(つまり根に到達する)まで、1 個先(親)に進み続ける\n\t\twhile self.par[x] != -1:\n\t\t\tx = self.par[x]\n\t\treturn x\n\t\n\t# 要素 u, v を統合する関数\n\tdef unite(self, u, v):\n\t\trootu = self.root(u)\n\t\trootv = self.root(v)\n\t\tif rootu != rootv:\n\t\t\t# u と v が異なるグループのときのみ処理を行う\n\t\t\tif self.size[rootu] < self.size[rootv]:\n\t\t\t\tself.par[rootu] = rootv\n\t\t\t\tself.size[rootv] += self.size[rootu]\n\t\t\telse:\n\t\t\t\tself.par[rootv] = rootu\n\t\t\t\tself.size[rootu] += self.size[rootv]\n\t\n\t# 要素 u と v が同一のグループかどうかを返す関数\n\tdef same(self, u, v):\n\t\treturn self.root(u) == self.root(v)\n\n# 入力\nN, Q = map(int, input().split())\nqueries = [ list(map(int, input().split())) for i in range(Q) ]\n\n# クエリの処理\nuf = unionfind(N)\nfor tp, u, v in queries:\n\tif tp == 1:\n\t\tuf.unite(u, v)\n\tif tp == 2:\n\t\tif uf.same(u, v):\n\t\t\tprint(\"Yes\")\n\t\telse:\n\t\t\tprint(\"No\")", | |
| "Java": "import java.util.*;\nimport java.io.*;\n\nclass Main {\n\tpublic static void main(String[] args) throws IOException {\n\t\t// 入力(高速な入出力のため、Scanner の代わりに BufferedReader を使っています)\n\t\tBufferedReader buff = new BufferedReader(new InputStreamReader(System.in));\n\t\tStringTokenizer st;\n\t\tst = new StringTokenizer(buff.readLine());\n\t\tint N = Integer.parseInt(st.nextToken());\n\t\tint Q = Integer.parseInt(st.nextToken());\n\t\tint[] query = new int[Q + 1];\n\t\tint[] u = new int[Q + 1];\n\t\tint[] v = new int[Q + 1];\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tst = new StringTokenizer(buff.readLine());\n\t\t\tquery[i] = Integer.parseInt(st.nextToken());\n\t\t\tu[i] = Integer.parseInt(st.nextToken());\n\t\t\tv[i] = Integer.parseInt(st.nextToken());\n\t\t}\n\t\t\n\t\t// クエリの処理\n\t\t// (高速な出力のため、System.out.println ではなく PrintWriter を使っています)\n\t\tPrintWriter output = new PrintWriter(System.out);\n\t\tUnionFind uf = new UnionFind(N);\n\t\tfor (int i = 1; i <= Q; i++) {\n\t\t\tif (query[i] == 1) {\n\t\t\t\tuf.unite(u[i], v[i]);\n\t\t\t}\n\t\t\tif (query[i] == 2) {\n\t\t\t\tif (uf.same(u[i], v[i])) {\n\t\t\t\t\toutput.println(\"Yes\");\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\toutput.println(\"No\");\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\toutput.flush();\n\t}\n\t\n\t// Union-Find 木を実装したクラス UnionFind\n\tstatic class UnionFind {\n\t\tint n;\n\t\tint[] par;\n\t\tint[] size;\n\n\t\t// n 頂点の Union-Find を作成\n\t\tpublic UnionFind(int n) {\n\t\t\tthis.n = n;\n\t\t\tpar = new int[n + 1];\n\t\t\tsize = new int[n + 1];\n\t\t\tArrays.fill(par, -1); // 最初は親が無い (par[i] = -1)\n\t\t\tArrays.fill(size, 1); // 最初はグループの頂点数が 1 (size[i] = 1)\n\t\t}\n\n\t\t// 頂点 x の根を返す関数\n\t\tint root(int x) {\n\t\t\twhile (true) {\n\t\t\t\tif (par[x] == -1) {\n\t\t\t\t\tbreak; // 1 個先(親)がなければ、ここが根\n\t\t\t\t}\n\t\t\t\tx = par[x]; // 1 個先(親)に進む\n\t\t\t}\n\t\t\treturn x;\n\t\t}\n\n\t\t// 要素 u と v を統合する関数\n\t\tvoid unite(int u, int v) {\n\t\t\tint rootU = root(u);\n\t\t\tint rootV = root(v);\n\t\t\tif (rootU == rootV) {\n\t\t\t\treturn; // u と v が同グループのときは処理を行わない\n\t\t\t}\n\t\t\tif (size[rootU] < size[rootV]) {\n\t\t\t\tpar[rootU] = rootV;\n\t\t\t\tsize[rootV] += size[rootU];\n\t\t\t}\n\t\t\telse {\n\t\t\t\tpar[rootV] = rootU;\n\t\t\t\tsize[rootU] += size[rootV];\n\t\t\t}\n\t\t}\n\n\t\t// 要素 u と v が同一のグループかどうかを返す関数\n\t\tboolean same(int u, int v) {\n\t\t\treturn root(u) == root(v);\n\t\t}\n\t}\n}" | |
| }, | |
| { | |
| "id": 69, | |
| "name": "answer_A67", | |
| "Python": "# Union-Find 木\nclass unionfind:\n\t# n 頂点の Union-Find 木を作成\n\t# (ここでは頂点番号が 1-indexed になるように実装しているが、0-indexed の場合は par, size のサイズは n でよい)\n\tdef __init__(self, n):\n\t\tself.n = n\n\t\tself.par = [ -1 ] * (n + 1) # 最初は親が無い\n\t\tself.size = [ 1 ] * (n + 1) # 最初はグループの頂点数が 1\n\t\n\t# 頂点 x の根を返す関数\n\tdef root(self, x):\n\t\t# 1 個先(親)がなくなる(つまり根に到達する)まで、1 個先(親)に進み続ける\n\t\twhile self.par[x] != -1:\n\t\t\tx = self.par[x]\n\t\treturn x\n\t\n\t# 要素 u, v を統合する関数\n\tdef unite(self, u, v):\n\t\trootu = self.root(u)\n\t\trootv = self.root(v)\n\t\tif rootu != rootv:\n\t\t\t# u と v が異なるグループのときのみ処理を行う\n\t\t\tif self.size[rootu] < self.size[rootv]:\n\t\t\t\tself.par[rootu] = rootv\n\t\t\t\tself.size[rootv] += self.size[rootu]\n\t\t\telse:\n\t\t\t\tself.par[rootv] = rootu\n\t\t\t\tself.size[rootu] += self.size[rootv]\n\t\n\t# 要素 u と v が同一のグループかどうかを返す関数\n\tdef same(self, u, v):\n\t\treturn self.root(u) == self.root(v)\n\n# 入力\nN, M = map(int, input().split())\nedges = [ list(map(int, input().split())) for i in range(M) ]\n\n# 辺を長さの小さい順にソートする\nedges.sort(key = lambda x: x[2])\n\n# 最小全域木を求める\nuf = unionfind(N)\nanswer = 0\nfor a, b, c in edges:\n\tif not uf.same(a, b):\n\t\tuf.unite(a, b)\n\t\tanswer += c\n\n# 答えの出力\nprint(answer)", | |
| "Java": "import java.util.*;\nimport java.io.*;\n\nclass Main {\n\tpublic static void main(String[] args) throws IOException {\n\t\t// 入力(高速な入出力のため、Scanner の代わりに BufferedReader を使っています)\n\t\tBufferedReader buff = new BufferedReader(new InputStreamReader(System.in));\n\t\tStringTokenizer st;\n\t\tst = new StringTokenizer(buff.readLine());\n\t\tint N = Integer.parseInt(st.nextToken());\n\t\tint M = Integer.parseInt(st.nextToken());\n\t\tint[] A = new int[M + 1];\n\t\tint[] B = new int[M + 1];\n\t\tint[] C = new int[M + 1];\n\t\tfor (int i = 1; i <= M; i++) {\n\t\t\tst = new StringTokenizer(buff.readLine());\n\t\t\tA[i] = Integer.parseInt(st.nextToken());\n\t\t\tB[i] = Integer.parseInt(st.nextToken());\n\t\t\tC[i] = Integer.parseInt(st.nextToken());\n\t\t}\n\n\t\t// 辺の長さの小さい順にソートする\n\t\t// (書籍内のコードでは edgeList は (長さ, 辺番号) のペアになっていますが、ここでは辺番号のみを記録した配列を長さの小さい順にソートするという方法をとります)\n\t\tInteger[] edgeList = new Integer[M];\n\t\tfor (int i = 0; i < M; i++) {\n\t\t\tedgeList[i] = i + 1;\n\t\t}\n\t\tArrays.sort(\n\t\t\tedgeList,\n\t\t\tnew Comparator<Integer>() {\n\t\t\t\t@Override\n\t\t\t\tpublic int compare(Integer id1, Integer id2) {\n\t\t\t\t\treturn C[id1] - C[id2]; // C[id1] < C[id2] のとき id1 が id2 よりも前に来るようにする\n\t\t\t\t}\n\t\t\t}\n\t\t);\n\t\t\n\t\t// 最小全域木を求める\n\t\tint answer = 0;\n\t\tUnionFind uf = new UnionFind(N);\n\t\tfor (int i = 0; i < M; i++) {\n\t\t\tint idx = edgeList[i];\n\t\t\tif (!uf.same(A[idx], B[idx])) {\n\t\t\t\tuf.unite(A[idx], B[idx]);\n\t\t\t\tanswer += C[idx];\n\t\t\t}\n\t\t}\n\n\t\t// 答えの出力\n\t\tSystem.out.println(answer);\n\t}\n\t\n\t// Union-Find 木を実装したクラス UnionFind\n\tstatic class UnionFind {\n\t\tint n;\n\t\tint[] par;\n\t\tint[] size;\n\n\t\t// n 頂点の Union-Find を作成\n\t\tpublic UnionFind(int n) {\n\t\t\tthis.n = n;\n\t\t\tpar = new int[n + 1];\n\t\t\tsize = new int[n + 1];\n\t\t\tArrays.fill(par, -1); // 最初は親が無い (par[i] = -1)\n\t\t\tArrays.fill(size, 1); // 最初はグループの頂点数が 1 (size[i] = 1)\n\t\t}\n\n\t\t// 頂点 x の根を返す関数\n\t\tint root(int x) {\n\t\t\twhile (true) {\n\t\t\t\tif (par[x] == -1) {\n\t\t\t\t\tbreak; // 1 個先(親)がなければ、ここが根\n\t\t\t\t}\n\t\t\t\tx = par[x]; // 1 個先(親)に進む\n\t\t\t}\n\t\t\treturn x;\n\t\t}\n\n\t\t// 要素 u と v を統合する関数\n\t\tvoid unite(int u, int v) {\n\t\t\tint rootU = root(u);\n\t\t\tint rootV = root(v);\n\t\t\tif (rootU == rootV) {\n\t\t\t\treturn; // u と v が同グループのときは処理を行わない\n\t\t\t}\n\t\t\tif (size[rootU] < size[rootV]) {\n\t\t\t\tpar[rootU] = rootV;\n\t\t\t\tsize[rootV] += size[rootU];\n\t\t\t}\n\t\t\telse {\n\t\t\t\tpar[rootV] = rootU;\n\t\t\t\tsize[rootU] += size[rootV];\n\t\t\t}\n\t\t}\n\n\t\t// 要素 u と v が同一のグループかどうかを返す関数\n\t\tboolean same(int u, int v) {\n\t\t\treturn root(u) == root(v);\n\t\t}\n\t}\n}" | |
| }, | |
| { | |
| "id": 70, | |
| "name": "answer_A68", | |
| "Python": "# 最大フロー用の辺の構造体\nclass maxflow_edge:\n\tdef __init__(self, to, cap, rev):\n\t\tself.to = to\n\t\tself.cap = cap\n\t\tself.rev = rev\n\n# 深さ優先探索\ndef dfs(pos, goal, F, G, used):\n\tif pos == goal:\n\t\treturn F # ゴールに到着:フローを流せる!\n\t# 探索する\n\tused[pos] = True\n\tfor e in G[pos]:\n\t\t# 容量が 1 以上でかつ、まだ訪問していない頂点にのみ行く\n\t\tif e.cap > 0 and not used[e.to]:\n\t\t\tflow = dfs(e.to, goal, min(F, e.cap), G, used)\n\t\t\t# フローを流せる場合、残余グラフの容量を flow だけ増減させる\n\t\t\tif flow >= 1:\n\t\t\t\te.cap -= flow\n\t\t\t\tG[e.to][e.rev].cap += flow\n\t\t\t\treturn flow\n\t# すべての辺を探索しても見つからなかった…\n\treturn 0\n\n# 頂点 s から頂点 t までの最大フローの総流量を返す(頂点数 N、辺のリスト edges)\ndef maxflow(N, s, t, edges):\n\t# 初期状態の残余グラフを構築\n\t# (ここは書籍とは少し異なる実装をしているため、8 行目は G[a] に追加された後なので len(G[a]) - 1 となっていることに注意)\n\tG = [ list() for i in range(N + 1) ]\n\tfor a, b, c in edges:\n\t\tG[a].append(maxflow_edge(b, c, len(G[b])))\n\t\tG[b].append(maxflow_edge(a, 0, len(G[a]) - 1))\n\tINF = 10 ** 10\n\ttotal_flow = 0\n\twhile True:\n\t\tused = [ False ] * (N + 1)\n\t\tF = dfs(s, t, INF, G, used)\n\t\tif F > 0:\n\t\t\ttotal_flow += F\n\t\telse:\n\t\t\tbreak # フローを流せなくなったら、操作終了\n\treturn total_flow\n\n# 入力\nN, M = map(int, input().split())\nedges = [ list(map(int, input().split())) for i in range(M) ]\n\n# 答えを求めて出力\nanswer = maxflow(N, 1, N, edges)\nprint(answer)", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\t// 入力\n\t\tScanner sc = new Scanner(System.in);\n\t\tint N = sc.nextInt();\n\t\tint M = sc.nextInt();\n\t\tint[] A = new int[M + 1];\n\t\tint[] B = new int[M + 1];\n\t\tint[] C = new int[M + 1];\n\t\tfor (int i = 1; i <= M; i++) {\n\t\t\tA[i] = sc.nextInt();\n\t\t\tB[i] = sc.nextInt();\n\t\t\tC[i] = sc.nextInt();\n\t\t}\n\n\t\t// 辺を追加\n\t\tMaximumFlow Z = new MaximumFlow(N);\n\t\tfor (int i = 1; i <= M; i++) {\n\t\t\tZ.addEdge(A[i], B[i], C[i]);\n\t\t}\n\n\t\t// 答えを求めて出力\n\t\tint answer = Z.maxFlow(1, N);\n\t\tSystem.out.println(answer);\n\t}\n\t\n\t// 最大フローを求める用の辺のクラス FlowEdge\n\tstatic class FlowEdge {\n\t\tint to, cap, rev;\n\t\tpublic FlowEdge(int to, int cap, int rev) {\n\t\t\tthis.to = to;\n\t\t\tthis.cap = cap;\n\t\t\tthis.rev = rev;\n\t\t}\n\t}\n\n\t// 最大フローを行うクラス MaximumFlow\n\tstatic class MaximumFlow {\n\t\tint n;\n\t\tboolean[] used;\n\t\tArrayList<FlowEdge>[] G;\n\n\t\t// 頂点数 N の残余グラフを準備\n\t\tpublic MaximumFlow(int n) {\n\t\t\tthis.n = n;\n\t\t\tused = new boolean[n + 1];\n\t\t\tG = new ArrayList[n + 1];\n\t\t\tfor (int i = 1; i <= n; i++) {\n\t\t\t\tG[i] = new ArrayList<FlowEdge>();\n\t\t\t}\n\t\t}\n\n\t\t// 頂点 a から b に向かう、上限 c リットル/秒の辺を追加\n\t\tvoid addEdge(int a, int b, int c) {\n\t\t\tint currentGa = G[a].size();\n\t\t\tint currentGb = G[b].size();\n\t\t\tG[a].add(new FlowEdge(b, c, currentGb));\n\t\t\tG[b].add(new FlowEdge(a, 0, currentGa));\n\t\t}\n\n\t\t// 深さ優先探索(F はスタートから pos に到達する過程での \" 残余グラフの辺の容量 \" の最小値)\n\t\t// 返り値は流したフローの量(流せない場合は 0 を返す)\n\t\tint dfs(int pos, int goal, int F) {\n\t\t\t// ゴールに到着:フローを流せる!\n\t\t\tif (pos == goal) {\n\t\t\t\treturn F;\n\t\t\t}\n\t\t\tused[pos] = true;\n\t\t\t// 探索する\n\t\t\tfor (int i = 0; i < G[pos].size(); i++) {\n\t\t\t\tFlowEdge e = G[pos].get(i);\n\t\t\t\t// 容量 0 の辺は使えない\n\t\t\t\tif (e.cap == 0) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\t// 既に訪問した頂点に行っても意味がない\n\t\t\t\tif (used[e.to]) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\t// 目的地までのパスを探す\n\t\t\t\tint flow = dfs(e.to, goal, Math.min(F, e.cap));\n\t\t\t\t// フローを流せる場合、残余グラフの容量を flow だけ増減させる\n\t\t\t\tif (flow >= 1) {\n\t\t\t\t\tG[pos].get(i).cap -= flow;\n\t\t\t\t\tG[e.to].get(e.rev).cap += flow;\n\t\t\t\t\treturn flow;\n\t\t\t\t}\n\t\t\t}\n\t\t\t// すべての辺を探索しても見つからなかった…\n\t\t\treturn 0;\n\t\t}\n\n\t\t// 頂点 s から頂点 t までの最大フローの総流量を返す\n\t\tint maxFlow(int s, int t) {\n\t\t\tfinal int INF = 1000000000;\n\t\t\tint totalFlow = 0;\n\t\t\twhile (true) {\n\t\t\t\tArrays.fill(used, false);\n\t\t\t\tint f = dfs(s, t, INF);\n\t\t\t\t// フローを流せなくなったら操作終了\n\t\t\t\tif (f == 0) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\ttotalFlow += f;\n\t\t\t}\n\t\t\treturn totalFlow;\n\t\t}\n\t}\n}" | |
| }, | |
| { | |
| "id": 71, | |
| "name": "answer_A69", | |
| "Python": "# 最大フロー用の辺の構造体\nclass maxflow_edge:\n\tdef __init__(self, to, cap, rev):\n\t\tself.to = to\n\t\tself.cap = cap\n\t\tself.rev = rev\n\n# 深さ優先探索\ndef dfs(pos, goal, F, G, used):\n\tif pos == goal:\n\t\treturn F # ゴールに到着:フローを流せる!\n\t# 探索する\n\tused[pos] = True\n\tfor e in G[pos]:\n\t\t# 容量が 1 以上でかつ、まだ訪問していない頂点にのみ行く\n\t\tif e.cap > 0 and not used[e.to]:\n\t\t\tflow = dfs(e.to, goal, min(F, e.cap), G, used)\n\t\t\t# フローを流せる場合、残余グラフの容量を flow だけ増減させる\n\t\t\tif flow >= 1:\n\t\t\t\te.cap -= flow\n\t\t\t\tG[e.to][e.rev].cap += flow\n\t\t\t\treturn flow\n\t# すべての辺を探索しても見つからなかった…\n\treturn 0\n\n# 頂点 s から頂点 t までの最大フローの総流量を返す(頂点数 N、辺のリスト edges)\ndef maxflow(N, s, t, edges):\n\t# 初期状態の残余グラフを構築\n\t# (ここは書籍とは少し異なる実装をしているため、8 行目は G[a] に追加された後なので len(G[a]) - 1 となっていることに注意)\n\tG = [ list() for i in range(N + 1) ]\n\tfor a, b, c in edges:\n\t\tG[a].append(maxflow_edge(b, c, len(G[b])))\n\t\tG[b].append(maxflow_edge(a, 0, len(G[a]) - 1))\n\tINF = 10 ** 10\n\ttotal_flow = 0\n\twhile True:\n\t\tused = [ False ] * (N + 1)\n\t\tF = dfs(s, t, INF, G, used)\n\t\tif F > 0:\n\t\t\ttotal_flow += F\n\t\telse:\n\t\t\tbreak # フローを流せなくなったら、操作終了\n\treturn total_flow\n\n# 入力\nN = int(input())\nC = [ input() for i in range(N) ]\n\n# 最大フローを求めたいグラフを構築する(辺の要素は (辺の始点の番号, 辺の終点の番号, 辺の容量) のタプル)\nedges = []\nfor i in range(N):\n\tfor j in range(N):\n\t\tif C[i][j] == '#':\n\t\t\tedges.append((i + 1, N + j + 1, 1))\nfor i in range(N):\n\tedges.append((2 * N + 1, i + 1, 1)) # 「s → 青色」の辺\n\tedges.append((N + i + 1, 2 * N + 2, 1)) # 「赤色 → t」の辺\n\n# 答えを求めて出力\nanswer = maxflow(2 * N + 2, 2 * N + 1, 2 * N + 2, edges)\nprint(answer)", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\t// 入力\n\t\tScanner sc = new Scanner(System.in);\n\t\tint N = sc.nextInt();\n\t\tString[] C = new String[N + 1];\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tC[i + 1] = sc.next();\n\t\t}\n\n\t\t// グラフを構成する\n\t\tMaximumFlow Z = new MaximumFlow(2 * N + 2);\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tfor (int j = 1; j <= N; j++) {\n\t\t\t\tif (C[i].charAt(j - 1) == '#') {\n\t\t\t\t\tZ.addEdge(i, j + N, 1);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tZ.addEdge(2 * N + 1, i, 1); // 「s → 青色」の辺\n\t\t\tZ.addEdge(i + N, 2 * N + 2, 1); // 「赤色 → t」の辺\n\t\t}\n\n\t\t// 答えを求めて出力\n\t\tint answer = Z.maxFlow(2 * N + 1, 2 * N + 2);\n\t\tSystem.out.println(answer);\n\t}\n\t\n\t// 最大フローを求める用の辺のクラス FlowEdge\n\tstatic class FlowEdge {\n\t\tint to, cap, rev;\n\t\tpublic FlowEdge(int to, int cap, int rev) {\n\t\t\tthis.to = to;\n\t\t\tthis.cap = cap;\n\t\t\tthis.rev = rev;\n\t\t}\n\t}\n\n\t// 最大フローを行うクラス MaximumFlow\n\tstatic class MaximumFlow {\n\t\tint n;\n\t\tboolean[] used;\n\t\tArrayList<FlowEdge>[] G;\n\n\t\t// 頂点数 N の残余グラフを準備\n\t\tpublic MaximumFlow(int n) {\n\t\t\tthis.n = n;\n\t\t\tused = new boolean[n + 1];\n\t\t\tG = new ArrayList[n + 1];\n\t\t\tfor (int i = 1; i <= n; i++) {\n\t\t\t\tG[i] = new ArrayList<FlowEdge>();\n\t\t\t}\n\t\t}\n\n\t\t// 頂点 a から b に向かう、上限 c リットル/秒の辺を追加\n\t\tvoid addEdge(int a, int b, int c) {\n\t\t\tint currentGa = G[a].size();\n\t\t\tint currentGb = G[b].size();\n\t\t\tG[a].add(new FlowEdge(b, c, currentGb));\n\t\t\tG[b].add(new FlowEdge(a, 0, currentGa));\n\t\t}\n\n\t\t// 深さ優先探索(F はスタートから pos に到達する過程での \" 残余グラフの辺の容量 \" の最小値)\n\t\t// 返り値は流したフローの量(流せない場合は 0 を返す)\n\t\tint dfs(int pos, int goal, int F) {\n\t\t\t// ゴールに到着:フローを流せる!\n\t\t\tif (pos == goal) {\n\t\t\t\treturn F;\n\t\t\t}\n\t\t\tused[pos] = true;\n\t\t\t// 探索する\n\t\t\tfor (int i = 0; i < G[pos].size(); i++) {\n\t\t\t\tFlowEdge e = G[pos].get(i);\n\t\t\t\t// 容量 0 の辺は使えない\n\t\t\t\tif (e.cap == 0) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\t// 既に訪問した頂点に行っても意味がない\n\t\t\t\tif (used[e.to]) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\t// 目的地までのパスを探す\n\t\t\t\tint flow = dfs(e.to, goal, Math.min(F, e.cap));\n\t\t\t\t// フローを流せる場合、残余グラフの容量を flow だけ増減させる\n\t\t\t\tif (flow >= 1) {\n\t\t\t\t\tG[pos].get(i).cap -= flow;\n\t\t\t\t\tG[e.to].get(e.rev).cap += flow;\n\t\t\t\t\treturn flow;\n\t\t\t\t}\n\t\t\t}\n\t\t\t// すべての辺を探索しても見つからなかった…\n\t\t\treturn 0;\n\t\t}\n\n\t\t// 頂点 s から頂点 t までの最大フローの総流量を返す\n\t\tint maxFlow(int s, int t) {\n\t\t\tfinal int INF = 1000000000;\n\t\t\tint totalFlow = 0;\n\t\t\twhile (true) {\n\t\t\t\tArrays.fill(used, false);\n\t\t\t\tint f = dfs(s, t, INF);\n\t\t\t\t// フローを流せなくなったら操作終了\n\t\t\t\tif (f == 0) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\ttotalFlow += f;\n\t\t\t}\n\t\t\treturn totalFlow;\n\t\t}\n\t}\n}" | |
| }, | |
| { | |
| "id": 72, | |
| "name": "answer_A70", | |
| "Python": "from collections import deque\n\n# 入力(ここでは書籍とは異なり、ランプの番号が 0-indexed になるように実装しています)\nN, M = map(int, input().split())\nA = list(map(int, input().split()))\nactions = [ list(map(lambda x: int(x) - 1, input().split())) for i in range(M) ] # ここでは X[i], Y[i], Z[i] を 0-indexed に変換して受け取る\n\n# 頂点 pos の状態から「ランプ x, y, z の状態」を反転させたときの頂点番号を返す関数\ndef get_next(pos, x, y, z):\n\t# pos の 2 進法表記を使って、頂点 pos が表すランプの状態 state を計算\n\t# (pos の 2^i の位は (pos // (2 ** i)) % 2 で計算できる → 1.4 節を参照)\n\tstate = [ (pos // (2 ** i)) % 2 for i in range(N) ]\n\t# ランプ x, y, z を反転\n\tstate[x] = 1 - state[x]\n\tstate[y] = 1 - state[y]\n\tstate[z] = 1 - state[z]\n\t# ランプの状態 state を指す頂点の番号を計算\n\t# (2 進法を 10 進法に変換する方法は 1.4 節を参照)\n\tret = 0\n\tfor i in range(N):\n\t\tif state[i] == 1:\n\t\t\tret += 2 ** i\n\treturn ret\n\n# グラフに辺を追加\nG = [ list() for i in range(2 ** N) ]\nfor i in range(2 ** N):\n\tfor x, y, z in actions:\n\t\tnextstate = get_next(i, x, y, z)\n\t\tG[i].append(nextstate)\n\n# スタート地点・ゴール地点の頂点番号を決める\nstart = 0\nfor i in range(N):\n\tif A[i] == 1:\n\t\tstart += 2 ** i\ngoal = 2 ** N - 1\n\n# 幅優先探索の初期化\ndist = [ -1 ] * (2 ** N)\ndist[start] = 0\nQ = deque()\nQ.append(start)\n\n# 幅優先探索\nwhile len(Q) >= 1:\n\tpos = Q.popleft() # キュー Q の先頭要素を取り除き、その値を pos に代入する\n\tfor nex in G[pos]:\n\t\tif dist[nex] == -1:\n\t\t\tdist[nex] = dist[pos] + 1\n\t\t\tQ.append(nex)\n\n# 答えを出力\nprint(dist[goal])\n\n# 注意 1:この問題に対してはより簡潔な実装もありますので、\n# もしよければ answer_A70_extra.py もご覧ください。", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\t// 入力\n\t\tScanner sc = new Scanner(System.in);\n\t\tint N = sc.nextInt();\n\t\tint M = sc.nextInt();\n\t\tint[] A = new int[N + 1];\n\t\tint[] X = new int[M + 1];\n\t\tint[] Y = new int[M + 1];\n\t\tint[] Z = new int[M + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tA[i] = sc.nextInt();\n\t\t}\n\t\tfor (int i = 1; i <= M; i++) {\n\t\t\tX[i] = sc.nextInt();\n\t\t\tY[i] = sc.nextInt();\n\t\t\tZ[i] = sc.nextInt();\n\t\t}\n\n\t\t// 隣接リストを作成し、グラフに辺を追加\n\t\tArrayList<Integer>[] G = new ArrayList[1 << N];\n\t\tfor (int i = 0; i < (1 << N); i++) {\n\t\t\tG[i] = new ArrayList<Integer>();\n\t\t}\n\t\tfor (int i = 0; i < (1 << N); i++) {\n\t\t\tfor (int j = 1; j <= M; j++) {\n\t\t\t\tint nextState = getNext(N, i, X[j], Y[j], Z[j]);\n\t\t\t\tG[i].add(nextState);\n\t\t\t}\n\t\t}\n\n\t\t// スタート・ゴールの頂点番号を求める\n\t\tint start = 0;\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tif (A[i] == 1) {\n\t\t\t\tstart += (1 << (i - 1));\n\t\t\t}\n\t\t}\n\t\tint goal = (1 << N) - 1;\n\n\t\t// 配列の初期化/スタート地点をキューに入れる\n\t\tint[] dist = new int[1 << N];\n\t\tArrays.fill(dist, -1);\n\t\tdist[start] = 0;\n\t\tQueue<Integer> Q = new LinkedList<>();\n\t\tQ.add(start);\n\n\t\t// 幅優先探索\n\t\twhile (Q.size() >= 1) {\n\t\t\tint pos = Q.remove();\n\t\t\tfor (int i = 0; i < G[pos].size(); i++) {\n\t\t\t\tint nex = G[pos].get(i);\n\t\t\t\tif (dist[nex] == -1) {\n\t\t\t\t\tdist[nex] = dist[pos] + 1;\n\t\t\t\t\tQ.add(nex);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// 答えを出力\n\t\tSystem.out.println(dist[goal]);\n\t}\n\t\n\t// 頂点 pos の状態から「ランプ x, y, z の状態」を反転させたときの頂点番号を返す関数\n\t// (書籍内のコードでは「頂点 pos の状態から idx 種類目の操作を行ったとき」の頂点番号を返す関数を実装しているが、これとは少し異なる実装をしていることに注意)\n\tstatic int getNext(int N, int pos, int x, int y, int z) {\n\t\t// pos の 2 進法表記を使って、頂点 pos が表すランプの状態 state を計算\n\t\t// (2 進法に変換する方法は 1.4 節を参照)\n\t\tint[] state = new int[N + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tint wari = (1 << (i - 1));\n\t\t\tstate[i] = (pos / wari) % 2;\n\t\t}\n\n\t\t// ランプ x, y, z の状態を反転\n\t\tstate[x] = 1 - state[x];\n\t\tstate[y] = 1 - state[y];\n\t\tstate[z] = 1 - state[z];\n\n\t\t// 10 進法に変換する方法も 1.4 節を参照\n\t\tint ret = 0;\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tif (state[i] == 1) {\n\t\t\t\tret += (1 << (i - 1));\n\t\t\t}\n\t\t}\n\t\treturn ret;\n\t}\n}\n\n// 注意 1:この問題に対してはより簡潔な実装もありますので、もしよければ answer_A70_extra.py もご覧ください。" | |
| }, | |
| { | |
| "id": 73, | |
| "name": "answer_A70_extra", | |
| "Python": "from collections import deque\n\n# 入力(ここでは書籍とは異なり、ランプの番号が 0-indexed になるように実装しています)\nN, M = map(int, input().split())\nA = list(map(int, input().split()))\nactions = [ list(map(lambda x: int(x) - 1, input().split())) for i in range(M) ] # ここでは X[i], Y[i], Z[i] を 0-indexed に変換して受け取る\n\n# スタート地点・ゴール地点の頂点番号を決める\nstart = sum(A[i] * (2 ** i) for i in range(N))\ngoal = 2 ** N - 1\n\n# 幅優先探索の初期化\ndist = [ -1 ] * (2 ** N)\ndist[start] = 0\nQ = deque()\nQ.append(start)\n\n# 幅優先探索\n# (ここではグラフを実際に持たずに、pos から出る辺をそのまま計算して幅優先探索を行います)\nwhile len(Q) >= 1:\n\tpos = Q.popleft() # キュー Q の先頭要素を取り除き、その値を pos に代入する\n\tfor x, y, z in actions:\n\t\t# ビット演算の XOR を使います(XOR についてはコラム 1 を参照)。\n\t\t# ランプ k を反転することは、頂点番号の 2^k の位を反転すること、すなわち 2^k を XOR することと同じになります。\n\t\tnex = pos ^ (1 << x) ^ (1 << y) ^ (1 << z)\n\t\tif dist[nex] == -1:\n\t\t\tdist[nex] = dist[pos] + 1\n\t\t\tQ.append(nex)\n\n# 答えを出力\nprint(dist[goal])\n\n# 注意 1:ビット演算は、掛け算 (*)、割り算 (//)、累乗 (**) などの演算と比べて高速であるため、\n# このプログラムは answer_A70.py と比較して約 1/5 の実行時間で答えが出せます。", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\t// 入力\n\t\tScanner sc = new Scanner(System.in);\n\t\tint N = sc.nextInt();\n\t\tint M = sc.nextInt();\n\t\tint[] A = new int[N + 1];\n\t\tint[] X = new int[M + 1];\n\t\tint[] Y = new int[M + 1];\n\t\tint[] Z = new int[M + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tA[i] = sc.nextInt();\n\t\t}\n\t\tfor (int i = 1; i <= M; i++) {\n\t\t\tX[i] = sc.nextInt();\n\t\t\tY[i] = sc.nextInt();\n\t\t\tZ[i] = sc.nextInt();\n\t\t}\n\n\t\t// スタート・ゴールの頂点番号を求める\n\t\tint start = 0;\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tif (A[i] == 1) {\n\t\t\t\tstart += (1 << (i - 1));\n\t\t\t}\n\t\t}\n\t\tint goal = (1 << N) - 1;\n\n\t\t// 配列の初期化/スタート地点をキューに入れる\n\t\tint[] dist = new int[1 << N];\n\t\tArrays.fill(dist, -1);\n\t\tdist[start] = 0;\n\t\tQueue<Integer> Q = new LinkedList<>();\n\t\tQ.add(start);\n\n\t\t// 幅優先探索\n\t\t// (ここではグラフを実際に持たずに、pos から出る辺をそのまま計算して幅優先探索を行います)\n\t\twhile (Q.size() >= 1) {\n\t\t\tint pos = Q.remove();\n\t\t\tfor (int i = 1; i <= M; i++) {\n\t\t\t\t// ビット演算の XOR を使って、操作後の頂点番号を求めます(XOR についてはコラム 1 を参照)。\n\t\t\t\t// ランプ k を反転することは、頂点番号の 2^k の位を反転すること、すなわち 2^k を XOR することと同じになります。\n\t\t\t\tint nex = pos ^ (1 << (X[i] - 1)) ^ (1 << (Y[i] - 1)) ^ (1 << (Z[i] - 1));\n\t\t\t\tif (dist[nex] == -1) {\n\t\t\t\t\tdist[nex] = dist[pos] + 1;\n\t\t\t\t\tQ.add(nex);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// 答えを出力\n\t\tSystem.out.println(dist[goal]);\n\t}\n}\n\n// 注意 1:このプログラムは answer_A70.java と比較してコードが簡潔であるだけでなく、実行速度の面でも優れています。\n// なぜなら、隣接リストを実際に作るのを省けたり、配列 state を作らずに 6 回のビット演算だけで「次の頂点番号」が求められたりするからです。\n// なお、ビット演算は、掛け算 (*) や割り算 (/) などの演算と比べて大幅に高速です。\n" | |
| }, | |
| { | |
| "id": 74, | |
| "name": "answer_A71", | |
| "Python": "# 入力\nN = int(input())\nA = list(map(int, input().split()))\nB = list(map(int, input().split()))\n\n# 配列のソート(A は昇順に、B は降順にソート)\nA.sort()\nB.sort(reverse = True)\n\n# 答えは A[i] * B[i] の総和\nprint(sum([ A[i] * B[i] for i in range(N) ]))", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\t// 入力(書籍とは異なり A[i], B[i] は 0-indexed で入力しています)\n\t\tScanner sc = new Scanner(System.in);\n\t\tint N = sc.nextInt();\n\t\tInteger[] A = new Integer[N];\n\t\tInteger[] B = new Integer[N];\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tA[i] = sc.nextInt();\n\t\t}\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tB[i] = sc.nextInt();\n\t\t}\n\n\t\t// 配列のソート\n\t\tArrays.sort(A);\n\t\tArrays.sort(B, Collections.reverseOrder());\n\n\t\t// 答えを求めて出力\n\t\tint answer = 0;\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tanswer += A[i] * B[i];\n\t\t}\n\t\tSystem.out.println(answer);\n\t}\n}" | |
| }, | |
| { | |
| "id": 75, | |
| "name": "answer_A72", | |
| "Python": "import itertools\n\n# 入力\nH, W, K = map(int, input().split())\nc = [ input() for i in range(H) ]\n\n# 残り remaining_steps 回の「列に対する操作」で、最大何個のマスを黒くできるかを返す関数\ndef paint_row(H, W, d, remaining_steps):\n\t# 各列に対して (白マスの個数, 列の番号) のタプルを記録し、大きい順にソートする\n\tcolumn = [ ([ d[i][j] for i in range(H) ].count('.'), j) for j in range(W) ]\n\tcolumn.sort(reverse = True)\n\n\t# 列に対して操作を行う\n\tfor j in range(remaining_steps):\n\t\tidx = column[j][1]\n\t\tfor i in range(H):\n\t\t\td[i][idx] = '#'\n\t\n\t# 黒マスの個数を数えて、これを返す\n\treturn sum(map(lambda l: l.count('#'), d))\n\n# 行の塗り方を全探索\n# (ここでは「ビット全探索」ではなく itertools.product を使って 2^H 通りの塗り方を全列挙しています)\nanswer = 0\nfor v in itertools.product([ 0, 1 ], repeat = H):\n\t# 行に対して操作を行う(paint_row 関数でいくつかの d[i][j] を書き換えるため、d は string の配列ではなく 2 次元リストにしています)\n\td = [ list(c[i]) for i in range(H) ]\n\tremaining_steps = K\n\tfor i in range(H):\n\t\tif v[i] == 1:\n\t\t\td[i] = [ '#' ] * W\n\t\t\tremaining_steps -= 1\n\tif remaining_steps >= 0:\n\t\tsubanswer = paint_row(H, W, d, remaining_steps)\n\t\tanswer = max(answer, subanswer)\n\n# 出力\nprint(answer)", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\t// 入力(書籍とは異なり c[i][j] は 0-indexed で入力しています)\n\t\tScanner sc = new Scanner(System.in);\n\t\tint H = sc.nextInt();\n\t\tint W = sc.nextInt();\n\t\tint K = sc.nextInt();\n\t\tchar[][] c = new char[H][W];\n\t\tfor (int i = 0; i < H; i++) {\n\t\t\tString cs = sc.next();\n\t\t\tfor (int j = 0; j < W; j++) {\n\t\t\t\tc[i][j] = cs.charAt(j);\n\t\t\t}\n\t\t}\n\n\t\t// ビット全探索\n\t\tint answer = 0;\n\t\tfor (int t = 0; t < (1 << H); t++) {\n\t\t\t// まずはマス目を初期盤面に設定\n\t\t\tchar[][] d = new char[H][W];\n\t\t\tfor (int i = 0; i < H; i++) {\n\t\t\t\tfor (int j = 0; j < W; j++) {\n\t\t\t\t\td[i][j] = c[i][j];\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// 行に対して操作を行う\n\t\t\t// 変数 remainingSteps は残り操作回数\n\t\t\tint remainingSteps = K;\n\t\t\tfor (int i = 0; i < H; i++) {\n\t\t\t\tint wari = (1 << i);\n\t\t\t\tif ((t / wari) % 2 == 1) {\n\t\t\t\t\tremainingSteps -= 1;\n\t\t\t\t\tfor (int j = 0; j < W; j++) {\n\t\t\t\t\t\td[i][j] = '#';\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// 列に対して操作を行う\n\t\t\tif (remainingSteps >= 0) {\n\t\t\t\tint subAnswer = paintRow(H, W, d, remainingSteps);\n\t\t\t\tanswer = Math.max(answer, subAnswer);\n\t\t\t}\n\t\t}\n\n\t\t// 出力\n\t\tSystem.out.println(answer);\n\t}\n\n\t// 残り remainingSteps 回の「列に対する操作」で、最大何個のマスを黒くできるかを返す関数\n\tstatic int paintRow(int H, int W, char[][] d, int remainingSteps) {\n\t\t// 各列に対する「白マスの個数」を計算し、大きい順にソートする\n\t\tPairInt[] column = new PairInt[W];\n\t\tfor (int j = 0; j < W; j++) {\n\t\t\tint cnt = 0;\n\t\t\tfor (int i = 0; i < H; i++) {\n\t\t\t\tif (d[i][j] == '.') {\n\t\t\t\t\tcnt += 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\tcolumn[j] = new PairInt(cnt, j);\n\t\t}\n\t\tArrays.sort(column, Collections.reverseOrder());\n\n\t\t// 列に対して操作を行う\n\t\tfor (int j = 0; j < remainingSteps; j++) {\n\t\t\tint idx = column[j].second;\n\t\t\tfor (int i = 0; i < H; i++) {\n\t\t\t\td[i][idx] = '#';\n\t\t\t}\n\t\t}\n\n\t\t// 黒マスの個数を数える\n\t\tint ret = 0;\n\t\tfor (int i = 0; i < H; i++) {\n\t\t\tfor (int j = 0; j < W; j++) {\n\t\t\t\tif (d[i][j] == '#') {\n\t\t\t\t\tret += 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn ret;\n\t}\n\n\t// int 型のペアのクラス PairInt\n\tstatic class PairInt implements Comparable<PairInt> {\n\t\tint first, second;\n\t\tpublic PairInt(int first, int second) {\n\t\t\tthis.first = first;\n\t\t\tthis.second = second;\n\t\t}\n\t\t@Override public int compareTo(PairInt p) {\n\t\t\t// PairInt 型同士の比較をする関数\n\t\t\tif (this.first < p.first || (this.first == p.first && this.second < p.second)) {\n\t\t\t\treturn -1;\n\t\t\t}\n\t\t\tif (this.first > p.first || (this.first == p.first && this.second > p.second)) {\n\t\t\t\treturn 1;\n\t\t\t}\n\t\t\treturn 0;\n\t\t}\n\t}\n}" | |
| }, | |
| { | |
| "id": 76, | |
| "name": "answer_A73", | |
| "Python": "import heapq\n\n# 入力\nN, M = map(int, input().split())\nroads = [ list(map(int, input().split())) for i in range(M) ]\n\n# グラフの作成\nG = [ list() for i in range(N + 1) ]\nfor a, b, c, d in roads:\n\tif d == 1:\n\t\tG[a].append((b, 10000 * c - 1))\n\t\tG[b].append((a, 10000 * c - 1))\n\telse:\n\t\tG[a].append((b, 10000 * c))\n\t\tG[b].append((a, 10000 * c))\n\n# ダイクストラ法(ダイクストラ法の詳しい説明については本書籍の 9.4 節、および ../chap09/answer_A64.py を参照)\nINF = 10 ** 10\nkakutei = [ False ] * (N + 1)\ncur = [ INF ] * (N + 1)\ncur[1] = 0\nQ = []\nheapq.heappush(Q, (cur[1], 1))\nwhile len(Q) >= 1:\n\tpos = heapq.heappop(Q)[1]\n\tif kakutei[pos] == True:\n\t\tcontinue\n\tkakutei[pos] = True\n\tfor e in G[pos]:\n\t\tif cur[e[0]] > cur[pos] + e[1]:\n\t\t\tcur[e[0]] = cur[pos] + e[1]\n\t\t\theapq.heappush(Q, (cur[e[0]], e[0]))\n\n# 答えを求めて出力\n# マラソンコースの距離:cur[N] / 10000 を小数点以下切り上げた値\n# コース上の木の数:cur[N] と distance * 10000 の差分\ndistance = (cur[N] + 9999) // 10000\nnum_trees = distance * 10000 - cur[N]\nprint(distance, num_trees)", | |
| "Java": "import java.util.*;\nimport java.io.*;\n\nclass Main {\n\tpublic static void main(String[] args) throws IOException {\n\t\t// 入力(高速な入出力のため、Scanner の代わりに BufferedReader を使っています)\n\t\tBufferedReader buff = new BufferedReader(new InputStreamReader(System.in));\n\t\tStringTokenizer st;\n\t\tst = new StringTokenizer(buff.readLine());\n\t\tint N = Integer.parseInt(st.nextToken());\n\t\tint M = Integer.parseInt(st.nextToken());\n\t\tint[] A = new int[M + 1];\n\t\tint[] B = new int[M + 1];\n\t\tint[] C = new int[M + 1];\n\t\tint[] D = new int[M + 1];\n\t\tfor (int i = 1; i <= M; i++) {\n\t\t\tst = new StringTokenizer(buff.readLine());\n\t\t\tA[i] = Integer.parseInt(st.nextToken());\n\t\t\tB[i] = Integer.parseInt(st.nextToken());\n\t\t\tC[i] = Integer.parseInt(st.nextToken());\n\t\t\tD[i] = Integer.parseInt(st.nextToken());\n\t\t}\n\t\t\n\t\t// グラフの作成\n\t\tArrayList<Edge>[] G = new ArrayList[N + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tG[i] = new ArrayList<Edge>();\n\t\t}\n\t\tfor (int i = 1; i <= M; i++) {\n\t\t\tif (D[i] == 1) {\n\t\t\t\tG[A[i]].add(new Edge(B[i], 10000L * C[i] - 1L));\n\t\t\t\tG[B[i]].add(new Edge(A[i], 10000L * C[i] - 1L));\n\t\t\t}\n\t\t\telse {\n\t\t\t\tG[A[i]].add(new Edge(B[i], 10000L * C[i]));\n\t\t\t\tG[B[i]].add(new Edge(A[i], 10000L * C[i]));\n\t\t\t}\n\t\t}\n\t\t\n\t\t// ダイクストラ法(ダイクストラ法の詳しい説明については本書籍の 9.4 節、および ../chap09/answer_A64.java を参照)\n\t\tfinal long INF = (1L << 60);\n\t\tboolean[] kakutei = new boolean[N + 1];\n\t\tlong[] cur = new long[N + 1];\n\t\tArrays.fill(cur, INF);\n\t\tcur[1] = 0;\n\t\tQueue<State> Q = new PriorityQueue<>();\n\t\tQ.add(new State(cur[1], 1));\n\t\twhile (Q.size() >= 1) {\n\t\t\tint pos = Q.remove().pos;\n\t\t\tif (kakutei[pos]) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tkakutei[pos] = true;\n\t\t\tfor (int i = 0; i < G[pos].size(); i++) {\n\t\t\t\tEdge e = G[pos].get(i);\n\t\t\t\tif (cur[e.to] > cur[pos] + e.cost) {\n\t\t\t\t\tcur[e.to] = cur[pos] + e.cost;\n\t\t\t\t\tQ.add(new State(cur[e.to], e.to));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\t// 答えを出力\n\t\t// マラソンコースの距離:cur[N]/10000 を小数点以下切り上げた値\n\t\t// コース上の木の数:cur[N] と distance*10000 の差分\n\t\tlong distance = (cur[N] + 9999) / 10000;\n\t\tlong numTrees = distance * 10000 - cur[N];\n\t\tSystem.out.println(distance + \" \" + numTrees);\n\t}\n\n\t// 重み付きグラフの辺のクラス Edge\n\tstatic class Edge {\n\t\tint to; long cost; // 行き先 to、長さ cost\n\t\tpublic Edge(int to, long cost) {\n\t\t\tthis.to = to;\n\t\t\tthis.cost = cost;\n\t\t}\n\t}\n\n\t// ダイクストラ法の (cur[x], x) を管理するクラス(cur[x] = dist, x = pos に対応)\n\tstatic class State implements Comparable<State> {\n\t\tlong dist; int pos;\n\t\tpublic State(long dist, int pos) {\n\t\t\tthis.dist = dist;\n\t\t\tthis.pos = pos;\n\t\t}\n\t\t@Override public int compareTo(State s) {\n\t\t\t// State 型同士の比較をする関数\n\t\t\tif (this.dist < s.dist) {\n\t\t\t\treturn -1;\n\t\t\t}\n\t\t\tif (this.dist > s.dist) {\n\t\t\t\treturn 1;\n\t\t\t}\n\t\t\treturn 0;\n\t\t}\n\t}\n}" | |
| }, | |
| { | |
| "id": 77, | |
| "name": "answer_A74", | |
| "Python": "# 入力\nN = int(input())\nP = [ list(map(int, input().split())) for i in range(N) ]\n\n# 「転倒数を求める問題」2 つに分解する\nX = [ None ] * N\nY = [ None ] * N\nfor i in range(N):\n\tfor j in range(N):\n\t\tif P[i][j] != 0:\n\t\t\tX[i] = P[i][j]\n\t\t\tY[j] = P[i][j]\n\n# 転倒数を求める関数\ndef inversion(A):\n\tanswer = 0\n\tfor i in range(len(A)):\n\t\tfor j in range(i + 1, len(A)):\n\t\t\tif A[i] > A[j]:\n\t\t\t\tanswer += 1\n\treturn answer\n\n# 答えを出力\nprint(inversion(X) + inversion(Y))", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\t// 入力\n\t\tScanner sc = new Scanner(System.in);\n\t\tint N = sc.nextInt();\n\t\tint[][] P = new int[N + 1][N + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tfor (int j = 1; j <= N; j++) {\n\t\t\t\tP[i][j] = sc.nextInt();\n\t\t\t}\n\t\t}\n\n\t\t// 「転倒数を求める問題」2 つに分解する\n\t\tint[] X = new int[N + 1];\n\t\tint[] Y = new int[N + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tfor (int j = 1; j <= N; j++) {\n\t\t\t\tif (P[i][j] != 0) {\n\t\t\t\t\tX[i] = P[i][j];\n\t\t\t\t\tY[j] = P[i][j];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// X の転倒数・Y の転倒数を求める\n\t\tint inversionX = 0;\n\t\tint inversionY = 0;\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tfor (int j = i + 1; j <= N; j++) {\n\t\t\t\tif (X[i] > X[j]) {\n\t\t\t\t\tinversionX += 1;\n\t\t\t\t}\n\t\t\t\tif (Y[i] > Y[j]) {\n\t\t\t\t\tinversionY += 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// 出力\n\t\tSystem.out.println(inversionX + inversionY);\n\t}\n}" | |
| }, | |
| { | |
| "id": 78, | |
| "name": "answer_A75", | |
| "Python": "# 入力\nN = int(input())\nproblems = [ list(map(int, input().split())) for i in range(N) ] # タプル (T[i], D[i]) が N 個並んだ配列になる\n\n# D[i] の小さい順に並べ替える\nproblems.sort(key = lambda p: p[1])\n\n# 動的計画法:前準備\nMAX_D = max(map(lambda p: p[1], problems)) # D[i] の最大値(書籍内のコードでは「1440」という定数を使っているが、ここでは代わりに MAX_D を使うことにする)\ndp = [ [ -(10 ** 10) ] * (MAX_D + 1) for i in range(N + 1) ]\n\n# 動的計画法\ndp[0][0] = 0\nfor i in range(N):\n\tt, d = problems[i] # 書籍中の T[i], D[i] に対応\n\tfor j in range(MAX_D + 1):\n\t\tif j > d or j < t:\n\t\t\tdp[i + 1][j] = dp[i][j]\n\t\telse:\n\t\t\tdp[i + 1][j] = max(dp[i][j], dp[i][j - t] + 1)\n\n# 答えを出力\nanswer = max(dp[N])\nprint(answer)", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\t// 入力\n\t\tScanner sc = new Scanner(System.in);\n\t\tint N = sc.nextInt();\n\t\tint[] T = new int[N + 1];\n\t\tint[] D = new int[N + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tT[i] = sc.nextInt();\n\t\t\tD[i] = sc.nextInt();\n\t\t}\n\n\t\t// D[i] の小さい順に並べ替える\n\t\tPairInt[] problems = new PairInt[N];\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tproblems[i] = new PairInt(D[i + 1], T[i + 1]);\n\t\t}\n\t\tArrays.sort(problems);\n\t\tfor (int i = 0; i < N; i++) {\n\t\t\tT[i + 1] = problems[i].second;\n\t\t\tD[i + 1] = problems[i].first;\n\t\t}\n\n\t\t// 動的計画法:前準備\n\t\tint maxD = 0; // D[i] の最大値(書籍内のコードでは「1440」という定数を使っているが、ここでは代わりに MAX_D を使うことにする)\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tmaxD = Math.max(maxD, D[i]);\n\t\t}\n\t\tint[][] dp = new int[N + 1][maxD + 1];\n\t\tfor (int i = 0; i <= N; i++) {\n\t\t\tfor (int j = 0; j <= maxD; j++) {\n\t\t\t\tdp[i][j] = -1000000000;\n\t\t\t}\n\t\t}\n\n\t\t// 動的計画法\n\t\tdp[0][0] = 0;\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tfor (int j = 0; j <= maxD; j++) {\n\t\t\t\tif (j > D[i] || j < T[i]) {\n\t\t\t\t\tdp[i][j] = dp[i - 1][j];\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tdp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - T[i]] + 1);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// 答えを求めて出力\n\t\tint answer = 0;\n\t\tfor (int i = 0; i <= maxD; i++) {\n\t\t\tanswer = Math.max(answer, dp[N][i]);\n\t\t}\n\t\tSystem.out.println(answer);\n\t}\n\t\n\t// int 型のペアのクラス PairInt\n\tstatic class PairInt implements Comparable<PairInt> {\n\t\tint first, second;\n\t\tpublic PairInt(int first, int second) {\n\t\t\tthis.first = first;\n\t\t\tthis.second = second;\n\t\t}\n\t\t@Override public int compareTo(PairInt p) {\n\t\t\t// PairInt 型同士の比較をする関数\n\t\t\tif (this.first < p.first || (this.first == p.first && this.second < p.second)) {\n\t\t\t\treturn -1;\n\t\t\t}\n\t\t\tif (this.first > p.first || (this.first == p.first && this.second > p.second)) {\n\t\t\t\treturn 1;\n\t\t\t}\n\t\t\treturn 0;\n\t\t}\n\t}\n}" | |
| }, | |
| { | |
| "id": 79, | |
| "name": "answer_A76", | |
| "Python": "import bisect\n\n# 入力\nN, W, L, R = map(int, input().split())\nX = list(map(int, input().split()))\n\n# 西岸を足場 0、東岸を足場 N+1 とみなす\nX = [ 0 ] + X + [ W ]\n\n# 動的計画法(書籍内の sum[i] は本コードの dpsum[i] に対応)\nMOD = 10 ** 9 + 7 # = 1000000007\ndp = [ 0 ] * (N + 2)\ndpsum = [ 0 ] * (N + 2)\ndp[0] = 1\ndpsum[0] = 1\nfor i in range(1, N + 2):\n\tposl = bisect.bisect_left(X, X[i] - R)\n\tposr = bisect.bisect_left(X, X[i] - L + 1) - 1\n\t# dp[i] の値を累積和で計算(C++ とは異なり、(負の値)% MOD も 0 以上 MOD-1 以下になることに注意)\n\tdp[i] = (dpsum[posr] if posr >= 0 else 0) - (dpsum[posl - 1] if posl >= 1 else 0)\n\tdp[i] %= MOD\n\t# 累積和 dpsum[i] の値を更新\n\tdpsum[i] = dpsum[i - 1] + dp[i]\n\tdpsum[i] %= MOD\n\n# 出力\nprint(dp[N + 1])", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\t// 入力\n\t\tScanner sc = new Scanner(System.in);\n\t\tint N = sc.nextInt();\n\t\tint W = sc.nextInt();\n\t\tint L = sc.nextInt();\n\t\tint R = sc.nextInt();\n\t\tint[] X = new int[N + 2];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tX[i] = sc.nextInt();\n\t\t}\n\n\t\t// 西岸を足場 0、東岸を足場 N+1 とみなす\n\t\tX[0] = 0;\n\t\tX[N + 1] = W;\n\n\t\t// 動的計画法\n\t\tfinal int MOD = 1000000007;\n\t\tint[] dp = new int[N + 2];\n\t\tint[] sum = new int[N + 2];\n\t\tdp[0] = 1;\n\t\tsum[0] = 1;\n\t\tfor (int i = 1; i <= N + 1; i++) {\n\t\t\tint posL = lowerBound(X, X[i] - R);\n\t\t\tint posR = lowerBound(X, X[i] - L + 1) - 1;\n\t\t\t// dp[i] の値を累積和で計算(引き算の余りに注意!)\n\t\t\tdp[i] += (posR >= 0 ? sum[posR] : 0);\n\t\t\tdp[i] -= (posL >= 1 ? sum[posL - 1] : 0);\n\t\t\tdp[i] = (dp[i] + MOD) % MOD;\n\t\t\t// 累積和 sum[i] を更新\n\t\t\tsum[i] = sum[i - 1] + dp[i];\n\t\t\tsum[i] %= MOD;\n\t\t}\n\n\t\t// 出力\n\t\tSystem.out.println(dp[N + 1]);\n\t}\n\n\t// ソートされた配列 A[0], A[1], ..., A[N-1] に対して、A[i] >= X となる最小の i を求める関数\n\tstatic int lowerBound(int[] A, int X) {\n\t\tint l = -1, r = A.length;\n\t\twhile (r - l > 1) {\n\t\t\tint m = (l + r) / 2;\n\t\t\tif (A[m] >= X) {\n\t\t\t\tr = m;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tl = m;\n\t\t\t}\n\t\t}\n\t\treturn r;\n\t}\n}" | |
| }, | |
| { | |
| "id": 80, | |
| "name": "answer_A77", | |
| "Python": "# 入力\nN, L = map(int, input().split())\nK = int(input())\nA = list(map(int, input().split()))\n\n# スコアの最大値が x 以上かを判定する関数\ndef check(x):\n\tcnt = 0 # 現時点で何回切ったかを表す\n\tlast_kireme = 0 # 最後どこで切ったかを表す\n\tfor i in range(N):\n\t\tif A[i] - last_kireme >= x and L - A[i] >= x:\n\t\t\tcnt += 1\n\t\t\tlast_kireme = A[i]\n\treturn cnt >= K\n\n# 二分探索(left: 現在の上限 / right: 現在の下限)\nleft, right = 1, 10 ** 9\nwhile left < right:\n\tmid = (left + right + 1) // 2\n\tanswer = check(mid)\n\tif answer == False:\n\t\tright = mid - 1 # 答えが前半部分に絞られる\n\telse:\n\t\tleft = mid # 答えが後半部分に絞られる\n\n# 出力\nprint(left)", | |
| "Java": "import java.util.*;\n\nclass Main {\n\tpublic static void main(String[] args) {\n\t\t// 入力\n\t\tScanner sc = new Scanner(System.in);\n\t\tint N = sc.nextInt();\n\t\tint L = sc.nextInt();\n\t\tint K = sc.nextInt();\n\t\tint[] A = new int[N + 1];\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tA[i] = sc.nextInt();\n\t\t}\n\n\t\t// 二分探索(left: 現在の下限 / right: 現在の上限)\n\t\tint left = 1, right = 1000000000;\n\t\twhile (left < right) {\n\t\t\tint mid = (left + right + 1) / 2;\n\t\t\tboolean answer = check(N, L, K, A, mid);\n\t\t\tif (answer == false) {\n\t\t\t\tright = mid - 1; // 答えが前半部分に絞られる\n\t\t\t}\n\t\t\telse {\n\t\t\t\tleft = mid; // 答えが後半部分に絞られる\n\t\t\t}\n\t\t}\n\n\t\t// 出力\n\t\tSystem.out.println(left);\n\t}\n\n\t// スコアの最大値が x 以上かを判定する関数\n\tstatic boolean check(int N, int L, int K, int[] A, int x) {\n\t\tint count = 0; // 現時点で何回切ったかを表す\n\t\tint lastKireme = 0; // 最後どこで切ったかを表す\n\t\tfor (int i = 1; i <= N; i++) {\n\t\t\tif (A[i] - lastKireme >= x && L - A[i] >= x) {\n\t\t\t\tcount += 1;\n\t\t\t\tlastKireme = A[i];\n\t\t\t}\n\t\t}\n\t\treturn (count >= K);\n\t}\n}" | |
| } | |
| ] |