zoukankan      html  css  js  c++  java
  • Yet Another Crosses Problem_被教育场(B题)_思维/经验_Educational Codeforces Round 68 [Rated for Div. 2]

    B. Yet Another Crosses Problem

    题目限制:

    time limit per test2 seconds
    memory limit per test256 megabytes
    inputstandard input outputstandard 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.

    Input
    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.

    Output
    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.

    Example
    输入

    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
    
    

    题目大意:
    给你n*m大小的区域,满足填满区域的十字(就是说边上无空白),我们现在有刷子和一桶油漆,请问我们需要刷多少次才能够满足黑十字(上述条件),一次只能刷一格;

    代码借鉴了,cf中的红名dalao,不得不说代码实在是太强了。
    Mingrui Liu
    想法:
    记录每行/每列的空白数,在从中找出最小的,a[i] + b[j] - (s[i*m + j] == ‘.’),且用ans,与之前的不断对比,找出最小。

    #include<iostream>
    #include<math.h>
    #include<cmath>
    #include<algorithm>
    
    #define rep(i,n) for(int i=0;i<n;++i)
    
    using namespace std;
    
    const int N = 4e5 + 5;
    int T, n, m, a[N], b[N];
    char s[N];
    
    int main() {
    
    	for (cin >> T; T--;) {
    		cin >> n >> m;
    		rep(i, n)scanf("%s", s + i * m);
    		rep(i, n)a[i] = 0; rep(j, m)b[j] = 0;
    		rep(i, n)rep(j, m)if (s[i*m + j] == '.')++a[i], ++b[j];
    		//a[i]/b[j]分别用于记录行/列内的空白部分个数,在这其中找出最小的数。
    		int ans = 1e9 + 7;
    		rep(i, n)rep(j, m)ans = min(ans, a[i] + b[j] - (s[i*m + j] == '.'));
    		//这里是最重要的地方,找出最小,
    		//两点重合时(就是例如空白十字的中心)需要-1
    		printf("%d
    ", ans);
    	}
    
    	return 0;
    }
    

    在这里插入图片描述

  • 相关阅读:
    产品管理:启示录 特约客户、产品验证、原型测试
    我对敏捷个人培训的“三不原则”
    《敏捷个人》周刊 第2期 (可下载)
    《敏捷个人》周刊 第7期 (可下载)
    敏捷个人2012.6月份线下活动报道:与北邮学子交流职业和成长
    《敏捷个人》周刊 第11期 (可下载)
    敏友的【敏捷个人】有感(15): 初探敏捷个人和敏捷开发的感想
    敏友的【敏捷个人】有感(14): 敏捷个人管理的历程
    从0开始在Android下开发生活方向盘应用(自绘雷达图)
    OpenExpressApp:OEA框架 2.9 PreAlpha 源码公布
  • 原文地址:https://www.cnblogs.com/gidear/p/11773639.html
Copyright © 2011-2022 走看看