C. Paprika and Permutation
传送门:Problem - 1617C - Codeforces
题意:
思路:
首先这个题要知道这个结论:
当 x > a[i] 时,a[i] mod x == a[i]
当 x <= a[i] 时,0 <= a[i] % x < ( a[i] +1 ) / 2
当 a[i] <= n && a[i] 在[1, n] 中只出现一次时,不用操作
当 a[i] > n || a[i] 在[ 1 , n ] 中出现超过两次,操作
1. 数组排序
2. 从小到大枚举每个数,若需要操作,这个数字只能变成 < ( a[i] + 1 ) / 2的任何数字
AC代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 10;
int vis[N];
void solve()
{
memset( vis , 0 , sizeof vis );
int n; cin >> n;
vector<int> a(n + 1);
for( int i = 1 ; i <= n ; i++ )
{
cin >> a[i];
if( a[i] <= n )vis[a[i]] = 1;
}
int p = 1; int sum = 0;
sort( a.begin() + 1 , a.end() );
for( int i = 1 ; i <= n ; i++)
{
if( a[i] <= n && a[i] != a[i-1] )continue;
while( vis[p] )p++;
if( p >= ( a[i] + 1 ) / 2 )
{
puts("-1"); return;
}
vis[p] = 1; sum++;
}
cout << sum << endl;
}
signed main()
{
int tt; cin >> tt;
while(tt--)solve();
return 0;
}