题目链接

Problem Statement

The land of a park AtCoder is an N×NN×NN×N grid with east-west rows and north-south columns. The height of the square at the i-th row from the north and j-th column from the west is given as Ai,jA_{i,j}Ai,j​.
Takahashi, the manager, has decided to build a square pond occupying K×KK×KK×K squares in this park.To do this, he wants to choose a square section of
K×K squares completely within the park whose median of the heights of the squares is the lowest. Find the median of the heights of the squares in such a section.
Here, the median of the heights of the squares in a K×K section is the height of the (⌊K22⌋+1)(\lfloor \frac{K^2}{2}⌋+1)(⌊2K2​⌋+1)-th highest square among the K2K^2K2 squares in the section, where ⌊x⌋⌊x⌋⌊x⌋ is the greatest integer not exceeding xxx.

Sample Input 1

3 2
1 7 0
5 8 11
10 4 2

Sample Output 1

4

Sample Input 2

3 3
1 2 3
4 5 6
7 8 9

Sample Output 2

5

二维前缀和+差分~
做个小转换,题目要求第 ⌊K22⌋+1\lfloor \frac{K^2}{2}⌋+1⌊2K2​⌋+1 大,实际上就是求第 ⌈K22⌉\lceil\frac{K^2}{2}\rceil⌈2K2​⌉ 小~
如果暴力的话时间复杂度 O(N∗N∗K∗K)O(N*N*K*K)O(N∗N∗K∗K),显然不行,比赛中我还想了用数据结构存,但是复杂度也有 O(N∗N∗K)O(N*N*K)O(N∗N∗K),还是会爆~
回到一开始,找一个 K∗KK*KK∗K 的矩阵第 ⌈K22⌉\lceil\frac{K^2}{2}\rceil⌈2K2​⌉ 小,即这个矩阵中有 ⌈K22⌉\lceil\frac{K^2}{2}\rceil⌈2K2​⌉ 个元素小于等于这个值,那么我们很容易想到二分,那怎么快速判断某个 K∗KK*KK∗K 矩阵中小于等于某个数的个数呢,离散化+二维前缀和即可,对二分的值 midmidmid,首先离散化,当 Ai,j≤midA_{i,j}\leq midAi,j​≤mid,则置为 111,反之置为 000,然后通过二维前缀和可以快速计算出一个 K∗KK*KK∗K 矩阵中大于 midmidmid 的元素个数,暴力判断即可,复杂度 O(N∗N∗log⁡1e9)O(N*N*\log{1e9})O(N∗N∗log1e9),AC代码如下:

n, k = map(int, input().split())
a = [[] for _ in range(805)]
b = [[0 for _ in range(805)] for _ in range(805)]
c = [[0 for _ in range(805)] for _ in range(805)]
for i in range(1, n + 1):a[i] = list(map(int, input().split()))# print(a[i])
l, r = 0, 10 ** 9
while l <= r:mid = l + r >> 1for i in range(1, n + 1):for j in range(1, n + 1):b[i][j] = 0 if a[i][j - 1] > mid else 1for i in range(1, n + 1):for j in range(1, n + 1):c[i][j] = c[i - 1][j] + c[i][j - 1] - c[i - 1][j - 1] + b[i][j]if c[n][n] < (k * k - 1) // 2 + 1:l = mid + 1continueflag = 0for i in range(k, n + 1):for j in range(k, n + 1):if c[i][j] - c[i - k][j] - c[i][j - k] + c[i - k][j - k] >= (k * k - 1) // 2 + 1:flag = 1breakif flag:breakif flag:r = mid - 1else:l = mid + 1
print(l)

