zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    用户场景故事
    我喜欢的输入法
    课堂练习-----查找水王
    《你的灯亮着吗》阅读笔记1
    补第二阶段冲刺站立会议6(原6月8日)
    补第二阶段冲刺站立会议5(原6月7日)
    补第二阶段冲刺站立会议4(原6月6日)
    补第二次冲刺站立会议3(原6月5日)
    补第二次冲刺站立会议2(原6月4日)
    补第二次阶段冲刺站立会议1(原6月3日)
  • 原文地址:https://www.cnblogs.com/hhlya/p/14025250.html
Copyright © 2011-2022 走看看