2 solutions

  • 0
    @ 2025-4-2 17:02:04
    #include <bits/stdc++.h>
    using namespace std;
    int main()
     {
    	int n, m;
    	cin >> n >> m;
    	int a[n][m];
    	for(int i = 0; i < n; i++)
    	{
    		for(int j = 0; j < m; j++)
    		{
    			cin >> a[i][j];
    		}
    	}
    	int mx = 0;
    	for(int i = 0; i < n; i++)
    	{
    		for(int j = 0; j < m; j++)
    		{
    			int now = 0;
    			int ci = i, cj = j;
    			while(ci >= 0 && ci < n && cj >= 0 && cj < m)
    			{
    				now+=a[ci][cj];
    				ci--;
    				cj--;
    			}
    			ci = i, cj = j;
    			while(ci >= 0 && ci < n && cj >= 0 && cj < m)
    			{
    				now+=a[ci][cj];
    				ci++;
    				cj--;
    			}
    			ci = i, cj = j;
    			while(ci >= 0 && ci < n && cj >= 0 && cj < m)
    			{
    				now+=a[ci][cj];
    				ci--;
    				cj++;
    			}
    			ci = i, cj = j;
    			while(ci >= 0 && ci < n && cj >= 0 && cj < m)
    			{
    				now+=a[ci][cj];
    				ci++;
    				cj++;
    			}
    			now-=a[i][j]*3;
    			mx = max(mx, now);
    		}
    	}
    	cout << mx << endl;
    }
    
    • 0
      @ 2025-4-1 17:20:55

      解决方法是检查每个单元格所有对角线的总和。

      对于一个单元格( i,ji,j ),我们可以遍历其所有对角线上的所有元素。这样总共会有 O(max(n,m))O(max(n, m)) 个元素。

      复杂度 O(nmmax(n,m))O(n \cdot m \cdot max(n, m))

      涉及预计算的 O(nm)O(n \cdot m) 解决方案也是可能的,但并非必需。

      • 1

      Information

      ID
      1446
      Time
      1000ms
      Memory
      256MiB
      Difficulty
      5
      Tags
      (None)
      # Submissions
      92
      Accepted
      35
      Uploaded By