B. Yet Another Crosses Problem
2 seconds
256 megabytes
standard input
standard output
You are given a picture consisting of n
rows and m columns. Rows are numbered from 1 to n from the top to the bottom, columns are numbered from 1 to m
from the left to the right. Each cell is painted either black or white.
You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers x
and y, where 1≤x≤n and 1≤y≤m, such that all cells in row x and all cells in column y
are painted black.
For examples, each of these pictures contain crosses:
The fourth picture contains 4 crosses: at (1,3)
, (1,5), (3,3) and (3,5)
.
Following images don't contain crosses:
You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black.
What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross?
You are also asked to answer multiple independent queries.
The first line contains an integer q
(1≤q≤5⋅104
) — the number of queries.
The first line of each query contains two integers n
and m (1≤n,m≤5⋅104, n⋅m≤4⋅105
) — the number of rows and the number of columns in the picture.
Each of the next n
lines contains m
characters — '.' if the cell is painted white and '*' if the cell is painted black.
It is guaranteed that ∑n≤5⋅104
and ∑n⋅m≤4⋅105
.
Print q
lines, the i-th line should contain a single integer — the answer to the i
-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross.
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
0
0
0
0
0
4
1
1
2
The example contains all the pictures from above in the same order.
The first 5 pictures already contain a cross, thus you don't have to paint anything.
You can paint (1,3)
, (3,1), (5,3) and (3,5) on the 6-th picture to get a cross in (3,3). That'll take you 4
minutes.
You can paint (1,2)
on the 7-th picture to get a cross in (4,2)
.
You can paint (2,2)
on the 8-th picture to get a cross in (2,2). You can, for example, paint (1,3), (3,1) and (3,3) to get a cross in (3,3) but that will take you 3 minutes instead of 1
.
There are 9 possible crosses you can get in minimum time on the 9
-th picture. One of them is in (1,1): paint (1,2) and (2,1).
题意:找补充次数最少的十字架
题解:暴力模拟,分别把每行每列的 * 个数存进数组,找出含有最长 * 的行列以及行列所在坐标i,j,还要判断a[i][j]是否为 .(特判),是的话次数还要减一
#include<iostream>
#include<math.h>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
vector<int>a[];
int xx[],yy[];
char s;
int main()
{
int t, n, m;
cin >> t;
while (t--)
{
int x = -, y = -, cnt = ,x_pos,y_pos;
cin >> n >> m;
for (int i = ; i < n; i++)
{
for (int j = ; j < m; j++)
{
cin >> s;
if (s == '.')
a[i].push_back();
else
a[i].push_back();
}
}
for (int i = ; i < n; i++)
{
cnt = ;
for (int j = ; j < m; j++)
{
cnt = cnt + a[i][j];
}
xx[i]=cnt;
if(cnt>x)
x=cnt;
}
for (int i = ; i < m; i++)
{
cnt = ;
for (int j = ; j < n; j++)
{
cnt = cnt + a[j][i];
}
yy[i]=cnt;
if(cnt>y)
y = cnt; }
int ans=,p=;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(xx[i]==x&&yy[j]==y&&a[i][j]==)
p=;
ans=min(m+n-xx[i]-yy[j],ans);
}
}
cout<<ans-p<<endl; for (int i = ; i < n; i++)//清空
a[i].clear();
}
// system("pause");
return ;
}
B. Yet Another Crosses Problem的更多相关文章
- Codeforces - 1194B - Yet Another Crosses Problem - 水题
https://codeforc.es/contest/1194/problem/B 好像也没什么思维,就是一个水题,不过蛮有趣的.意思是找缺黑色最少的行列十字.用O(n)的空间预处理掉一维,然后用O ...
- Codeforces 1194B. Yet Another Crosses Problem
传送门 直接枚举填满哪一行,然后看看这一行填满以后哪一列最小 这个预处理一下 $cnt[i]$ 表示初始时第 $i$ 列有几个位置填满就可以做到 $O(m)$ 对于所有情况取个 $min$ 就是答案, ...
- Educational Codeforces Round 68 Editorial
题目链接:http://codeforces.com/contest/1194 A.Remove a Progre ...
- Educational Codeforces Round 68
目录 Contest Info Solutions A.Remove a Progression B.Yet Another Crosses Problem C.From S To T D.1-2-K ...
- Educational Codeforces Round 68 (Rated for Div. 2)补题
A. Remove a Progression 签到题,易知删去的为奇数,剩下的是正偶数数列. #include<iostream> using namespace std; int T; ...
- QFNU 10-02 19 training
B - Yet Another Crosses Problem 题意:找如果使图中某一行某一列全部变成黑色,至少需要把多少个白方格变成黑方格 思路:直接找就可以,注意存储的时候要记得进行分开存储,存储 ...
- HDU 4978 A simple probability problem
A simple probability problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...
- Problem O
Problem Description Before bridges were common, ferries were used to transport cars across rivers. R ...
- Codeforces Round #425 (Div. 2) Problem A Sasha and Sticks (Codeforces 832A)
It's one more school day now. Sasha doesn't like classes and is always bored at them. So, each day h ...
随机推荐
- Linux CentOS7 VMware LAMP架构Apache用户认证、域名跳转、Apache访问日志
一.Apache用户认证 vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //把111.com那个虚拟主机编辑成如下内容 <Virtu ...
- Python中property属性的概论和使用方法
property属性 概念: 定义一个方法但是使用装饰器property,只可以有一个self形参 可以用这样的属性动态的获取属性的值 定义方式(经典类) class Fun(): @property ...
- Idea牛逼插件,拿走不谢
1.grep console java 开发的过程中,日志都会输出到console,输出的内容是非常多的,所以需要有一个工具可以方便的查找日志,或者可以非常明显显示我们关注的内容,grep conso ...
- Python 数组
使用之前要先导入函数库 import numpy as np 数组名=np.zeros(数组大小,数据类型) 初始化为0值,这里的数据类型只能是数值类型,字符类型不能用 一.一维数组 impo ...
- 信号强度(RSSI)知识整理
来源: https://www.cnblogs.com/lele/articles/2832885.html 为什么无线信号(RSSI)是负值 答:其实归根到底为什么接收的无线信号是负值,这样子是不是 ...
- [SUCTF 2019]CheckIn
进入靶场只有一个文件上传界面,上传php一句话木马提示非法后缀. 尝试利用php后缀的多样性绕过,发现都被过滤掉了,先把php改成.jpg的图片,提示说有问号在内容中. php后缀的多样性:php语言 ...
- Link Analysis_1_Basic Elements
1. Edge Attributes 1.1 Methods of category 1.1.1 Basic three categories in terms of number of layers ...
- xcode 6 如何将 模拟器(simulator) for iphone/ipad 转变成 simulator for iphone
xcode 6默认模拟器是iphone/ipad通用的,如果想只针对iphone或者ipad可以进行如下设置: 1.修改模拟器大小(非必须) 模拟器->WIndow->scale-> ...
- 前端学习笔记系列一:11@vue/cli3.x中实现跨域的问题
由于浏览器的同源访问策略,vue开发时前端服务器通常与后端api服务器并非是相同的服务器,因此需要使用一个代理服务器实现跨域访问.在@vue/cli3.x根目录下创建一个vue.config.js文件 ...
- jsp中获取attribute
value="<%=request.getSession().getAttribute("username")%>"