樹狀陣列---區間更新(差分陣列實現)
阿新 • • 發佈:2018-12-18
/*
* @Author: Achan
* @Date: 2018-10-28 12:55:01
* @Last Modified by: Achan
* @Last Modified time: 2018-10-28 19:59:13
*/
#include<bits/stdc++.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include <queue>
#include<cmath>
using namespace std;
#define X first
#define Y second
#define eps 1e-2
#define gcd __gcd
#define pb push_back
#define PI acos(-1.0)
#define lowbit(x) (x)&(-x)
#define fin freopen("in.txt","r",stdin);
#define fout freopen("out.txt","w",stdout);
#define bug printf("!!!!!\n");
#define mem(x,y) memset(x,y,sizeof(x))
#define rep(i,j,k) for(int i=j;i<(int)k;i++)
#define per(i,j,k) for(int i=j;i<=(int)k;i++)
#define pset(x) setiosflags(ios::fixed)<<setprecision(x)
#define io std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
typedef long long ll;
typedef long double LD;
typedef pair<int,int> pii;
typedef unsigned long long ull;
const int inf = 1<<30;
const ll INF = 1e18 ;
const int mod = 1e9+7;
const int maxn = 1e5+2;
const int mov[4][2] = {-1,0,1,0,0,1,0,-1};
const int Mov[8][2] = {-1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1};
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int a[maxn];
bool vis[maxn];
int c[maxn]; //儲存排名資訊
int n;
double rak[maxn];
int tree[maxn],tree1[maxn];
inline void update(int *c,int x, int val)
{
while(x<=n)
{
c[x] += val;
x += (x& -x);
}
}
inline int getsum(int *c,int x)
{
int sum = 0;
while(x>0)
{
sum += c[x];
x -= x&(-x);
}
return sum;
}
int main(void)
{
//fin
//fout
io
int T;cin>>T;
while(T--)
{
cin>>n;
memset(tree,0,sizeof(tree));
memset(tree1,0,sizeof(tree1));
for(int i=1; i<=n;i++)
{
cin>>a[i];
update(tree,a[i],1); //起始
update(tree,n+1,-1); //末尾
update(tree1,a[i],1*(a[i]-1)); //起始
update(tree1,n+1, -1*n); //末尾
int x,y;
x=y=a[i];
rak[a[i]] = (y*getsum(tree,y)-(x-1)*getsum(tree,x-1))-(getsum(tree1,y)-getsum(tree1,x-1)) ;
}
// for(int i=1;i<=n; i++) cout<<rak[a[i]]<<endl;
for(int i=1;i<=n;i++)
{
double rank = rak[a[i]];
double ex = (double)rank/(1.0 * i);
double bex = 1.0 /(double)(n-i);
// cout<<"rank "<<rank<<" "<<ex<<" "<<bex<<endl;
if(ex < bex)
{
cout<<a[i]<<endl;
break;
}
// else if(ex==bex)
// {
// cout<<min(a[i],a[i+1])<<endl;
// break;
// }
}
//printf("Case %d: ",cas++);
//cout<<"Case #"<<cas++<<endl;
}
#ifdef LOCAL
Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
}