AtCoder Beginner Contest 330
A - Counting Passes
#include <bits/stdc++.h>
using namespace std;
#define int long long
using vi = vector<int>;
using i32 = int32_t;
using pii = pair<int, int>;
#define mp make_pair
const int inf = 1e9;
i32 main() {
int n , l;
cin >> n >> l;
int res = 0;
for( int i = 1 , x ; i <= n ; i ++ )
cin >> x , res += x >= l;
cout << res << "\n";
return 0;
}
B - Minimize Abs 1
#include <bits/stdc++.h>
using namespace std;
#define int long long
using vi = vector<int>;
using i32 = int32_t;
using pii = pair<int, int>;
#define mp make_pair
const int inf = 1e9;
i32 main() {
int n, l, r;
cin >> n >> l >> r;
for (int a, i = 1; i <= n; i++) {
cin >> a;
if (l <= a and a <= r) cout << a << " ";
else if( a > r ) cout << r << " ";
else cout << l << " ";
}
cout << "\n";
return 0;
}
C - Minimize Abs 2
预处理所有的平方数,然后枚举加二分
#include <bits/stdc++.h>
using namespace std;
#define int long long
using vi = vector<int>;
using i32 = int32_t;
using pii = pair<int, int>;
#define mp make_pair
const int inf = 1e9, INF = 1e18;
i32 main() {
int n;
cin >> n;
vi a;
for (int i = 0, x;; i++) {
x = i * i, a.push_back(x);
if (x > n) break;
}
int res = INF;
for (int y; const auto &x: a) {
if (n - x <= 0) res = min(res, x - n);
else {
y = lower_bound(a.begin(), a.end(), n - x) - a.begin();
res = min(res, abs(x + a[y] - n));
if (y > 1 ) res = min(res, abs(x + a[y - 1] - n));
}
}
cout << res << "\n";
return 0;
}
D - Counting Ls
直接统计每行每列o
的数量,然后枚举一下中心点,计算出两边两个的点的选择方式
#include <bits/stdc++.h>
using namespace std;
#define int long long
using vi = vector<int>;
using i32 = int32_t;
using pii = pair<int, int>;
#define mp make_pair
const int inf = 1e9, INF = 1e18;
i32 main() {
int n;
cin >> n;
vector<string> g(n);
for (auto &i: g) cin >> i;
vi cntX(n), cntY(n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if (g[i][j] == 'x') continue;
cntX[i]++, cntY[j]++;
}
int res = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if (g[i][j] == 'o') res += (cntX[i] - 1) * (cntY[j] - 1);
}
cout << res << "\n";
return 0;
}
E - Mex and Update
用set
维护当前序列中哪些数字没有即可,注意的是所有大于n
的数字都可以等效为n
#include <bits/stdc++.h>
using namespace std;
#define int long long
using vi = vector<int>;
using i32 = int32_t;
using pii = pair<int, int>;
#define mp make_pair
const int inf = 1e9, INF = 1e18;
i32 main() {
int n, q;
cin >> n >> q;
vector<int> cnt(n + 1), a(n);
for (auto &i: a)
cin >> i, i = min(i, n), cnt[i]++;
set<int> mex;
for (int i = 0; i <= n; i++)
if (cnt[i] == 0) mex.insert(i);
for (int i, x; q; q--) {
cin >> i >> x, i--, x = min(x, n);
cnt[a[i]]--;
if (cnt[a[i]] == 0) mex.insert(a[i]);
cnt[x]++;
if (cnt[x] == 1) mex.erase(x);
a[i] = x;
cout << *mex.begin() << "\n";
}
return 0;
}