对a排个序,然后从1往后加即可,当然a中有的自然不加了。
#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#define MAX 100005
#define mod 998244353ll
#define INF 0x3f3f3f3f
#define ll long long
using namespace std;
int a[100005];
queue<int> q;
int main() {
freopen("a.txt", "r", stdin);
freopen("b.txt", "w", stdout);
int n, m, s = 0, c = 0;
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
sort(a + 1, a + n + 1);
bool f = false;
for (int i = 1; i <= n; ++i) {
for (int j = a[i - 1] + 1; j < a[i]; ++j) {
s += j;
if (s <= m) {
c++;
q.push(j);
}
else {
break;
}
}
if (s > m) break;
}
a[n]++;
while (s <= m) {
s += a[n];
if (s <= m) {
c++;
q.push(a[n]);
}
a[n]++;
}
cout << q.size() << endl;
while (!q.empty()) {
int t = q.front();
q.pop();
if (!f) {
printf("%d", t);
f = true;
}
else {
printf(" %d", t);
}
}
putchar('\n');
return 0;
}