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 ...
随机推荐
- python爬虫(六) Cookie
什么是Cookie 在网站中,http的请求通常是无状态的(第一个和服务器连接并且登录之后,此时服务器知道是哪个用户,但是当第二次请求服务器时,服务器依然不知道当前请求的是哪个用户),cookie就是 ...
- python字典中值为列表或字典的构造方式
1.值为列表的构造方法 dic = {} dic.setdefault(key,[]).append(value) >>dic.setdefault('a',[]).append(1) & ...
- Linux centosVMware apache 限定某个目录禁止解析php、限制user_agent、php相关配置
一.限定某个目录禁止解析php 核心配置文件内容 vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 先创建.编辑一个php 配置 vim /u ...
- ch3 盒模型、定位
标准盒模型.怪异盒模型 外边距叠加 当两个或者争夺垂直外边距相遇时,他们将形成一个外边距,这个外边距的高度等于两个发生叠加的外边距的高度中的较大者. 当一个元素出现在另一个元素上面时,第一个元素的底外 ...
- 「CH6901」骑士放置
「CH6901」骑士放置 传送门 将棋盘黑白染色,发现"日"字的两个顶点刚好一黑一白,构成一张二分图. 那么我们将黑点向源点连边,白点向汇点连边,不能同时选的一对黑.白点连边. 当 ...
- DoMes平台首页菜单栏
问题1:左侧菜单栏数据是在哪里获取的? 答案1: 项目根目录的Views/Home/Index文件为平台首页 打开Index.cshtml文件,有一个framework-clientdata.js引入 ...
- 运行cmd直接进入指定目录下的命令
新建一个.bat批处理文件,文件命令为@ECHO OFF cmd /k cd /d c:data 运行该批处理文件cmd就可进入指定的文件夹,感兴趣的朋友可以参考下啊 新建一个.bat批处理文件,文件 ...
- springMVC的执行请求过程
springMVC的运行流程: 1.用户发送请求至前端控制器DispatcherServlet 2.DispatcherServlet收到请求调用HandlerMapping处理器映射器 3.处理器映 ...
- 【剑指Offer面试编程题】题目1510:替换空格--九度OJ
题目描述: 请实现一个函数,将一个字符串中的空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 输入: 每个 ...
- luogu P3358 最长k可重区间集问题
网络流建图好难,这题居然是网络流(雾,一般分析来说,有限制的情况最大流情况可以拆点通过capacity来限制,比如只使用一次,把一个点拆成入点出点,capacity为1即可,这题是限制最大k重复,可以 ...