結果

問題 No.655 E869120 and Good Triangles
ユーザー pekempeypekempey
提出日時 2018-02-24 00:00:43
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,580 ms / 2,500 ms
コード長 2,759 bytes
コンパイル時間 1,084 ms
コンパイル使用メモリ 86,612 KB
実行使用メモリ 316,672 KB
最終ジャッジ日時 2024-04-18 09:24:38
合計ジャッジ時間 30,051 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 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 1,527 ms
316,416 KB
testcase_11 AC 1,419 ms
316,556 KB
testcase_12 AC 1,443 ms
316,544 KB
testcase_13 AC 1,484 ms
316,544 KB
testcase_14 AC 1,440 ms
316,544 KB
testcase_15 AC 1,486 ms
316,484 KB
testcase_16 AC 1,492 ms
316,672 KB
testcase_17 AC 1,580 ms
316,548 KB
testcase_18 AC 1,446 ms
316,416 KB
testcase_19 AC 1,450 ms
316,544 KB
testcase_20 AC 1,487 ms
316,492 KB
testcase_21 AC 1,516 ms
316,416 KB
testcase_22 AC 1,476 ms
316,416 KB
testcase_23 AC 1,460 ms
316,416 KB
testcase_24 AC 1,234 ms
316,288 KB
testcase_25 AC 1,226 ms
316,288 KB
testcase_26 AC 1,226 ms
316,416 KB
testcase_27 AC 1,321 ms
316,288 KB
testcase_28 AC 1,222 ms
316,416 KB
testcase_29 AC 1,296 ms
316,288 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 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 + 1][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