搜到的方法:对于给出的每个基地的坐标,将他们相减得到两两基地之间的距离再存入数组,每次取最小值,并将其加到左右值中更小得那个。注意要在头和尾插入最大值。
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main() {
int last = 0;
int l, n, m, t;
cin >> l >> n >> m;
vector<int>a(n+3);
a[0] = 10000;
for (int i = 1; i < n+1; i++) {
cin >> t;
a[i] = t - last;
last = t;
}
a[n + 1] = l - last;
a[n + 2] = 10000;
while (m--) {
vector<int>::iterator it = min_element(a.begin(), a.end());
if (*(it - 1) < *(it + 1))
*(it - 1) += *it;
else
*(it + 1) += *it;
a.erase(it);
}
cout << *min_element(a.begin(), a.end()) << endl;
return 0;
}
ps:用到了min_element(),迭代器。是一道简单的贪心。