码迷,mamicode.com
首页 > 其他好文 > 详细

hdu1937 Finding Seats

时间:2015-07-26 22:26:25      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

hdu1937 Finding Seats

题意是 求最小的矩形覆盖面积内包含 k 个 空位置

枚举上下边界然后 双端队列 求 最小面积

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <string>
using namespace std;
typedef long long ll;
const double ESP = 10e-8;
const int MOD = 1000000000+7;
const int MAXN = 300+10;
char graph[MAXN][MAXN];
int sum[MAXN][MAXN];

int main(){
   // freopen("input.txt","r",stdin);
    int R,C,K;
    while(scanf("%d%d%d",&R,&C,&K)){
        if(!R && !C && !K){
            break;
        }
        for(int i = 0;i < R;i++){
            scanf("%s",graph[i]);
        }
        memset(sum,0,sizeof(sum));
        for(int i = 1;i <= R;i++){
            for(int j = 1;j <= C;j++){
                sum[i][j] = sum[i][j-1];
                sum[i][j] += sum[i-1][j] - sum[i-1][j-1];
                if(graph[i-1][j-1] == .){
                    sum[i][j]++;
                }
            }
        }
        int ans = R * C;
        for(int x2 = R;x2 > 0;x2--){
            if(sum[x2][C] < K){
                break;
            }
            for(int x1 = 1;x1 <= R;x1++){
                if(sum[x2][C] - sum[x1-1][C] < K){
                    break;
                }
                int y1 = 1;
                int y2 = 1;
                while(y1 <= C && y2 <= C){
                    int cnt = sum[x2][y2]-sum[x1-1][y2]-
                    (sum[x2][y1-1] - sum[x1-1][y1-1]);
                    if(cnt < K){
                        y2++;
                    }else{
                        ans = min(ans,(x2-x1+1)*(y2-y1+1));
                        y1++;
                        if(y1 > y2){
                            break;
                        }
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

upc3111 star 

跟上面是类似的题,今年省赛的题 T_T

求最小矩形面积覆盖的 星星数 至少 为 k 个

就跟上面一样的题型了23333

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <string>
using namespace std;
typedef long long ll;
const double ESP = 10e-8;
const int MOD = 1000000000+7;
const int MAXN = 400+10;
int graph[MAXN][MAXN];
int sum[MAXN][MAXN];
int main(){
    //freopen("input.txt","r",stdin);
    int t;
    scanf("%d",&t);
    while(t--){
        int n,k;
        scanf("%d%d",&n,&k);
        memset(graph,0,sizeof(graph));
        memset(sum,0,sizeof(sum));
        int stX = 400,edX = 1,stY = 400,edY = 1;
        while(n--){
            int a,b;
            scanf("%d%d",&a,&b);
            graph[a][b]++;
            stX = min(a,stX);
            stY = min(b,stY);
            edX = max(a,edX);
            edY = max(b,edY);
        }
        for(int i = stX;i <= edX;i++){
            for(int j = stY;j <= edY;j++){
                sum[i][j] = sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1];
                sum[i][j] += graph[i][j];
            }
        }
 
        int ans = (edX-stX+1)*(edY-stY+1);
        for(int x2 = edX;x2 >= stX;x2--){
            if(sum[x2][edY] < k){
                break;
            }
            for(int x1 = stX;x1 <= edX;x1++){
                if(sum[x2][edY] - sum[x1-1][edY] < k){
                    break;
                }
                int y1 = stY;
                int y2 = stY;
                while(y1 <= edY && y2 <= edY){
                    int cnt = sum[x2][y2] - sum[x2][y1-1]-(sum[x1-1][y2]-sum[x1-1][y1-1]);
                    if(cnt < k){
                        y2++;
                    }else{
                        ans = min(ans,(x2-x1+1)*(y2-y1+1));
                        y1++;
                        if(y1 > y2){
                            break;
                        }
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

hdu1937 Finding Seats

标签:

原文地址:http://www.cnblogs.com/hanbinggan/p/4678832.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!