結果

問題 No.2509 Beam Shateki
ユーザー InTheBloom
提出日時 2023-10-20 23:13:09
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 4,411 bytes
コンパイル時間 5,121 ms
コンパイル使用メモリ 181,444 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-20 22:31:33
合計ジャッジ時間 7,136 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 61
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

import std;
void main () {
int H, W; readln.read(H, W);
int[][] A = new int[][](H, W);
foreach (i; 0..H) A[i] = readln.split.to!(int[]);
solve(H, W, A);
}
void solve (int H, int W, int[][] A) {
/**
* vertical horizontally DUR DLR
* | -------> _ \
* | /\ _\/
* v /
*
* 0 1 2 0 0 3 2 1 0
* 1 1 4
* 2 2 5
* 3 4 5
*
*/
int[] vertical = new int[](W);
int[] horizontally = new int[](H);
int[] DLR = new int[](H+W-1);
int[] DUR = new int[](H+W-1);
foreach (j; 0..W) foreach (i; 0..H) vertical[j] += A[i][j];
foreach (i; 0..H) foreach (j; 0..W) horizontally[i] += A[i][j];
bool isInGrid (int y, int x) {
return 0 <= y && y < H && 0 <= x && x < W;
}
{ /* DLR */
int idx = 0;
int y = 0, x = W-1;
int dy = 1, dx = 1;
for (; 0 < x; x--) {
int k = 0;
while (isInGrid(y+k*dy, x+k*dx)) {
DLR[idx] += A[y+k*dy][x+k*dx];
k++;
}
idx++;
}
for (; y < H; y++) {
int k = 0;
while (isInGrid(y+k*dy, x+k*dx)) {
DLR[idx] += A[y+k*dy][x+k*dx];
k++;
}
idx++;
}
}
{ /* DUR */
int idx = 0;
int y = 0, x = 0;
int dy = -1, dx = 1;
for (; y < H-1; y++) {
int k = 0;
while (isInGrid(y+k*dy, x+k*dx)) {
DUR[idx] += A[y+k*dy][x+k*dx];
k++;
}
idx++;
}
for (; x < W; x++) {
int k = 0;
while (isInGrid(y+k*dy, x+k*dx)) {
DUR[idx] += A[y+k*dy][x+k*dx];
k++;
}
idx++;
}
}
/*
writeln(vertical);
writeln(horizontally);
writeln(DLR);
writeln(DUR);
*/
long ans = -long.max;
/* 2 */
if (1 < vertical.length) ans = max(ans, vertical.dup.sort!"a>b"[0..2].sum);
if (1 == vertical.length) ans = max(ans, vertical.fold!max);
if (1 < horizontally.length) ans = max(ans, horizontally.dup.sort!"a>b"[0..2].sum);
if (1 == horizontally.length) ans = max(ans, horizontally.fold!max);
if (1 < DUR.length) ans = max(ans, DUR.dup.sort!"a>b"[0..2].sum);
if (1 == DUR.length) ans = max(ans, DUR.fold!max);
if (1 < DLR.length) ans = max(ans, DLR.dup.sort!"a>b"[0..2].sum);
if (1 == DLR.length) ans = max(ans, DLR.fold!max);
/* 2 */
/* 1. vertical and horizontally */
foreach (i; 0..W) foreach (j; 0..H) ans = max(ans, vertical[i]+horizontally[j] - A[j][i]);
/* 2. vertical and DUR */
foreach (i; 0..W) foreach (j; 0..H+W-1) {
if (isInGrid(j-i, i)) {
ans = max(ans, vertical[i]+DUR[j] - A[j-i][i]);
} else {
ans = max(ans, vertical[i] + DUR[j]);
}
}
/* 3. vertical and DLR */
foreach (i; 0..W) foreach (j; 0..H+W-1) {
if (isInGrid(1-W+i+j, i)) {
ans = max(ans, vertical[i]+DLR[j] - A[1-W+i+j][i]);
} else {
ans = max(ans, vertical[i]+DLR[j]);
}
}
/* 4. horizontally + DUR */
foreach (i; 0..H) foreach (j; 0..H+W-1) {
if (isInGrid(i, j-i)) {
ans = max(ans, horizontally[i]+DUR[j] - A[i][j-i]);
} else {
ans = max(ans, horizontally[i]+DUR[j]);
}
}
/* 5. horizontally + DLR */
foreach (i; 0..H) foreach (j; 0..H+W-1) {
if (isInGrid(i, W-1+i-j)) {
ans = max(ans, horizontally[i]+DLR[j] - A[i][W-1+i-j]);
} else {
ans = max(ans, horizontally[i]+DLR[j]);
}
}
/* 6. DUR and DLR */
foreach (i; 0..H+W-1) foreach (j; 0..H+W-1) {
if ((W-1+j-i)%2 == 0 && isInGrid(j-(W-1+j-i)/2, (W-1+j-i)/2)) {
ans = max(ans, DLR[i]+DUR[j] - A[j-(W-1+j-i)/2][(W-1+j-i)/2]);
} else {
ans = max(ans, DLR[i]+DUR[j]);
}
}
writeln(ans);
}
void read(T...)(string S, ref T args) {
auto buf = S.split;
foreach (i, ref arg; args) {
arg = buf[i].to!(typeof(arg));
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0