您的位置:首页 > 其它

枚举 51Nod1487 占领资源

2016-04-07 20:18 232 查看
传送门:点击打开链接

题意:有一个矩形区域被划分为N行M列的网格,每个格子里有一定数量的资源并记录在矩阵val中,坐标(x,y)位置上资源量为val[x][y],其val中每个元素的值为0~9的整数。如果你在某个网格(a,b)上造一座保护塔,那么你可以占领K个网格中的资源,这K个格子分别是(a+dx[1],b+dy[1]),(a+dx[2],b+dy[2]),...,(a+dx[K],b+dy[K]),注意(a,b)这格本身可能未必会被占领。现在你能建造不超过2个塔,问最多能占领多少资源?一个网格被多个塔占领时其资源只计算一次。另外如果计算的位置(a+dx[i],b+dy[i])在网格外,则不贡献任何资源。

思路:如果直接去枚举,肯定是不行的。

我们先考虑去预处理出一个塔能得到的最多资源。

之后,我们去枚举第一个塔的位置,那么第二个塔的位置,有2种可能。

一种是与第一座塔有冲突,一种是与第一座塔没有冲突。

我们可以去枚举有冲突的情况,因为k<=10,我们直接枚举是哪两个地方的资源冲突,复杂度O(k^2)

对于有冲突的塔,我们算出冲突的资源总和,之后要减掉重复的

对于没有冲突的塔,在我们算出哪些冲突以后,剩下的就是没有冲突的了

我们直接用RMQ预处理,然后区间O(1)查询最大

最后总复杂度O(n*m*log(n*m))

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define fuck(x) cout<<"["<<x<<"]";
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w+",stdout);
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int MX = 1e2 + 5;
const int MS = 1e4 + 5;
const int INF = 0x3f3f3f3f;

int n, m, k;
char W[MX][MX];
int A[MS], MAX[MS][15], vis[MX][MX];
int dx[15], dy[15], dq[MS];
void RMQ_init(int n) {
for(int i = 0; i < n + 1; i++) {
MAX[i][0] = A[i];
}
for(int j = 1; (1 << j) <= n + 1; j++) {
for(int i = 0; i + (1 << j) - 1 < n + 1; i++) {
MAX[i][j] = max(MAX[i][j - 1], MAX[i + (1 << (j - 1))][j - 1]);
}
}
}
int RMQ_max(int L, int R) {
int k = 0;
while((1 << (k + 1)) <= R - L + 1) k++;
return max(MAX[L][k], MAX[R - (1 << k) + 1][k]);
}
bool umax(int &a, int b) {
if(b > a) {
a = b; return true;
}
return false;
}
inline int ID(int x, int y) {
return (x - 1) * m + y;
}
int solve() {
int ret = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
int Max = 0, id = ID(i, j), dsz = 0;
for(int q = 1; q <= k; q++) {
int nx = i + dx[q], ny = j + dy[q];
if(nx < 1 || nx > n || ny < 1 || ny > m) continue;
vis[nx][ny] = id;

for(int w = 1; w <= k; w++) {
int nnx = nx - dx[w], nny = ny - dy[w];
if(nnx < 1 || nnx > n || nny < 1 || nny > m) continue;
dq[++dsz] = ID(nnx, nny);
}
}

dq[++dsz] = 0;
dq[++dsz] = m * n + 1;
sort(dq + 1, dq + 1 + dsz);
dsz = unique(dq + 1, dq + 1 + dsz) - dq - 1;

for(int q = 1; q <= dsz - 1; q++) {
if(dq[q] + 1 <= dq[q + 1] - 1) umax(Max, RMQ_max(dq[q] + 1, dq[q + 1] - 1));
}
for(int q = 2; q <= dsz - 1; q++) {
int x = (dq[q] - 1) / m + 1, y = (dq[q] - 1) % m + 1, sum = A[ID(x, y)];
for(int w = 1; w <= k; w++) {
int nx = x + dx[w], ny = y + dy[w];
if(nx < 1 || nx > n || ny < 1 || ny > m) continue;
if(vis[nx][ny] == id) sum -= W[nx][ny] - '0';
}
umax(Max, sum);
}
umax(ret, Max + A[id]);
}
}
return ret;
}
int main() {
//FIN;
int T; scanf("%d", &T);
while(T--) {
scanf("%d%d%d", &n, &m, &k);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
vis[i][j] = A[ID(i, j)] = 0;
}
}

for(int i = 1; i <= n; i++) scanf("%s", W[i] + 1);
for(int i = 1; i <= k; i++) {
scanf("%d%d", &dx[i], &dy[i]);
}

for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
int id = ID(i, j);
for(int q = 1; q <= k; q++) {
int nx = i + dx[q], ny = j + dy[q];
if(nx < 1 || nx > n || ny < 1 || ny > m) continue;
A[id] += W[nx][ny] - '0';
}
}
}
RMQ_init(n * m);
printf("%d\n", solve());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: