(ps.学习打卡记录)
其实是在洛谷写的题,AcWing的还没做
1 . 花生采摘
题意简述:给一张花生的分布图,从底部任意位置出发,每次都去花生最多的地方,问体力耗尽前能采多少。
因为固定了每次都去最多的地方,所以不存在 d p dp dp 的问题,直接照着题意模拟即可。
#include <bits/stdc++.h>
using namespace std;
struct node {
int val, x, y;
explicit node(int a = 0, int b = 0, int c = 0) : val(a), x(b), y(c) {
}
bool operator<(const node &t1) const {
if (val != t1.val) return val < t1.val;
if (x != t1.x) return x > t1.x;
return y > t1.y;
}
};
static priority_queue<node> point;
int n, m, k, ans;
bool check(node cur, node next) {
int take = abs(cur.x - next.x) + abs(cur.y - next.y);
return (k - take - 1) >= next.x;
}
int main(){
cin >> n >> m >> k;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
int kw;
cin >> kw;
if (kw) point.push(node(kw, i, j));
}
}
node cur = point.top();
point.pop();
bool flag = true;
if (cur.x * 2 + 1 <= k) {
ans += cur.val;
k -= cur.x;
k--;
} else flag = false;
while (!point.empty() && flag) {
node next = point.top();
if (check(cur, next)) {
k -= (abs(cur.x - next.x) + abs(cur.y - next.y) + 1);
ans += next.val;
cur = next;
point.pop();
} else
flag = false;
}
cout << ans << endl;
}
2 . 数的划分
题意简述:将一个正整数分成几个数的和,问有多少中方案。
递归枚举组合即可,需要注意的是递归剪枝,有些肯定没有合法方案的方向不用递归。每一层枚举到总数减去当前和就行。