Gym 100741G Yet Another Median Task

题意:

给出一个n*n的矩阵。然后有很多操作,每次给你两个点(x1,y1)和(x2,y2),求出这两个点作为新矩阵的左上角和有下角,求出新矩阵所有值的中点。


直接暴力,用scanf输入,然后就爆过了,用cin cout会超时。

用nth_element函数。

使用方法:nth_element(start, start+n, end)

使第n大元素处于第n位置(从0开始,其位置是下标为n的元素),并且比这个元素小的元素都排在这个元素之前,比这个元素大的元素都排在这个元素之后,但不能保证他们是有序的。

代码:

#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;

int a[1000005];
int g[1005][1005];
int main()
{
    int n, q; scanf("%d%d", &n, &q);
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            scanf("%d", &g[i][j]);
        }
    }
    int x1, y1, x2, y2;
    while(q--)
    {
        scanf("%d%d%d%d",&x1, &y1, &x2, &y2);
        int cnt = 0;
        for(int i = x1; i <= x2; i++)
        {
            for(int j = y1; j <= y2; j++)
            {
                a[cnt++] = g[i][j];
            }
        }
        nth_element(a, a+(cnt-1)/2, a+cnt);
        printf("%d\n", a[(cnt-1)/2]);
        //cout << a[(cnt-1)/2] << endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值