• [2015hdu多校联赛补题]hdu5301 Buildings


    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5301

    题目大意:给你一块由1x1方格组成的矩形区域,其中有且仅有一个坏块,现在你要在上面建矩形的房子,

    要求:

    1、除坏块以外任何一个1x1方格上都要有房子覆盖

    2、任何一座房子都必须有一部分作为矩形区域的边界

    3、要使所建房子中面积最大的面积尽量小

    要你输出这个所建房子中面积最大的那个房子的面积

    解:

    大概想一下,面积最大的房子肯定是1*x的长条,因为对任何y来说1*x的长条都可以拼成y*x的长条

    然后考虑主要用横条还是竖条来铺,全部横向的话花费是ansX=min(max(x-1, n-x+1), max(x, n-x));(这是考虑坏块的结果)

    如果坏块距离上下边界比较近的时候(y<ansX || m-y+1<ansX)->即比全部横向求得的答案小的时候,可以在边界上填一些竖条使得剩下一块没有坏块的矩形,这样可以有更优的解ansX=max(min(y, m-y+1), n/2+(n & 1));

    主要竖条的和横条相似,取两个方向求出来最小的那个即可

    。。。还有一种特殊情况,在奇数边长的正方形中心有坏块的话,答案是x-1

     1 /*
     2  * Problem:  
     3  * Author:  SHJWUDP
     4  * Created Time:  2015/9/4 星期五 19:11:14
     5  * File Name: 1006.cpp
     6  * State: 
     7  * Memo: 
     8  */
     9 #include <iostream>
    10 #include <cstdio>
    11 #include <vector>
    12 #include <cstring>
    13 #include <algorithm>
    14 
    15 using namespace std;
    16 
    17 int n, m, x, y;
    18 int main() {
    19 #ifndef ONLINE_JUDGE
    20     freopen("in", "r", stdin);
    21     //freopen("out", "w", stdout);
    22 #endif
    23     while(~scanf("%d%d%d%d", &n, &m, &x, &y)) {
    24         if(n==m && (n & 1) && x==y && (n+1)/2==x) {
    25             printf("%d
    ", x-1); continue;
    26         }
    27         int ansX=min(max(x-1, n-x+1), max(x, n-x));
    28     //    cout<<"first	"<<ansX<<endl;
    29         if(y<ansX || m-y+1<ansX) {    
    30             ansX=max(min(y, m-y+1), n/2+(n & 1));
    31         }
    32         int ansY=min(max(y-1, m-y+1), max(y, m-y));
    33     //    cout<<"first	"<<ansY<<endl;
    34         if(x<ansY || n-x+1<ansY) {
    35             ansY=max(min(x, n-x+1), m/2+(m & 1));
    36         }
    37         printf("%d
    ", min(ansX, ansY));
    38     }
    39     return 0;
    40 }
    41 /*
    42 Sample:
    43 in:
    44 2 3 2 2
    45 3 3 1 1
    46 50 30 2 3
    47 out:
    48 1
    49 2
    50 15
    51 */
    View Code
  • 相关阅读:
    git rror: RPC失败
    linux加载模块报错:could not insert module xxx.ko: Unknown symbol in module
    Ubuntu 20.04 添加当前用户 Root 权限
    C程序编译过程
    静态代码块和非静态代码块
    java反射
    ==和equal的区别
    solr中配置域
    Solr的简介以及安装
    Spring Data Redis 小demo
  • 原文地址:https://www.cnblogs.com/shjwudp/p/4782253.html
Copyright © 2020-2023  润新知