結果
| 問題 |
No.655 E869120 and Good Triangles
|
| コンテスト | |
| ユーザー |
vwxyz
|
| 提出日時 | 2024-05-03 15:55:18 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 901 ms / 2,500 ms |
| コード長 | 3,116 bytes |
| コンパイル時間 | 1,330 ms |
| コンパイル使用メモリ | 104,252 KB |
| 実行使用メモリ | 449,832 KB |
| 最終ジャッジ日時 | 2024-11-24 18:07:08 |
| 合計ジャッジ時間 | 17,996 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 30 |
ソースコード
#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;
}
vwxyz