0924 - 模拟赛改题
0924 - 模拟赛改题
T380094 零用钱
思路
由于题目要求的是最大值,所以我们肯定尽可能的去用
59 59 59 59 -237
考虑下一个会是什么:先算前
59 59 59 59 -237 59
依此类推,我们会发现,其实算出了前
code
#include <iostream>
using namespace std;
using ll = long long;
const int MaxN = 10010;
ll a[MaxN], t, n, k, s, d;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
for (cin >> t; t; t--) {
cin >> n >> k >> s >> d, d *= -1;
ll sum = 0;
for (int i = 1; i <= k; i++) {
if (sum + s + d * (k - i) < 0) {
a[i] = s;
} else {
a[i] = d;
}
sum += a[i];
}
sum *= n / k;
for (int i = 1; i <= n % k; i++) {
sum += a[i];
}
if (sum >= 0) {
cout << sum << '\n';
} else {
cout << "Deficit" << '\n';
}
}
return 0;
}
时空复杂度
时间:计算前
T380095 矩形求和
思路
二位前缀和板子
时空复杂度
时间:
T380096 射击
思路
可以将答案看作为一个整体
code
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
const int MaxN = 1010;
int a[MaxN], n, m, ans;
set<int> b;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
b.insert(a[i] + a[j]);
}
}
for (auto i : b) {
auto p = b.upper_bound(m - i);
if (p != b.begin()) {
p--;
ans = max(ans, (*p) + i);
}
}
cout << ans << endl;
return 0;
}
时空复杂度
时间:预处理任意一组:
知识点
折半搜+二分
P1631 序列合并
思路
思路一
题目要求的是二维的,太麻烦,所以我们可以将其用一维划分,将每一组都变成线性的,那线性的就很好求了,直接排序然后从前往后算即可,那么就可以将这
code1
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
const int MaxN = 100010;
struct S {
int i, j, t;
bool operator<(const S &j) const {
return t > j.t;
}
};
int a[MaxN], b[MaxN], n;
priority_queue<S> q;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
cin >> b[i];
}
sort(a + 1, a + n + 1);
sort(b + 1, b + n + 1);
for (int i = 1; i <= n; i++) {
q.push({1, i, a[1] + b[i]});
}
for (int i = 1; i <= n; i++) {
S u = q.top();
q.pop();
cout << u.t << " ";
q.push({u.i + 1, u.j, a[u.i + 1] + b[u.j]});
}
return 0;
}
时空复杂度
时间:排序:
思路2
如果是两个已经排好序的数列,那么当前最小值肯定是两个头,那下一个最小值呢?可以是之前就已经加入的值,也可以是两个数列中删掉一个头的情况,处理一下即可。
时空复杂度
时间: