USACO Ski Course Design 解题报告

来源:互联网 发布:深圳淘宝运营培训 编辑:程序博客网 时间:2024/04/29 22:06

这里用的是naive的方法,测试所有的高度组合(改造后的)(最低高度,最高高度)。最低高度的范围是从[low, high - 17]。low和high分别是当前的最低和最高高度。由于高度的种类很少(0~100),所以这里用一个数组(cnt)统计每个类别的高度。

/* ID: thestor1 LANG: C++ TASK: skidesign */#include <iostream>#include <fstream>  #include <cmath>  #include <cstdio>  #include <cstring>  #include <climits>  #include <cassert>  #include <string>  #include <vector>  #include <set>#include <map>  #include <queue>  #include <stack>  #include <algorithm>#include <cassert>using namespace std;int main(){ifstream fin("skidesign.in");ofstream fout("skidesign.out");int N;fin>>N;std::vector<int> cnt(101, 0);for (int i = 0; i < N; ++i){int height;fin>>height;assert(0 <= height && height <= 100);cnt[height]++;}int low = 0, high = 100;while (cnt[low] == 0){low++;}while (cnt[high] == 0){high--;}int mincost = -1;for (int l = low; l <= high - 17; ++l){int h = l + 17;int cost = 0;for (int i = low; i < l; ++i){if (cnt[i]){cost += (l - i) * (l - i) * cnt[i];}}for (int i = high; i > h; --i){if (cnt[i]){cost += (i - h) * (i - h) * cnt[i];}}if (mincost < 0 || cost < mincost){mincost = cost;}}fout<<max(mincost, 0)<<endl;fin.close();fout.close();return 0;  }


0 0
原创粉丝点击