E2. Asterism (Hard Version)(思维)

const int N = 6e5 + 5;
int t;
int main()
{	ios::sync_with_stdio(false);cin.tie(nullptr);
	//freopen("in.txt", "r", stdin);
	int n, p;
	while (cin >> n >> p)
	{
		vector<int> a(N);
		f(i, 1, n)cin >> a[i];
		sort(a.begin()+1, a.begin()+1+ n);
		int l = -2e9, r = 2e9;
		f(i, 1, n)l = max(l, a[i] - (i - 1));
		f(i, 1, n - p + 1)r = min(r, a[i + p - 1] - (i - 1)-1);
		//不能有超过p个位置满足当前的位置,否则组合超过p,必不行
		if (l > r)puts("0");
		else
		{
			cout << r - l + 1 << endl;
			f(i, l, r)cout << i << " ";
		}
	}
	return 0;
}

你可能感兴趣的:(Codeforces,pupil)