XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem L. Canonical duel
题目:Problem L. Canonical duel
Input file: standard input
Output file: standard output
Time limit: 2 seconds
Memory limit: 256 megabytes
In the game «Canonical duel» board N × M is used. Some of the cells of the board contain turrets. A
turret is the unit with four cannons oriented to north, east, south and west. Each turret can shoot exactly
once. When turret is hit by the cannon of another turret, its activated. When turret is activated, all four
cannons shoot simultaneously, then self-destruction process causes the turret to disappear.
Given the board with some turrets. You may add exactly one turret on one of cells which does not
contains a turret and activate this new turret. Your goal is to destroy maximum number of turrets.
Input
First line of the input contains two positive integers N and M, does not exceeding 2000 — size of the
board.
Each of the next N lines contain exactly M chars: ‘+’ denotes that cell is occupied by a turret, and ‘.’
that cell is empty.
Output
In the first line print maximum number of existing turrets you may destroy, then in second line print two
space-separated integers — row and column of place where turret can be set. If it is impossible to destroy
ever one turret in such a way, print only one line containing a zero; if several solutions exist, print any of
them.
Examples
| standard input | standard output |
| 3 4 ++.. +... ..++ |
5 2 4 |
| 4 5 ++... ..+.. ....+ ...++ |
5 4 1 |
| 3 3 +++ +++ +++ |
0 |
思路:
用并查集维护每个点所能炸的点数量,然后处理出每个空位置上下左右最近的炮台的位置。之后枚举放置位置即可。
#include <bits/stdc++.h> using namespace std; #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e6+;
const int mod=1e9+; char ss[][];
int n,m,ans,ansx,ansy,f[],cnt[];
int dir[][],vis[];
int idx(int i,int j)
{
if(i<||j<||i>n||j>m) return ;
return (i-)*m+j;
}
int fd(int x)
{
return f[x]==x?x:f[x]=fd(f[x]);
}
void join(int y,int x)
{
int fx=fd(x),fy=fd(y);
if(fx!=fy)
f[fy]=fx,cnt[fx]+=cnt[fy];
}
int main(void)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%s",&ss[i][]);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
f[idx(i,j)]=idx(i,j),cnt[idx(i,j)]=ss[i][j]=='+';
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
dir[idx(i,j)][]=ss[i-][j]=='+'?idx(i-,j):dir[idx(i-,j)][];
dir[idx(i,j)][]=ss[i][j-]=='+'?idx(i,j-):dir[idx(i,j-)][];
}
for(int i=n;i;i--)
for(int j=m;j;j--)
{
dir[idx(i,j)][]=ss[i+][j]=='+'?idx(i+,j):dir[idx(i+,j)][];
dir[idx(i,j)][]=ss[i][j+]=='+'?idx(i,j+):dir[idx(i,j+)][];
}
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if(ss[i][j]=='+')
{
if(dir[idx(i,j)][]) join(idx(i,j),dir[idx(i,j)][]);
if(dir[idx(i,j)][]) join(idx(i,j),dir[idx(i,j)][]);
//printf("f[%d][%d]:%d\n",i,j,f[idx(i,j)]);
}
// for(int i=1;i<=n;i++)
// for(int j=1;j<=m;j++)
// {
// printf("dir[%d][%d]:",i,j);
// for(int k=1;k<=4;k++)
// printf("%d ",dir[idx(i,j)][k]);
// printf("\n");
// }
// for(int i=1;i<=n;i++)
// for(int j=1;j<=m;j++)
// if(f[idx(i,j)]==idx(i,j))
// printf("cnt[%d][%d]:%d\n",i,j,cnt[idx(i,j)]);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if(ss[i][j]=='.')
{
int sum=;
for(int k=;k<=;k++)
if(!vis[fd(dir[idx(i,j)][k])])
vis[fd(dir[idx(i,j)][k])]=,sum+=cnt[fd(dir[idx(i,j)][k])];
for(int k=;k<=;k++)
vis[fd(dir[idx(i,j)][k])]=;
//printf("sum:%d %d %d\n",i,j,sum);
if(sum>ans)
ans=sum,ansx=i,ansy=j;
}
if(ans) printf("%d\n%d %d\n",ans,ansx,ansy);
else printf("0\n");
return ;
}
XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem L. Canonical duel的更多相关文章
- XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem F. Matrix Game
题目: Problem F. Matrix GameInput file: standard inputOutput file: standard inputTime limit: 1 secondM ...
- XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem J. Terminal
题目:Problem J. TerminalInput file: standard inputOutput file: standard inputTime limit: 2 secondsMemo ...
- XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem A. Arithmetic Derivative
题目:Problem A. Arithmetic DerivativeInput file: standard inputOutput file: standard inputTime limit: ...
- 【二分】【字符串哈希】【二分图最大匹配】【最大流】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem I. Minimum Prefix
给你n个字符串,问你最小的长度的前缀,使得每个字符串任意循环滑动之后,这些前缀都两两不同. 二分答案mid之后,将每个字符串长度为mid的循环子串都哈希出来,相当于对每个字符串,找一个与其他字符串所选 ...
- XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem D. Clones and Treasures
题目:Problem D. Clones and TreasuresInput file: standard inputOutput file: standard outputTime limit: ...
- 【二分图】【并查集】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem L. Canonical duel
给你一个网格(n<=2000,m<=2000),有一些炸弹,你可以选择一个空的位置,再放一个炸弹并将其引爆,一个炸弹爆炸后,其所在行和列的所有炸弹都会爆炸,连锁反应. 问你所能引爆的最多炸 ...
- 【动态规划】【滚动数组】【bitset】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem J. Terminal
有两辆车,容量都为K,有n(10w)个人被划分成m(2k)组,依次上车,每个人上车花一秒.每一组的人都要上同一辆车,一辆车的等待时间是其停留时间*其载的人数,问最小的两辆车的总等待时间. 是f(i,j ...
- 【枚举】【最小表示法】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem F. Matrix Game
给你一个n*m的字符矩阵,将横向(或纵向)全部裂开,然后以任意顺序首尾相接,然后再从中间任意位置切开,问你能构成的字典序最大的字符串. 以横向切开为例,纵向类似. 将所有横排从大到小排序,枚举最后切开 ...
- 【推导】【构造】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem E. Space Tourists
给你n,K,问你要选出最少几个长度为2的K进制数,才能让所有的n位K进制数删除n-2个元素后,所剩余的长度为2的子序列至少有一个是你所选定的. 如果n>K,那么根据抽屉原理,对于所有n位K进制数 ...
随机推荐
- UE4中VR模式下窗口单目双目的问题
如果要是单目的话使用HMD MIRROR MODE 3或者4
- Android之检查网络是否可用(跳转网络设置页面)
private boolean NetWorkStatus() { ConnectivityManager cwjManager = (ConnectivityManager) getSystemSe ...
- [NOI2008] 志愿者招募[流量平衡]
288. [NOI2008] 志愿者招募 ★★★★ 输入文件:employee.in 输出文件:employee.out 简单对比时间限制:2 s 内存限制:512 MB [问题描述] ...
- 160304-01、mysql数据库插入速度和读取速度的调整记录
需求:由于项目变态,需要在一个比较短时间段急剧增加数据库记录(两三天内,由于0增加至5亿).在整个过程调优过程非常艰辛 思路: (1)提高数据库插入性能中心思想:尽量将数据一次性写入到Data Fil ...
- Yii2 Model的一些常用rules规则,使用Validator验证
1. Yii2里 model在使用load方法加载浏览器的值的时候,会进行rules验证.这时候可以使用场景,让model对不同场景使用不同验证方式 2. 可以用attributeLabels()来指 ...
- 启动phpstyle Apache的80端口被win7的System PID=4的进程占用的解决方法 以及 如何在phpStyle里发布程序
学习前端是,用到Ajax,php语言,操作mysql数据库,浏览器无法解析php代码(把源码输出):原因,我之前用的是tomcat服务器写jsp,servlet,php用的是apache服务器,没有配 ...
- SAS9.4安装
安装教程请查看博客https://blog.csdn.net/qq_38960682/article/details/80567686 启动SAS时就报下面的错了:WARNING: 连接逻辑库“SAS ...
- python console
print(sys.stdout.encoding, locale.getpreferredencoding ()) windows console : chcp 65001; 在设置了这个环境变量时 ...
- Spring Data 查询方法的规则定义(五)
有句话这样说 欲练神功 挥刀自宫 请亲们先回到第一个 从Spring data 介绍 开始看 搭好环境 跟着步伐一块走 Spring Data 的方法必须严格按照它的规范进行编写,如果写错了 ...
- Git 使用vi或vim
1.vi & vim 有两种工作模式: (1) 命令模式:接受.执行 vi & vim 操作命令的模式,打开文件后的默认模式: (2) 编辑模式:对打开的文件内容进行 增.删.改 操作 ...