soj 12646. ORCHARD

12646. ORCHARD

Constraints

Time Limit: 3 secs, Memory Limit: 256 MB

Description

Alex and Bert are brothers who had been working for many years in a big orchard of their uncle where they planted trees. The orchard is arranged as an array of size n by m of trees. Alex had been planting apple trees and Bert had been planting banana trees; however, the brothers were not systematic and so apple trees stand among banana trees and vice versa. Each of them has planted at least one tree.

When coming near to retirement, the uncle decided to officially transfer the ownership of the trees to the brothers. The uncle informed the brothers that he will first pass the orchard to Alex. Next, Alex and Bert can cut out a rectangular area from the orchard, and the ownerships of all trees in the rectangular area are to be transferred to Bert. All further adjustments of the splitting had to be done with a lawyer.

Alex would like to keep all apple trees and Bert would like to keep all banana trees, but do not want to replant any tree. When they talked to a lawyer, the lawyer informed them that the ownership of a tree can be transferred from one owner to another, but the lawyer would charge $1 to transfer the ownership of a tree. Therefore the brothers try to place the starting rectangle such that the legal fees are as low as possible.

The following figures show three examples, where 0 and 1 indicates an apple and banana tree respectively.

In the first example, the best way is to cut out the fourth column and to assign it to Bert, as indicated by the rectangular outline. Afterwards, there are two trees that are out of place, and their ownerships have to be transferred – one banana tree from Alex to Bert and one apple tree from Bert to Alex. Hence, the fees are $2.

In the second example, the best way is to cut out trees that are not at the border of the field and then to transfer the ownership of six trees. The fees are $6.

In the third example, the best way is to cut out the rectangle consisting of 3 banana trees, and then to transfer the ownership of the rightmost banana tree to Bert. The fee is $1. Note that there is an alternative way that costs $1 as well.

Input

The first line of the input are the two positive numbers n and m (1<=n<=150, 2<=n*m<=750000) indicating the orchard’s size. Then follow n lines each consisting of m numbers, where 0 represents an apple tree and 1 represents a banana tree.

Output

You program must write to the standard output a number, which is the smallest fee required.

Sample Input

5 7
0 0 1 0 0 1 0
0 1 1 1 1 1 0
0 1 1 0 0 1 0
0 1 1 1 1 1 0
0 0 1 0 0 1 0

Sample Output

6

Problem Source

2014年每周一赛第十三场

题意: 给出n*m的01矩阵,你选出一个x*y的矩阵,f = x*y里面白点的个数 +不在x*y 里面黑点的个数

我们求的是 f最小

思路 : f=a+len-b ,a是x*y里面0点个数,b 是x*y里面1点的个数

只要 求a-b最小就可以了

我们枚举选出的矩阵的行,然后O(m)求答案

这个大概想求最大连续和一样 ,如果前面的对后面的有贡献,就加上

没有就置 0

// Problem#: 12646
// Submission#: 3287759
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <vector>
#define LL long long
#define maxn 1000010
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;

int sum[750010];
vector<int>a[160];
int ans1,ans2;

void getans(int L,int m)
{
    int sum1=0,sum2=0;
    for(int i = 1 ; i <= m ;i++)
    {
        sum2 += sum[i] ;
        sum1 += L-sum[i] ;
        if(sum1>=sum2)
        {
            sum1=sum2=0;
            continue ;
        }
        if(sum2-sum1>ans2-ans1)
        {
            ans1=sum1;
            ans2=sum2;
        }
    }
}
int main()
{
    int i,n,m,j,Max;
    int len ,k;
    while(scanf("%d%d",&n,&m) != EOF)
    {
        len=0;
        for( i = 1 ; i <= n ;i++){
          a[i].clear();
          a[i].push_back(1);
          for( j =1 ; j <= m;j++){
              scanf("%d",&k) ;
              if(k==1)len++;
              a[i].push_back(k);
          }
        }
        ans1=ans2=0;
        for( i = 1 ; i <= n ;i++)
        {
            for(k = 1 ;k <= m;k++)sum[k]=0;
            for( j = i ; j <= n ;j++)
            {
                for(k = 1 ;k <= m;k++)
                    sum[k] += a[j][k];
                getans(j-i+1,m) ;
            }
        }
        //cout<<ans1<<" " <<ans2<<endl;
        cout<<ans1+len-ans2<<endl;
    }
    return 0 ;

}                                 

时间: 2024-11-08 10:59:58

soj 12646. ORCHARD的相关文章

Orchard 之:Widget,兼看 Layer 在权限控制中的作用

一:Widget 可以理解为控件,可以直接被页面所引用.行为类似与分部页面,比如,我们可以创建一个 商品列表 Widget,然后这个 Widget 就可以被很多页面所引用. 理解 Widget 这个概念,我们不得不理解另外两个概念: 1:Layer Orchard 默认有这么几个层,Default.Authenticated.Anonymous.Disabled.TheHomepage.Layer 用于承载什么时候 Widget 将会被展现,这么讲大家一定觉得很抽象,其实 Layer 存在的意义

