USACO 1.3 Ski Course Design

#include<stdio.h>
#define MAX_HILLS 1000
#define MAX_ELEVATION 100

int numOfHills;
int elevation[MAX_HILLS + 1];
int minCost = (1 << 31 - 1);

int main(){
	scanf("%d", &numOfHills);
	int maxHeight = 0;
	int hill;
	for (hill = 1; hill <= numOfHills; hill++){
		scanf("%d", &elevation[hill]);
		if (elevation[hill] > maxHeight)
			maxHeight = elevation[hill];
	}
	int limitOfMinElevation = maxHeight - 17;
	//枚举范围而不是单单枚举高度可以提高效率
	int minElevation;
	for (minElevation = 0; minElevation <= limitOfMinElevation; minElevation++){
		int maxElevation = minElevation + 17;
		int cost = 0;
		for (hill = 1; hill <= numOfHills; hill++){
			int height = elevation[hill];
			int change = 0;
			if (height < minElevation)
				change = minElevation - height;
			else if (height > maxElevation)
				change = height - maxElevation;
			cost += change * change;
		}
		if (cost < minCost)
			minCost = cost;
	}

	printf("%d\n", minCost);

	return 0;
}


 

你可能感兴趣的:(USACO,design,1.3,Course,Ski)