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进制数 ...
随机推荐
- freemarker1 一些内建函数和用法
${" green mouse"?cap_first} --> Green mouse //字符串中的第一个单词的首字母大写 ${"ABCDF" ...
- HTML 标签 参考手册
按功能类别排列 基础 标签 描述 <!DOCTYPE> 定义文档类型. <html> 定义 HTML 文档. <title> 定义文档的标题. <body& ...
- UVALive 6044(双连通分量的应用)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34902 思路:首先是双连通缩点,然后就是搜索一下,搜索时要跳过连通 ...
- log4net写txt日志
1.配置: <configSections>节点下添加: <section name="log4net" type="log4net.Config.Lo ...
- 部署全局ajax处理
$.ajaxSetup({ beforeSend:function(){ $('.loading').show(); }, complete:function(){ $('.loading').fad ...
- NSUserDefaults设置bool值重启后bool只设置丢失问题
本文转载至 http://blog.csdn.net/cerastes/article/details/38036875 NSUserDefaultsbool同步synchronize无效 今天使 ...
- 解决 Unable to load native-hadoop library for your platform
安装hadoop启动之后总有警告:Unable to load native-hadoop library for your platform... using builtin-java classe ...
- java基础---->数组的基础使用(一)
数组是一种效率最高的存储和随机访问对象引用序列的方式,我们今天来对数组做简单的介绍.手写瑶笺被雨淋,模糊点画费探寻,纵然灭却书中字,难灭情人一片心. 数组的简单使用 一.数组的赋值 String[] ...
- vue兄弟组件传值
vue中除了父子组件传值,父传子用props,子传父用$emit,有时候兄弟组件之间也需要传值 1. 先定义一个中间件,src下面新建self.js import Vue from 'vue'; le ...
- 解决<pre>标签里的文本换行(兼容IE, FF和Opera等)
我们都知道<pre> 标签可定义预格式化的文本,一个常见应用就是用来表示计算机的源代码.被包围在 pre 元素中的文本通常会保留空格和换行符,但不幸的是,当你在<pre>标 ...