USACO 1.3 Barn Repair (Greedy)
This question is also greedy.
To minimize the total length of a wooden board, the maximum length of the uncovered wooden board is required.
We first use a piece of wooden board to cover the cowshed. Then, each time we select the largest gap from the covered range, divide the wooden board into two blocks Based on the gap, and repeat until it is divided into m blocks or there is no gap.
/* ID: twd30651 PROG: barn1 LANG: C ++ */# include
# Include
# Include
// # Define DEBUGusing namespace std; int M; int S; int C; int origin_data [300]; int process_data [300]; int comp (const void *, const void * B) {return * (int *) a-* (int *) B;} int comp2 (const void * a, const void * B) {return * (int *) B-* (int *) a;} int main (int argc, char * argv []) {freopen ("barn1.in", "r ", stdin); freopen ("barn1.out", "w", stdout); scanf ("% d", & M, & S, & C ); int I = 0; int min = 300; int max = 0; while (scanf ("% d", & origin_data [I]) = 1) {if (origin_data [I]> max) max = origin_data [I]; if (origin_data [I]
Dp can be used on the Internet.