当前位置:网站首页>Triangle ranch (0/1 backpack)
Triangle ranch (0/1 backpack)
2022-07-19 06:06:00 【lovesickman】
Triangle ranch
Title Description
Like everyone , Cows love change . They are imagining new pastures . Dairy architects Hei Want to build a triangular ranch with beautiful white fences . She has n n n A board , The length of each piece l i l_i li Are integers. , She wanted to form a triangle with all the boards to make the pasture the largest .
Please help Hei Miss, build such a pasture , And calculate the area of the largest pasture .
Input format
The first 1 1 1 That's ok : An integer n n n;
The first 2 2 2 To the first ( n + 1 ) (n + 1) (n+1) That's ok , One integer per row , The first ( i + 1 ) (i + 1) (i+1) The whole number of lines l i l_i li It means the first one i i i The length of the board .
Output format
Only one integer : Multiply the maximum pasture area by 100 100 100 Then the ending result . If you can't build , Output − 1 -1 −1.
Examples #1
The sample input #1
5
1
1
3
3
4
Sample output #1
692
Tips
Sample input and output 1 explain
692 = Left behind ( 100 × Triangle area ) 692=\text{ Left behind }(100\times\text{ Triangle area }) 692= Left behind (100× Triangle area ), This triangle is an equilateral triangle , Side length is 4 4 4.
Data scale and agreement
about 100 % 100\% 100% The data of , Guarantee 3 ≤ n ≤ 40 3\le n\le40 3≤n≤40, 1 ≤ l i ≤ 40 1\le l_i\le40 1≤li≤40.
obvious d f s dfs dfs The time complexity of is O ( 3 40 ) O(3^{40}) O(340)
void dfs(int u,int A,int B,int C){
if(u>n){
if(A+B>C && A+C>B && B+C>A){
double p = (A+B+C)/2;
double s = sqrt(p*(p-A)*(p-B)*(p-C));
ans = max(ans, (ll)(s*100));
}
return ;
}
dfs(u+1,A+a[u],B,C);
dfs(u+1,A,B+a[u],C);
dfs(u+1,A,B,C+a[u]);
}
Easy to think of f [ i ] [ j ] [ k ] f[i][j][k] f[i][j][k] Before use i i i A stick , The first 1 The side length of the strip is j j j, The first 2 The side length of the strip is k k k Can the triangle of form .40 * 800 * 800 It is acceptable .
I encountered a big hole when I was writing , Some strange , There is no judgment 3 Border relations also A 了 , Is it data water ?
Of course, this question can be changed 2 dimension .
/* A: 10min B: 20min C: 30min D: 40min */
#include <iostream>
#include <stack>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <sstream>
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define mem(f, x) memset(f,x,sizeof(f))
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo_(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
template<typename T>
ostream& operator<<(ostream& os,const vector<T>&v){
for(int i=0,j=0;i<v.size();i++,j++)if(j>=5){
j=0;puts("");}else os<<v[i]<<" ";return os;}
template<typename T>
ostream& operator<<(ostream& os,const set<T>&v){
for(auto c:v)os<<c<<" ";return os;}
template<typename T1,typename T2>
ostream& operator<<(ostream& os,const map<T1,T2>&v){
for(auto c:v)os<<c.first<<" "<<c.second<<endl;return os;}
template<typename T>inline void rd(T &a) {
char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {
if (c == '-')f = -1; c = getchar();}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
typedef pair<int,int>PII;
typedef pair<long,long>PLL;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+10;
int n,m,_;
int a[50],f[50][810][810];
double ans,sum;
double area(double a,double b){
double c = sum-a-b;
double p = (a+b+c)/2;
double s = sqrt(p*(p-a)*(p-b)*(p-c));
return s;
}
void solve(){
cin>>n;
fo(i,1,n){
cin>>a[i];
sum+=a[i];
}
f[0][0][0]=1;
for(int i=1;i<=n;i++){
for(int j=sum/2;j>=0;j--){
for(int k=sum/2;k>=0;k--){
f[i][j][k] |= f[i-1][j][k];
if(j-a[i]>=0){
f[i][j][k] |= f[i-1][j-a[i]][k];
}
if(k-a[i]>=0){
f[i][j][k] |= f[i-1][j][k-a[i]];
}
}
}
}
ans = -1;
for(int k=1;k<=n;k++){
for(int i=sum/2;i>0;i--)
for(int j=sum/2;j>0;j--)
{
if(!f[k][i][j]) continue;
ans=max(ans,area(i,j));// Update answers
}
}
if(ans!=-1){
cout<<(ll)(ans*100)<<endl;
}
else
cout<<ans<<endl;
}
int main(){
solve();
return 0;
}
边栏推荐
- c语言 指定日期开始多少天 显示
- Solve cannot read properties of null (reading 'pickalgorithm')
- 处理中文分词 ik分词器以及拓展和停止字典
- Hm8203 linear two string charging management controller IC
- Basic mathematics course 2_ Euler function, linear sieve, extended Euler
- 4-channel encoder pulse counter, 8-Channel do, Modbus TCP module
- Chrome浏览器设置 【显示右上角 翻译语言图标】
- 【力扣】环形链表 II
- Darwin分析经验
- 2021-11-10 micropyton tb6600 step drive class
猜你喜欢

【简单快速】启动后桌面正常下方任务栏无反应/鼠标一直转圈

开源在线的MarkDown编辑器 --【Editor.md】

2022/07/09 第五小组 丁帅 学习笔记 day02

4-20mA to 4-20mA 0-5V to 0-5V analog signal isolation transmitter

2022/07/14 learning notes (day07) array

Simple chrome script automatically skips the charging acknowledgment page after the video playback of station B ends

WebService接口的创建与实现

2021 - 09 - 15

Introduction to goroutine, a high concurrency feature of golang

Hm8203 linear two string charging management controller IC
随机推荐
Darwin分析经验
软考初、中、高级考试全体验
Vscode configuring golang development environment
Basic mathematics course 2_ Euler function, linear sieve, extended Euler
Computational geometry (2)
Qt Creator闪退解决办法
MCU single chip OTP
MySQL workbench basically uses [create a data table]
Darwin 反射总结
2021-11-26 pyautogui cooperates with lightning simulator to realize the automation of mobile app check-in and answer questions
Qtss data type
【力扣】设计循环队列
谷歌浏览器不能手动修改cookies,cookie报红标红
Introduction to easydarawin streaming media server
c语言 指定日期开始多少天 显示
嵌入式C语言重点(const、static、voliatile、位运算)
Darwin Streaming Server 介绍
绝世好题(位运算优化dp)
Proportional valve amplifier 1a, 2a, 3a, 5A proportional valve drive module 0-10V to 0-24v
升高压模块隔离模块HRA2460D-2W