SRM 624 Building Heights DivI 题解
生活随笔
收集整理的這篇文章主要介紹了
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; } };
總結
以上是生活随笔為你收集整理的SRM 624 Building Heights DivI 题解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机专业第五轮学科评估排名,计算机第五
- 下一篇: 如何在 JavaScript 中清空数组