USACO 1.3 Barn Repair (快排+贪心)

来源:互联网 发布:des算法加密过程 编辑:程序博客网 时间:2024/05/16 18:06
#include <stdio.h>#define DEBUG 0#define TESTCASES 9#define MAX_STALLS 200int 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 DEBUGint 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);#endifscanf("%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 DEBUGcontinue;#elsereturn 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}#endifreturn 0;}

0 0
原创粉丝点击