# [2017.7.7 popularization] ultimate number

Posted by phpvn.org on Thu, 09 Apr 2020 18:39:01 +0200

# subject

Title Description

Given a sequence a of length n, we try to find the ultimate number x of each prefix of sequence a Minimum, try to find the ultimate number t (if there are multiple ultimate numbers t, just output the smallest one)

input
The first line is an integer n, the second line is an integer n, separated by spaces

output
Output an integer line by line, that is, the ultimate number t

# Solving problems

#### On the contrary, if x is smaller than the top of the big root heap when adding to the small root heap, then temp2 operation is performed.

temp1: put x in the small root heap, and put the top of the small root heap into the large root heap, and maintain the two properties.
temp2: put x in the big root heap, and put the top of the big root heap into the small root heap, and maintain the two properties.

# Code

```#include<cstdio>
#include<algorithm>
using namespace std;
int n,u,num,nuu,a,b,d;

void upa(int x)//Because there are two root heaps, which are easy to operate, two up and down are defined
{
while (x>1&&a[x]<a[x/2]) swap(a[x],a[x/2]),x/=2;
}

void downa(int x) //Heap heap
{
int y;
while ((a[x*2]<a[x]&&x*2<=nuu)||(a[x*2+1]<a[x]&&x*2+1<=nuu))
{
y=x*2;
if (a[y+1]<a[y]&&y+1<=nuu) y++;
swap(a[x],a[y]);
x=y;
}
}

void upb(int x)
{
while (x>1&&b[x]>b[x/2]) swap(b[x],b[x/2]),x/=2;
}

void downb(int x)//Big root pile
{
int y;
while ((b[x*2]>b[x]&&x*2<=num)||(b[x*2+1]>b[x]&&x*2+1<=num))
{
y=x*2;
if (b[y+1]>b[y]&&y+1<=num) y++;
swap(b[x],b[y]);
x=y;
}
}

int main()
{
//freopen("c.in","r",stdin);
//freopen("c.out","w",stdout);
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
scanf("%d",&u);
if (nuu<=num) //If the length of the small root heap is less than or equal to the length of the large root heap, put it in the small root heap
{
if (u<b) {//If u is smaller than the top of the big root heap, exchange a[++nuu] and b and maintain two heaps
a[++nuu]=b;
upa(nuu);
downa(1);
b=u;
downb(1);
} else {//Normal processing
a[++nuu]=u; upa(nuu); downa(1);
}
} else //Or put it in the big pile
{
if (u>a) {//If u is larger than the top of the small root heap, exchange the original b[++num] and a and maintain the two heaps
b[++num]=a;
upb(num);
downb(1);
a=u;
downa(1);
} else {//Normal processing
b[++num]=u; upb(num); downb(1);
}
}
d[i]=a; //Median
}
sort(d+1,d+n+1);
printf("%d",d[n/2]);//Take the median of multiple median
return 0;
}```

Topics: less