P1541 乌龟棋

时间:2022-12-28 17:01:02

30分做法,暴力枚举:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 400;
int n, m;
int a[maxn], b[maxn]; //a:格子,b:卡牌
void read() {
cin >> n >> m;
for(int i = 0; i < n; i++) cin >> a[i];
for(int i = 0; i < m; i++) cin >> b[i];
}
int main() {
read();
sort(b, b+m);
int ans = 0, tmp, pos, i;
do{
i = 0;
tmp = 0;
pos = 0;
while(i <= m && pos <= n) {
tmp += a[pos];
pos += b[i++];
}
ans = max(ans, tmp);
}while(next_permutation(b, b+m));
cout << ans;
}

100分做法:dp,f[i][j][k][l]为使用i张1,j张2,k张3,l张4;

开始没有想到这个办法是因为没有注意到条件;

上代码

#include <bits/stdc++.h>
using namespace std;
const int maxn = 355;
const int maxm = 125;
const int maa = 41;
int n, m,one = 0, two = 0, three = 0, four = 0;
int a[maxn], b[maxm]; //a:格子,b:卡牌
void read() {
cin >> n >> m;
for(int i = 0; i < n; i++) cin >> a[i];
for(int i = 0; i < m; i++) {cin >> b[i];
if(b[i] == 1) one++;
else if(b[i] == 2) two++;
else if(b[i] == 3) three++;
else four++;
}
} int main() {
read();
int f[maa][maa][maa][maa];
f[0][0][0][0] = a[0];
for(int i = 0; i <= one; i++) {
for(int j = 0; j <= two; j++) {
for(int k = 0; k <= three; k++) {
for(int l = 0; l <= four; l++) {
int maxx = 0;
if(i > 0)maxx = max(maxx, f[i-1][j][k][l]);
if(j > 0)maxx = max(maxx, f[i][j-1][k][l]);
if(k > 0)maxx = max(maxx, f[i][j][k-1][l]);
if(l > 0)maxx = max(maxx, f[i][j][k][l-1]);
f[i][j][k][l] = maxx + a[i*1+j*2+k*3+l*4];
}
}
}
}
cout << f[one][two][three][four];
}