CF1617C 题解
思路
首先每个数可以不变或变成任意一个 的数,显然不可以对于 , 不可能比 大,所以我们要先排序。首先 且没重复的数肯定不改,其它的数就找一个不大于 的数删掉,如果没数了就无解。
代码
# 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;
}

浙公网安备 33010602011771号