HDU 1937 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.

题解

意思就是给你一个电影院的购票情况,让我们找到一个面积最小的矩形,可以坐下所有的k个人,那么题目的意思很清楚,很明显的特征,满足条件的最小区间,尺取即可。因为行列的数据量不大,因此我们枚举矩形的上下边,然后取尺取就好了。哦,对了,不加前缀和会T。

code

#include<vector>
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int inf=0x3f3f3f3f;
void check_max (int &a,int b) {a=max (a,b);}
void check_min (int &a,int b) {a=min (a,b);}
const int maxn=3e3+10;
int maze[maxn][maxn];
int n,m,k;
int pre[maxn][maxn];

int main () {
    ios::sync_with_stdio (false);
    while (cin>>n>>m>>k) {
        if (n==0&&m==0&&k==0) break;
        for (int i=1;i<=n;i++) 
         for (int j=1;j<=m;j++) {
            // char s; scanf ("%c",&s);
            char s; cin>>s;
            if (s=='.') maze[i][j]=1;
            else maze[i][j]=0;
            pre[i][j]=pre[i-1][j]+maze[i][j];
         }
        int ans=inf;
        for (int i=1;i<=n;i++) 
         for (int j=i;j<=n;j++) {
            int res=0;
            int l=1,r=1;
            while (1) {
                while (r<=m&&res<k) {
                    // for (int k=i;k<=j;k++) res+=maze[k][r];
                    res+=(pre[j][r]-pre[i-1][r]);
                    r++;
                }
                if (res<k) break;
                check_min (ans,(r-l)*(j-i+1));
                // for (int k=i;k<=j;k++) res-=maze[k][l];
                res-=(pre[j][l]-pre[i-1][l]);
                l++;
            }
         }
        cout<<ans<<endl;
    }
    return 0;
}
posted @ 2020-11-23 16:33  Luglucky  阅读(79)  评论(0编辑  收藏  举报