Hdu 5135 status compression dp
The question is to give the most 12 sides. You ask the maximum area of a triangle that can be formed (multiple triangles are allowed). Typical state compression (up to 12 sides)
Ideas:
Enumerate the area of all possible triangles in a brute force manner, and then compress the area based on the state to form up to n/3 triangles. Each triangle is recorded on three sides or (operation) merge with area and then determine (based on and calculate ).
#include
#include
#include
#include
using namespace std
;double dp
[1
<<14
];double max
(double a
,double b
){ return a
>b
?a
:b
;}struct node
{ int pp
; double area
;}cont
[1
<<14
];int main(){ int n
,i
,j
,k
; int num
[15
]; double S
; while(~scanf
("%d"
,&n
),n
) { for(i
=1
;i
<=n
;i
++) scanf
("%d"
,&num
[i
]); memset
(dp
,0
,sizeof(dp
)); int t
=0
; S
=0
; for(i
=1
;i
<=n
;i
++) for(j
=i
+1
;j
<=n
;j
++) { for(k
=j
+1
;k
<=n
;k
++) { if(num
[i
]+num
[j
]>num
[k
]&&num
[i
]+num
[k
]>num
[j
]&&num
[j
]+num
[k
]>num
[i
]) { double p
=1.0
*(num
[i
]+num
[j
]+num
[k
])/2
; t
++; double s
=sqrt
(p
*(p
-num
[i
])*(p
-num
[j
])*(p
-num
[k
])); cont
[t
].pp
=(1
<<i
)|(1
<<j
)|(1
<<k
); dp
[(1
<<i
)|(1
<<j
)|(1
<<k
)]=cont
[t
].area
=s
; } } } for(i
=1
;i
<(1
<<(n
+1
));i
++) { for(j
=1
;j
<=t
;j
++) { if((i
&cont
[j
].pp
)==0
) { dp
[i
|cont
[j
].pp
]=max
(dp
[i
|cont
[j
].pp
],dp
[i
]+cont
[j
].area
); if(dp
[i
|cont
[j
].pp
]>S
) S
=dp
[i
|cont
[j
].pp
]; } } } printf
("%.2lf\n"
,S
); } return 0
;}