Usaco Barn Repair

Source: Internet
Author: User

The farmer needs to cover up a bunch of barn-sized barns, but he can only make m-planks, not all barns have cows, so they don't have to cover all the barns. Ask how many sheds can be used at least in the case where all cattle barns are covered and only m planks can be customized.

Greedy to find M-1 largest (connected and cattle-free barn range), the sum of these intervals is not allowed to cover the sum, and then the smallest number of the occupied barn and the number of the largest occupied barn between the total number of barns, minus the sum of the newly obtained not to cover the total is positive solution.

/*id:modengd1prog:barn1lang:c++*/#include <iostream> #include <stdio.h> #include <algorithm># Include <queue>using namespace Std;int main () {    freopen ("barn1.in", "R", stdin);    Freopen ("Barn1.out", "w", stdout);    int m,s,c,sum,no_occupied;    int input[201];    Priority_queue<int> Q;    scanf ("%d%d%d", &m,&s,&c);    for (int i=0;i<c;i++)    {        scanf ("%d", &input[i]);    }    Sort (input,input+c);    sum=input[c-1]-input[0]+1;    for (int i=1;i<c;i++)    {        if (input[i]-input[i-1]>1)            Q.push (input[i]-input[i-1]-1);    }    no_occupied=0;    M=min (m,c);//There may be a case where the number of planks is greater than the occupied barn for    (int i=0;i<m-1;i++)    {        no_occupied+=q.top ();        Q.pop ();    }    cout<<sum-no_occupied<<endl;    return 0;}

  

Usaco Barn Repair

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.