标签:上下左右 tips char cout beta seq span 链接 test
比赛链接:https://codeforces.com/contest/1390
给出一个只含有 $1,2,3$ 的数组,问使所有元素相同至少要替换多少元素。
统计数组中出现次数最多的元素即可。
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int mx = 0; map<int, int> cnt; for (int i = 0; i < n; ++i) { int x; cin >> x; mx = max(mx, ++cnt[x]); } cout << n - mx << "\n"; }
给出一个只含有 ‘*‘,‘.‘ 的 $n \times m$ 的方阵,问方阵中有多少两条直角边与方阵平行的由 ‘*‘ 组成三个顶点的直角三角形。
预处理出每个点上下左右 ‘*‘ 的个数,然后枚举四个方向的直角三角形即可。
== 的优先级要先于 = ,所以如果赋值中有 == ,应将其括起来。
#include <bits/stdc++.h> using ll = long long; using namespace std; const int N = 1005; int n, m; char MP[N][N]; ll u[N][N], d[N][N], l[N][N], r[N][N]; int main() { cin >> n >> m; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { cin >> MP[i][j]; } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { l[i][j] = l[i][j - 1] + (MP[i][j] == ‘*‘); } for (int j = m; j >= 1; --j) { r[i][j] = r[i][j + 1] + (MP[i][j] == ‘*‘); } } for (int i = 1; i <= m; ++i) { for (int j = 1; j <= n; ++j) { u[j][i] = u[j - 1][i] + (MP[j][i] == ‘*‘); } for (int j = n; j >= 1; --j) { d[j][i] = d[j + 1][i] + (MP[j][i] == ‘*‘); } } ll ans = 0; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (MP[i][j] == ‘*‘) { --u[i][j], --d[i][j], --l[i][j], --r[i][j]; //减去自身 ans += u[i][j] * l[i][j]; ans += u[i][j] * r[i][j]; ans += d[i][j] * l[i][j]; ans += d[i][j] * r[i][j]; } } } cout << ans << "\n"; }
今晚的 cf 明天再补,早些休息吧
标签:上下左右 tips char cout beta seq span 链接 test
原文地址:https://www.cnblogs.com/Kanoon/p/13374153.html