USACO Section 1.3 Barn Repair

/*
ID: lucien23
PROG: barn1
LANG: C++
*/

#include<iostream>
#include<fstream>
#include<algorithm>
#include<functional>
using namespace std;

int main()
{
	ifstream infile("barn1.in");
	ofstream outfile("barn1.out");
	if(!infile || !outfile)
	{
		cout<<"file operation failure!"<<endl;
		return -1;
	}

	int M,S,C;
	infile>>M>>S>>C;
	int *nums=new int[C];
	for (int i=0;i<C;i++)
	{
		infile>>nums[i];
	}
	sort(nums,nums+C);
	int *interval=new int[C-1];
	for (int i=1;i<C;i++)
	{
		interval[i-1]=nums[i]-nums[i-1]-1;
	}
	sort(interval,interval+C-1,greater<int>());

	int minLen=nums[C-1]-nums[0]+1;
	for (int i=0;i<M-1 && i<C-1;i++)
	{
		minLen-=interval[i];
	}

	outfile<<minLen<<endl;

	delete[] nums;
	delete[] interval;

	return 0;
}

你可能感兴趣的:(Algorithm,C++,USACO)