题目是P4447,参考题解中大佬的思路,但是写出来全是RE,估计是迭代器越界了,但是看不出哪里越界了,求助大佬 (迭代器的使用主要在函数huaxian()中)
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define qwq (cout << "qwq" << endl)
using ll = long long;
const ll inf = 0x3f3f3f3f;
const int N = 1e5 + 100;
typedef map<int, int>::iterator it;
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = x * 10 + (ch ^ 48);
ch = getchar();
}
return x * f;
}
map<int, int> mp;
int sum = inf;
void huaxian()
{
it i = mp.begin(), j = i;
j++;
--(*i).second;
int tmpsum = 1;
for (; j != mp.end() and (*j).second > (*i).second and (*j).first - (*i).first == 1; j++, i++)
tmpsum++, --(*j).second;
i = mp.begin();
while (i != mp.end() and !(*i).second)
mp.erase((*i).first), i++;
if (tmpsum < sum)
sum = tmpsum;
}
void solve()
{
int n = read();
for (int i = 1; i <= n; i++)
++mp[read()];
while (!mp.empty())
huaxian();
cout << sum;
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int _ = 1;
// cin >> _;
while (_--)
solve();
return 0;
}