zoukankan      html  css  js  c++  java
  • AtCoder

    题意

    https://vjudge.net/problem/AtCoder-2565

    将一个H*W的矩形切成三份,每一次只能水平或者垂直切,问最大块的面积-最小快的面积 的最小值是多少。

    思路

    先枚举水平切第一块的高i,那么剩余部分h-i要么继续水平切、要么垂直切,因为要使最大快-最小快 最小,那么剩下的两块肯定是相差最少为好,所以可以拆成w/2和w-w/2两块,也可以拆成(h-i)/2和(h-i)-(h-i)/2。

    再枚举垂直切的第一块的宽度,后续操作类似。

    代码

    #include<bits/stdc++.h>
    using namespace std;
    #define inf 0x3f3f3f3f
    #define ll long long
    const int N=200005;
    const int mod=1e9+7;
    const double eps=1e-8;
    const double PI = acos(-1.0);
    #define lowbit(x) (x&(-x))
    
    int main()
    {
        std::ios::sync_with_stdio(false);
        ll h,w;
        while(cin>>h>>w)
        {
            ll ans=inf;
            for(int i=1;i<h;i++)
            {
                ll a=h-i;
                if(i!=h-1)
                {
                    ans=min(ans,max(i*w,max(a/2*w,(a-a/2)*w))-min(i*w,min(a/2*w,(a-a/2)*w)));
                }
                ans=min(ans,max(i*w,max(a*(w/2),a*(w-w/2)))-min(i*w,min(a*(w/2),a*(w-w/2))));
             //   cout<<i<<" "<<max(i*w,max(a*(w/2),a*(w-w/2)))-min(i*w,min(a*(w/2),a*(w-w/2)))<<" "<<ans<<endl;
           //     cout<<a<<" "<<i<<" "<<w<<" "<<endl;
            }
        //    cout<<ans<<endl;
            for(int i=1;i<w;i++)
            {
                ll a=w-i;
                if(i!=w-1)
                {
                    ans=min(ans,max(i*h,max(a/2*h,(a-a/2)*h))-min(i*h,min(a/2*h,(a-a/2)*h)));
                }
                ans=min(ans,max(i*h,max(a*(h/2),a*(h-h/2)))-min(i*h,min(a*(h/2),a*(h-h/2))));
            }
            cout<<ans<<endl;
        }
        return 0;
    }
    

      

  • 相关阅读:
    ultraedit 窗口布局
    Oracle之Char VarChar VarChar2
    Python之pickle
    Python之xpath
    Python常用数据结构之heapq模块
    Python实现排序算法之快速排序
    Python常用数据结构之collections模块
    New York is 3 hours ahead of California
    leetcode978
    leetcode979
  • 原文地址:https://www.cnblogs.com/mcq1999/p/11859302.html
Copyright © 2011-2022 走看看