当前位置: 首页 > news >正文

题解:AtCoder AT_awc0080_e Paint Drop

本文分享的必刷题目是从蓝桥云课洛谷AcWing等知名刷题平台精心挑选而来,并结合各平台提供的算法标签和难度等级进行了系统分类。题目涵盖了从基础到进阶的多种算法和数据结构,旨在为不同阶段的编程学习者提供一条清晰、平稳的学习提升路径。

欢迎大家订阅我的专栏:算法题解:C++与Python实现!

附上汇总贴:算法竞赛备考冲刺必刷题(C++) | 汇总


【题目来源】

AtCoder:E - Paint Drop

【题目描述】

Takahashi is playing a paint game on a grid canvas withH HHrows andW WWcolumns. The cell at thei ii-th row from the top and thej jj-th column from the left is called cell( i , j ) (i, j)(i,j).

Cell( i , j ) (i, j)(i,j)has a scoreA i , j A_{i,j}Ai,jassigned to it. Initially, no cells are painted.

Takahashi will drop paint onto the canvasQ QQtimes.

In thet tt-th paint drop, all cells on the canvas whose Manhattan distance from cell( R t , C t ) (R_t, C_t)(Rt,Ct)is at mostD t D_tDtare painted.

In other words, among all cells( i , j ) (i, j)(i,j)satisfying1 ≤ i ≤ H 1 \leq i \leq H1iHand1 ≤ j ≤ W 1 \leq j \leq W1jW, all cells satisfying

∣ i − R t ∣ + ∣ j − C t ∣ ≤ D t |i - R_t| + |j - C_t| \leq D_tiRt+jCtDt

are painted.

When a cell that has not yet been painted becomes newly painted, Takahashi earns the score of that cell. No score is earned when paint reaches a cell that is already painted.

For each paint drop, find the total score newly earned by Takahashi from that paint drop.

高桥在一个有H HHW WW列的网格画布上玩一个涂色游戏。从上往下第i ii行、从左往右第j jj列的单元格称为单元格( i , j ) (i, j)(i,j)

单元格( i , j ) (i, j)(i,j)有一个分配的分数A i , j A_{i,j}Ai,j。初始时,没有单元格被涂色。

高桥将在画布上滴下涂料Q QQ次。

在第t tt次涂料滴下时,所有距离单元格( R t , C t ) (R_t, C_t)(Rt,Ct)的曼哈顿距离不超过D t D_tDt的单元格都会被涂色。

换句话说,在满足1 ≤ i ≤ H 1 \leq i \leq H1iH1 ≤ j ≤ W 1 \leq j \leq W1jW的所有单元格( i , j ) (i, j)(i,j)中,所有满足

∣ i − R t ∣ + ∣ j − C t ∣ ≤ D t |i - R_t| + |j - C_t| \leq D_tiRt+jCtDt

的单元格都会被涂色。

当一个尚未被涂色的单元格被涂色时,高桥获得该单元格的分数。当涂料到达一个已经涂色的单元格时,不会获得分数。

对于每次涂料滴下,求高桥从该次滴下中新获得的分数总和。

【输入】

H HHW WW
A 1 , 1 A_{1,1}A1,1A 1 , 2 A_{1,2}A1,2⋯ \cdotsA 1 , W A_{1,W}A1,W
A 2 , 1 A_{2,1}A2,1A 2 , 2 A_{2,2}A2,2⋯ \cdotsA 2 , W A_{2,W}A2,W
⋮ \vdots
A H , 1 A_{H,1}AH,1A H , 2 A_{H,2}AH,2⋯ \cdotsA H , W A_{H,W}AH,W
Q QQ
R 1 R_1R1C 1 C_1C1D 1 D_1D1
R 2 R_2R2C 2 C_2C2D 2 D_2D2
⋮ \vdots
R Q R_QRQC Q C_QCQD Q D_QDQ

  • The first line contains the number of rowsH HHand the number of columnsW WWof the canvas, separated by a space.
  • In the followingH HHlines, thei ii-th line contains the scoresA i , 1 , A i , 2 , … , A i , W A_{i,1}, A_{i,2}, \ldots, A_{i,W}Ai,1,Ai,2,,Ai,Wseparated by spaces.
  • The next line contains the number of paint dropsQ QQ.
  • In the followingQ QQlines, thet tt-th line contains the center rowR t R_tRt, columnC t C_tCt, and reach distanceD t D_tDtof thet tt-th paint drop, separated by spaces.

【输出】

OutputQ QQlines.

Thet tt-th line should contain the total score newly earned by Takahashi from thet tt-th paint drop.

【输入样例】

3 4 1 2 3 4 5 6 7 8 9 10 11 12 4 2 2 1 1 4 0 3 1 2 2 3 7

【输出样例】

30 4 21 23

【算法标签】

#并查集

【代码详解】

