从大到小遍历一遍,每次取M个元素,然后求得最小的floor即可
int minimum(int M, vector <int> heights) { sort(heights.begin(),heights.end()); int minFloor = 10000; for(int i = heights.size()-1; i >=M-1; -- i){ int floor = 0; for(int j = 0; j < M; ++j) floor +=heights[i]-heights[i-j]; minFloor =min(minFloor,floor); } return minFloor; }
topcoder SRM 624 DIV2 BuildingHeightsEasy,布布扣,bubuko.com
topcoder SRM 624 DIV2 BuildingHeightsEasy
原文地址:http://www.cnblogs.com/xiongqiangcs/p/3785461.html