結果

問題 No.655 E869120 and Good Triangles
ユーザー pekempeypekempey
提出日時 2018-02-23 23:53:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,755 bytes
コンパイル時間 1,020 ms
コンパイル使用メモリ 86,312 KB
実行使用メモリ 316,684 KB
最終ジャッジ日時 2024-04-18 09:21:55
合計ジャッジ時間 29,942 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
5,376 KB
testcase_02 WA -
testcase_03 AC 2 ms
5,376 KB
testcase_04 WA -
testcase_05 AC 2 ms
5,376 KB
testcase_06 WA -
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 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 1,202 ms
316,288 KB
testcase_26 AC 1,226 ms
316,416 KB
testcase_27 WA -
testcase_28 AC 1,123 ms
316,288 KB
testcase_29 WA -
testcase_30 AC 1 ms
5,376 KB
testcase_31 WA -
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

  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;
      auto f = [&](int k) {
        int d = k - i;
        long long hoge = tri[k][j + d];
        hoge -= sum[k + 1][j] - sum[i][j];
        hoge -= tri[i][j];
        hoge += dist[i][j];
        return hoge;
      };
      while (ok - ng > 1) {
        int mid = (ok + ng) / 2;
        if (f(mid) >= p) {
          ok = mid;
        } else {
          ng = mid;
        }
      }
#ifdef FOO
      for (int k = ok; k < n; k++) {
        printf("%d %d %d %lld\n", i, j, k, f(k));
      }
#endif
      ans += n - ok;
    }
  }
  cout << ans << endl;
}
0