A - Five Antennas
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
typedef long long LL;
int a[5];
int main() {
for (int i = 0; i < 5; i++) cin >> a[i];
int k;
cin >> k;
for (int i = 0; i < 4; i++) {
for (int j = i + 1; j < 5; j++) {
if (a[j] - a[i] > k) {
cout << ":(" << endl;
return 0;
}
}
}
cout << "Yay!" << endl;
return 0;
}
B - Five Dishes
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
typedef long long LL;
int a[5];
int main() {
int res = 0x3f3f3f3f, sum = 0;
for (int i = 0; i < 5; i++) {
cin >> a[i];
sum += int(ceil(1.0 * a[i] / 10)) * 10;
}
for (int i = 0; i < 5; i++) {
res = min(res, sum - (int(ceil(1.0 * a[i] / 10)) * 10 - a[i]));
}
cout << res << endl;
return 0;
}
C - Five Transportations
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
typedef long long LL;
LL a[5];
int main() {
LL MIN = 0x3f3f3f3f3f3f3f3f;
int pos = -1;
LL n;
cin >> n;
for (int i = 0; i < 5; i++) {
cin >> a[i];
if (a[i] < MIN) {
MIN = a[i];
pos = i;
}
}
cout << 4 + LL(ceil(1.0*n / MIN)) << endl;
return 0;
}
D - Cake 123
给出三个数组,长度均为1e3
要求求出这三个数组任意组合中前k大的数(即前k大的ai+bj+ck),k小于3000
直接暴力会超时,所以可以先n方求出a与b的和,然后取前k大的数,与c求和,再取前k大的数,这样复杂度为n方
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7 + 5;
typedef long long LL;
int X, Y, Z, k;
LL x[N], y[N], z[N];
LL tmp[N], res[N];
int main() {
cin >> X >> Y >> Z >> k;
for (int i = 0; i < X; i++) cin >> x[i];
for (int i = 0; i < Y; i++) cin >> y[i];
for (int i = 0; i < Z; i++) cin >> z[i];
int cnt = 0;
for (int i = 0; i < X; i++) {
for (int j = 0; j < Y; j++) {
tmp[cnt++] = x[i] + y[j];
}
}
sort(tmp, tmp + cnt,greater<LL>());
cnt = 0;
for (int i = 0; i < min(k, X * Y); i++) {
for (int j = 0; j < Z; j++) {
res[cnt++] = tmp[i] + z[j];
}
}
sort(res, res + cnt,greater<LL>());
for (int i = 0; i < k; i++) {
cout << res[i] << endl;
}
return 0;
}