Finding Seats

编程入门 行业动态 更新时间:2024-10-12 03:23:25

<a href=https://www.elefans.com/category/jswz/34/1758506.html style=Finding Seats"/>

Finding Seats

题目

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

题意

有r排c列座位,其中有一些是已经有人占的(‘X’),其余的是没人占的(‘.’)。一共有k个人去看电影,他们想挨的越近越好,也就是越不分散越好,求一个最小的矩形,使得包含的空座位数大于等于k。

分析

这个题要用尺取法。首先用一个num[ i ][ j ]数组,表示左上角为(1,1),右下角为(i,j)的矩形中空座位的个数,然后,枚举列的起点和终点(i,j),然后在枚举行的起点和终点(p,t)的时候用尺取法,求出最小的矩形。

代码

#include<iostream>
#include<cstdio>
#include<string.h>
#include<stack>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f
int r,c,k;
char mp[400][400];
int num[400][400];
int main()
{while(~scanf("%d%d%d",&r,&c,&k)){if(r==0&&c==0&&k==0)break;for(int i=1;i<=r;i++)scanf("%s",mp[i]+1);memset(num,0,sizeof(num));for(int i=1;i<=r;i++){int tmp=0;for(int j=1;j<=c;j++){if(mp[i][j]=='.')tmp++;num[i][j]=num[i-1][j]+tmp;}}int ans=1e9;for(int i=1;i<=c;i++)//列的起点 {for(int j=i;j<=c;j++)//列的终点 {int p=1;//行起点 for(int t=1;t<=r;t++)//行终点 {while(num[t][j]-num[t][i-1]-num[p-1][j]+num[p-1][i-1]>=k){ans=min(ans,(j-i+1)*(t-p+1));p++;}}}}printf("%d\n",ans);}return 0;
}

 

更多推荐

Finding Seats

本文发布于:2024-03-04 01:16:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1707869.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Finding   Seats

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!