int main(){
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; ++i) cin >> v[i];
sort(v.begin(), v.end());
int ans = 0, currmax = 0, currmin = INT_MAX;
for (int i = 0; i < n-1; ++i){
if ((v[i+1] - v[i]) > 1){
if (currmax == 0) currmax = max(v[i], v[i+1]); // first time
else{ // second time and more times
ans += min(currmin, currmax); // take the smaller one to get the maximum sum of the pairings of numbers
currmin = INT_MAX, currmax = 0; // reset for next pairing
if ((v[i+1] - v[i]) > 2) { // there is a gap between two numbers which is bigger than 2 -> add it to current max and min for next pairing
int midpoint = (v[i] + v[i+1])/2 ; // find the midpoint between two numbers with gap bigger than 2
currmin = min(midpoint, min(v[
创作工场
免责声明:
以上内容除特别注明外均来源于网友提问,创作工场回答,未经许可,严谨转载。
点击这里>>使用🔥专业版,更聪明、更完整、更原创!