AT_otemae2019_b 駒 (Pieces) 题解
题目大意
题目要求通过一定的操作将棋子尽可能集中在某个格子上。需要通过指定的操作,选择合适的棋子并移动,使格子上的棋子数达到最大。
思路
对于每个可能的目标格子 ,遍历整个棋盘,计算可以集中到该格子的最大棋子数。
考虑用一个集合来维护能够合法移动到目标位置 不同类型操作。
如果位置 的棋子的初始位置就是目标,那么直接增加计数器。否则,需要检查当前与目标位置之间的整数差值是否在移动范围内。如果符合,就将差值加入集合中。
对于模拟测试不同目标位置 ,通过将当前可直接或经过合法操作能集中于目的地后可能形成的新总和与之前记录下来的最大值进行比较,更新整个过程中可得出的最大化结果。
代码
#include<bits/stdc++.h>
using namespace std;
int m, n, k;
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> m >> n >> k;
vector<int> x(n);
for (auto &y: x) cin >> y, y--;
int ans = 0;
for (int i = 0; i < m; i++) {
set<int> se;
int cnt = 0;
for (auto y: x) {
if (i == y) cnt++;
else if (abs(i - y) <= k) se.insert(abs(i - y));
}
ans = max(ans, cnt + int(se.size()));
}
cout << ans << endl;
return 0;
}