AhmedAshraff

Untitled

Nov 23rd, 2024
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define boAshraf ios_base::sync_with_stdio(false); cin.tie(NULL);
  3. #define ll long long
  4. #define sz(s) (int)(s).size()
  5. #define all(s) (s).begin(),(s).end()
  6. using namespace std;
  7. void File();
  8. void sol();
  9. int main() {
  10.     boAshraf
  11.     File();
  12.     int t = 1;
  13.     cin >> t;
  14.     while (t--) {
  15.         sol();
  16.     }
  17.     return 0;
  18. }
  19.  
  20. void sol() {
  21.     int n;
  22.     cin>>n;
  23.     map<int,int>mp;
  24.     vector<int>v;
  25.     int ans=1e9;
  26.     for (int i = 0; i < n; ++i) {
  27.         int x;cin>>x;
  28.         mp[x]++;
  29.         v.emplace_back(x);
  30.     }
  31.     sort(all(v));
  32.     int cnt=0;
  33.     for(int st=1;st<=2*n;st++){
  34.         if(mp[st])cnt+=mp[st]-1;
  35.         else cnt++;
  36.         auto idx= upper_bound(all(v),st);
  37.         int temp=cnt;
  38.         if(idx!=v.end()){
  39.             int rem=idx-v.begin();
  40.             rem=n-rem;
  41.             temp+=rem;
  42.         }
  43.         ans=min(ans,temp);
  44.     }
  45.     cout<<ans<<'\n';
  46. }
  47.  
  48. void File() {
  49. // #ifndef ONLINE_JUDGE
  50.     freopen("task.in", "r", stdin);
  51.     // freopen("output.txt", "w", stdout);
  52. // #endif
  53. }
Advertisement
Add Comment
Please, Sign In to add comment