题目链接  Problem J
这道题思路还是很直观的,但是有两个难点:

1、题目中说$1<=NM<=10^{6}$,但没具体说明$N$和$M$的值,也就是可能出现:

$N = 1, M = 1000000$ 这样的数据。

2、对每个查询的分类讨论。

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n)                for(int i(0); i <  (n); ++i)
#define rep(i,a,b) for(int i(a); i <= (b); ++i)
#define dec(i,a,b) for(int i(a); i >= (b); --i)
#define for_edge(i,x) for(int i = H[x]; i; i = X[i]) const int N = 1000000 + 10;
const int Q = 1000 + 10; struct node{
int num, x, y, id;
} c[N], f[N]; int ret[Q], nc[Q], nd[Q], ne[Q], ccc[Q]; int ct[N];
int n, m, q;
int cnt;
char st[N];
int T;
int v[N], t[N];
int l, r;
int color;
int col[N];
int x, y; struct nnnn{
int x, y;
} nf[6]; inline bool check(int nu, int pos){
if (nu < 1 || nu > cnt) return false;
if (v[nu]) return false;
int xx1 = c[nu].x, yy1 = c[nu].y, xx2 = c[pos].x, yy2 = c[pos].y;
if (xx1 < 1 || xx1 > n || yy1 < 1 || yy1 > m) return false;
if (abs(xx1 - xx2) + abs(yy1 - yy2) != 1) return false;
if (c[nu].num != c[pos].num) return false;
return true;
} inline bool cc(int nu, int pos){
if (nu < 1 || nu > cnt) return false;
int xx1 = c[nu].x, yy1 = c[nu].y, xx2 = c[pos].x, yy2 = c[pos].y;
if (xx1 < 1 || xx1 > n || yy1 < 1 || yy1 > m) return false;
if (abs(xx1 - xx2) + abs(yy1 - yy2) != 1) return false;
return true;
} inline int pos(int x, int y){ return (x - 1) * m + y;} inline void work(int nu){
t[nu] = color;
f[++r].x = c[nu].x,
f[r].y = c[nu].y,
f[r].id = nu;
v[nu] = 1;
} int main(){ scanf("%d", &T);
while (T--){
scanf("%d%d%d", &n, &m, &q);
cnt = 0;
rep(i, 1, n){
scanf("%s", st + 1);
rep(j, 1, m) ++cnt,
c[cnt].num = (int)st[j] - 48,
c[cnt].x = i, c[cnt].y = j;
} color = 0;
rep(i, 1, cnt) v[i] = 0, t[i] = 0;
rep(i, 1, cnt) if (v[i] == 0){
++color;
t[i] = color;
ct[color] = c[i].num;
f[1].x = c[i].x,
f[1].y = c[i].y,
f[1].id = i;
for (l = r = 1; l <= r; ++l){
int now = f[l].id;
if (check(now - 1, now)) work(now - 1);
if (check(now + 1, now)) work(now + 1);
if (check(now - m, now)) work(now - m);
if (check(now + m, now)) work(now + m);
}
} int maxcol = 0;
rep(i, 1, cnt) col[i] = 0;
int ta = 0;
rep(i, 1, n){
rep(j, 1, m){
++ta;
++col[t[ta]];
maxcol = max(maxcol, t[ta]);
}
} while (q--){
scanf("%d%d", &x, &y);
int df = 0, np = pos(x, y);
if (cc(np - 1, np)) ret[++df] = np - 1;
if (cc(np + 1, np)) ret[++df] = np + 1;
if (cc(np - m, np)) ret[++df] = np - m;
if (cc(np + m, np)) ret[++df] = np + m; rep(i, 1, df) nc[i] = t[ret[i]];
sort(nc + 1, nc + df + 1);
nd[1] = nc[1];
int dx = 1;
rep(i, 2, df) if (nc[i] != nc[i - 1]) nd[++dx] = nc[i];
bool fl = false;
rep(i, 1, dx) if (nd[i] == t[np]){ fl = true; break;} if (!fl){
rep(i, 1, dx) ne[i] = ct[nd[i]];
rep(i, 0, 9) ccc[i] = 0;
rep(i, 1, dx) ccc[ne[i]] += col[nd[i]];
int lk = 0; rep(i, 0, 9) lk = max(lk, ccc[i]);
printf("%d\n", lk + 1);
} else
{
int spj = 0; rep(i, 1, dx) if (nd[i] == t[np]) spj += col[nd[i]];
int dd = 0; rep(i, 1, dx) if (nd[i] != t[np]) nf[++dd].x = ct[nd[i]], nf[dd].y = nd[i];
rep(i, 0, 9) ccc[i] = 0; rep(i, 1, dd) ccc[nf[i].x] += col[nf[i].y];
int lk = 0; rep(i, 0, 9) lk = max(lk, ccc[i]);
printf("%d\n", max(lk + 1, spj));
}
}
} return 0;
}

