Title Description
In a dark night, under a stormy night, farmer John's barn roof, the door was blown away. Fortunately, many cows are on holiday, so the barn is not full. The bullpen was lined up next to the other, and the cows stayed overnight. There are some cows in the barn, some not. All the barns have the same width. After the door was lost, farmer John had to erect a new plank in front of the barn as soon as possible. His new lumber supplier will supply him with any length he wants, but stingy suppliers can only offer a limited number of planks. Farmer John wants to minimize the total length of wood he buys.
Given: The maximum number of planks possible to buy M (1<= m<=50); The total number of cattle sheds S (1<= s<=200); The total number of cows in the Barn is C (1 <= C <=s), and the cow's Barn number stall_number (1 <= stall_number <= S), calculates the minimum total length of the planks required to stop all cattle sheds. Output the minimum total length of the required planks as an answer.
Input/output format
Input format:
Line 1th: The maximum number of planks m, the total number of cattle sheds s and the total number of cows C (separated by a space)
Line 2nd to c+1: Each line contains an integer that represents the number of the cow's barn.
Output format:
A single line contains an integer representing the minimum total length of the required planks.
Input and Output Sample input example # #:
4 50 183 4 6 8 1415 16 17 2125 26 27 30 31 40 41 42 43
Sample # # of output:
25
Description
The title translation comes from Nocow.
Usaco Training Section 1.3
This question requires a minimum length and can be considered greedy. First, it is obvious that the minimum length is at least the number of cattle sheds, because the barn is not necessarily linked together, so the number of planks may be greater than the maximum number of topics. If so, then the adjacent board to connect (that is, to fill in the space between), which is used to sort, the adjacent empty length from small to large row, each time to the smallest fill. If the number of planks is equal to the number of planks, then the output answer
1#include <cstdio>2#include <cmath>3#include <cstdlib>4#include <cstring>5#include <queue>6#include <stack>7#include <vector>8#include <iostream>9#include"algorithm"Ten using namespacestd; OnetypedefLong LongLL; A Const intmax=205; - intn,m,c; - intA[max],b[max],d[max]; the intBla[max]; - intMain () { -Freopen ("repair.in","R", stdin); -Freopen ("Repair.out","W", stdout); + inti,j; - intNum0), Len,first; +scanf"%d%d%d",&m,&n,&c); Alen=C; atmemset (b,0,sizeof(b)); - for(i=1; i<=c;i++) -{scanf ("%d", A +i); -b[a[i]]++; - } - for(i=1; i<=n;i++) in if(b[i]>0&& b[i-1]==0) -num++; to if(num<=m) +{printf ("%d", Len); - return 0; the } *d[1]=0; $ for(i=2; i<=n;i++)Panax Notoginseng if(b[i-1]>0) -d[i]=0; the Else +d[i]=d[i-1]+1; A for(first=1; b[first]==0; first++); thebla[0]=0; + for(i=first+1; i<=n;i++) - if(b[i]>0&& d[i]!=0) $bla[++bla[0]]=D[i]; $Sort (bla+1, bla+bla[0]+1); - for(i=1; i<=num-m;i++) -len+=Bla[i]; theprintf"%d", Len); - return 0;Wuyi}
[USACO1.3] Repair the barn Barn Repair (greedy)