Yet Another Median Task Gym
来源:互联网 发布:网络语狗子是啥意思 编辑:程序博客网 时间:2024/06/03 22:38
题目链接:https://vjudge.net/contest/181019#problem/G
题意:给定一个n*n的矩阵,有q次查询,每次查询给定四个数x1 y1 x2 y2,求矩形区域 x1 <= x <= x1, y1 <= y <= y2的中位数。
思路:若每次对小矩形区域内的所有数进行排序找出中位数,则肯定会超时。STL中有一个函数ntn_element(a, x, a + n),用于使数组a中n个数中第x大的数位于第x个位置,x左边的数都大于a[x],x右边的数都小于a[x]但两边的数都可能是无序的。
代码如下:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<cstdlib>#include<sstream>#include<deque>#include<stack>#include<set>#include<map>using namespace std;typedef long long ll;typedef unsigned long long ull;const double eps = 1e-6;const int maxn = 1000 + 20;const int maxt = 300 + 10;const int mod = 10;const int dx[] = {1, -1, 0, 0};const int dy[] = {0, 0, -1, 1};const int Dis[] = {-1, 1, -5, 5};const int inf = 0x3f3f3f3f;const int MOD = 1000;int n, m, k;int g[maxn][maxn], num[maxn * maxn];int main(){ int 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){ num[cnt++] = g[i][j]; } }// cout << "cnt == " << cnt << " (cnt - 1) / 2 == " << (cnt - 1) / 2 << endl; nth_element(num, num + (cnt - 1) / 2 ,num + cnt); printf("%d\n", num[(cnt - 1) / 2]); } return 0;}
阅读全文
0 0
- Yet Another Median Task Gym
- Yet Another Median Task Gym
- Gym 100741G Yet Another Median Task
- Yet Another Median Task
- Codeforces Gym 100741G Yet Another Median Task 二分乱搞
- Gym 100735 - I. Yet another A + B
- Gym 100735I Yet another A + B (java大数)
- GYM 100488 Yet Another Goat in the Garden
- GYM 100488 A. Yet Another Goat in the Garden(计算几何)
- Codeforces Beta Round #95 (Div. 2) -- E. Yet Another Task with Queens(STL)
- Codeforces Beta Round #95 (Div. 2) E. Yet Another Task with Queens(模拟)
- Yet Another Multiple Problem
- Yet Another PhotoMosaic Generator
- Yet Another Analog Clock
- Yet Another Lambda Tutorial
- Codeforces868F Yet Another MinimizationProblem
- Yet Another Web Framework 1
- Yacc: Yet Another Compiler-Compiler
- 快讯:方正证券股东政泉控股所持公司股权继续遭冻结两年
- com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class ...
- java中使用Arrays.binarySearch()在数组中查找指定元素
- 40个多线程问题总结
- 2、粘包现象(struct模块)
- Yet Another Median Task Gym
- CDH5.12.0 (5.X)安装笔记(离线parcel安装)
- 使用Yandex
- 作为云计算 2.0 时代的里程碑,云数据库PolarDB 还需要做什么?
- Java 内存溢出(java.lang.OutOfMemoryError)的常见情况和处理方式总结
- MD5 加密应用 Utils
- java synchronize锁
- NBUOJ 2707 liylho的难题(因子)
- 关于list排序