-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUVA348.cpp
43 lines (43 loc) · 1 KB
/
UVA348.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <algorithm>
#include <pair>
#include <climits>
#define lli long long int
#define m 1000000000
using namespace std;
pair<int,lli> ans[11][11]={};
pair<int,int> dim[11];
int n;
void output(int l,int r)
{
if(l==r){cout<<"A"<<l+1;return;}
int tmp=ans[l][r].first;
cout<<"("; output(l,tmp); cout<<" x "; output(tmp+1,r); cout<<")"; }
}
int main()
{
int i,j,k,l;
while(cin>>n && n)
{
for(i=0;i<n;i++)cin>>dim[i].first>>dim[i].second;
for(i=0;i<n;i++)ans[i][i]={make_pair(i,0)};
for(i=0;i<n-1;i++)ans[i][i+1]={make_pair(i,dim[i].first*dim[i+1].first*dim[i+1].second)};
for(i=2;i<n;i++)
{
for(j=0;j<n-i;j++)
{
k=j+i;ans[j][k]=make_pair(0,INT_MAX);
for(l=j;l<k;l++)
{
if(ans[j][l].second+ans[l+1][k].second+(dim[j].first*dim[l].second*dim[k].second)<ans[j][k].second)
{
ans[j][k].second = ans[j][l].second + ans[l+1][k].second + (dim[j].first*dim[l].second*dim[k].second);
ans[j][k].first=l;
}
}
}
}
output(0,n-1);
}
}
//megatron