barnrepair - codetrick
Farmer John must quickly erect new boards in front of the stalls, since the doors were lost. His new lumber supplier will supply him boards of any length he wishes, but the supplier can only deliver a small number of total boards. Farmer John wishes to minimize the total length of the boards he must purchase.
Given M (1 <= M <= 50), the maximum number of boards that can be purchased; S (1 <= S <= 200), the total number of stalls; C (1 <= C <= S) the number of cows in the stalls, and the C occupied stall numbers (1 <= stall_number <= S), calculate the minimum number of stalls that must be blocked in order to block all the stalls that have cows in them.
Print your answer as the total number of stalls blocked.
http://mangogao.com/read/73.html
http://code.antonio081014.com/2012/09/usaco-barn-repair.html
https://frostpines.wordpress.com/2010/11/06/usaco-1-3-2-barn-repair/
Read full article from barnrepair - codetrick
It was a dark and stormy night that ripped the roof and gates off the stalls that hold Farmer John's cows. Happily, many of the cows were on vacation, so the barn was not completely full.
The cows spend the night in stalls that are arranged adjacent to each other in a long line. Some stalls have cows in them; some do not. All stalls are the same width. Farmer John must quickly erect new boards in front of the stalls, since the doors were lost. His new lumber supplier will supply him boards of any length he wishes, but the supplier can only deliver a small number of total boards. Farmer John wishes to minimize the total length of the boards he must purchase.
Given M (1 <= M <= 50), the maximum number of boards that can be purchased; S (1 <= S <= 200), the total number of stalls; C (1 <= C <= S) the number of cows in the stalls, and the C occupied stall numbers (1 <= stall_number <= S), calculate the minimum number of stalls that must be blocked in order to block all the stalls that have cows in them.
Print your answer as the total number of stalls blocked.
PROGRAM NAME: barn1
INPUT FORMAT
Line 1: | M, S, and C (space separated) |
Lines 2-C+1: | Each line contains one integer, the number of an occupied stall. |
SAMPLE INPUT (file barn1.in)
4 50 18
3 4 6 8 14 15 16 17 21 25 26 27 30 31 40 41 42 43
OUTPUT FORMAT
A single line with one integer that represents the total number of stalls blocked.SAMPLE OUTPUT (file barn1.out)
25[One minimum arrangement is one board covering stalls 3-8, one covering 14-21, one covering 25-31, and one covering 40-43.]
http://mangogao.com/read/73.html
在一个夜黑风高,下着暴风雨的夜晚,农民约翰的牛棚的屋顶、门被吹飞了。 好在许多牛正在度假,所以牛棚没有住满。 剩下的牛一个紧挨着另一个被排成一行来过夜。 有些牛棚里有牛,有些没有。 所有的牛棚有相同的宽度。 自门遗失以后,农民约翰必须尽快在牛棚之前竖立起新的木板。 他的新木材供应商将会供应他任何他想要的长度,但是供应商只能提供有限数目的木板。 农民约翰想将他购买的木板总长度减到最少。
给出:可能买到的木板最大的数目M(1<= M<=50);牛棚的总数S(1<= S<=200); 牛棚里牛的总数C(1 <= C <=S);和牛所在的牛棚的编号stall_number(1 <= stall_number <= S),计算拦住所有有牛的牛棚所需木板的最小总长度。 输出所需木板的最小总长度作为答案。
int
main()
{
ifstream fin(
"barn1.in"
);
ofstream fout(
"barn1.out"
);
int
m(0),M,S,C,ans,stall[200],kong[200];
fin >> M >> S >> C;
for
(
int
i = 0; i < C; ++i) fin >> stall[i];
sort(stall,stall+C);
//初始解有m块木板,总长度为ans=c
ans = C;
for
(
int
i = 1; i < C; ++i)
if
(stall[i] - stall[i-1] != 1)
kong[m++] = stall[i] - stall[i-1] - 1;
sort(kong,kong+m);
//从小到大补木板
for
(
int
i = 0; i <= m-M; ++i) ans += kong[i];
fout << ans << endl;
return
0;
}
https://frostpines.wordpress.com/2010/11/06/usaco-1-3-2-barn-repair/
class Gap implements Comparable<Gap> { int start, end; Gap(int s, int e) { start = s; end = e; } public int compareTo(Gap o) { return o.getSize() - getSize(); } public int getSize() { return end - start - 1; } } // Gap class public class barn1 { public static void main(String[] args) throws IOException { BufferedReader f = new BufferedReader(new FileReader("barn1.in")); PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter( "barn1.out"))); StringTokenizer st = new StringTokenizer(f.readLine()); int m = Integer.parseInt(st.nextToken()); int s = Integer.parseInt(st.nextToken()); int c = Integer.parseInt(st.nextToken()); int boards, stallsCovered; int[] stalls = new int[c]; List<Gap> gaps = new ArrayList<Gap>(); for (int i = 0; i < c; i++) stalls[i] = Integer.parseInt(f.readLine()); Arrays.sort(stalls); for (int i = 1; i < c; i++) { if (stalls[i] - stalls[i - 1] > 1) { gaps.add(new Gap(stalls[i - 1], stalls[i])); } } Collections.sort(gaps); stallsCovered = stalls[c - 1] - stalls[0] + 1; for (int i = 0; i < gaps.size() && i < m - 1; i++) { stallsCovered -= gaps.get(i).getSize(); } out.println(stallsCovered); out.close(); System.exit(0); } }
Read full article from barnrepair - codetrick