显示原始代码
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
#include <stdio.h>
#include <string.h>
using namespace std;
long mode = 0;
long multiplicity = 0;
long x = 0, y = 0;
void FindMode(long n[], long low, long high) {
long mid = n[(low + high) / 2];
long first = 0, last = 0;
for (long i = low; i <= high; i++) {
if (n[i] == mid) {
first = i;
break;
}
}
for (long i = first; i <= high; i++) {
last = i;
if (n[i] != mid) {
last--;
break;
}
}
if (multiplicity < last - first + 1) {
multiplicity = last - first + 1;
mode = mid;
}
if (first + 1 > last - first + 1) {
FindMode(n, low, first);
printf("mode=%ld\n", mode);
}
if (high - last + 1 > last - first + 1) {
FindMode(n, last, high);
}
x = first;
y = last;
return;
}
int main() {
long n;
int ans = 0;
long cnt = 0;
scanf("%ld", &n);
long a[n];
memset(a, 0, sizeof(a));
for (long i = 0; i < n; i++) {
scanf("%ld", &a[i]);
a[i] = a[i] % 10;
}
sort(a, a + n - 1);
for (long i = 0; i < n - 1; i++) {
if (a[i + 1] == a[i]) {
ans++;
}
}
if (ans == 0) {
for (long i = 0; i < n - 1; i++) {
while (a[i] != a[n - 1]) {
a[i] = a[i] + 1;
cnt++;
}
}
printf("%ld", cnt);
return 0;
}
FindMode(a, 0, n - 1);
if (y - x + 1 == n) {
cnt = 0;
} else {
for (long i = 0; i < n; i++) {
while (a[i] != (mode + 1)) {
a[i] = (a[i]++) % 10;
cnt++;
}
}
}
printf("%ld", cnt);
return 0;
}