URAL 1976 Game Optimization

题目链接:URAL1976

1976. Game Optimization

Time limit: 2.0 second
Memory limit: 64 MB
‘Is anything not working again?’
‘It works, but too slowly. I must constantly recalculate the distance from the ship to the nearest base to check if we can teleport the ship there. When there are too many bases on the map, it works very slowly.’
‘Is the map discrete?’
‘Yes. The map is an  n×  m grid made up of square cells. Ships and bases can be located at the centers of the cells only.’
‘Then you can precalculate the distances to the nearest base from the centers of each cell.’
‘That’s true. Let’s try it.’

Input

The first line contains integers  n and  m (1 ≤  nm ≤ 1 000), which are the numbers of rows and columns in the grid that makes up the map. Then you are given an  n×  m matrix consisting of zeros and ones. One means that there is a base at the center of the corresponding cell, and zero means that there is no base at that cell. It is guaranteed that there is at least one base and at least one free cell on the map.

Output

Output  n lines with  m integers in each line. The  j-th number in the  i-th line must be equal to the squared distance from the center of the cell ( ij) to the nearest base (1 ≤  i ≤  n; 1 ≤  j ≤  m). The numbers in each line must be separated by a space.

Sample

input output
3 4
1000
0000
0010
0 1 4 5 
1 2 1 2 
4 1 0 1 
Problem Source: Ural Sport Programming Championship 2013
Tags: none   (
hide tags for unsolved problems
)

大概题意就是给你一个01矩阵,1代表该处有一个基地,让你求出任意点到最近基地的欧几里得距离的平方。

一开始以为是裸的K-D树来着,虽然感觉可能会T,但是没有什么好想法,就直接用K-D树来搞。结果妥妥的T了。

到最后只有半小时左右才想到一个算法雏形,但是最后还是没搞完。

这题据说可以用线段树维护左上最近点,然后跑4个方向来搞(不会= = ) 或者是用dp+斜率优化(高端 = =)

当时想的算法是先对每一行更新最短距离,然后对每一列进行更新,这样。

但当时没找到什么比较好的数据结构来搞这个东西。赛后想了一下

发现可以利用一个距离函数一个比较好的性质来维护最短


啊啊啊啊  好麻烦啊  不想写了。。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;


char str[1005];
int a[1005][1005];
int cro[1005],lin[1005][2];
int cross(int i,int d,int x)
{
    double x0=1.0*(lin[i][0]-d+lin[i][1]*lin[i][1]-x*x)/(2*lin[i][1]-2*x);
    if(x0>0) x0=(int)x0;
    else x0=(int)x0-1.0;
    return (int)x0;
}
int main()
{
    freopen("1.txt","r",stdin);
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            a[i][j]=10000000;
    for(int i=0;i<n;i++)
    {
        scanf("%s",str);
        for(int j=0;j<m;j++)
            if(str[j]=='1') a[i][j]=0;
    }
    for(int i=0;i<n;i++)
    {
        int x=10000,y=10000;
        for(int j=0;j<m;j++)
        {
            if(!a[i][j]) x=j;
            if(!a[i][m-j-1]) y=j;
            a[i][j]=min(a[i][j],(j-x)*(j-x));
            a[i][m-j-1]=min(a[i][m-j-1],(j-y)*(j-y));
        }
    }
    for(int i=0;i<m;i++)
    {
        int x,k=1;
        lin[0][0]=0;lin[0][1]=-10000;
        cro[0]=-10000;
        for(int j=0;j<n;j++)
            if(a[j][i]<10000000)
            {
                while(cross(k-1,a[j][i],j)<cro[k-1]) k--;
                x=cross(k-1,a[j][i],j);
                cro[k]=max(0,x);
                lin[k][0]=a[j][i];
                lin[k++][1]=j;
            }
        cro[k]=n+5;k=1;
        for(int j=0;j<n;j++)
        {
            while(j==cro[k+1]+1) k++;
            a[j][i]=min(a[j][i],lin[k][0]+(lin[k][1]-j)*(lin[k][1]-j));
        }
    }
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(j==m-1) printf("%d\n",a[i][j]);
            else printf("%d ",a[i][j]);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值