zoukankan      html  css  js  c++  java
  • Yet Another Crosses Problem

    Yet Another Crosses Problem
      时间限制: 2 Sec  内存限制: 128 MB

    题目描述

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


    题解,暴力for一遍,记录行和列的“*”的个数,再for一遍,维护最小值;

    坑是:1.会出现极端情况,比如一列*,长度是1e5,,memset的话会T,所以直接for一遍,初始化。

               2.不能开二维数组存,会爆,但又要记录“*”的位置,就用结构体set,存<i,j>的下标,然后枚举十字架中心的位置,如果中心点是“.”,那么加上重复减去的1;记得set.clear().

    代码如下:

    #include <bits/stdc++.h>
    using namespace std;
    const int N=1e5+50;
    int ha[N],le[N];
    set<pair<int,int> >q;
    int main()
    {
        int t;
        scanf("%d
    ",&t);
        while(t--)
        {
            int n,m;
            q.clear();
            scanf("%d %d
    ",&n,&m);
            for(int i=0;i<n;i++) ha[i]=0;
            for(int i=0;i<m;i++) le[i]=0;
            char now[m];
            for(int i=0;i<n;i++)
            {
                scanf("%s",now);
                for(int j=0;j<m;j++)
                {
                    if(now[j]=='*') {
                            ha[i]++,le[j]++;
                            q.insert(make_pair(i,j));
                    }
                }
            }
            int res=0,minn=INT_MAX;
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                     int k=0;
                     if(q.count(make_pair(i,j))==0)
                     {
                         k=1;
                     }
                     res=n-ha[i]+m-le[j]-k;
                     minn=min(minn,res);
                }
            }
            printf("%d
    ",minn);
        }
        return 0;
    }
  • 相关阅读:
    一行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10(如果今天你的老板还在要求你兼容IE6~8,别犹豫,辞职吧。)
    HTML元素分类【三种类型】
    React-Native 学习笔记-Android开发平台-开发环境搭建
    常用原生JS函数和语法集合
    jQuery选择器总结
    jQuery选择器大全
    Sublime Text 3 的HTML代码格式化插件Tag
    用CSS画三角形
    纯CSS绘制三角形(各种角度)
    纯CSS写三角形-border法[晋级篇01]
  • 原文地址:https://www.cnblogs.com/sylvia1111/p/11274371.html
Copyright © 2011-2022 走看看