結果

問題 No.655 E869120 and Good Triangles
ユーザー vwxyzvwxyz
提出日時 2024-05-03 15:55:18
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 890 ms / 2,500 ms
コード長 3,116 bytes
コンパイル時間 1,033 ms
コンパイル使用メモリ 104,292 KB
実行使用メモリ 449,920 KB
最終ジャッジ日時 2024-05-03 15:55:40
合計ジャッジ時間 17,510 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 872 ms
449,792 KB
testcase_11 AC 831 ms
449,796 KB
testcase_12 AC 852 ms
449,692 KB
testcase_13 AC 832 ms
449,792 KB
testcase_14 AC 819 ms
449,664 KB
testcase_15 AC 880 ms
449,664 KB
testcase_16 AC 890 ms
449,832 KB
testcase_17 AC 879 ms
449,664 KB
testcase_18 AC 840 ms
449,688 KB
testcase_19 AC 859 ms
449,920 KB
testcase_20 AC 880 ms
449,792 KB
testcase_21 AC 826 ms
449,792 KB
testcase_22 AC 830 ms
449,740 KB
testcase_23 AC 829 ms
449,720 KB
testcase_24 AC 640 ms
449,536 KB
testcase_25 AC 597 ms
449,536 KB
testcase_26 AC 604 ms
449,536 KB
testcase_27 AC 656 ms
449,536 KB
testcase_28 AC 576 ms
449,536 KB
testcase_29 AC 659 ms
449,536 KB
testcase_30 AC 1 ms
6,940 KB
testcase_31 AC 2 ms
6,940 KB
testcase_32 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <deque>
#define int long long

using namespace std;

signed main() {
    int N, K, P;
    cin >> N >> K >> P;

    int inf = 1 << 30;
    vector<vector<int>> A(N, vector<int>(N, inf));
    deque<pair<int, int>> queue;

    for (int i = 0; i < K; ++i) {
        int x, y;
        cin >> x >> y;
        x -= 1;
        y -= 1;
        queue.push_back({x, y});
        A[x][y] = 0;
    }

    while (!queue.empty()) {
        int x = queue.front().first;
        int y = queue.front().second;
        queue.pop_front();

        if (0 <= y - 1 && y - 1 <= x - 1 && x-1<N && A[x][y] + 1 < A[x - 1][y - 1]) {
            A[x - 1][y - 1] = A[x][y] + 1;
            queue.push_back({x - 1, y - 1});
        }
        if (0 <= y && y <= x - 1 && x-1<N && A[x][y] + 1 < A[x - 1][y]) {
            A[x - 1][y] = A[x][y] + 1;
            queue.push_back({x - 1, y});
        }
        if (0 <= y - 1 && y - 1 <= x && x<N && A[x][y] + 1 < A[x][y - 1]) {
            A[x][y - 1] = A[x][y] + 1;
            queue.push_back({x, y - 1});
        }
        if (0 <= y + 1 && y + 1 <= x && x<N && A[x][y] + 1 < A[x][y + 1]) {
            A[x][y + 1] = A[x][y] + 1;
            queue.push_back({x, y + 1});
        }
        if (0 <= y && y <= x + 1 && x+1<N && A[x][y] + 1 < A[x + 1][y]) {
            A[x + 1][y] = A[x][y] + 1;
            queue.push_back({x + 1, y});
        }
        if (0 <= y + 1 && y + 1 <= x + 1 && x+1<N && A[x][y] + 1 < A[x + 1][y + 1]) {
            A[x + 1][y + 1] = A[x][y] + 1;
            queue.push_back({x + 1, y + 1});
        }
    }

    vector<vector<int>> AL(N, vector<int>(N));
    vector<vector<int>> AR(N, vector<int>(N));
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j <= i; ++j) {
            AL[i][j] = A[i][j] + (i > 0 ? AL[i - 1][j] : 0);
        }
    }
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j <= i; ++j) {
            AR[i][j] = A[i][j] + (j > 0 && i>0 ? AR[i - 1][j - 1] : 0);
        }
    }

    for (int i = 0; i < N; ++i) {
        A[i].insert(A[i].begin(), 0);
        for (int j = 1; j <= i + 1; ++j) {
            A[i][j] += A[i][j - 1];
        }
    }

    int ans = 0;
    vector<pair<int, int>> dp(N + 1, {0, 0});
    for (int i = 0; i < N; ++i) {
        vector<pair<int, int>> prev = dp;
        dp = vector<pair<int, int>>(i + 2, {0, 0});
        for (int j = 0; j <= i; ++j) {
            int ii = prev[j].first;
            int s = prev[j].second;
            while (ii < N && s < P) {
                s += A[ii][j - i + ii + 1] - A[ii][j];
                ii++;
            }
            prev[j] = {ii, s};
            if (i != N - 1) {
                dp[j] = max(dp[j], {ii, s - (AR[ii - 1][j - i + ii - 1] - (i > 0 && j > 0 ? AR[i - 1][j - 1] : 0))});
                dp[j + 1] = max(dp[j + 1], {ii, s - (AL[ii - 1][j] - (i > 0 && i != j ? AL[i - 1][j] : 0))});
            }
        }
        for (int j = 0; j <= i; ++j) {
            if (prev[j].second >= P) {
                ans += N - prev[j].first + 1;
            }
        }
    }

    cout << ans << endl;

    return 0;
}
0