【NOIP2014】无线网络发射器选址
本题在洛谷上的链接:https://www.luogu.org/problemnew/show/P2038
估计是NOIP2014的Day1 T1,虽然比较水,但我还是正儿八经做了。
可能只要考虑每个发射器,然后暴力做加法就可以A掉。而我是用二维前缀和与差分做的。
还好这样做,让我找到了我二维差分的一个错误,原来我二维矩阵做加法的式子就搞错了,默默改正。。。
没有太多坑,就看二维前缀和与差分掌握得如何。
1 #include <cstdio> 2 3 const int mmax = 130; 4 5 int diff[mmax][mmax], sum[mmax][mmax]; 6 7 int main() { 8 int d, n, x, y, k, ans = 0, cnt = 0; 9 scanf("%d%d", &d, &n); 10 for (int i = 1; i <= n; ++i) { 11 scanf("%d%d%d", &x, &y, &k); 12 int xa = x - d, xb = x + d, ya = y - d, yb = y + d; 13 if (xa < 0) xa = 0; 14 if (xb > 128) xb = 128; 15 if (ya < 0) ya = 0; 16 if (yb > 128) yb = 128; 17 diff[xa][ya] += k; 18 diff[xa][yb + 1] -= k; 19 diff[xb + 1][ya] -= k; 20 diff[xb + 1][yb + 1] += k; 21 } 22 for (int i = 0; i <= 128; ++i) { 23 sum[i][0] = diff[i][0], sum[0][i] = diff[0][i]; 24 if (i) sum[i][0] += sum[i - 1][0], sum[0][i] += sum[0][i - 1]; 25 if (sum[i][0] > ans) 26 ans = sum[i][0], cnt = 1; 27 else if (sum[i][0] == ans) ++cnt; 28 if (!i) continue; 29 if (sum[0][i] > ans) 30 ans = sum[0][i], cnt = 1; 31 else if (sum[0][i] == ans) ++cnt; 32 } 33 for (int i = 1; i <= 128; ++i) 34 for (int j = 1; j <= 128; ++j) { 35 sum[i][j] = sum[i][j - 1] + sum[i - 1][j] - sum[i - 1][j - 1] + diff[i][j]; 36 if (sum[i][j] > ans) 37 ans = sum[i][j], cnt = 1; 38 else if (sum[i][j] == ans) ++cnt; 39 } 40 printf("%d %d", cnt, ans); 41 return 0; 42 }