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