腾讯机试题 AcWing 603 打怪兽

2023-05-24,,

题目链接:https://www.acwing.com/problem/content/605/

题目大意:

  略

分析:

  用dp[i][j]表示用j元钱能在前i只怪兽上所能贿赂到的最大武力值。

  有一种情况就是打到第i只怪兽所需的最低花费大于j,那么令dp[i][j] = -1。

  那么dp[i + 1][j],也就是同样用j元钱能在前i + 1只怪兽上所能贿赂到的最大武力值是多少呢?有3种情况:

    1:dp[i][j] = -1,显然dp[i + 1][j] = -1。

    2:dp[i][j] < d[i + 1],也就是用j元钱在前i只怪兽上所能贿赂到的最大武力值都没有第i+1只怪兽的武力值大,这个时候必须腾出p[i + 1]元来贿赂第i + 1只怪兽,此时又有3种小情况:

      (1):j < p[i + 1],这就没的说了,钱不够,直接dp[i + 1][j] = -1。

      (2):dp[i][j - p[i + 1]] == -1,这说明腾不出钱来,于是dp[i + 1][j] = -1。

      (3):dp[i][j - p[i + 1]] != -1,那么dp[i + 1][j] = dp[i][j - p[i + 1]] + d[i + 1],不存在选择,必须贿赂。

    3:dp[i][j] >= d[i + 1],在这种情况下可以选择贿赂,也可以选择不贿赂,两种情况取最大即可,dp[i + 1][j] = max(dp[i][j], dp[i][j - p[i + 1]] + d[i + 1])。

  递推关系有了,现在需要第0列的初始值:除了第0行,其他全部为-1;和第0行的初始值:全部为0。

  那dp数组算出来了有啥用呢?

  假设你打到第i个怪的时候,武力值为D,这时D < d[i],那你就在dp数组第i行找第一个使得dp[i][j] >= d[i]的j,这个j一定是打前i个怪最优的,因为花费比j小的金钱过不了这个boss,如果这个j能一直保持到游戏结束,那这个j就是总体最优的。

代码如下:

 #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std; #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
#define EL() printf("\n") #define SORT(c, s, t) sort(c + s, c + t + 1) #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define pii pair<int,int>
#define piii pair<pair<int,int>,int>
#define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef set< int > SI;
typedef vector< int > VI;
const double EPS = 1e-;
const int inf = 1e9 + ;
const LL mod = 1e9 + ;
const int maxN = 1e5 + ;
const LL ONE = ; //! n 表示怪兽的数量
//! d[i] 表示第i只怪兽的武力值
//! p[i] 表示收买第i只怪兽所需的金币数
//! ans 最小花费
int n, p[], ans;
LL d[];
//! dp[i][j] 表示用j元钱能在前i只怪兽上所能贿赂到的最大武力值
LL dp[][]; int main(){
INIT();
cin >> n;
For(i, , n) cin >> d[i];
For(i, , n) cin >> p[i]; For(i, , n) dp[i][] = -; For(j, , * n) {
For(i, , n) {
if(dp[i - ][j] == -) dp[i][j] = -;
else if(dp[i - ][j] < d[i]) {
if(j < p[i]) dp[i][j] = -;
else if(dp[i - ][j - p[i]] == -) dp[i][j] = -;
else dp[i][j] = dp[i - ][j - p[i]] + d[i];
}
else dp[i][j] = max(dp[i - ][j], dp[i - ][j - p[i]] + d[i]);
}
} // maxD 最大武力值
// maxDi 最大武力值下标
LL maxD = -, maxDi;
For(i, , n) {
if(d[i] > maxD) {
maxD = d[i];
maxDi = i;
}
} ans = lower_bound(dp[maxDi], dp[maxDi] + * n, maxD) - dp[maxDi]; cout << ans << endl;
return ;
}

腾讯试题 AcWing 603 打怪兽的相关教程结束。

《腾讯机试题 AcWing 603 打怪兽.doc》

下载本文的Word格式文档,以方便收藏与打印。