1069 The Black Hole of Numbers
按题意模拟…
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
string N; cin >> N;
array<int, 4> a{}, b{};
for (int i = 0; i < N.size(); i++) {
a[i] = b[i] = N[i] - '0';
}
auto atoi = [](const array<int, 4>& nums) -> int {
int res = 0;
for (int i = 0; i < 4; i++) {
res += pow(10, 3 - i) * nums[i];
}
return res;
};
while (true) {
sort(a.begin(), a.end(), greater<>());
sort(b.begin(), b.end(), less<>());
int x = atoi(a), y = atoi(b);
int d = x - y;
printf("%04d - %04d = %04d\n", x, y, d);
if (d == 6174 || d == 0) break;
fill(a.begin(), a.end(), 0);
fill(b.begin(), b.end(), 0);
string s = to_string(d);
for (int i = 0; i < s.size(); i++) a[i] = b[i] = s[i] - '0';
}
return 0;
}
1070 Mooncake
按 每吨多少钱 排序
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int n, M; cin >> n >> M;
vector<pair<double, double>> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i].first;
}
for (int i = 0; i < n; i++) {
cin >> v[i].second;
}
sort(v.begin(), v.end(),
[](const pair<double, double>& A, const pair<double, double>& B) -> bool {
return A.second / A.first > B.second / B.first;
});
double res = 0;
for (int i = 0; i < n; i++) {
if (M < v[i].first) {
res += double(M) / v[i].first * v[i].second;
break;
} else {
M -= v[i].first;
res += v[i].second;
}
}
cout << fixed << setprecision(2) << res << endl;
return 0;
}
1071 Speech Patterns
cpp切片不太好用,可以写个py
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
getline(cin, s);
for (int i = 0; i < s.size(); i++) {
if (s[i] >= '0' && s[i] <= '9') {
continue;
} else if (s[i] >= 'a' && s[i] <= 'z') {
continue;
} else if (s[i] >= 'A' && s[i] <= 'Z') {
s[i] = tolower(s[i]);
} else s[i] = ' ';
}
stringstream ss(s);
map<string, int> mp;
int res = 0;
while (ss >> s) {
mp[s]++;
res = max(res, mp[s]);
}
for (const auto& [a, b] : mp) {
if (b == res) {
cout << a << ' ' << b << '\n';
break;
}
}
return 0;
}
1072 Gas Station
理解题意
A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible.
先按最短距离从大到小排(也可以理解,毕竟是加油站,远一点为了安全。),然后再按平均距离从小到大排,然后再按id从小到大排。
因为是一个点到所以house
距离之和,显然是单源最短路,dijkstra
就可以了。
G1
可以转为1001
,这样映射一下。输出的时候再转回来。
还有注意四舍五入…
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
static constexpr i64 inf = 0x3f3f3f3f3f;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
vector<vector<pair<int, i64>>> graph(1020);
int N, M, K, D; cin >> N >> M >> K >> D;
while (K--) {
string a, b, c; cin >> a >> b >> c;
int C = stoi(c), A, B;
if (a[0] == 'G') {
A = 1000 + stoi(a.substr(1));
} else A = stoi(a);
if (b[0] == 'G') {
B = 1000 + stoi(b.substr(1));
} else B = stoi(b);
graph[A].emplace_back(B, C);
graph[B].emplace_back(A, C);
}
auto dijkstra = [&](int start) -> vector<i64> {
vector<i64> dist(1020, inf);
dist[start] = 0;
priority_queue<pair<i64, int>, vector<pair<i64, int>>, greater<>> pq;
pq.push({0, start});
while (!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
if (d > dist[u]) continue;
for (const auto& [v, w] : graph[u]) {
if (d + w < dist[v]) {
dist[v] = d + w;
pq.push({dist[v], v});
}
}
}
return dist;
};
struct GAS {
int mix, id;
double ave;
explicit GAS(int _m, int _id, double _a): mix(_m), id(_id), ave(_a) {}
};
vector<GAS> res;
for (int i = 1001; i <= 1000 + M; i++) {
const auto& d = dijkstra(i);
i64 dis = 0, mi = inf;
for (int j = 1; j <= N; j++) {
if (d[j] > D) goto ne;
dis += d[j];
mi = min(d[j], mi);
}
res.emplace_back(mi, i, round(double(dis) / double(N) * 10) / 10);
ne: continue;
}
sort(res.begin(), res.end(), [](const GAS& A, const GAS& B) -> bool {
if (A.mix == B.mix) {
if (A.ave == B.ave) return A.id < B.id;
return A.ave < B.ave;
}
return A.mix > B.mix;
});
if (res.empty()) {
cout << "No Solution\n";
} else {
const auto& r = res[0];
cout << "G" + to_string(r.id - 1000) + "\n";
cout << fixed << setprecision(1) << double(r.mix);
cout << ' ' << fixed << setprecision(1) << r.ave;
}
return 0;
}