AtCoder Beginner Contest 194 Editorial
A - I Scream
根据 奶脂率 和 乳脂率 判断是何种冰淇淋
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int a, b;
cin >> a >> b;
a = a + b;
if (a >= 15 && b >= 8) cout << 1;
else if (a >= 10 && b >= 3)
cout << 2;
else if (a >= 3)
cout << 3;
else
cout << 4;
return 0;
}
B - Job Assignment
分开存储,循环判断。如果是同一个人完成任务则总时间累计
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
vector<int> a, b;
int n, ans = 1e9 + 5;
cin >> n;
a.resize(n);
b.resize(n);
for (int i = 0; i < n; ++i) {
cin >> a[i] >> b[i];
}
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) {
if (i == j) ans = min(ans, a[i] + b[j]);
else
ans = min(ans, max(a[i], b[j]));
}
cout << ans;
return 0;
}
C - Squared Error
公式推导
\[\begin{gather}
∑_{i = 2}^n∑_{j = 1}^{i - 1}(A_i - A_j)^2 \notag \\
={}& \frac1{2}(∑_{i = 1}^n∑_{j = 1}^{n}(A_i - A_j)^2) & \text{because ($A_i - A_i)^2 = 0$} \\
={}& \frac1{2}(∑_{i = 1}^n∑_{j = 1}^{n}(A_i^2 - A_j^2-2A_iA_j)) \\
={}& \frac1{2}(2N∑_{i = 1}^nA_i^2 - 2∑_{j = 1}^n(A_i∑_{j = 1}^nA_j)) \\
={}& N∑_{i = 1}^nA_i^2 - (∑_{i = 1}^nA_i^2),
\end{gather}
\]
注意用 long long
,溢出了WA了我3次....
using ll = long long;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int n;
cin >> n;
vector<ll> a(n);
ll sos = 0, sum = 0; // sos sum of squares
for (ll &x : a) cin >> x;
for (int i = 0; i < n; i++) {
sos += a[i] * a[i];
sum += a[i];
}
cout << n * sos - sum * sum << endl;
return 0;
}
D - Journey
按题意来即可,注意输出位数来保持精度
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int n;
cin >> n;
double cnt = 0.0;
for (int i = 1; i < n; ++i) {
cnt += 1.0 * n / (n - i);
}
cout << setprecision(20) << cnt << "\n";
return 0;
}
E - Mex Min
熟悉的定义...
using ll = long long;
ll n, m, a[5000005], s[5000005], mina, temp = 1;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (i <= m) s[a[i]]++;
}
for (int i = 0; i <= n; i++) {
if (s[i] == 0) {
mina = i;
break;
}
}
for (int i = m + 1; i <= n; i++) {
s[a[temp]]--;
s[a[temp + m]]++;
if (a[temp] < mina && s[a[temp]] == 0) {
// cout<<a[temp];
mina = min(mina, a[temp]);
}
temp++;
}
cout << mina;
return 0;
}
F - Digits Paradise in Hexadecimal
不会,先记录下dalao们的解法
__builtin_popcount()
用于计算一个 32 位无符号整数有多少个位为1
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
constexpr int MOD = 1e9 + 7;
long long dp[2][17];
string s;
int n, k, m, a, e;
int main() {
cin >> s >> k;
for (char c : s) {
a = '0' <= c && c <= '9' ? c - '0' : c - 'A' + 10;
rep(i, 16) dp[e][i + 1] =
(dp[e ^ 1][i] * (16 - i) + dp[e ^ 1][i + 1] * (i + 1)) % MOD;
dp[e][1] += m ? 15 : a - 1;
if (m) {
int t = __builtin_popcount(m), b1 = t - __builtin_popcount(m >> a);
dp[e][t + 1] += a - b1;
dp[e][t] += b1;
}
m |= 1 << a;
e ^= 1;
}
dp[e ^ 1][__builtin_popcount(m)] += 1;
cout << dp[e ^ 1][k] % MOD << endl;
}