AtCoder Beginner Contest 203(Sponsored by Panasonic)D.Pond相关推荐

  1. AtCoder Beginner Contest 203(Sponsored by Panasonic) D.Pond(二分+二维前缀和)

    link 思路: 先来想想暴力的写法: n2n^{2}n2枚举左上角的顶点,k2k^{2}k2求最小值. 考虑优化: 1.1.1.答案有单调性,可以二分答案,省去枚举左上角顶点的复杂度. 2.2.2. ...

  2. AtCoder Beginner Contest 203(Sponsored by Panasonic)题解

    文章目录 A - Chinchirorin B - AtCoder Condominium C - Friends and Travel costs D - Pond E - White Pawn F ...

  3. 数学--数论-- AtCoder Beginner Contest 151(组合数+数学推导)好题(๑•̀ㅂ•́)و✧

    思路统计最大值出现的次数,和最小值出现的次数.虽然是每次都是MAX-MIN,我们先求MAX的和,然后再求MIN的和,做差. 这次代码写的真的很漂亮 题目地址: #include <bits/st ...

  4. AtCoder Beginner Contest 198 (A ~ F)题解

    目录 A. Div B. Palindrome with leading zeros C. Compass Walking D. Send More Money E. Unique Color F. ...

  5. AtCoder Beginner Contest 203 (Sponsored by Panasonic) A~E 题解

    ABC203 A~E [A - Chinchirorin](https://atcoder.jp/contests/abc203/tasks/abc203_a) 题目大意 输入格式 输出格式 样例 分 ...

  6. AtCoder Beginner Contest 084(AB)

    A - New Year 题目链接:https://abc084.contest.atcoder.jp/tasks/abc084_a Time limit : 2sec / Memory limit  ...

  7. AtCoder Beginner Contest 266(C- G)「判凸包」「dp」「期望」「基环树」「组合数」

    abc好好好. C - Convex Quadrilateral (atcoder.jp) 思路: 判凸包,向量叉积×=|a|*|b|*sin.叉积<0即角>180°. (可以勉勉强强算我 ...

  8. AtCoder Beginner Contest 295(DEG)

    D - Three Days Ago (atcoder.jp) (1)题目大意 (2)解题思路 考虑使用前缀和的思路,把每一个位置0-9的数量的奇偶表示出来,用一个map或者hash维护一下即可,每一 ...

  9. AtCoder Beginner Contest 262(ABC262)A-Ex 题解

    A - World Cup 我懒得分类讨论,直接枚举. #include<bits/stdc++.h> #define Max(a,b) ((a<b)&&(a=b)) ...

最新文章

  1. Golang+Python 实现安全动态开机密码+服务器存储
  2. 文本检索秘技之正则表达式grep和egrep
  3. 数据结构应用实例#栈#迷宫寻路
  4. Table 'mysql.plugin' doesn't exist引发de血案
  5. 【design pattern】工厂方法模式和抽象工厂模式
  6. 科学计算机java算法实现,(Java)科学型计算器开发及实现.doc
  7. ABCDE类IP地址的解释
  8. Android Studio下“Error:Could not find com.android.tools.build:gradle:2.2.1”的解决方法
  9. Tensorflow保存神经网络参数有妙招:Saver和Restore
  10. 图书封面的故事之——“巴别塔”选自《七周七语言:理解多种编程范型》
  11. struts2 国际化资源文件自定义的路径解决方法
  12. 网络协议从入门到底层原理(9)HTTP/1.1的升级改进(HTTP/2、HTTP/3)
  13. mysql最简单的查看_查看Mysql版本号 (最简单的是status )
  14. Tomcat6升级到Tomcat9
  15. 如何把鼠标宏用c语言写出来,鼠标宏怎么设置,手把手教你鼠标如何设置宏
  16. android imageview 获取bitmap缩放大小,android – Imageview缩放方法“centercrop”作为代码...
  17. 怎么用计算机属性打开文件,鼠标双击打不开文件怎么办 鼠标双击变成属性的解决方法...
  18. ASP.NET Development Server的Directory Browsing模式HTML垃圾代码
  19. 高一下学期计算机考试知识点,高一年级信息技术期末考试复习题
  20. Win7自动登录,免密码

热门文章

  1. AtCoder Beginner Contest 249题解(E,F)
  2. The Pilots Brothers' refrigerator DFS+枚举
  3. 英语Canutillos祖母绿canutillos单词
  4. [bzoj4722]由乃
  5. 智能优化算法之蚁群算法(ACO)
  6. HTML的mous事件
  7. 电话号码的数字组合(Java)
  8. C1认证:修改《植物大战僵尸》的文件以及code.org绘图以及bmp画图
  9. Information and Influence Propagation in Social Networks学习笔记
  10. C#毕业设计——基于C#+asp.net+sqlserver的网络在线考试系统设计与实现(毕业论文+程序源码)——网络在线考试系统