题目链接:URAL1976
1976. Game Optimization
Time limit: 2.0 second
Memory limit: 64 MB
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.’
‘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 ≤
n,
m ≤ 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 (
i,
j) 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
)
大概题意就是给你一个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;
}