1、题目名称:圆小艺
最近小艺酱渐渐变成了一个圆滑的形状-球!! 小艺酱开始变得喜欢上球! 小艺酱得到n个同心圆。 小艺酱对着n个同心圆 进行染色。 相邻的圆范围内不能有相同的颜色。相隔一层的圆颜色相同。 小艺酱想知道两种颜色中最外层圆的那种颜色总 共染了多少?
题解:把 0 放进去之后从外往里减计算就可以。
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> r(n);
for (auto& x : r) {
cin >> x;
}
r.push_back(0);
sort(r.begin(), r.end());
double ans = 0;
for (int i = n; i >= 1; i -= 2) {
ans += r[i] * r[i] - r[i - 1] * r[i - 1];
}
cout << fixed << setprecision(3) << 3.1415926535 * ans << '\n';
return 0;
}
2、题目名称:近视的小张
题解:去除掉不可能被看到的(可以记录前缀最大值然后标记,也可以维护一个第一个元素开始的单调递增的序列),然后二分查找即可。
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int m, n;
cin >> m >> n;
vector<pair<int, int>> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i].first;
}
for (int i = 0; i < n; ++i) {
cin >> a[i].second;
}
sort(a.begin(), a.end(), [&](const pair<int, int>& a, const pair<int, int>& b) {
return a.second < b.second;
});
int id = n - 1;
vector<bool> ok(n, true);
int mx = a[0].first;
for (int i = 1; i < n; ++i) {
if (a[i].first <= mx) {
ok[i] = false;
} else {
mx = a[i].first;
}
}
n = static_cast<int>(a.size());
vector<int> b;
for (int i = 0; i < n; ++i) {
if (ok[i]) {
b.push_back(a[i].second);
}
}
for (int i = 0; i < m; ++i) {
int A;
cin >> A;
int id = upper_bound(b.begin(), b.end(), A) - b.begin();
if (id == 0) {
cout << -1 << '\n';
} else {
cout << b[id - 1] << '\n';
}
}
return 0;
}
3、题目名称:小股炒股
已知n天后的股票行情,现在已有的本金是m, 规定只能入手一次股票和抛售一次股票。 最大收益(含本金)是?
题解:每个位置计算取max即可。
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vector<int> suf(n);
suf[n - 1] = a[n - 1];
for (int i = n - 2; i >= 0; --i) {
suf[i] = max(a[i], suf[i + 1]);
}
int ans = m;
for (int i = 0; i < n - 1; ++i) {
if (m >= a[i]) {
ans = max(ans, m - (m / a[i] * a[i]) + (m / a[i] * suf[i + 1]));
}
}
cout << ans << '\n';
return 0;
}
4、题目名称:买铅笔
P老师需要去商店买n支铅笔作为小朋友们参加编程比赛的礼物。她发现商店一共有 3 种包装的铅笔,不同包装内的铅笔数 量有可能不同,价格也有可能不同。为了公平起 见,P老师决定只买同一种包装的铅笔。 商店不允许将铅笔的包装拆开, 因此P老师可能需要购买超过 n 支铅笔才够给小朋 友们发礼物。 现在P老师想知道,在商店每种包装的数量都足够的情况 下,要买够至少 n 支铅笔最少需要花费多少钱。
题解:每个计算然后取最小值。
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<pair<int, int>> a(3);
for (int i = 0; i < 3; ++i) {
cin >> a[i].first >> a[i].second;
}
vector<int> ans(3);
for (int i = 0; i < 3; ++i) {
ans[i] = (n + a[i].first - 1) / a[i].first * a[i].second;
}
cout << *min_element(ans.begin(), ans.end()) << '\n';
return 0;
}