全RE,不知道是哪里出问题了
查看原帖
全RE,不知道是哪里出问题了
1281794
postpone楼主2024/4/23 12:11

全RE,请问是哪里越界了呀,不是很懂

#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;
}
2024/4/23 12:11
加载中...