HDU

来源:互联网 发布:吉林美术生分数算法 编辑:程序博客网 时间:2024/05/16 02:57

Finding Seats

Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 720 Accepted Submission(s): 217

Problem Description

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

Source

ACM South American Programming Contest 2007
题意: 给你一个r,c,k然后给你一个r*c的矩阵,其中’.’代表空座,’X’代表已占,让你求最小的矩阵面积使得矩阵中空座的个数大于等于k个,范围:1<=R,C<=300,1<=K<=R×C

分析:一个很明确的思路就是枚举起点终点,并维护一个二维前缀和即可,但是究竟怎么枚举法呢,暴力的话3004会超,考虑优化!我们可以在内层加一个尺取来优化,可以减掉一次循环,将为3003,还是维护个前缀和,我们固定行,然后在列这进行尺取即可(这里是个关键,如果我们先固定一个点,在找另一个点的话,就不好进行优化了,我们先固定两行,然后在找到两列,如果具体点的话,就以x1,x1,y2,y2,来说的话,就是先枚举x1,x2,然后尺取的来找y1,y2)

参考代码

#include<bits/stdc++.h>using namespace std;const int N = 1000;int pre[N][N];int get(int x1,int y1,int x2,int y2){    return pre[x2][y2] - pre[x1-1][y2] - pre[x2][y1-1]+pre[x1-1][y1-1];}int main(){    int r,c,k;    while(cin>>r>>c>>k,r+c+k){        char ch;        memset(pre,0,sizeof(pre));        for(int i = 1;i <= r;i++){            for(int j = 1;j <= c;j++){                cin>>ch;                pre[i][j] = pre[i-1][j] + pre[i][j-1] - pre[i-1][j-1] + ((ch == '.')?1:0);            }        }        int ans = 1<<20;        for(int i = 1;i <= r;i++){            for(int j = i;j <= r;j++){                int l,r;                l = r = 1;                while(true){                    while(get(i,l,j,r) < k && r < c) r++;                    if(get(i,l,j,r) < k) break;                    if(get(i,l,j,r) >= k) {                        ans = min(ans,(r-l+1)*(j-i+1));                    }                    l++;                }            }        }        cout<<ans<<endl;    }    return 0;}
  • 如有错误或遗漏,请私聊下UP,thx
原创粉丝点击