Los solution to a problem Valley P1209 [[USACO1.3] repair bullpen Barn Repair]

Questions surface:

Title Description

In a dark and stormy night high, under a stormy night, the roof of farmer John bullpen, the door was blown away. Fortunately, many cattle are on vacation, so no cow lived. A cow is aligned next to the other, cows live there overnight. Some of the cow a cow, some do not. Cowshed all have the same width. Since the door is missing, farmer John must erect the bullpen before the new board as soon as possible. His new timber suppliers will supply him any length he wants, but stingy suppliers can only provide a limited number of wood. farmer John wanted to buy his wood to minimize the total length.

Is given by: The maximum possible number of commercially available board M (1 <= M <= 50); Total byre S (1 <= S <= 200); the total number of cattle cowshed C (1 <= C < = S); and a cowshed where cows number stall_number (1 <= stall_number <= S), calculate the required minimum total length of the wood stopped all of a cow barn. Minimum total length of the output board as desired answer.

Input Format

Line 1: The maximum number of boards M, the total number of S and the total number of cattle barn C (separated by spaces)

C + 1 to the second line: Each row contains an integer indicating the number of cattle barn occupied.

Output Format

A single line contains an integer representing the desired minimum total length of the wood.


 

This question can first turn to

Be understood as covering the entire piece of wood, the middle of each of two cattle shed voids can be disconnected m-1 times, like legend

But need Laid determination m> c, where, because of the need of wood than wood consumption will lead to the sixth and seventh points or RE WA

The Code

#include <algorithm> 
#include <the iostream> 
#include <The iomanip> 
#include <CString> 
#include <cstdio> 
#include <the cmath> 
#include <Queue> 
the using namespace STD; 
int m, S, C, A [205] , B [205], ANS = 0; 
BOOL CMP (int X, Y int) {return X> Y;} // descending row 
int Read () 
{int X = 0; char CH = getchar (); 
    the while (CH < '0' || CH> '. 9') CH = getchar (); 
    the while (CH> = '0' && CH <= '. 9') = {X * X + CH 10-'0 '; CH getchar = ();} 
    return X; 
} // read fast 
int main () 
{ 
    m = read (); S = read (); C = read (); 
    IF (m> C) {the printf ( "% D" , c); return 0;} // Laid judgment needs specific consumption of wood planks number of cases 
    for (int i = 1;i<=c;i++)a[i]=read();
    sort(a+1,a+c+1);
    for (int i = 2; i <= c; i ++) b [i-1] = a [i] -a [i-1]; // obtained intermediate of bovine cow voids 
    ans = a [ c] -a [1] +1; // initially covering the entire 
    sort (b + 1, b + c, cmp); // void descending row 
    for (int i = 1; i <= m- 1; i ++) ans = ans -b [i] +1; // void subtracting 
    the printf ( "% D", ANS); 
    return 0; 
}

  

Guess you like

Origin www.cnblogs.com/Peter-Rabbit/p/11308080.html