题目大意:
给n个长度为l[i]的围栏要求围成一个面积最大的矩形【围栏可不用】
解题思路:
状压可以组成的边长
然后暴搜
A c c e p t e d c o d e : Accepted\ code: Accepted code:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n, ans;
int l[16], f[10000], a[1<<17];
void dp(int sum, int x) {
if (sum & 1) return;
for (int i = 0; i < n; ++i)
if (x & (1<<i)) {
for (int j = sum; j >= l[i]; --j)
if (f[j-l[i]] != 0) f[j] = 1;
}
if (f[sum>>1] != 0) a[x] = 1;
}
void dfs(int x, int len1, int y, int len2, int deep) {
if (deep == n) {
if (a[x] && a[y]) ans = max(ans, (len1*len2) >> 2);
return;
}
dfs(x, len1, y, len2, deep + 1);
dfs(x + (1<<deep), len1 + l[deep], y, len2, deep + 1);
dfs(x, len1, y + (1<<deep), len2 + l[deep], deep + 1);
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i)
scanf("%d", &l[i]);
for (int i = 1; i < (1 << n); ++i) {
int sum = 0;
for (int j = 0; j < n; ++j)
if (i & (1<<j)) sum += l[j];
memset(f, 0, sizeof(f));
f[0] = 1;
dp(sum, i);
}
dfs(0, 0, 0, 0, 0);
if (ans != 0) printf("%d", ans);
else printf("No Solution");
return 0;
}