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