提交时间:2023-08-14 04:54:51
运行 ID: 24576
#include <bits/stdc++.h> using namespace std; int moveStone(vector<int> x) { int n = x.size(); int sum = 0; int average = -1; int move = 0; for (int i = 0; i < n; i++) { sum += x[i]; } if (sum % n != 0) { cout << "Input is not allowed!"; return -1; } else { average = sum / n; if (n < 2) { return 0; } else if (n == 2) { return abs(x[0] - average); } else { vector<int> m; m.push_back(x[0] - average); for (int i = 1; i < n; i++) { m.push_back(m.back() - average + x[i]); } int minMove = 0; for (int v : m) { minMove += abs(v); } for (int j = 0; j < n; j++) { vector<int> mm; for (int i = 0; i < n; i++) { mm.push_back(m[i] - m[j]); } int temp = 0; for (int v : mm) { temp += abs(v); } if (temp < minMove) { minMove = temp; } } move = minMove; } return move; } } int main() { vector<int> stoneHeaps; int n; cin >> n; for (int i = 0; i < n; i++) { int temp; cin >> temp; stoneHeaps.push_back(temp); } cout << moveStone(stoneHeaps); }