2023年10月13日
Acwing1049 大盗阿福
题目理解
代码实现
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n;
int f[N][2], w[N];
void solve()
{
memset(f, 0, sizeof f);
cin >> n;
for(int i = 1; i <= n; i++) cin >> w[i];
f[0][0] = 0, f[0][1] = -1;
for(int i = 1; i <= n; i++)
{
f[i][0] = max(f[i - 1][0], f[i - 1][1]);
f[i][1] = f[i - 1][0] + w[i];
}
cout << max(f[n][0], f[n][1]) << endl;
return;
}
int main()
{
int t;
cin >> t;
while(t--) solve();
}
Acwing1057 股票买卖Ⅳ
题目理解
这个图里,忘记减W[i]了,尴尬
代码实现
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, m;
int f[N][110][3], w[N];
void solve()
{
cin >> n >> m;
for(int i = 1; i <= n; i++) cin >> w[i];
// init
memset(f, -0x3f3f3f3f, sizeof f);
for(int i = 0; i <= n; i++) f[i][0][0] = 0;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
f[i][j][0] = max(f[i - 1][j][0], f[i - 1][j][1] + w[i]);
f[i][j][1] = max(f[i - 1][j][1], f[i - 1][j - 1][0] - w[i]);
}
int res = 0;
for(int i = 1; i <= m; i++)
res = max(res, f[n][i][0]);
cout << res << endl;
return;
}
int main()
{
int t = 1;
while(t--) solve();
return 0;
}
Acwing1057 股票买卖Ⅴ
题目理解
代码实现
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n;
int w[N], f[N][3];
int main()
{
cin >> n;
for(int i = 1; i <= n; i++) cin >> w[i];
f[0][2] = 0;
f[0][1] = f[0][0] = -0x3f3f3f3f;
for(int i = 1; i <= n; i++)
{
f[i][0] = max(f[i - 1][0], f[i - 1][2] - w[i]);
f[i][1] = f[i - 1][0] + w[i];
f[i][2] = max(f[i - 1][1], f[i - 1][2]);
}
cout << max(f[n][1], f[n][2]);
return 0;
}
标签:10,道题,int,namespace,1e5,250,include,第四十二
From: https://www.cnblogs.com/wxzcch/p/17765549.html