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

Output
For 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的最小矩形。显然暴力会超时,当时有想到求二维前缀和,但是没有想到好的方法实现。看了题解后,才知道可以在输入的时候就求前缀和(指以该点为右下角的矩形中“.”的个数)。实现方法见代码。之后,固定行的上限与下限,用尺取法枚举列。算每个小矩形时要注意减一。比如一个小矩形左上角的点为(x1,y1),右下角的点为(x2,y2)。那么该小矩形中的个数为sum[x2][y2]-sum[x2][y1-1]-sum[x1-1][y2]+sum[x1-1][y1-1]。

PS:注意因为有i-1,j-1,所以数组下标都从1开始。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define LL long long 
using namespace std;
int main()
{
    char  map[310][310];
    int   sum[310][310];
	int r,c,k;
	while(scanf("%d%d%d",&r,&c,&k)!=EOF)
    {
    	if(r==0&&c==0&&k==0)
    	    break;
    	int i,j;
    	memset(sum,0,sizeof(sum));
    	char ch;
		for(i=1;i<=r;i++)
		{
		  for(j=1;j<=c;j++) 
		  {
		  	cin>>ch;
		  	int t;
		  	t=(ch=='.');
		  	sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+t;
		  }			
		}
		int ans=r*c;
	    for(i=1;i<=r;i++)     //i,j分别为行的上限和下限
		for(j=i;j<=r;j++)    
		{
		   int l,rr;  
		   l=rr=1;       //尺取找符合条件的列的上限与下限
		   while(1)  
		   {
		   //这里特别注意计算小矩形内“.”的个数的方法,这里的(i,j)代表的不是一个点,而是一个小格
		       while(sum[j][rr]-sum[j][l-1]-sum[i-1][rr]+sum[i-1][l-1]<k&&rr<=c)
			        rr++;
			   if(sum[j][rr]-sum[j][l-1]-sum[i-1][rr]+sum[i-1][l-1]<k)
			       break;
			   if(sum[j][rr]-sum[j][l-1]-sum[i-1][rr]+sum[i-1][l-1]>=k)
			   {
			   	 ans=min(ans,(rr-l+1)*(j-i+1));
			   	 
			   }			      
			   l++;			   	
		   }	
			
	    }
	    cout<<ans<<endl;
		
	}


    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值