Advertisement
fireLUFFY

CF823_B

Sep 25th, 2022 (edited)
985
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.10 KB | None | 0 0
  1. // AUTHOR: fireLUFFYY
  2.  
  3. #pragma GCC optimize("Ofast")
  4. #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
  5. #pragma GCC optimize("unroll-loops")
  6. #pragma comment(linker, "/stack:200000000")
  7. #include<bits/stdc++.h>
  8. #include<ext/pb_ds/assoc_container.hpp>
  9. #include<ext/pb_ds/tree_policy.hpp>
  10. using namespace std;
  11. using namespace __gnu_pbds;
  12.  
  13. #define int long long
  14. #define vi vector<int>
  15. #define vvl vector<vector<int>>
  16. #define forr(i,n) for(int i=0;i<n;i++)
  17. #define forn(i,a,b) for(int i=a;i<=b;i++)
  18. #define sortv(a) sort(a.begin(),a.end())
  19. #define sortvr(a) sort(a.rbegin(),a.rend())
  20. #define verase(a) a.erase(unique(a.begin(),a.end()),a.end())
  21. #define all(a) a.begin(),a.end()
  22. #define getv(v,n) for(int i=0;i<n;i++){int x; cin>>x; v.push_back(x);};
  23. #define dbg(x) cerr<<#x<<" = "; _print(x);cerr<<"\n";
  24. #define mp make_pair
  25. #define pb push_back
  26. #define pf push_front
  27. #define fi first
  28. #define se second
  29. #define endl "\n"
  30. #define inf 2e18
  31.  
  32. int MOD=1000000007;
  33. int mod=998244353;
  34. const int N=200050;
  35. const int nn=1000050;
  36. int fact[N]; // array to store values of factorial
  37. bool prime[nn];    //array to store precalculated primes till 10^6
  38. void _print(int a){cerr<<a;}  void _print(char a){cerr<<a;}  void _print(bool a){cerr<<a;}  void _print(string a){cerr<<a;}
  39. template<class T> void _print(vector<T> v1){ cerr<<"[ "; for(T i:v1){_print(i);cerr<<" ";}cerr<<"]";}
  40. template<class T> void _print(set<T> s1){ cerr<<"[ "; for(T i:s1){_print(i);cerr<<" ";}cerr<<"]";}
  41. template<class T> void _print(multiset<T> s1){ cerr<<"[ "; for(T i:s1){_print(i);cerr<<" ";}cerr<<"]";}
  42. template<class T> void _print(map<T,T> m1){for(auto i:m1){cerr<<"[ "; _print(i.first);cerr<<" : ";_print(i.second);cerr<<" ]";cerr<<endl;}}
  43. typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
  44. template<class T> T gcd(T a,T b){if(!a||!b)return a|b; unsigned shift=__builtin_ctz(a|b); a>>=__builtin_ctz(a); do{b>>=__builtin_ctz(b); if(a>b)swap(a,b);b-=a;}while(b); return a<<shift;}
  45. template<class T> T lcm(T a,T b){ unsigned val=max(a,b)/gcd(a,b);val*=min(a,b);return val;}
  46. bool pow2(int x){if(x&&(!(x&(x-1)))) return true; else return false;}
  47. int multiply(int x, int y){return (x * 1ll * y) % MOD;}
  48. int power(int x, int y){int z = 1;while(y){if(y & 1) z = multiply(z, x);x = multiply(x, x);y >>= 1;}return z;}
  49. int modInverse(int x){return power(x, MOD - 2);}
  50. int divide(int x, int y){return multiply(x, modInverse(y));}
  51. void cal_factorial(){fact[0] = 1;for(int i = 1; i < N; i++)fact[i] = multiply(fact[i - 1], i);}// function to calculate factorial upto N
  52. void cal_primes(){memset(prime,true,sizeof(prime)); forn(i,2,sqrt(nn)){ if(prime[i]==true){ for(int j=i*i;j<=nn;j+=i){prime[j]=false;}}}}
  53. int nCr(int n, int k){return divide(fact[n], multiply(fact[k], fact[n - k]));}
  54. vector<int>Intersection(vi &v1,vi &v2){vi v3;sort(all(v1));sort(all(v2));set_intersection(all(v1),all(v2),back_inserter(v3));return v3;}
  55. vector<int>Union(vi &v1,vi&v2){vi v3;sort(all(v1));sort(all(v2));set_union(all(v1),all(v2),back_inserter(v3));return v3;}
  56. void FASTIO(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);}
  57. int dx[8]={0,1,0,-1,1,1,-1,-1}; //direction vectors
  58. int dy[8]={1,0,-1,0,1,-1,-1,1}; //direction vectors
  59.  
  60.  
  61. void solve(int t)
  62. {
  63.     int testcases=t;
  64.     while(t--)
  65.     {
  66.         //cout<<"#"<<(testcases-t)<<" Test-Case: "<<endl;
  67.         //cout<<"Case #"<<(testcases-t)<<": ";
  68.         int n;cin>>n;
  69.         multiset<int>st;
  70.         int x;
  71.         forr(i,n){
  72.             cin>>x;
  73.             st.insert(x);
  74.         }
  75.  
  76.         // dbg(st)
  77.         vi v;
  78.         for(auto it:st){
  79.             v.pb(it);
  80.         }
  81.         int ttm=0,m=st.size();
  82.         forr(i,n){
  83.             cin>>x;
  84.         }
  85.         // dbg(ttm)
  86.  
  87.         int pos=v[0];
  88.         vector<int>pref(st.size()),suf(st.size());
  89.         pref[0]=v[0];suf[m-1]=v[m-1];
  90.  
  91.         for(int i=1;i<m;i++)
  92.             pref[i]=pref[i-1]+v[i];
  93.  
  94.         for(int i=m-2;i>=0;i--)
  95.             suf[i]=suf[i+1]+v[i];
  96.  
  97.         // dbg(pref)
  98.         // dbg(suf)
  99.  
  100.         int val=0,ans=0;
  101.  
  102.         for(int i=0;i<m;i++){
  103.             if(i==0){
  104.                 val=abs(suf[i+1]-((m-1)*v[i]));
  105.             }
  106.             else if(i==m-1){
  107.                 val=abs(pref[i-1]-((m-1)*v[i]));
  108.             }
  109.             else
  110.                 val=abs(suf[i+1]-((m-i-1)*v[i]))+abs(pref[i-1]-((i)*v[i]));
  111.  
  112.             // cerr<<val<<endl;
  113.  
  114.  
  115.             if(i==0)
  116.                 ans=val;
  117.             else{
  118.                 if(val<ans){
  119.                     pos=v[i];
  120.                     ans=val;
  121.                 }
  122.             }
  123.         }
  124.  
  125.         cout<<pos<<endl;
  126.     }
  127. }
  128.  
  129. main()
  130. {
  131.     auto start=chrono::system_clock::now();
  132.     {
  133.         #ifndef ONLINE_JUDGE
  134.             freopen("input.txt","r",stdin);
  135.             freopen("output.txt","w",stdout);
  136.             freopen("error.txt","w",stderr);
  137.         #endif
  138.         FASTIO();    
  139.         int t=1;
  140.         cin>>t;
  141.         solve(t);
  142.     }
  143.     auto end=chrono::system_clock::now();
  144.     chrono::duration<double> elapsed=end-start;
  145. //    cout<<" Time taken: "<<elapsed.count()<<" sec";
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement