-
Notifications
You must be signed in to change notification settings - Fork 2
/
10003.cpp
executable file
·46 lines (39 loc) · 943 Bytes
/
10003.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
44
45
46
#include <iostream>
#include <cctype>
#include <string>
#include <algorithm>
using namespace std ;
#define MaxN 50
int main()
{
int l = 0 ;
int n = 0 ;
int c[ MaxN + 2 ] = { 0 } ;
int optimal[ MaxN + 1 ][ MaxN + 1 ] = { 0 } ;
int i = 0 ;
int j = 0 ;
int k = 0 ;
while( cin >> l && l != 0 )
{
cin >> n ;
c[ 0 ] = 0 ;
for( i = 1 ; i <= n ; i++ )
cin >> c[ i ] ;
c[ n + 1 ] = l ;
for( i = 1 ; i <= n ; i++ )
{
for( j = 1 ; j <= n - i + 1 ; j++ )
{
optimal[ j ][ j + i - 1 ] = optimal[ j ][ j + i - 2 ] ;
for( k = j ; k < j + i - 1 ; k++ )
{
optimal[ j ][ j + i - 1 ] = min( optimal[ j ][ j + i - 1 ],
optimal[ j ][ k - 1 ] + optimal[ k + 1 ][ j + i - 1 ] ) ;
}
optimal[ j ][ j + i - 1 ] += c[ j + i ] - c[ j - 1 ] ;
}
}
cout << "The minimum cutting is " << optimal[ 1 ][ n ] << "." << endl ;
}
return 0 ;
}