Finding Seats HDU - 1937 (尺取法)

A group of K friends is going to see a movie. However, they are too late to get good tickets, so they are looking for a good way to sit all nearby. Since they are all science students, they decided to come up with an optimization problem instead of going on with informal arguments to decide which tickets to buy. 

The movie theater has R rows of C seats each, and they can see a map with the currently available seats marked. They decided that seating close to each other is all that matters, even if that means seating in the front row where the screen is so big it’s impossible to see it all at once. In order to have a formal criteria, they thought they would buy seats in order to minimize the extension of their group. 

The extension is defined as the area of the smallest rectangle with sides parallel to the seats that contains all bought seats. The area of a rectangle is the number of seats contained in it. 

They’ve taken out a laptop and pointed at you to help them find those desired seats. 
InputEach test case will consist on several lines. The first line will contain three positive integers R, C and K as explained above (1 <= R,C <= 300, 1 <= K <= R × C). The next R lines will contain exactly C characters each. The j-th character of the i-th line will be ‘X’ if the j-th seat on the i-th row is taken or ‘.’ if it is available. There will always be at least K available seats in total. 
Input is terminated with R = C = K = 0. 
OutputFor each test case, output a single line containing the minimum extension the group can have.Sample Input
3 5 5
...XX
.X.XX
XX...
5 6 6
..X.X.
.XXX..
.XX.X.
.XXX.X
.XX.XX
0 0 0
Sample Output
6
9

题意 :让你找到最小的矩形体积,满足在他范围内存在至少k个 .  

解  :我们先计算一个a[ i ] [ j ] ,代表从(1,1)出发到( i , j )的  .  的个数 

显然任意范围内的点数量我们就能计算

我们 枚举一个 i 从 1到 m      再枚举一个 j 从 i  到 m   表示我们每次取的 横向范围从 i 到 j  

那么我们要取的就是竖向范围不确定 横向范围固定的当中满足k而且体积最小的矩形

我们再枚举 一个 t  从1 到 n  代表 矩形下端   ,显然上端p 是从 1开始的

如果 这么一个矩形满足有k个 那么       p++     直到不满足为止  记录最小体积

下面代码枚举的是          (i,p)                           (j,p)

 这么一个矩形                                     

                                    (i,t )                            (j,t )

#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#include<queue>
#include<deque>
#include<math.h>
#define ll long long
const int inf=1e9;
using namespace std;
int n,m,k;
int a[400][400];
char mapp[400][400];
int main()
{
    while(~scanf("%d%d%d",&n,&m,&k)&&n+m+k)
    {
      for(int i=1;i<=n;i++)
            scanf("%s",mapp[i]+1);
      memset(a,0,sizeof(a));
      for(int i=1;i<=n;i++)
      {
          int o=0;
          for(int j=1;j<=m;j++)
          {
              if(mapp[i][j]=='.') o++;
              a[i][j]=a[i-1][j]+o;
          }
      }
      int ans=inf;//最小体积
      for(int i=1;i<=m;i++)
          for(int j=i;j<=m;j++)
          {
              int p=1;
              for(int t=1;t<=n;t++)
              {
                  while(a[t][j]-a[t][i-1]-a[p-1][j]+a[p-1][i-1]>=k)
                  {
                      ans=min(ans,(j-i+1)*(t-p+1));
                      p++;
                  }
              }
          }
          printf("%d\n",ans);

    }
}



猜你喜欢

转载自blog.csdn.net/dsaghjkye/article/details/80421614