# 提高组—2023年2.7日打卡

2023-02-07 21:51:34

1.

``````#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int> PII;
#define x first
#define y second
#define ms(a,s) memset(a, s, sizeof a);
#define gcd(a,b) __gcd(a,b)
#define pb(a) push_back(a)
const int mod = 1e6+7;
const int inf = 0x3f3f3f3f;
const int N = 11;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int a[N][N];
int f[N][N][N][N];
void solve() {
int n;
cin >> n;
while(1) {
int x, y, z;
cin >> x >> y >> z;
if(x == 0 && y == 0 && z == 0) break;
a[x][y] = z;
}
for(int i = 1; i <= n; i ++ ) {
for(int j = 1; j <= n; j ++ ) {
for(int i2 = 1; i2 <= n; i2 ++ ) {
for(int j2 = 1; j2 <= n; j2 ++ ) {
int &ans = f[i][j][i2][j2];
ans = max(ans, f[i - 1][j][i2][j2 - 1]);
ans = max(ans, f[i - 1][j][i2 - 1][j2]);
ans = max(ans, f[i][j - 1][i2 - 1][j2]);
ans = max(ans, f[i][j - 1][i2][j2 - 1]);
if(i == i2 && j == j2) ans += a[i][j];
else ans += a[i][j] + a[i2][j2];
}
}
}
}
cout << f[n][n][n][n];
}
int main()
{
int t = 1;

//	cin >> t;

while(t --) {
solve();
}

return 0;
}
``````

2. 区间dp

``````#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int> PII;
#define x first
#define y second
#define ms(a,s) memset(a, s, sizeof a);
#define gcd(a,b) __gcd(a,b)
#define pb(a) push_back(a)
const int mod = 1e6+7;
const int inf = 0x3f3f3f3f;
const int N = 1010;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int a[N];
int f[N][N];
void solve() {
int n;
cin >> n;
for(int i = 1; i <= n; i ++ ) {
cin >> a[i];
a[i + n] = a[i];
}
int num = 0;
for(int l = 1; l <= n; l ++ ) {
for(int i = 1; i + l <= 2 * n; i ++ ) {
int j = i + l;
for(int k = i + 1; k < j; k ++ ) { // 单个没有能量所以两个为一个切点
f[i][j] = max(f[i][j], f[i][k] + f[k][j] + a[i] * a[k] * a[j]);
num = max(num, f[i][j]);
}
}
}
cout << num;
}
int main()
{
int t = 1;

//	cin >> t;

while(t --) {
solve();
}

return 0;
}``````

...全文
28 回复 打赏 收藏 转发到动态 举报

50,819

• 近7日
• 近30日
• 至今

刷题！