Zoj2059 The Twin Towers (dp)
Description
Twin towers we see you standing tall, though a building's lost our faith will never fall.
Twin towers the world hears your call, though you're gone it only strengthens our resolve.
We couldn't make it through this without you Lord, through hard times we come together more ....
Twin Towers-A Song for America
In memory of the tragic events that unfolded on the morning of September 11,200 1, five-year-old Rosie decids to rebuild a tallest Twin Towers by using the crystals her brother has collected for years. will she succeed in building the two towers of the same height?
Input
There are mutiple test cases.
One line forms a test case. the first integer N (N <100) tells you the number of crystals her brother has collected. then each of the next N integers describs the height of a certain crystal.
A negtive N indicats the end.
Note that all crytals are in cube shape. And the total height of crystals is smaller than 2000.
Output
If it is impossible, you wocould say "Sorry", otherwise tell her the height of the Twin Towers.
Sample Input
4 11 11 11 11 11
4 1 11 111 1111
-1
Sample Output
22
Sorry
Give you the height of something .. Then ask if you can use these two towers with the same height ..
Dp [a] [B] the height difference between the second stone and the two towers .. Constantly maintain low Towers ..
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Pragma comment (linker,/STACK: 102400000,102400000) # define pi acos (-1.0) # define EPS 1e-6 # define INF (1 <24) using namespace std; int dp [105] [2005]; int a [105]; int main () {int n; while (scanf (% d, & n), n> 0) {int I, j; for (I = 1; I <= n; I ++) scanf (% d, & a [I]); memset (dp,-1, sizeof (dp); dp [0] [0] = 0; for (I = 1; I <= n; I ++) {memcpy (dp [I], dp [I-1], sizeof (dp [I-1]); // do not put I, I-1 is all I case for (j = 0; j <2001; j ++) // height {if (dp [I-1] [j]! =-1) {if (a [I]
Later I found that I could optimize it .....
Use a scrolling array to optimize the memory (although there is nothing to use) (to remind yourself, use this method)
# Include # Include # Include # Include # Include # Include # Include # Include # Pragma comment (linker,/STACK: 102400000,102400000) # define pi acos (-1.0) # define EPS 1e-6 # define INF (1 <24) using namespace std; int dp [2005]; // dp [j] = height of the height int t of the height tower with a height difference j [2005]; // The scrolling array, the record is equivalent to dp [I] int a [105]; int main () {int n; while (scanf (% d, & n), n> 0) {int I, j; for (I = 1; I <= n; I ++) scanf (% d, & a [I]); memset (dp,-1, sizeof (dp); memset (t,-1, sizeof (t); dp [0] = t [0] = 0; for (I = 1; I <= n; I ++) {memcpy (dp, t, sizeof (t); // do not place I, I-1 is all I case for (j = 0; j <2001; j ++) {if (dp [j]! =-1) {if (a [I]