USACO 1.3 Barn Repair (快排+贪心)

#include <stdio.h>
#define DEBUG 0
#define TESTCASES 9

#define MAX_STALLS 200

int numOfBoards, numOfStalls, numOfCows;
int stallOccupied[MAX_STALLS + 1];
int arrayOfIntervals[MAX_STALLS];
int numOfIntervals;

void sort(int array[], int first, int last){
	while (first < last){
		int pivotValue = array[first];
		int left = first;
		int right = last;
		while (left < right){
			while (left < right && array[right] >= pivotValue)
				right--;
			array[left] = array[right];
			while (left < right && array[left] <= pivotValue)
				left++;
			array[right] = array[left];
		}
		array[left] = pivotValue;
		int pivot = left;
		sort(array, first, pivot);
		first = pivot + 1;
	}
}

int main(){
#if DEBUG
	int testCase;
	for (testCase = 1; testCase <= TESTCASES; testCase++){
		char inputFileName[20] = "barn1.inX";
		inputFileName[8] = '1' +  (testCase - 1);
		freopen(inputFileName, "r", stdin);
		printf("\n#%d\n", testCase);
#endif

	scanf("%d %d %d", &numOfBoards, &numOfStalls, &numOfCows);
	int cow;
	for (cow = 1; cow <= numOfCows; cow++)
		scanf("%d", &stallOccupied[cow]);

	if (numOfBoards >= numOfCows){
		printf("%d\n", numOfCows);
#if DEBUG
		continue;
#else
		return 0;
#endif
	}

	sort(stallOccupied, 1, numOfCows);

	numOfIntervals = 0;
	for (cow = 1; cow < numOfCows; cow++)
		arrayOfIntervals[++numOfIntervals] = stallOccupied[cow + 1] - stallOccupied[cow] - 1;
	sort(arrayOfIntervals, 1, numOfIntervals);

	int numOfIntervalsUncovered = numOfBoards - 1;	

	int stallsUncovered = 0;
	while (numOfIntervalsUncovered--)
		stallsUncovered += arrayOfIntervals[numOfIntervals--];

	int stallsBlocked = stallOccupied[numOfCows]  - stallOccupied[1] + 1 - stallsUncovered;
	printf("%d\n", stallsBlocked);

#if DEBUG
	}
#endif
	return 0;	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值