SRM 624 Building Heights DivI 解读

几乎相同的一标题。欲了解更多请参阅:http://community.topcoder.com/stat?c=problem_statement&pm=13211&rd=15857

思维:

1 序列

2 大厦的当前数量的计算i时候,全部可能的最小建筑物改动数

3 每次计算i+1的时候。全部可能的最小建筑物改动数

4 同一时候能够比較得到i+1的时候最小改动数

得到的程序也不复杂

#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;
}
};



版权声明:笔者心脏靖,景空间地址:http://blog.csdn.net/kenden23/,可能不会在未经作者同意转载。

posted @ 2015-08-07 12:33  hrhguanli  阅读(135)  评论(0编辑  收藏  举报