結果

問題 No.655 E869120 and Good Triangles
ユーザー pekempeypekempey
提出日時 2018-02-24 00:04:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,095 ms / 2,500 ms
コード長 2,912 bytes
コンパイル時間 1,138 ms
コンパイル使用メモリ 87,356 KB
実行使用メモリ 379,264 KB
最終ジャッジ日時 2024-04-18 09:26:24
合計ジャッジ時間 21,151 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 991 ms
379,136 KB
testcase_11 AC 993 ms
379,016 KB
testcase_12 AC 897 ms
379,136 KB
testcase_13 AC 871 ms
379,008 KB
testcase_14 AC 945 ms
379,136 KB
testcase_15 AC 974 ms
379,200 KB
testcase_16 AC 890 ms
379,264 KB
testcase_17 AC 1,051 ms
379,004 KB
testcase_18 AC 991 ms
379,008 KB
testcase_19 AC 1,040 ms
379,008 KB
testcase_20 AC 1,081 ms
379,088 KB
testcase_21 AC 1,095 ms
379,008 KB
testcase_22 AC 883 ms
379,008 KB
testcase_23 AC 906 ms
379,136 KB
testcase_24 AC 617 ms
379,136 KB
testcase_25 AC 631 ms
379,008 KB
testcase_26 AC 630 ms
379,008 KB
testcase_27 AC 686 ms
379,008 KB
testcase_28 AC 592 ms
379,008 KB
testcase_29 AC 628 ms
378,880 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 1 ms
5,376 KB
testcase_32 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std; 
int main() {
  int n, K;
  long long p;
  cin >> n >> K >> p;

  vector<vector<int>> dist(n, vector<int>(n, -1));

  queue<pair<int, int>> q;
  for (int i = 0; i < K; i++) {
    int y, x;
    cin >> y >> x;
    y--;
    x--;
    dist[y][x] = 0;
    q.emplace(y, x);
  }

  while (!q.empty()) {
    int y = q.front().first;
    int x = q.front().second;
    q.pop();
    int dy[] = {1, 1, 0, -1, -1, 0};
    int dx[] = {0, 1, 1, 0, -1, -1};
    for (int k = 0; k < 6; k++) {
      int ny = y + dy[k];
      int nx = x + dx[k];
      if (ny < 0 || nx < 0 || nx > ny || ny >= n) continue;
      if (dist[ny][nx] == -1) {
        dist[ny][nx] = dist[y][x] + 1;
        q.emplace(ny, nx);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (dist[i][j] == -1) dist[i][j] = 0;
    }
  }

#ifdef FOO
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cout << dist[i][j];
      cout << ' ';
    }
    cout << endl;
  }
  cout << endl;
#endif

  vector<vector<long long>> sum(n + 1, vector<long long>(n + 1));

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      sum[i + 1][j + 1] = dist[i][j] + sum[i + 1][j] + sum[i][j + 1] - sum[i][j];
    }
  }

#ifdef FOO
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      cout << sum[i][j];
      cout << ' ';
    }
    cout << endl;
  }
  cout << endl;
#endif


  vector<vector<long long>> tri(n, vector<long long>(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      tri[i][j] = dist[i][j];
      if (i - 1 >= 0 && j - 1 >= 0) {
        tri[i][j] += tri[i - 1][j - 1];
      }
      if (j - 1 >= 0) {
        tri[i][j] += tri[i][j - 1];
      }
      if (i - 1 >= 0 && j - 2 >= 0) {
        tri[i][j] -= tri[i - 1][j - 2];
      }
    }
  }

#ifdef FOO
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cout << tri[i][j];
      cout << ' ';
    }
    cout << endl;
  }
  cout << endl;
#endif

  vector<vector<int>> shaku(n + 1, vector<int>(n + 1));

  long long ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= i; j++) {
      int ng = i - 1;
      int ok = n;
      shaku[i][j] = max(shaku[i][j], i);
      auto f = [&](int k) {
        int d = k - i;
        long long hoge = tri[k][j + d];
        hoge -= sum[k + 1][j] - sum[i + 1][j];
        hoge -= tri[i][j];
        hoge += dist[i][j];
        return hoge;
      };
      int &k = shaku[i][j];
      while (k < n && f(k) < p) {
        k++;
      }
      shaku[i + 1][j] = max(shaku[i + 1][j], shaku[i][j]);
      shaku[i + 1][j + 1] = max(shaku[i + 1][j + 1], shaku[i][j]);
#ifdef FOO
      for (int k = ok; k < n; k++) {
        printf("%d %d %d %lld\n", i, j, k, f(k));
      }
#endif
      ans += n - shaku[i][j];
    }
  }
  cout << ans << endl;
}
0