首页 > 其他分享 >abc086d <二维前缀和 同余>

abc086d <二维前缀和 同余>

时间:2023-07-15 12:35:51浏览次数:50  
标签:k2 int abc086d ++ res y1 x1

题目

D - Checker

思路

  • 坐标对 2k 取余, 通过二维前缀和计算满足条件的个数;
  • 也可对 k 取余, 参考;

代码

Code
// https://atcoder.jp/contests/abc086/tasks/arc089_b
// 二维前缀和 同余
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
using LL = long long;
const int K = 1010;
int a[K * 4][K * 4][2];

int get(int x1, int y1, int x2, int y2, int p)
{
    int res = a[x2][y2][p];
    if (x1 - 1 >= 0) res -= a[x1-1][y2][p];
    if (y1 - 1 >= 0) res -= a[x2][y1-1][p];
    if (x1 - 1 >= 0 && y1 - 1 >= 0) res += a[x1-1][y1-1][p];
    return res;
}

void solv()
{
    int n, k, k2;
    cin >> n >> k; k2 = k * 2;
    int x, y; char c;
    int sum[2] = {0};
    for (int i = 1; i <= n; i ++)
    {
        cin >> x >> y >> c;
        int p = c == 'B';
        a[x % k2][y % k2][p] ++;
        a[x % k2 + k2][y % k2 + k2][p] ++;
        sum[p] ++;
    }
    for (int i = 0; i < k2; i ++)
        for (int j = 0; j < k2; j ++)
            for (int p = 0; p < 2; p ++)
            {
                if (i-1 >= 0) a[i][j][p] += a[i-1][j][p];
                if (j-1 >= 0) a[i][j][p] += a[i][j-1][p];
                if (i-1 >= 0 && j-1 >= 0) a[i][j][p] -= a[i-1][j-1][p];
                // a[i][j][p] += a[i-1][j][p] + a[i][j-1][p] - a[i-1][j-1][p];
            }
    int ans = 0;
    for (int i = 0; i < k; i ++)
        for (int j = 0; j < k; j ++)
        {
            int x[] = {0, i + 1, i + k + 1, k2};  // 将 2k * 2k 的区域划分成 9 个矩形
            int y[] = {0, j + 1, j + k + 1, k2};
            int a0 = 0, a1 = 0;
            for (int p = 0; p < 3; p ++)
            {
                for (int q = 0; q < 3; q ++)
                {
                    a0 += get(x[p], y[q], x[p + 1] - 1, y[q + 1] - 1 ,(p + q) & 1);
                    a1 += get(x[p], y[q], x[p + 1] - 1, y[q + 1] - 1, (p + q + 1) & 1);
                }
            }
            ans = max({ans, a0, a1});
        }
    cout << ans << endl;
}

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int T = 1;
	// cin >> T;
    while (T --)
    {
        solv();
    }
    return 0;
}

标签:k2,int,abc086d,++,res,y1,x1
From: https://www.cnblogs.com/o2iginal/p/17555939.html

相关文章