[NOIP 2014] 无线网络发射器选址

[题目链接]

        http://uoj.ac/problem/18

[算法]

        二维前缀和

[代码]

        

#include<bits/stdc++.h>
using namespace std;
#define MAXN 300
#define calc(x1,y1,x2,y2) s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1]

int i,j,d,n,ans,cnt,x,y,k,ta,tb,tx,ty;
int s[MAXN][MAXN];

template <typename T> inline void read(T &x)
{
    int f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) 
        { 
                if (c == '-') f = -f; 
        }
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
    x *= f;
}

int main() 
{
        
        read(d); read(n);
        for (i = 1; i <= n; i++) 
        {
                read(x); read(y); read(k);
                x++; y++;
                s[x][y] = k;
        }
        for (i = 1; i <= 129; i++)
        {
                for (j = 1; j <= 129; j++)
                {
                        s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + s[i][j];
                }
        }
        for (i = 1; i <= 129; i++)
        {
                for (j = 1; j <= 129; j++)
                {
                        ta = max(i - d,1);
                        tb = max(j - d,1);
                        tx = min(i + d,129);
                        ty = min(j + d,129);
                        if (calc(ta,tb,tx,ty) > ans)
                        {
                                ans = calc(ta,tb,tx,ty);
                                cnt = 1;
                        } else if (calc(ta,tb,tx,ty) == ans) cnt++;        
                }        
        }
        printf("%d %d
",cnt,ans);
        
        return 0;
    
}
原文地址:https://www.cnblogs.com/evenbao/p/9477409.html