#include<bits/stdc++.h> using namespace std; int h[55]; int n; int main(void){ cin >> n; for(int i = 0; i < n; i++) cin >> h[i]; sort(h, h + n); int tmp, ans = 0; if(n % 2 == 0) { tmp = n / 2; for(int i = 0; i < tmp; i++) { ans += 2 * (h[tmp + i] - h[i]); } ans += h[tmp - 1] - h[tmp]; cout << ans << endl; } else { tmp = n / 2; for(int i = 0; i < tmp; i++) { ans += 2 * (h[tmp + 1 + i] - h[i]); } ans += -h[tmp + 1] + h[tmp]; cout << ans << endl; } return 0; } 疯狂队列,贴子里给的答案ans2越界了,这是我修改后的答案。还是很佩服作者的思路
点赞 3

相关推荐

点赞 评论 收藏
转发
牛客网
牛客企业服务