第十二届北航程序设计竞赛决赛网络同步赛 J题 两点之间的更多相关文章

  1. 第十二届北航程序设计竞赛决赛网络同步赛 B题 前前前世(数论推导 + DP)

    题目链接  2016 BUAA-Final Problem B 考虑一对可行的点$(x, y)$ 根据题意,设$x = ak + 1,y = bk + 1$ 又因为$x$是$y$的祖先的祖先的祖先,所 ...

  2. 第十三届北航程序设计竞赛决赛网络同步赛 B题 校赛签到(建树 + 打标记)

    题目链接  校赛签到 对每个操作之间建立关系. 比较正常的是前$3$种操作,若第$i$个操作属于前$3$种,那么就从操作$i-1$向$i$连一条有向边. 比较特殊的是第$4$种操作,若第$i$个操作属 ...

  3. A. Srdce and Triangle--“今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛)

    如下图这是“今日头条杯”首届湖北省大学程序设计竞赛的第一题,作为赛后补题 题目描述:链接点此 这套题的github地址(里面包含了数据,题解,现场排名):点此 Let  be a regualr tr ...

  4. “今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛 )--E. DoveCCL and Resistance

    题目描述:链接点此 这套题的github地址(里面包含了数据,题解,现场排名):点此 链接:https://www.nowcoder.com/acm/contest/104/D来源:牛客网 题目描述 ...

  5. I. Five Day Couple--“今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛)

    题目描述:链接点此 这套题的github地址(里面包含了数据,题解,现场排名):点此 链接:https://www.nowcoder.com/acm/contest/104/H来源:牛客网 题目描述 ...

  6. D. Who killed Cock Robin--“今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛)

    题目描述:链接点此 这套题的github地址(里面包含了数据,题解,现场排名):点此 题目描述 由于系统限制,C题无法在此评测,此题为现场赛的D题 Who killed Cock Robin? I, ...

  7. H. GSS and Simple Math Problem--“今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛)

    题目描述:链接点此 这套题的github地址(里面包含了数据,题解,现场排名):点此 题目描述 Given n positive integers , your task is to calculat ...

  8. 2016广东工业大学新生杯决赛网络同步赛暨全国新生邀请赛 题解&源码

    Problem A: pigofzhou的巧克力棒 Description 众所周知,pigofzhou有许多妹子.有一天,pigofzhou得到了一根巧克力棒,他想把这根巧克力棒分给他的妹子们.具体 ...

  9. 北京师范大学第十六届程序设计竞赛决赛 I 如何办好比赛

    链接:https://www.nowcoder.com/acm/contest/117/I来源:牛客网 如何办好比赛 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他 ...

随机推荐

  1. datatime来计算代码段运行时长

    小知识点:编程中一般都是先乘后除,这样结果更为精确 先定义 DataTime startTime = DataTime.Now; 中间是运行代码 最后TimeSpan ts = DataTime.No ...

  2. Python 代码优化技巧(一)

    Table of Contents 1. 代码优化Part1 1.1. if 判断的短路特性 1.2. join 合并字符串 1.3. while 1 和 while True 1.4. cProfi ...

  3. P1198 [JSOI2008]最大数(单调栈)

    P1198 [JSOI2008]最大数 题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. 限制: ...

  4. python如何合并两个字典

    我有两个Python字典,如何合并它们呢?update()方法正是你所需要的. >>> x = {'a':1, 'b': 2} >>> y = {'b':10, ' ...

  5. loj2059 「TJOI / HEOI2016」字符串

    字符串好难啊不会啊 #include <iostream> #include <cstdio> using namespace std; int n, m, rnk[10000 ...

  6. 【Copy List with Random Pointer】cpp

    题目: A linked list is given such that each node contains an additional random pointer which could poi ...

  7. IOS开发学习笔记027-UITableView 使用模型对象

    1.模型对象 2.单组数据的显示 1.模型对象 继续优化上一个程序 上一次用到字典,但是坏处多多.这里将这些数据封装到类中. 这就是MVC中得模型,模型就是数据的显示结构 新建一个类,添加几个属性和一 ...

  8. RSA进阶之低加密指数攻击

    适用场景: n很大,4000多位,e很小,e=3 一般来说,e选取65537.但是在RSA加密的时候有可能会选用e=3(不要问为什么,因为选取e =3省时省力,并且他可能觉得n在4000多位是很安全的 ...

  9. python 学习分享-面向对象

    好激动,学了四个月的面向对象,我终于勉勉强强的把作业做完了,现在来重构我的面向对象的知识! 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 ...

  10. LeetCode with Python -> Dynamic Programming

    198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...