結果

問題 No.2509 Beam Shateki
ユーザー anonymouslyanonymously
提出日時 2023-10-20 23:04:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 2,578 bytes
コンパイル時間 3,162 ms
コンパイル使用メモリ 183,852 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-20 23:04:52
合計ジャッジ時間 3,903 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 4 ms
4,348 KB
testcase_04 AC 4 ms
4,348 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 3 ms
4,348 KB
testcase_09 AC 4 ms
4,348 KB
testcase_10 AC 3 ms
4,348 KB
testcase_11 AC 3 ms
4,348 KB
testcase_12 AC 3 ms
4,348 KB
testcase_13 AC 8 ms
4,348 KB
testcase_14 AC 8 ms
4,348 KB
testcase_15 AC 8 ms
4,348 KB
testcase_16 AC 8 ms
4,348 KB
testcase_17 AC 8 ms
4,348 KB
testcase_18 AC 8 ms
4,348 KB
testcase_19 AC 8 ms
4,348 KB
testcase_20 AC 8 ms
4,348 KB
testcase_21 AC 8 ms
4,348 KB
testcase_22 AC 8 ms
4,348 KB
testcase_23 AC 8 ms
4,348 KB
testcase_24 AC 8 ms
4,348 KB
testcase_25 AC 8 ms
4,348 KB
testcase_26 AC 8 ms
4,348 KB
testcase_27 AC 8 ms
4,348 KB
testcase_28 AC 8 ms
4,348 KB
testcase_29 AC 8 ms
4,348 KB
testcase_30 AC 8 ms
4,348 KB
testcase_31 AC 9 ms
4,348 KB
testcase_32 AC 8 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 4 ms
4,348 KB
testcase_35 AC 6 ms
4,348 KB
testcase_36 AC 3 ms
4,348 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 4 ms
4,348 KB
testcase_39 AC 2 ms
4,348 KB
testcase_40 AC 4 ms
4,348 KB
testcase_41 AC 1 ms
4,348 KB
testcase_42 AC 3 ms
4,348 KB
testcase_43 AC 14 ms
4,348 KB
testcase_44 AC 7 ms
4,348 KB
testcase_45 AC 3 ms
4,348 KB
testcase_46 AC 4 ms
4,348 KB
testcase_47 AC 7 ms
4,348 KB
testcase_48 AC 4 ms
4,348 KB
testcase_49 AC 5 ms
4,348 KB
testcase_50 AC 5 ms
4,348 KB
testcase_51 AC 2 ms
4,348 KB
testcase_52 AC 5 ms
4,348 KB
testcase_53 AC 2 ms
4,348 KB
testcase_54 AC 2 ms
4,348 KB
testcase_55 AC 2 ms
4,348 KB
testcase_56 AC 2 ms
4,348 KB
testcase_57 AC 2 ms
4,348 KB
testcase_58 AC 2 ms
4,348 KB
testcase_59 AC 1 ms
4,348 KB
testcase_60 AC 1 ms
4,348 KB
testcase_61 AC 1 ms
4,348 KB
testcase_62 AC 2 ms
4,348 KB
testcase_63 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main(){
	int h,w;
	cin >> h >> w;
	vector<vector<int>> A(h,vector<int>(w));
	vector<int> R(h,0); // 行合計
	vector<int> C(w,0); // 列合計
	vector<int> S(h+w-1,0); // 斜め合計(右上から左下、添字の和が一定)
	map<int,int> M; //斜め合計(左上から右下、添字の差(行番号-列番号が一定))
	
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			cin >> A[i][j];
			int& a=A[i][j];
			R[i]+=a;
			C[j]+=a;
			S[i+j]+=a;
			M[i-j]+=a;
		}
	}
	
	int ans=0;
	//平行方向にチェック
	//行
	for(int i=0;i<h;i++){
		//ビーム1回の場合
		if(R[i]>ans)ans=R[i];
		
		for(int j=i+1;j<h;j++){
			int s=R[i]+R[j];
			if(s>ans)ans=s;
		}
	}
	
	//列番号が一定
	for(int i=0;i<w;i++){
		//ビーム1回の場合
		if(C[i]>ans)ans=C[i];
		
		for(int j=i+1;j<w;j++){
			int s=C[i]+C[j];
			if(s>ans)ans=s;
		}
	}
	
	//斜め(添字の和が一定)
	for(int i=0;i<h+w-1;i++){
		//ビーム1回の場合
		if(S[i]>ans)ans=S[i];
		
		for(int j=i+1;j<h+w-1;j++){
			int s=S[i]+S[j];
			if(s>ans)ans=s;
		}
	}
	
	//斜め(添字の差が一定)
	for(int i=1-w;i<h;i++){
		//ビーム1回の場合
		if(M[i]>ans)ans=M[i];
		
		for(int j=i+1;j<h;j++){
			int s=M[i]+M[j];
			if(s>ans)ans=s;
		}
	}
	
	//タテ・ヨコ
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			int s=R[i]+C[j]-A[i][j]; //交差している箱は引く
			if(s>ans)ans=s;
		}
	}
	
	//ヨコ・ナナメ(添字の和が一定)
	for(int i=0;i<h;i++){
		for(int j=0;j<h+w-1;j++){
			int x=j-i; //交点
			int s=R[i]+S[j];
			if(0<=x && x<w)s-=A[i][x];
			if(s>ans)ans=s;
		}
	}

	//ヨコ・ナナメ(添字の差が一定)
	for(int i=0;i<h;i++){
		for(int j=1-w;j<h;j++){
			int x=i-j; //交点
			int s=R[i]+M[j];
			if(0<=x && x<w)s-=A[i][x];
			if(s>ans)ans=s;
		}
	}

	//タテ・ナナメ(添字の和が一定)
	for(int i=0;i<w;i++){
		for(int j=0;j<h+w-1;j++){
			int y=j-i; //交点
			int s=C[i]+S[j];
			if(0<=y && y<h)s-=A[y][i];
			if(s>ans)ans=s;
		}
	}

	//タテ・ナナメ(添字の差が一定)
	for(int i=0;i<w;i++){
		for(int j=1-w;j<h;j++){
			int y=j+i; //交点
			int s=C[i]+M[j];
			if(0<=y && y<h)s-=A[y][i];
			if(s>ans)ans=s;
		}
	}
	
	//ナナメ直交
	for(int i=0;i<h+w-1;i++){
		for(int j=1-w;j<h;j++){
			int s=S[i]+M[j];
			if((i+j)%2==0){
				//y+x=i, y-x=j
				int y=(i+j)/2;
				int x=(i-j)/2;//x+yが偶数ならx-yも偶数
				if(0<=y&&y<h&&0<=x&&x<w){
					s-=A[y][x];
				}
			}
			if(s>ans)ans=s;
		}
	}
	
	cout << ans << endl;
	return 0;
	
}
0