第十二届北航程序设计竞赛决赛网络同步赛 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题 两点之间的更多相关文章
- 第十二届北航程序设计竞赛决赛网络同步赛 B题 前前前世(数论推导 + DP)
题目链接 2016 BUAA-Final Problem B 考虑一对可行的点$(x, y)$ 根据题意,设$x = ak + 1,y = bk + 1$ 又因为$x$是$y$的祖先的祖先的祖先,所 ...
- 第十三届北航程序设计竞赛决赛网络同步赛 B题 校赛签到(建树 + 打标记)
题目链接 校赛签到 对每个操作之间建立关系. 比较正常的是前$3$种操作,若第$i$个操作属于前$3$种,那么就从操作$i-1$向$i$连一条有向边. 比较特殊的是第$4$种操作,若第$i$个操作属 ...
- A. Srdce and Triangle--“今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛)
如下图这是“今日头条杯”首届湖北省大学程序设计竞赛的第一题,作为赛后补题 题目描述:链接点此 这套题的github地址(里面包含了数据,题解,现场排名):点此 Let be a regualr tr ...
- “今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛 )--E. DoveCCL and Resistance
题目描述:链接点此 这套题的github地址(里面包含了数据,题解,现场排名):点此 链接:https://www.nowcoder.com/acm/contest/104/D来源:牛客网 题目描述 ...
- I. Five Day Couple--“今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛)
题目描述:链接点此 这套题的github地址(里面包含了数据,题解,现场排名):点此 链接:https://www.nowcoder.com/acm/contest/104/H来源:牛客网 题目描述 ...
- D. Who killed Cock Robin--“今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛)
题目描述:链接点此 这套题的github地址(里面包含了数据,题解,现场排名):点此 题目描述 由于系统限制,C题无法在此评测,此题为现场赛的D题 Who killed Cock Robin? I, ...
- H. GSS and Simple Math Problem--“今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛)
题目描述:链接点此 这套题的github地址(里面包含了数据,题解,现场排名):点此 题目描述 Given n positive integers , your task is to calculat ...
- 2016广东工业大学新生杯决赛网络同步赛暨全国新生邀请赛 题解&源码
Problem A: pigofzhou的巧克力棒 Description 众所周知,pigofzhou有许多妹子.有一天,pigofzhou得到了一根巧克力棒,他想把这根巧克力棒分给他的妹子们.具体 ...
- 北京师范大学第十六届程序设计竞赛决赛 I 如何办好比赛
链接:https://www.nowcoder.com/acm/contest/117/I来源:牛客网 如何办好比赛 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他 ...
随机推荐
- 11、python中的函数(基础)
一.什么是函数? 在数学中,x2+2x2+3=10这样的叫方程. 而ax2+bx2+c=d这样的才叫函数.数学的函数中,abcd等待输入的未知量叫自变量,它需要我们自己去输入,而x这种待求得未知量叫因 ...
- 动态加载css,js
function dynamicLoadCss(url) { var head = document.getElementsByTagName('head')[0]; var link = docum ...
- java多线程安全的问题
多线程中一定会遇到线程安全的问题,也就是在同一时间N条线程操作了同一变量,这个变量也叫做共享变量. 举例:(错误的示范) 设计一个投票系统,多个人可以对同一个的票数进行投票,下面看具体代码: 代码: ...
- Python 命令总结
本章内容 pip pip install -r requirement.py(里面写入需要安装的包的名字) pip install django==1.9 #需要安装那个版本 P ...
- 《HTTP协议详解》读书笔记---请求篇之响应状态码
在接收和解释请求消息后,服务器返回一个http响应消息.它也分为3个部分:状态行.消息报头.响应正文,格式如下: HTTP-VersionStatus-CodeReason-PhraseCRLF(CR ...
- git+jenkins持续集成二-jenkins定时构建语法:定时构建语法
构建位置:选择或创建工程_设置_构建触发器 1. 定时构建语法:* * * * * (五颗星,多个时间点,中间用逗号隔开)第一个*表示分钟,取值0~59第二个*表示小时,取值0~23第三个*表示一个月 ...
- 爬虫:Scrapy2 - 命令行工具
Scrapy 是通过 scrapy 命令行工具进行控制的. 这里我们称之为 “Scrapy tool” 以用来和子命令进行区分.对于子命令,我们称为 “command” 或者 “Scrapy comm ...
- [报错处理]Could not find a version that satisfies the requirement xml (from versions)
安装xml库发生报错 pip3 install xml Collecting xml Could not find a version that satisfies the requirement x ...
- 最简单的动态代理实例(spring基于接口代理的AOP原理)
JDK的动态代理是基于接口的 package com.open.aop; public interface BusinessInterface { public void processBus ...
- [canvas入坑0] Jquery + HTML5 做最简易的画板
查看效果请到 http://philippica.github.io/ 点击paint 嗯,心血来潮想做个东西的一部分 html部分不用多说了,重点就是一个canvas <!DOCTYPE h ...