题解 | #正则序列#简洁易懂,不用STL
正则序列
http://www.nowcoder.com/questionTerminal/0771ab500d424415af6b1aa4c13afcdd
#include<iostream>
using namespace std;
int main(){
int n;
cin >> n;
int b[20001] = {0};
for (int i = 0, num; i < n; i++) {
cin >> num;
b[num + 10000]++;
}
int ans = 0;
for (int num = 0, j = 1; num < 20001 && j <= n; num++) {
while (b[num]--) {
ans += abs(j++ - num + 10000);
}
}
cout << ans;
return 0;
}