题面传送门
贪心
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int a[50000];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
sort(a, a + n);
int ans = 0;
for (int i = 0; i < n-1; i++)
if (a[i+1]-1 >= a[i]) continue;
else {
ans = ans + a[i] - a[i+1] + 1;
a[i+1] = a[i] + 1;
}
cout << ans << endl;
return 0;
}
原文地址:https://www.cnblogs.com/baoyihan/p/11068518.html
时间: 2024-11-01 09:37:02