QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#821#569047#9314. The Median of the Median of the Mediantkt0506tkt0506Failed.2024-09-16 20:23:502024-09-16 20:23:52

Details

Extra Test:

Accepted
time: 0ms
memory: 3548kb

input:

4
1 1 1000 1000

output:

1

result:

ok 1 number(s): "1"

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#569047#9314. The Median of the Median of the Mediantkt0506AC ✓526ms34892kbC++141.2kb2024-09-16 20:09:382024-09-18 18:11:26

answer

#include <bits/stdc++.h>
using namespace std;

int main(){
    int n;
    cin >> n;

    vector<int>a(n+1), sa(n+1);
    for(int i=1; i<=n; i++)cin >> a[i];

    int b[n+1][n+1], sum[n+1][n+1];
    memset(b, 0, sizeof b);

    int l=0, r=1e9;
    while(l<=r){
        int m = (l+r)/2;
        
        // compute b
        sa[0] = 0;
        for(int i=1; i<=n; i++){
            sa[i] = sa[i-1];
            if(a[i] <= m)sa[i]++;
            else sa[i]--;
        }
        for(int i=1; i<=n; i++){
            for(int j=i; j<=n; j++){
                if(sa[j]-sa[i-1] >= 0)b[i][j] = 1;
                else b[i][j] = -1;
            }
        }

        // prefix sum
        for(int i=1; i<=n; i++){
            for(int j=1; j<=n; j++){
                sum[i][j] = sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1] + b[i][j];
            }
        }

        // compute c
        int tot = 0;
        for(int i=1; i<=n; i++){
            for(int j=i; j<=n; j++){
                int v = sum[j][j] - sum[i-1][j] - sum[j][i-1] + sum[i-1][i-1];
                if(v>=0)tot++;
                else tot--;
            }
        }

        if(tot >= 0)r = m-1;
        else l = m+1;
    }
    cout << l;
}