Solution: 2018 algorithm machine Zexal second pipe cutting

Subject description:

 

 

 

Example:

 

 

Implementing an interpreter:

Classic deformed steel cutting issues: cutting the most to lose money

Knowledge Point: dynamic programming, pipe cutting

I.e., obtain the implementation of state transition equation code to improve, to set the array. Price [i] i stored minimum length of the tubes after cutting, P [i] stores the value of i is not cut length of the tubes, both the array. Price dp array of this problem.

After analysis shows the state transition equation is:

price[0] = 0;

price[i] = min(p[1]+price[i-1],p[2]+price[i-2],...p[i-1]+price[1],p[i]);

Because the price [i] already is the minimum under the current circumstances, so just follow the transfer equation can improve the code.

 

Pit:

Writing initialization and state transition equation

 

Complete code:

 

// minimum cutting steel 
#include <the iostream> 
the using namespace STD; 
int main () 
{ 
	iOS :: sync_with_stdio (to false); 
	int length, MIN; // are the length and the minimum value 
	the while (CIN >> length) 
	{ 
		int P [length +. 1]; 
		for (int i =. 1; i <= length; i ++) 
			CIN >> P [i]; // meaning of the questions based on the value of length i 
		
		int price [length + 1]; // save each segment value 
		price [0] = 0; // use the price [0], the initialization prevent errors 
		
		for (int I =. 1; I <= length; I ++) 
		{ 
			MIN = 2147483647; // needs to be set when the minimum value is obtained the maximum value 
			for (int j =. 1; j <= i; j ++) 
			{ 
				IF (MIN> P [j] +. price [ij]) 
				// constantly i j length of the cutting front and rear portions ij 
				// to find the minimum value after cutting to length i 
				{ 
				} 
					MIN = P [J] +. price [ij of]; // replace minimum
					price [i] = MIN;
				// implement state transition equation describes see explanation 
			} 
		} 
		COUT. Price << [length] << '\ n-'; 
		the minimum value after the output of the longest part cutting // (I) 
	} 
	return 0; 
}

  

 

Guess you like

Origin www.cnblogs.com/doUlikewyx/p/11700775.html