zoukankan      html  css  js  c++  java
  • TZOJ 4359: Partition the beans (二分)

    描述

    Given an N x N square grid (2 <= N <= 15) and each grid has some beans in it. You want to write at most K (1 <= K <= 2N - 2) horizontal or vertical lines going across the entire grid, which to partition the grid into some piles.

    But you want to minimize the number of the largest resulting piles of beans. Given the number of beans in each square, please compute the
    number of the largest pile of beans.

    输入

    The first line has two integers, N and K and then follows N lines, each line has N non-negative integers, indicating the number of beans no more than 1000.

    输出

    Output the minimum possible number of the largest pile of beans.

    样例输入

      3 2

     1 1 2

     1 1 2

     2 2 4

    样例输出

     4

    提示

    write vertical line between columns 2 and 3, and another horizontal line between rows 2 and 3, which creates 4 piles, each with 4 beans.

    题意:

    给一个n*n的矩阵,横着或竖着切最多k次,最小化切完后所有块数值和的最大值。

    题目分析:

    可以枚举横着切的情况,然后二分答案,用贪心判可不可行。

    #include <bits/stdc++.h>
    using namespace std;
    const int N=20;
    int a[N][N],t[N],s[N],cnt;
    int n,k,sum;
    bool check(int x) {
        for(int i=0;i<cnt;i++) s[i]=0;
        int ans=0;
        for(int i=1;i<=n;i++) {
            bool f=true;
            for(int j=1;j<cnt;j++) {
                s[j]+=a[t[j]][i]-a[t[j-1]][i];
                if(s[j]>x) f=false;
            }
            if(f) continue;
            ans++;
            for(int j=1;j<cnt;j++) {
                s[j]=a[t[j]][i]-a[t[j-1]][i];
                if(s[j]>x) return false;
            }
        }
        return ans+cnt-2<=k;
    }
    int main() {
        scanf("%d%d",&n,&k);
        for(int i=1;i<=n;i++) {
            for(int j=1;j<=n;j++) {
                scanf("%d",&a[i][j]);
                sum+=a[i][j];
                a[i][j]+=a[i-1][j];
            }
        }
        int S=1<<(n-1),ans=1e9;
        for(int s=0;s<S;s++) {//枚举横切情况
            cnt=0;
            t[cnt++]=0;
            for(int i=0;i<n-1;i++) {
                if((1<<i)&s) t[cnt++]=i+1;
            }
            t[cnt++]=n;
            if(cnt-2>k) continue;
            int L=0,R=sum,x;
            while(L<=R) {
                int mid=(L+R)>>1;
                if(check(mid)) x=mid,R=mid-1;
                else L=mid+1;
            }
            ans=min(ans,x);
        }
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    SpringMVC数据绑定
    SpringMVC概述
    应用Spring和Hibernate(C3P0数据池)写数据库交互项目
    Spring的AOP特性
    Sping框架的IOC特性
    MD5加密字符串
    重力感应 视频横竖屏切换
    自定义View(三)实现简单的可拖动、可缩放的ImageView
    自定义View(二)增加View的属性
    自定义View的学习(一) 自绘制控件
  • 原文地址:https://www.cnblogs.com/zdragon1104/p/11927448.html
Copyright © 2011-2022 走看看