2 Orchard汉化资源包的使用

Orchard安装完毕之后我们就可以在后台尝试做一些基本的操作感受下Orchard提供的一些功能,比如添加一个页面.菜单.文章什么的.也可以试着新建一些部件.布局之类的感受下.个人建议摆弄一下了解下就可以,因为我们现在对Orhard的系统设计还没有一个很好的认识,所以很多东西都会感到摸不到头脑,因此也没必要在这里花费太多精力. 对于本人这种英语渣来说,还是搞不定这全是英文的站点,Orchard项目在设计之初就已经考虑到了全球化的问题.官网上面也提供了本地化包.只要下载对应的本地化包就可以将网站设

Orchard分类Taxonomies图文教程

Orchard分类和标签都实现对内容的分类管理,两者区别是分类的子项之间是具有级别(同级.上下级)关系,而标签是很随意的,子项之间可以有关系也可以没有,今天给大家分享分类的使用方法. 一.环境说明 Orchard版本1.9.2,主题默认TheThemeMachine 二.建立分类 打开Orchard,切换到Dashboard,在左侧菜单栏找到Taxonomies并单击,在页面右击找到“Add a Taxonomiy”并单击,由于分类本身也是个类型,所以不支持中文,我们先建一个Subject,保存

SOJ 4445 2015四川省赛模拟题

背景:赛场上就是因为没开这道题,而没拿到银,回来A了,感觉代码能力还是很弱,一定要先想好再敲,而且注重代码的函数化,这样无论是观感,还是调试都要好很多,逻辑要清晰,看代码要仔细,提交之前通读代码. 题意:起点在原点的frog,开始向右运动,且碰到障碍物就右转,问转多少次? 思路:关键是图的大小范围是109,无法存下,只有用类似链表的方法来存图.这里用了两个容器,一个以X为基准,一个一Y为基准,这两个容器设置很特殊,是为了满足题中特殊的查询需要:查询前进方向最近障碍物. 我的代码: #includ

Orchard官方文档翻译(十一) 使用Tags组织文本

原文地址:http://docs.orchardproject.net/Documentation/Organizing-content-with-tags 想要查看文档目录请用力点击这里 最近想要学习了解orchard,但却没有找到相关的中文文档,只有英文文档.于是决定自行翻译,以便日后方便翻阅. 转载请注明原作者与出处. 本人英文水平有限,错误之处欢迎指出以便修正 使用Tags组织文本 Content Orchard中的文本内容可以通过tags标签来分类.我肯可以通过URL中附加的关键字来进

Orchard官方文档翻译(六) 建立你的第一个Orchartd站点

让我们开始 该主题内容已在Orchard1.8Release版本下测试通过. 这里通过向导式的教程来告诉大家Orchard的功能如何使用.如果你是第一次使用Orchard,该文档就是为你而准备的! Orchard使用从零开始 对于初次接触Orchard的你来说,这里就是你的圣地,因为你能在这里找到最新的Orchard资源. Orchard 初学者 Orchard CodePlex - Orchard代码库 Orchard 讨论区 - 关于Orchard讨论区 Orchard 文档 - 与Orch

把Orchard部署到Windows Azure Web Sites

很久前就想做个人站点,主要用来记录自己的生活,我喜欢摄影,烘焙…然后又刚刚入皮坑,这些都可以放在网站上展示一下,或许还能为自己带来收入. 然后手上刚好有Azure的试用,于是乎动力就上来了. 以下是部署步骤. 一.创建Web Sites,SQL Database ,并下载Orchard源码 这些步骤超级简单就不再手把手教了,据说7月份Web Site的收费标准才会出来.到时候看看自己能否供得起 = = 昨天看到1.8.1 RC出来了….版本更新真快. 源码下载地址如下: https://orch

Orchard运用 - 理解App_Data目录结构

了解一个系统,应该基本上要了解目录结构及其组织形式.这样对于开发人员更是必备的知识,比如开发模块最终安装到哪,主题Themes是如何配置启用. 今天跟大家分享其实是个笔记记录,就是看到有一篇文章介绍App_Data目录结构,标注一下其他心得: Orchard网站App_Data目录里的文件介绍 其实目前对我来说: Logs目录:这是存放系统日志的目录.查看日志文件可以检查系统在运行中都出现了哪些错误.默认文件名按照日期标记. 在此你可以看到所有的错误信息,比如模块出错了,添加Alternativ

Orchard运用 - 在页面每篇随笔添加编辑链接

今天继续捣鼓Orchard系统,在此分享一个小技巧,如何在页面每个随笔添加编辑链接,这样方便管理员直接点击进去编辑内容.是的,只对管理员可见. 话说这一个特性一开始是默认启用并集成在核心实现中的,后来分离并封装到一个新的模块,该模块名称叫"Content Control Wrapper", 不过需要你启用它 - 很简单, 管理页面进入模块页面,搜索"Content"并找到该模块,点击Enable激活即可. 参考链接 http://stackoverflow.com/