Usaco Barn Repair solution report

Source: Internet
Author: User

Question:
On a dark, stormy night, the roof and doors of farmer John's cowshed were blown away. Fortunately, many cows are on vacation, so the cowshed is not full. The cowshed is lined up next to the other, and the ox stays in the room for the night. Some cowshed have cows, and some do not. All cowshed have the same width. Farmer John had to erect a new plank before the cowshed as soon as possible after the door was lost. His new timber supplier will supply him any length he wants, but the stingy supplier can only provide a limited number of boards. Farmer John wants to minimize the total length of the Board he purchased.
Given: the maximum number of boards that may be purchased is M (1 <= M <= 50); Total number of Cowshed S (1 <= S <= 200 ); total number of cattle in a cowshed C (1 <= C <= S); number of the cowshed where the ox is located stall_number (1 <= stall_number <= S ), calculate the minimum total length of the plank required to stop all cowshed with cattle. Output the minimum length of the required Board as the answer.
Question:
There are M wooden boards that indicate from the smallest number of Cowshed to the largest middle there is a M-1 segment can not be covered to find out every two cowshed with cattle interval how much distance, find the first M-1 in these distances, and these do not need to be covered, then the minimum total length of the board is the continuous length of the whole Cowshed (the smallest number to the maximum) minus the length of the M-1 does not need to be covered, the answer is displayed.
Code:
[Cpp]
/*
ID: lishicao
PROG: barn1
LANG: C ++
*/
# Include <iostream>
# Include <fstream>
# Include <cstring>
# Include <algorithm>
Using namespace std;
 
Ifstream fin ("barn1.in ");
Ofstream fout ("barn1.out ");
 
Bool cmp (int a, int B)
{
Return a> B;
}
 
Int main ()
{
Int length [205], num [205];
Int M, S, C, Count = 0, ans;
Int Max = 0, Min = 999999;
 
Memset (length, 0, sizeof (length ));
 
Fin> M> S> C;
For (int I = 0; I <C; I ++)
Fin> num [I];
 
Sort (num, num + C );
 
For (int I = 0; I <C; I ++)
{
If (num [I]> Max) Max = num [I];
If (num [I] <Min) Min = num [I];
If (I! = 0)
{
Length [Count] = num [I]-num [I-1]-1;
Count ++;
}
}
Ans = Max-Min + 1;
Sort (length, length + Count, cmp );
For (int I = 0; I <M-1; I ++)
Ans-= length [I];
Fout <ans <endl;
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.