SRM 624 DIV2

1. 250-point
(1). 题意
取一个 vector<int> 中任意 K 个值相加的最小和. 
(2). 思路
(a). 从小到大排序
(b). 取前 K 个值相加
(3). 代码

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <utility>
#include <limits>
#include <bitset>
using namespace std;

class CostOfDancing{
  public:
  int minimum(int K, vector <int> danceCost) {
    // 从小到大排序
    sort(danceCost.begin(), danceCost.end());

    // 取前 K 个值相加
    int sum = 0;
    for (int i=0; i<K; i++) {
      sum += danceCost[i];
    }

    return sum;
  }
};

2. 500-point
(1). 题意
(a). 用最小的代价, 使 vector<int> 中有 M 个相等的数
(b). 代价: 较小的数与较大的数的差的和
(2). 思路
(a). 从小到大排序
(b). 从第 M 个数开始往后遍历
(c). 每个数都与其前面 M-1 个数的差的和, 就是所需生成与 M 个相同 height[i] 的最小和
(d). 最后取所有的最小和中取最小的那个即可
(3). 代码

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <utility>
#include <limits>
#include <bitset>
using namespace std;

#define MAX_INT (numeric_limits<int>::max())

class BuildingHeightsEasy{
  public:
  int minimum(int M, vector <int> heights) {
    // 如果 M 为 1, 则无需进行操作
    if (M == 1) return 0;

    // 按从小到大排序
    sort(heights.begin(), heights.end());

    // 从第 M 个数开始往后遍历
    // 每个数都与其前面 M-1 个数的差的和, 
    // 就是所需生成与 M 个相同 height[i] 的最小和
    // 最后取所有的最小和中取最小的那个即可
    int min = MAX_INT;
    for (int i=M-1; i<heights.size(); i++) {
      int num = 0;
      
      for (int j=i-M+1; j<i; j++) {
        num += (heights[i] - heights[j]);
      }

      min = min < num ? min : num;
    }

    return min;
  }

};

3. 1000-point
(1). 题意
(a). 由 N 个点组成的凸边形
(b). 两个人交替画线, 每次可以画一条边, 或者画一条对角线
(c). 所画的线不能与前面的线相交
(d). 最后无法画线的人输
(2). 思路
注: 见官方题解
(3). 代码
见官方题解.

4. 官方题解

你可能感兴趣的:(topcoder,SRM,624)