SDKD 2017 Summer Team Training #12, tm ( Yet Another Median Task )

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SDKD 2017 Summer Team Training #12, tm ( Yet Another Median Task )相关的知识,希望对你有一定的参考价值。

题目大意:给定一个矩阵,给你多次查询,查询一个小矩阵中的数的中位数;

解题思路:完全没往二分上想,我们可以二分枚举中位数数值,搜索矩阵中数值小于中位数的个数。

#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxn=805,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);  
int a[maxn][maxn];
int p[maxn*maxn];

int main() {
    int n,i,j,k,l,r,u,d,q;
    ll m=-llinf,w=llinf;
    scanf("%d%d",&n,&q);
    for (i=1;i<=n;i++) {
        for (j=1;j<=n;j++) {
            scanf("%d",&a[i][j]);
            m=max(m,(ll)a[i][j]);
            w=min(w,(ll)a[i][j]);
        }
    }
    for (i=1;i<=q;i++) {
        scanf("%d%d%d%d",&u,&l,&d,&r);
        int lc=w,rc=m,mid,tot=(r-l+1)*(d-u+1),ans;
        if (tot%2) tot/=2; else tot=tot/2-1;
        while (lc<=rc) {
        int cnt=0;
        mid=(lc+rc)/2;
        for (j=u;j<=d;j++) {
            for (k=l;k<=r;k++) {
                if (a[j][k]<mid) cnt++;
            }
        }
        if (cnt<=tot) lc=mid+1,ans=mid; else {
            rc=mid-1;
        }
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

以上是关于SDKD 2017 Summer Team Training #12, tm ( Yet Another Median Task )的主要内容,如果未能解决你的问题,请参考以下文章

SDKD 2017 Summer Single Training #03

SDUT 2021 summer team contest 1st(for 20)(补题)

#Java# SDKD 软件18-1,3 实验1

Adding Reversed Numbers(summer2017)

2017ecjtu-summer training #1 UVA 10399

2017ecjtu-summer training #11 POJ 1018