Almost the same title. For more information, see: http://community.topcoder.com/stat?c=problem_statement&pm=13211&rd=15857
Thinking:
1 sequence
2 calculation of the current number of buildings I, all possible minimum building changes
3 each time the calculation i+1. Total possible minimum number of building changes
4 minimum number of changes when you can get i+1 at the same time
And the procedures are not complicated.
#include <vector> #include <algorithm> #include <limits.h> #include <math.h>using namespace std ; Class Buildingheights{public:int minimum (vector<int> heights) {int n = (int) heights.size (); Sort (Heights.begin ( ), Heights.end ());vector<int> cost (n, 0); int ans = 0;for (int i = 0; i < n-1; i++) {int c = int_max;for (int j = n -1; J > i; j--) {Cost[j] = Cost[j-1] + (heights[j]-heights[j-1]) * (i+1); c = min (c, cost[j]);} Ans ^= c;} return ans;};
Copyright notice: The author Heart Jing, Jing Space address: http://blog.csdn.net/kenden23/, may not be reproduced without the consent of the author.
SRM 624 Building Heights DivI Interpretation