2016 ACM/ICPC 青岛赛区网络赛 XM Reserves

2 篇文章 0 订阅

HDU5885

Problem Description
As an eligible Ingress Resistance Agent you should know your power source, the Exotic Matter.
We call it XM, which is the driving force behind all of our actions in Ingress.
XM allows us to construct items through hacking portals, to attack enemy portals, make links and create fields.
We try to collect XM from the ground. XM concentration come from location based services, meaning that areas with a lot of foot traffic have higher amounts versus places that don’t.
You can collect XM by moving through those areas.
The XM will be automatically harvested by your Scanner when it is within your interaction circle/range.
Alice decides to select a location such that she can collect XM as much as possible.
To simplify the problem, we consider the city as a grid map with size `n*m’ numbered from (0,0) to (n−1,m−1).
The XM concentration inside the block (i,j) is p(i,j).
The radius of your interaction circle is r.
We can assume that XM of the block (i,j) are located in the centre of this block.
The distance between two blocks is the Euclidean distance between their centres.
Alice stands in the centre of one block and collects the XM.
For each block with the distance d smaller than r to Alice, and whose XM concertation is p(i,j), Alice’s scanner can collects p(i,j)/(1+d) XM from it.
Help Alice to determine the maximum XM which she can collect once he stands in the centre of one block.
Input
There are multiple cases.
For each case, the first line consists two integers n,m (1≤n,m≤500) and one float-point number r (0≤r≤300).
Each of the following n line consists m non-negative float-point numbers corresponding to the XM concentrations inside each blocks.
Output
For each case, output the maximum XM which Alice can collect in one line.
Your answers should be rounded to three decimal places.
Sample Input
3 3 1
1 3 6
7 9 4
2 8 1
3 3 2
1 3 6
7 9 4
2 8 1
5 5 1.5
4 3 2 9 1
3 4 3 2 8
9 4 3 2 1
2 3 0 1 2
6 3 4 3 1
Sample Output
9.000
24.142
17.956
Time limit
10000 ms
Memory limit
102400 kB

此算法核心是利用FFT在 O(nlgn) 内计算出两个n次多项式相乘。

题目要求计算在 (nm) 矩阵内与矩阵内某一点欧式距离小于定长的元素加权值和的最大值,由于权和n,m,定点都有关系,所以不能为每个元素生成一个表。显然,暴力解法需要遍历每一点计算,它的复杂度是 O(mnr2) ,简单计算超过30s。

ps: 1e9 int/ll multiplications/additions in C++ is about 1.5/2 seconds 可利用here计算

若利用FFT计算,可以用一个向量表示位置信息和值,另一个向量表示某一个位置平移后的权。这样的复杂度为 O(max(mn,r2)lg(max(mn,r2))) ,足够完成任务。

向量A表示位置信息的值,最大为 mn 向量B表示某一个偏移量下的权,最大为 rr 向量C表示卷积得到的向量。

C(i,j)=i1+i2=ij1+j2=jA(i1,j1)B(i2,j2)

注意到向量C最大为 ((m+2r)(n+2R)) ,方便起见,将其转化为 (MM) 的向量,其中 M=max(m,n)+2R
式中 R=floor(r)

然后进行投影:

A(i,j)=A[iM+j]=Value[i][j]

B(i,j)=B[(i+R)M+j+R]=Weight[i][j],if(i2+j2<r2)

加R的原因是B中偏移量i,j最小为-R。

卷积计算结果即是向量C。

C(i,j)=C[M(i+R)+j+R]

最后取C(的实部)中最大值即可。

//FFT 模板 Begin
#include<algorithm> //swap max
#include<cmath> //acos
#include<vector>
using namespace std;
const double PI = acos(-1.0);
struct Complex // 复数
{
    double r, i;
    Complex(double _r = 0, double _i = 0) :r(_r), i(_i) {}
    Complex operator +(const Complex &b) {
        return Complex(r + b.r, i + b.i);
    }
    Complex operator -(const Complex &b) {
        return Complex(r - b.r, i - b.i);
    }
    Complex operator *(const Complex &b) {
        return Complex(r*b.r - i*b.i, r*b.i + i*b.r);
    }
};

void change(vector<Complex> &y, int len) // 二进制平摊反转置换 O(logn)  
{
    int i, j, k;
    for (i = 1, j = len / 2;i < len - 1;i++)
    {
        if (i < j) swap(y[i], y[j]);
        k = len / 2;
        while (j >= k)
        {
            j -= k;
            k /= 2;
        }
        if (j < k) j += k;
    }
}

void fft(vector<Complex> &y, int len, int on) //FFT:on=1; IFFT:on=-1
{
    change(y, len);
    for (int h = 2;h <= len;h <<= 1)
    {
        Complex wn(cos(-on * 2 * PI / h), sin(-on * 2 * PI / h));
        for (int j = 0;j < len;j += h)
        {
            Complex w(1, 0);
            for (int k = j;k < j + h / 2;k++)
            {
                Complex u = y[k];
                Complex t = w*y[k + h / 2];
                y[k] = u + t;
                y[k + h / 2] = u - t;
                w = w*wn;
            }
        }
    }
    if (on == -1)
        for (int i = 0;i < len;i++)
            y[i].r /= len;
}
//FFT 模板 End

#include<stdio.h>
int main(){
    #ifndef ONLINE_JUDGE
    freopen("D:/in.txt","r",stdin);
    #endif
    int n,m;
    double r;
    while(~scanf("%d%d%lf",&n,&m,&r)){
        int R = floor(r);
        int M = max(n,m) + 2 * R;
        int len = 1;
        while (len<=M*M) len<<=1; //扩容至2^n
        vector<Complex> A(len,0);
        vector<Complex> B(len,0);
        vector<Complex> C(len,0);
        for(int i = 0;i<n;i++){
            for(int j = 0;j<m;j++){
                int p;
                scanf("%d",&p);
                A[i*M+j] = Complex(p);
            }
        }
        for(int i = - R;i<=R;i++){
            for(int j = - R;j<=R;j++){
                if(i*i + j*j < r*r){
                    B[(i+R)*M+j+R] = Complex(1/(sqrt(i*i + j*j) + 1));
                }
            }
        }
        fft(A, len, 1);
        fft(B, len, 1);
        for (int i = 0;i < len;i++) {
            C[i] = A[i] * B[i];
        }
        fft(C, len, -1);
        double ans = C[R*M+R].r;
        for(int i = 0;i<n;i++){
            for(int j = 0;j<m;j++){
                ans = ans>C[(i+R)*M+j+R].r?ans:C[(i+R)*M+j+R].r;
            }
        }
        printf("%.3f\n",ans);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值