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;
    }
    
  • 相关阅读:
    Promise 解决回调地狱问题
    同步 异步 API 区别
    静态资源 读取方法
    路由
    HTTP 协议 get post 请求方式
    linux 查看日志
    putty完全使用手册--多窗口---git提交---连接数据库--自动日志显示
    strpos 返回0时 ,比较false 不能加单引号
    禁止使用test类的就是禁止使用本来的$this对象.可以调用父类的对象
    大D实例化model-->调用自定义类方法,大M调用原声model方法
  • 原文地址:https://www.cnblogs.com/hhlya/p/14025250.html
Copyright © 2011-2022 走看看