故障机器人共有
故障机器人将依次与
题解
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
int n, x, k;
std::cin >> n >> x >> k;
std::vector<int> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
std::priority_queue<int, std::vector<int>, std::greater<int>> pq;
int i = 0;
for (i = 0; i < n; i++) {
if (pq.size() < k) { // k次以内先不减生命值
pq.push(a[i]);
continue;
}
pq.push(a[i]);
if (pq.top() >= x) {
break;
}
x -= pq.top(); // 减去最小的
pq.pop();
}
std::cout << i << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int t = 1;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}您可以完美预测某只股票未来 N 天的价格。每天你要么买入一股,要么卖出一股,要么什么都不做。起初你拥有零股,当你没有任何股票时,你不能卖出股票。在 N 天结束时,您希望再次拥有零股,但希望拥有尽可能多的资金。
CF865D Buy Low Sell High
题目描述
你可以完美地预测某只股票接下来
输入格式
第一行一个整数
接下来一行
输出格式
输出你第
样例解释
在股价为
Translated by uid
输入输出样例 #1
输入 #1
9
10 5 4 7 9 12 6 2 10输出 #1
20输入输出样例 #2
输入 #2
20
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4输出 #2
41题解
考虑
- 如果在第
天买入,第 天卖出,利润为 - 如果在第
天买入,第 天卖出,利润为 更大
每步贪心+返回的策略:如果当天有利可图,就卖出,同时买入当天的股票,后面再涨再卖出
从前往后枚举
如果满足
时间复杂度:
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
int n;
std::cin >> n;
std::vector<int> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
i64 ans = 0;
std::priority_queue<int, std::vector<int>, std::greater<int>> q;//从小到大
for (int i = 0; i < n; i++) {
if (!q.empty() && q.top() < a[i]) {
ans += (a[i] - q.top());
q.pop(); // 卖出a[i]
q.push(a[i]); // 买入a[i](为了反悔)
}
q.push(a[i]); // 买入a[i](为了交易)
}
std::cout << ans << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int t = 1;
// std::cin >> t;
while (t--) {
solve();
}
return 0;
}P2949 [USACO09OPEN] Work Scheduling G
题目描述
Farmer John has so very many jobs to do! In order to run the farm efficiently, he must make money on the jobs he does, each one of which takes just one time unit.
His work day starts at time 0 and has 1,000,000,000 time units (!). He currently can choose from any of N (1 <= N <= 100,000) jobs
conveniently numbered 1..N for work to do. It is possible but
extremely unlikely that he has time for all N jobs since he can only work on one job during any time unit and the deadlines tend to fall so that he can not perform all the tasks.
Job i has deadline D_i (1 <= D_i <= 1,000,000,000). If he finishes job i by then, he makes a profit of P_i (1 <= P_i <= 1,000,000,000).
What is the maximum total profit that FJ can earn from a given list of jobs and deadlines? The answer might not fit into a 32-bit integer.
输入格式
* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 contains two space-separated integers: D_i and P_i
输出格式
* Line 1: A single number on a line by itself that is the maximum possible profit FJ can earn.
输入输出样例 #1
输入 #1
3
2 10
1 5
1 7输出 #1
17说明/提示
Complete job 3 (1,7) at time 1 and complete job 1 (2,10) at time 2 to maximize the earnings (7 + 10 -> 17).
题解
先对
然后枚举每项工作,
如果当前工作有时间做,就先做了它(贪心),并把它的利润压入一个小根堆
如果当前工作与前项工作截止日期相同,就进一步比较,如果当前的工作利润大于堆顶工作,就放弃堆顶的那个工作(反悔),用做他的时间去做当前工作,利润更大
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
int n;
std::cin >> n;
std::vector<std::array<int, 2>> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i][0] >> a[i][1];
}
std::sort(a.begin(), a.end()); // 按截止时间排序
i64 ans = 0;
std::priority_queue<int, std::vector<int>, std::greater<int>> pq;
for (int i = 0; i < n; i++) {
if (a[i][0] == pq.size()) { // 截止时间相同
if (a[i][1] > pq.top()) { // 利润更大
ans -= pq.top(); // 反悔
pq.pop();
pq.push(a[i][1]);
ans += a[i][1]; // 贪心
}
} else {
pq.push(a[i][1]);
ans += a[i][1]; // 贪心
}
}
std::cout << ans << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int t = 1;
// std::cin >> t;
while (t--) {
solve();
}
return 0;
}P3045 [USACO12FEB] Cow Coupons G
题目背景
Subtask 0 为原数据,Subtask 1,2 为 hack 数据。
题目描述
Farmer John needs new cows! There are N cows for sale (1 <= N <= 50,000), and FJ has to spend no more than his budget of M units of money (1 <= M <= 10^14). Cow i costs P_i money (1 <= P_i <= 10^9), but FJ has K coupons (1 <= K <= N), and when he uses a coupon on cow i, the cow costs C_i instead (1 <= C_i <= P_i). FJ can only use one coupon per cow, of course.
What is the maximum number of cows FJ can afford?
FJ 准备买一些新奶牛。市场上有
输入格式
* Line 1: Three space-separated integers: N, K, and M.
* Lines 2..N+1: Line i+1 contains two integers: P_i and C_i.
输出格式
* Line 1: A single integer, the maximum number of cows FJ can afford.
输入输出样例 #1
输入 #1
4 1 7
3 2
2 2
8 1
4 3输出 #1
3说明/提示
FJ has 4 cows, 1 coupon, and a budget of 7.
FJ uses the coupon on cow 3 and buys cows 1, 2, and 3, for a total cost of 3 + 2 + 1 = 6.
Solve
我们先选取
当前考虑第
如果要反悔,那么用优惠券买
意思是:
整理一下可得
所以只要在已经使用优惠券的物品里面,存在一个
用优先队列维护最小值
#include <bits/stdc++.h>
using i64 = long long;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
i64 n, k, m;
std::cin >> n >> k >> m;
std::vector<i64> p(n), c(n);
std::priority_queue<std::pair<i64, i64>, std::vector<std::pair<i64, i64>>, std::greater<std::pair<i64 ,i64>>> P, C;
for (int i = 0; i < n; i++) {
std::cin >> p[i] >> c[i];
P.push({p[i], i});
C.push({c[i], i});
}
std::vector<bool> vis(n + 1);
std::priority_queue<i64, std::vector<i64>, std::greater<i64>> pq;
for (int i = 0; i < k; i++) { // 用k张优惠券直接买 c 最小的
pq.push(0);
}
int ans = 0;
while (!P.empty()) {
auto [a, b] = P.top();
auto [c1, d] = C.top();
if (vis[b]) {
P.pop();
continue;
}
if (vis[d]) {
C.pop();
continue;
}
if (pq.top() > a - c1) { // 如果 省的钱大于直接买 那就不用
m -= a;
P.pop();
vis[b] = true;
} else {
m -= c1 + pq.top(); // -pq.top 相当于之前原价买 -c1 用券买当前的物品
pq.pop();
C.pop();
vis[d] = true;
pq.push(p[d] - c[d]); // pq 存的是 省了多少价值
}
if (m >= 0) {
ans++;
} else {
break;
}
}
std::cout << ans << "\n";
return 0;
}