CF1617C 题解

思路

首先每个数可以不变或变成任意一个 n2\le\lceil\frac n2\rceil 的数,显然不可以对于 nmn\le mn2\lceil\frac n2\rceil 不可能比 m2\lceil\frac m2\rceil 大,所以我们要先排序。首先 aina_i\le n 且没重复的数肯定不改,其它的数就找一个不大于 ai2\lceil\frac{a_i}2\rceil 的数删掉,如果没数了就无解。

代码

# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
int t, n, a[100005], sum;
bitset <100005> vis;
set <int> st;
int main () {
	ios::sync_with_stdio (0);
	cin.tie (0);
	cout.tie (0);
	cin >> t;
	while (t --) {
		cin >> n;
		st.clear ();
		sum = 0;
		for (int i = 1; i <= n; ++ i)
			cin >> a[i], vis[i] = 0, st.insert (i);
		sort (a + 1, a + 1 + n);
		for (int i = 1; i <= n; ++ i)
			if (a[i] > n || vis[a[i]])
				if (*st.begin () * 2 < a[i])
					st.erase (st.begin ()), ++ sum;
				else {
					cout << "-1\n";
					goto there;
				} else
				vis[a[i]] = 1, st.erase (a[i]);
		cout << sum << '\n';
there:
		;
	}
	return 0;
}
posted @ 2024-04-01 13:29  Vitamin_B  阅读(12)  评论(0)    收藏  举报  来源