【ABC 203】D - Pond:二分 + 前缀和

本文介绍了一种利用二分搜索和矩阵前缀和解决特定问题的方法。通过将矩阵转换为01矩阵并计算前缀和,可以有效地判断是否存在满足条件的子矩阵。此方法适用于竞赛编程中涉及子矩阵计数的问题。

传送门

分析

二分一下,把矩阵改成 01 01 01矩阵,搞一下前缀和即可

代码

#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 1e3 + 10;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
	char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
	while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
int a[N][N];
int sum[N][N];
int n,k;

bool check(int mid){
	for(int i = 1;i <= n;i++)
		for(int j = 1;j <= n;j++)
			sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + (a[i][j] >= mid);
	for(int i = k;i <= n;i++)
		for(int j = k;j <= n;j++){
			if(sum[i][j] - sum[i - k][j] - sum[i][j - k] + sum[i - k][j - k] < (k * k / 2) + 1) return false;
		}
	return true;
}

int main() {
	read(n),read(k);
	for(int i = 1;i <= n;i++)
		for(int j = 1;j <= n;j++)
			read(a[i][j]);
	int l = 0,r = INF;
	while(l < r){
		int mid = (l + r + 1) >> 1;
		if(check(mid)) l = mid;
		else r = mid - 1;
	}
	di(l);
	return 0;
}

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃        ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/


<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>鱿鱼与池塘的奔跑冒险</title> <style> body { margin: 0; padding: 20px; display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 100vh; background: linear-gradient(to bottom, #87CEEB, #1E90FF); font-family: 'Arial Rounded MT Bold', 'Arial', sans-serif; color: #333; } h1 { color: #FF6B6B; text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3); margin-bottom: 10px; } .game-container { position: relative; margin: 20px 0; } #gameCanvas { background-color: #7CFC00; border: 5px solid #8B4513; border-radius: 10px; box-shadow: 0 0 20px rgba(0, 0, 0, 0.5); } .controls { background-color: rgba(255, 255, 255, 0.8); padding: 15px; border-radius: 10px; margin-top: 20px; max-width: 600px; text-align: center; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); } .character-info { display: flex; justify-content: space-around; margin-bottom: 15px; } .character { text-align: center; padding: 10px; border-radius: 8px; width: 45%; } .squid { background-color: #FFB6C1; } .pond { background-color: #ADD8E6; } button { background-color: #4CAF50; border: none; color: white; padding: 12px 24px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 10px 5px; cursor: pointer; border-radius: 5px; transition: background-color 0.3s; } button:hover { background-color: #45a049; } .score { font-size: 24px; font-weight: bold; margin: 10px 0; color: #FFD700; text-shadow: 1px 1px 2px #000; } .instructions { margin-top: 15px; font-size: 14px; line-height: 1.5; } </style> </head> <body> <h1>鱿鱼与池塘的奔跑冒险</h1> <div class="score">得分: <span id="score">0</span></div> <div class="game-container"> <canvas id="gameCanvas" width="800" height="400"></canvas> </div> <div class="controls"> <div class="character-info"> <div class="character squid"> <h3>鱿鱼</h3> <p>按 W 键跳跃</p> </div> <div class="character pond"> <h3>池塘</h3> <p>按 ↑ 键跳跃</p> </div> </div> <button id="startBtn">开始游戏</button> <button id="pauseBtn">暂停</button> <button id="restartBtn">重新开始</button> <div class="instructions"> <p>控制两个角色跳跃躲避障碍物!鱿鱼按W键,池塘按↑键。两个角色都需要安全通过障碍物才能得分!</p> </div> </div> <script> // 获取Canvas和上下文 const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); // 游戏变量 let gameRunning = false; let gamePaused = false; let score = 0; let gameSpeed = 5; // 角色定义 const squid = { x: 150, y: canvas.height - 100, width: 40, height: 60, color: '#FF69B4', jumping: false, jumpPower: 0, jumpForce: 15, gravity: 0.8 }; const pond = { x: 250, y: canvas.height - 100, width: 40, height: 60, color: '#1E90FF', jumping: false, jumpPower: 0, jumpForce: 15, gravity: 0.8 }; // 障碍物数组 let obstacles = []; // 绘制角色 function drawCharacter(character, name) { // 身体 ctx.fillStyle = character.color; ctx.fillRect(character.x, character.y, character.width, character.height); // 眼睛 ctx.fillStyle = 'white'; ctx.fillRect(character.x + 5, character.y + 10, 10, 10); ctx.fillRect(character.x + character.width - 15, character.y + 10, 10, 10); ctx.fillStyle = 'black'; ctx.fillRect(character.x + 8, character.y + 12, 5, 5); ctx.fillRect(character.x + character.width - 12, character.y + 12, 5, 5); // 名字标签 ctx.fillStyle = 'black'; ctx.font = '12px Arial'; ctx.fillText(name, character.x, character.y - 5); } // 绘制鱿鱼 function drawSquid() { drawCharacter(squid, '鱿鱼'); // 鱿鱼触手 ctx.fillStyle = squid.color; for (let i = 0; i < 5; i++) { ctx.fillRect(squid.x - 10, squid.y + 15 + i * 8, 10, 5); } } // 绘制池塘 function drawPond() { drawCharacter(pond, '池塘'); // 池塘的水波纹 ctx.strokeStyle = '#87CEEB'; ctx.lineWidth = 2; ctx.beginPath(); ctx.arc(pond.x + pond.width/2, pond.y + pond.height/2, 15, 0, Math.PI * 2); ctx.stroke(); ctx.beginPath(); ctx.arc(pond.x + pond.width/2, pond.y + pond.height/2, 25, 0, Math.PI * 2); ctx.stroke(); } // 绘制障碍物 function drawObstacles() { ctx.fillStyle = '#8B4513'; obstacles.forEach(obstacle => { ctx.fillRect(obstacle.x, obstacle.y, obstacle.width, obstacle.height); }); } // 更新角色位置 function updateCharacters() { // 更新鱿鱼 if (squid.jumping) { squid.y -= squid.jumpPower; squid.jumpPower -= squid.gravity; if (squid.y >= canvas.height - 100) { squid.y = canvas.height - 100; squid.jumping = false; } } // 更新池塘 if (pond.jumping) { pond.y -= pond.jumpPower; pond.jumpPower -= pond.gravity; if (pond.y >= canvas.height - 100) { pond.y = canvas.height - 100;
09-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值