AtCoder Beginner Contest 203 Pond(二分+二维前缀和)

简介: 大体思路:二分,将原矩阵根据二分的值变成01矩阵,如果元素值> val 就变为1,否则0对于k * k 的矩阵,统计区域内元素之和,如果 sum < ⌊k2 / 2⌋ + 1,意味着当前k * k矩阵的中位数小于x,而x是我们的答案(最小中位数),①sum < ⌊k2 / 2⌋ + 1 情况下x取得太大,r = mid②反之,x还可能取更小的,l = mid但是需要注意下l的初始值,当取0 or 1的时候是会wa掉的:

a0e101f785fd1a485e2e4c86113d8bba.pngaf87492e483f574a9212ef829a0cd898.png


样例输入


【样例1】

3 2
1 7 0
5 8 11
10 4 2


【样例2】

3 3
1 2 3
4 5 6
7 8 9


样例输出


【样例1】

4

【样例2】

5


据说这个题用对顶堆维护被卡了

先挂一手官方该题题解链接

1a6eb0db1fff28f5180811f9bbc3ec21.png


大体思路:


二分,将原 矩阵 根据二分的值变成01矩阵,如果元素值> val 就变为1,否则0

对于k * k 的矩阵,统计区域内元素之和,如果 sum < ⌊k2 / 2⌋ + 1,意味着当前k * k矩阵的中位数小于x,而x是我们的答案(最小中位数),

①sum < ⌊k2 / 2⌋ + 1 情况下x取得太大,r = mid

②反之,x还可能取更小的,l = mid

但是需要注意下l的初始值,当取0 or 1的时候是会wa掉的:


c3705b4aa08030ad45197c5aae2fac7e.png


下限要取-1

typedef int itn;
int a[809][809];
int s[809][809];
int main() {
  int n = read,k =  read;
  for(int i=0; i<n; i++) {
    for(int j=0; j<n; j++) {
      a[i][j] = read;
    }
  }
  int p = k * k / 2 + 1;
  int l = -1,r = 0x3f3f3f3f;
  bool flag = 0;
  while(l + 1 < r) {
    int mid = l+r >> 1;
    for(int i=0; i<n; i++) {
      for(int j=0; j<n; j++) {
        s[i+1][j+1] = s[i+1][j] + s[i][j+1] - s[i][j];
        if(a[i][j] > mid) s[i+1][j+1] ++;
      }
    }
    flag = 0;
    for(int i=0; i<n-k+1; i++) {
      for(int j=0; j<n-k+1; j++) {
        int val = s[i+k][j+k] + s[i][j] - s[i][j+k] - s[i+k][j];
        if(val < p) {
          flag = 1;
          break;
        }
      }
      if(flag) break;
    }
    if(flag) r = mid;
    else l = mid;
  }
  cout << l + 1 <<endl;
  return 0;
}





目录
相关文章
AtCoder Beginner Contest 203(Sponsored by Panasonic) D.Pond(二分+二维前缀和)
AtCoder Beginner Contest 203(Sponsored by Panasonic) D.Pond(二分+二维前缀和)
94 0
AtCoder Beginner Contest 216 G - 01Sequence (并查集 贪心 树状数组 差分约束)
AtCoder Beginner Contest 216 G - 01Sequence (并查集 贪心 树状数组 差分约束)
157 0
AtCoder Beginner Contest 221 E - LEQ(组合数学 树状数组)
AtCoder Beginner Contest 221 E - LEQ(组合数学 树状数组)
158 0
AtCoder Beginner Contest 223 D - Restricted Permutation(建图 思维 构造 拓扑排序)
AtCoder Beginner Contest 223 D - Restricted Permutation(建图 思维 构造 拓扑排序)
134 0
|
机器学习/深度学习
AtCoder Beginner Contest 215 F - Dist Max 2 (二分 单调队列)
AtCoder Beginner Contest 215 F - Dist Max 2 (二分 单调队列)
147 0
AtCoder Beginner Contest 216 D - Pair of Balls (思维建图 拓扑排序判断有向图是否有环)
AtCoder Beginner Contest 216 D - Pair of Balls (思维建图 拓扑排序判断有向图是否有环)
127 0
|
人工智能 JavaScript BI
AtCoder Beginner Contest 222 D - Between Two Arrays(前缀和优化dp)
AtCoder Beginner Contest 222 D - Between Two Arrays(前缀和优化dp)
114 0
AtCoder Beginner Contest 221 D - Online games(差分 离散化 区间)
AtCoder Beginner Contest 221 D - Online games(差分 离散化 区间)
141 0
|
人工智能 BI Go
[Atcoder ABC222] F - Expensive Expense | 妙用树的直径 | Dijkstra
Problem Statement The Kingdom of AtCoder is composed of N towns and N−1 roads. The towns are labeled as Town 1, Town 2, …, Town N. Similarly, the roads are labeled as Road 1, Road 2, …, Road N−1. Road i connects Towns Ai and Bi bidirectionally, and you have to pay the toll of Ci to go through it.
219 0
[Atcoder ABC222] F - Expensive Expense | 妙用树的直径 | Dijkstra

热门文章

最新文章