【HT-110-Div.4】核桃新手组周赛
C:P11972 摊位编号
思路
给定
数据范围
核心代码
int ans = 1e9;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
int diff = 0;
for (int k = 0; k < m; ++k)
diff += abs(s[i][k] - s[j][k]);
if (diff < ans) ans = diff;
}
}
cout << ans;
完整代码
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005;
char s[MAXN][55];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; ++i)
cin >> s[i];
int ans = 1e9;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
int diff = 0;
for (int k = 0; k < m; ++k)
diff += abs(s[i][k] - s[j][k]);
if (diff < ans) ans = diff;
}
}
cout << ans << '\n';
return 0;
}
D:P11881 边境巡逻
思路
正方形边长为
- 若在底边
y=0 :pos = x - 若在右边
x=n :pos = n + y - 若在顶边
y=n :pos = 2n + (n - x) - 若在左边
x=0 :pos = 3n + (n - y)
计算两点的
核心代码
int get_pos(int n, int x, int y) {
if (y == 0) return x;
if (x == n) return n + y;
if (y == n) return 2 * n + (n - x);
return 3 * n + (n - y);
}
int d = abs(pos1 - pos2);
int ans = min(d, 4 * n - d);
cout << ans;
完整代码
#include <bits/stdc++.h>
using namespace std;
int get_pos(int n, int x, int y) {
if (y == 0) return x;
if (x == n) return n + y;
if (y == n) return 2 * n + (n - x);
return 3 * n + (n - y);
}
int main() {
int n, x1, y1, x2, y2;
cin >> n >> x1 >> y1 >> x2 >> y2;
int pos1 = get_pos(n, x1, y1);
int pos2 = get_pos(n, x2, y2);
int d = abs(pos1 - pos2);
int ans = min(d, 4 * n - d);
cout << ans << '\n';
return 0;
}