#include <bits/stdc++.h>
using namespace std;
int main(void)
{
size_t d, n;
vector<vector<size_t>> v(179, vector<size_t>(179));
map<size_t, size_t> cnt;
cin >> d >> n;
for (size_t i = 0, x, y, t; i < n; ++i)
{
cin >> x >> y >> t;
v[x + 20][y + 20] = t;
}
auto count = [](const vector<vector<size_t>> &v, const size_t &x, const size_t &y, const size_t &d) {
size_t count = 0;
for (size_t i = x - d; i <= x + d; ++i)
for (size_t j = y - d; j <= y + d; ++j)
try
{
count += v.at(i).at(j);
}
catch (const std::exception)
{
}
return count;
};
for (size_t i = 20; i < 149; i++)
for (size_t j = 20; j < 149; j++)
cnt[count(v, i, j, d)]++;
cout << cnt.rbegin()->second << ' ' << cnt.rbegin()->first << '\n';
// for_each(cnt.begin(), cnt.end(), [](auto &x) { cout << x.second << ' ' << x.first << '\n'; });
return 0;
}
这段代码是可以通过的 但是,注意,当不填充时,即只开 129 x 129的数组,无论是判定范围还是异常处理,都会出现部分求和为0的情况,也就是答案为32 10 而不是 38 10 即
#include <bits/stdc++.h>
using namespace std;
int main(void)
{
size_t d, n;
vector<vector<size_t>> v(129, vector<size_t>(129)); // 范围
map<size_t, size_t> cnt;
cin >> d >> n;
for (size_t i = 0, x, y, t; i < n; ++i)
{
cin >> x >> y >> t;
v[x][y] = t; // 不做偏移
}
auto count = [](const vector<vector<size_t>> &v, const size_t &x, const size_t &y, const size_t &d) {
size_t count = 0;
for (size_t i = x - d; i <= x + d; ++i)
for (size_t j = y - d; j <= y + d; ++j)
try
{
count += v.at(i).at(j);
}
catch (const std::exception)
{
}
return count;
};
for (size_t i = 0; i < v.size(); i++)
for (size_t j = 0; j < v[i].size(); j++) // 不做偏移
cnt[count(v, i, j, d)]++;
cout << cnt.rbegin()->second << ' ' << cnt.rbegin()->first << '\n';
// for_each(cnt.begin(), cnt.end(), [](auto &x) { cout << x.second << ' ' << x.first << '\n'; });
return 0;
}
测试用例 1 11 0 0 10 64 64 10 0 124 5 4 128 5 3 128 1 0 128 4 128 128 10 127 0 10 128 64 10 64 128 9 30 20 10 正确输出 38 10