P1284 三角形牧场
链接
https://www.luogu.com.cn/problem/P1284
思路
一个dp。dp状态:前k根,两边长为i,j的时候的可行性。
dp[k][i][j] = dp[k-1][i-a[k]][j] || dp[k-1][i][j-a[k]]|| f[k-1][i][j];
如果可行并且满足构建三角形就直接取面积的max
代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define tin long long
#define itn long long
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
void solve();
const int N = 42;
int a[N];
int n;
int sumint;
int dp[2][N * N / 2][N * N / 2];
bool check(int a, int b, int c)
{
if (a + b <= c)return false;
if (b+c <= a)return false;
if (a + c <= b)return false;
return true;
}
double cal(int a, int b, int c)
{
double p = ((double)a + b + c) / 2;
return sqrt(p * (p - a) * (p - b) * (p - c));
}
void solve()
{
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i]; sumint += a[i];
}
int now = 0, old = 1;
dp[now][0][0] = 1;
for (int k = 1; k <= n; k++)
{
swap(now, old);
for(int i=sumint/2;i>=0;i--)
for (itn j = 0; j <= sumint / 2;j++)
{
dp[now][i][j] = dp[old][i][j];
if (i >= a[k] and dp[old][i - a[k]][j])dp[now][i][j] = 1;
if (j >= a[k] and dp[old][i][j - a[k]])dp[now][i][j] = 1;
}
}
double ans = -1;
for(int i=sumint/2;i-->=0;)
for (int j = sumint / 2; j-- >= 0;)
{
if (check(i, j, sumint - i - j) and dp[now][i][j])
ans = max(ans, cal(i, j, sumint - i - j));
}
if (ans == (double)-1) { cout << -1; return; }
ans *= 100;
cout << (int)ans;
}
signed main()
{
IOS;
solve();
return 0;
}

浙公网安备 33010602011771号