[COCI2015]JABUKE
题目大意:
一个n×m(n,m≤500)的网格图中有若干个标记点,有q(q\leq10^5)个操作,每次新加入一个标记点,并询问和新加入点最近的点的距离。
思路:
记录对于每个点(x,y),同一列上在它上面最近的点u(x,y)和在它下面最近的点d(x,y)。
每次询问时枚举同一行的点的u(x,j)和d(x,j),取距离最小值即可,时间复杂度O(q(n+m))。
1 #include<cstdio> 2 #include<cctype> 3 #include<climits> 4 #include<algorithm> 5 inline int getint() { 6 register char ch; 7 while(!isdigit(ch=getchar())); 8 register int x=ch^'0'; 9 while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0'); 10 return x; 11 } 12 inline bool isblock(const char &ch) { 13 return ch=='.'||ch=='x'; 14 } 15 inline bool getblock() { 16 register char ch; 17 while(!isblock(ch=getchar())); 18 return ch=='x'; 19 } 20 const int N=500; 21 bool map[N][N]; 22 int n,m,u[N][N],d[N][N]; 23 inline int sqr(const int &x) { 24 return x*x; 25 } 26 inline int dist(const int &x1,const int &y1,const int &x2,const int &y2) { 27 return sqr(x1-x2)+sqr(y1-y2); 28 } 29 inline void update(const int &j) { 30 for(register int i=0,last=-1;i<n;i++) { 31 if(map[i][j]) last=i; 32 u[i][j]=last; 33 } 34 for(register int i=n-1,last=-1;~i;i--) { 35 if(map[i][j]) last=i; 36 d[i][j]=last; 37 } 38 } 39 inline int query(const int &x,const int &y) { 40 int ans=INT_MAX; 41 for(register int j=0;j<m;j++) { 42 if(~u[x][j]) ans=std::min(ans,dist(x,y,u[x][j],j)); 43 if(~d[x][j]) ans=std::min(ans,dist(x,y,d[x][j],j)); 44 } 45 return ans; 46 } 47 int main() { 48 n=getint(),m=getint(); 49 for(register int i=0;i<n;i++) { 50 for(register int j=0;j<m;j++) { 51 map[i][j]=getblock(); 52 } 53 } 54 for(register int j=0;j<m;j++) { 55 update(j); 56 } 57 for(register int q=getint();q;q--) { 58 const int x=getint()-1,y=getint()-1; 59 printf("%d\n",query(x,y)); 60 map[x][y]=true; 61 update(y); 62 } 63 return 0; 64 }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· 一个基于 .NET 开源免费的异地组网和内网穿透工具
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单