#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongconstintN=200005;inth,w,q;// h: 矩阵行数,w: 矩阵列数,q: 查询次数vector<vector<int>>a;// 矩阵vector<vector<int>>nxt;// 并查集数组,记录每个位置的下一个可访问位置intans[N];// 存储查询结果intfind(introw,intcol)// 查找当前行col位置的下一个可访问位置{if(col>w)// 如果超出列范围returnw+1;// 返回w+1表示结束if(nxt[row][col]!=col)// 路径压缩nxt[row][col]=find(row,nxt[row][col]);returnnxt[row][col];// 返回下一个可访问位置}signedmain(){cin>>h>>w;// 输入矩阵行数和列数a.assign(h+1,vector<int>(w+1));// 初始化矩阵nxt.assign(h+1,vector<int>(w+2));// 初始化并查集数组for(inti=1;i<=h;i++)// 输入矩阵元素for(intj=1;j<=w;j++)cin>>a[i][j];for(inti=1;i<=h;i++)// 初始化并查集数组for(intj=1;j<=w+1;j++)nxt[i][j]=j;cin>>q;// 输入查询次数while(q--)// 处理每个查询{intr,c,d;// 查询中心(r, c)和半径dcin>>r>>c>>d;intsum=0;// 初始化总和for(inti=max(1LL,r-d);i<=min(h,r+d);i++)// 遍历行范围{intrem=d-abs(r-i);// 计算剩余半径intjL=max(1LL,c-rem);// 计算列范围左边界intjR=min(w,c+rem);// 计算列范围右边界intj=find(i,jL);// 找到第一个可访问位置while(j<=jR)// 遍历可访问位置{sum+=a[i][j];// 累加元素值nxt[i][j]=find(i,j+1);// 标记当前位置已访问j=find(i,j);// 查找下一个可访问位置}}cout<<sum<<endl;// 输出查询结果}return0;}

【运行结果】

3 4 1 2 3 4 5 6 7 8 9 10 11 12 4 2 2 1 30 1 4 0 4 3 1 2 21 2 3 7 23
http://www.cnnetsun.cn/news/2692746.html

相关文章:

  • “聚焦法则”——把所有资源钉在一个窄点上,击穿后形成复利
  • Streamlit(十八)- API 参考文档(十一)- 页面导航组件
  • SpikingJelly泊松编码实战:从图像处理到SNN模型输入的完整数据流水线
  • 智能垃圾桶项目成本大揭秘:从零到量产,SG90舵机、SW-18010P震动传感器到底怎么选最划算?
  • 用于自动维护一个 C# 源码文件(AutoVersion.cs)
  • CANoe自动化测试进阶:巧用setPreTrigger和setPostTrigger,让你的CPAL脚本精准捕获‘事发瞬间’的数据
  • 医疗边缘AI硬件加速:CMOS ASIC、FPGA与忆阻器技术解析与应用
  • 告别‘元素不可见’:Selenium+Pytest处理shadow-root的完整避坑指南
  • 新能源电站电能质量数据采集解决方案
  • java matches Java匹配上瘾?这编程语言让你从菜鸟秒变大神
  • DownGit:基于GitHub API的前端资源精准下载技术方案
  • 如何在Fusion 360中创建完美适配3D打印的螺纹:终极配置指南
  • 基于GSM与Arduino的远程门锁系统:从硬件选型到软件编程的完整实战指南
  • 3分钟掌握ComfyUI IPAdapter Plus:让AI绘画学会“看图说话“的神器
  • 电路设计跨界实践:从Arduino到智能生活项目的创意实现
  • AWS DevOps Agent 集成运维文档
  • LinkSwift:一站式网盘直链下载助手,高效解决九大平台文件下载难题
  • 戴森吸尘器电池复活终极指南:开源固件如何打破32次闪烁的死亡魔咒
  • STM32小车主控工程:支持思岚雷达、自动回充与多传感器避障(IAR环境)
  • 基于Arduino与Python的PC屏幕自动亮度调节系统设计与实现
  • KMS_VL_ALL_AIO:Windows和Office智能激活工具的终极完整指南
  • 3个颠覆性功能:为什么Trelby正在改变剧本创作的游戏规则?
  • Linux内核里那个默默无闻的‘搬运工’:SWIOTLB的bounce buffer机制详解
  • 零成本免费用,每年少花400块省130小时,2026快手视频总结,不算这笔账你亏大了
  • CDMP 认证赋能企业数据治理实战指南
  • STM32F4智能鱼缸实战工程:FreeRTOS多任务管理+LCD触摸显示+ESP8266直连机智云
  • 从“激光灭蚊神器”爆单说起:出口企业,你的数据扛得住“幸福的烦恼”吗?
  • 从《孤勇者》到周杰伦:拆解流行歌谱里的‘换气V’和‘跳音三角’,让你的翻唱更有细节
  • 练习题题目
  • 5个关键特性深度解析:RTL8821CU Linux驱动如何让USB Wi-Fi适配器在Linux上完美运行