Uva--108(贪心)

2014-07-27 14:28:12

 Maximum Sum 

Background

A problem that is simple to solve in one dimension is often much more difficult to solve in more than one dimension. Consider satisfying a boolean expression in conjunctive normal form in which each conjunct consists of exactly 3 disjuncts. This problem (3-SAT) is NP-complete. The problem 2-SAT is solved quite efficiently, however. In contrast, some problems belong to the same complexity class regardless of the dimensionality of the problem.

The Problem

Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest sum. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle. A sub-rectangle is any contiguous sub-array of size tex2html_wrap_inline33 or greater located within the whole array. As an example, the maximal sub-rectangle of the array:

displaymath35

 

is in the lower-left-hand corner:

displaymath37

 

and has the sum of 15.

Input and Output

The input consists of an tex2html_wrap_inline39 array of integers. The input begins with a single positive integer N on a line by itself indicating the size of the square two dimensional array. This is followed by tex2html_wrap_inline43 integers separated by white-space (newlines and spaces). These tex2html_wrap_inline43 integers make up the array in row-major order (i.e., all numbers on the first row, left-to-right, then all numbers on the second row, left-to-right, etc.). N may be as large as 100. The numbers in the array will be in the range [-127, 127].

The output is the sum of the maximal sub-rectangle.

Sample Input 

4
0 -2 -7  0 9  2 -6  2
-4  1 -4  1 -1
8  0 -2

 

Sample Output 

15

思路:这题是求最大连续和的二维版,通过枚举上下界把二维转一维,然后就是求最大连续和的问题了,具体见代码。
 1 /*************************************************************************
 2     > File Name: o.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com 
 5     > Created Time: Sat 26 Jul 2014 06:51:59 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 
16 int n,g[105][105],sumr[105][105],sumc[105][105];
17 
18 int main(){
19     scanf("%d",&n);
20     for(int i = 1; i <= n; ++i){
21         for(int j = 1; j <= n; ++j){
22             scanf("%d",&g[i][j]);
23             sumr[i][j] = sumr[i][j - 1] + g[i][j];
24             sumc[i][j] = sumc[i - 1][j] + g[i][j];
25         }
26     }
27     int ansmax = 0;
28     for(int i = 1; i <= n; ++i){
29         for(int j = i; j <= n; ++j){
30             int tmax = 0,tsum = 0;
31             for(int k = 1; k <= n; ++k){
32                 tsum += sumc[j][k] - sumc[i - 1][k];
33                 if(tsum < 0)
34                     tsum = 0;
35                 if(tsum > tmax)
36                     tmax = tsum;
37             }
38             ansmax = tmax > ansmax ? tmax : ansmax;
39         }
40     }
41     printf("%d\n",ansmax);
42     return 0;
43 }

 

 

转载于:https://www.cnblogs.com/naturepengchen/articles/3871357.html

基于 FPGA 的 CNN 卷积神经网络之池化层 Verilog 实现 池化层是卷积神经网络(CNN)中的关键组件,用于降低特征图的维度,减少计算量和防止过拟合。常见的池化操作包括最大池化和平均池化。FPGA 提供了高效的并行处理能力,可以加速这些操作。池化层在 CNN 中扮演重要角色,通过降低数据维度提升计算效率。在 FPGA 上实现池化层,可以充分利用硬件资源实现高效的并行计算,有助于提高实时应用中的推理速度。 阅读详情

相关推荐

【实战】——零代码玩转Supervisely人像分割:从标注到模型训练的数据流水线搭建

本文详细介绍了如何零代码使用Supervisely平台搭建人像分割数据流水线,从智能标注到数据集格式转换,再到模型训练与优化。Supervisely的智能标注工具和协作功能大幅提升了人像分割项目的效率,特别适合视频会议背景虚化、虚拟试衣等应用场景。

weixin_30694389的博客 416

UVA108 Maximum Sum(最大和)矩阵(最大连续子序列的变形)(最大连续子矩阵)

A problem that is simple to solve in one dimension is often much more difficult to solve in more than one dimension. Consider satisfying a boolean expression in conjunctive normal form in which each ...

shiuxin的博客 264

《缠中说禅108课》5:市场无须分析,只要看和干

喜欢吹牛皮的,在市场里最常见,例如一种以分析市场、吹牛皮为生的职业,叫什么股评、专家的。此类人不过是市场上的寄生虫,真正的猎手只会观察、操作,用嘴是打不了豺狼的。 市场就是一个狩猎场,首先你要成为一个好猎手,而一个猎手,首先要习惯于无言。如果真有什么真理,那真理也是无言的。可言说的,都不过是人类思想的分泌物,臭气熏天。真不可言说了,就无不可言,言而无言,是乃真言。 一个好的猎手,可以没有嘴巴,但一定会有一双不为外物所动的眼睛,在这眼睛下,一切如幻化般透明。要不被外物所动,则首先要不被自我所迷惑,其实..

BenRen1314的博客 802

Maximum Sum

Maximum Sum UVA - 108(二维子矩阵最大和) A problem that is simple to solve in one dimension is often much more difficult to solve in more than one dimension. Consider satisfying a boolean expression in conjunc...

rg_lzm的博客 558

Aurix TC3XX Cache memory分析

Aurix Cache memory 分析

qwe0405080615的博客 4348

基于文件通信的QMT量化交易信号桥接方案设计与实现

在量化交易系统开发中,进程间通信(IPC)是连接不同计算模块、实现策略逻辑与交易执行解耦的关键技术。其核心原理在于允许运行于独立环境或进程的程序安全、可靠地交换数据。文件系统通信作为一种经典的IPC方法,因其实现简单、跨平台兼容性好且数据可持久化,在非高频场景下具有重要技术价值。它尤其适用于需要将外部复杂计算环境(如配备丰富数据科学库的Python环境)与专用交易终端(如迅投QMT)集成的应用场景。通过约定文件路径与数据格式,外部程序可将包含交易指令的JSON信号文件写入特定目录,而交易终端通过轮询或监听机

weixin_28678151的博客 419

Maximum Sum (最大的子矩阵的和)

A problem that is simple to solve in one dimension is often much more difficult to solve in more than one dimension. Consider satisfying a boolean expression in conjunctive normal form in which each c...

自渡的博客 691

使用AI编写测试用例——详细教程

现阶段AI还不能完全替代人工测试用例编写,但如果把AI当做一个提高效率的工具,它将会是一个很强大的辅助。

公众号|软件测试老吴 3万+

[解题报告]108 - Maximum Sum

Maximum Sum Background A problem that is simple to solve in one dimension is often much more difficult to solve in more than one dimension. Consider satisfying a boolean express...

weixin_30649859的博客 137

maya导入导出bvh 自动 脚本

maya打开bvh脚本

jacke121的专栏 1551

DAVIS前言:事件相机资料调研

Event-Camera 资料集锦 1. 官网资料 假设了你买了事件相机,就可以在以下几个链接了解相关的参数和gui界面安装,如果不喜欢gui界面安装,可以第2节,安装相关的源码驱动。 DVS/DAVIS产品说明 等相关产品的软硬件介绍。 iniVation DV教程文档 给出在不同平台下的dvs-gui界面,可按照其说明进行相应的操作。 另外,Neuromorphic vision com...

Ian的博客 4507

uva-108-贪心

uva-108-贪心 题意: 求二维数组中子数组中的最大和. 使用二维数组,第i行表示前i行的和.那么a[i-j]表示从j行到i行的和.注意第三层循环,每次都保存当前最大的sum,如果sum小于0,直接置0. #include"pch.h" #include <string> #include<iostre...

weixin_33929309的博客 139

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

UVA - 108 - Maximum Sum (简单贪心)

UVA - 108 Maximum Sum Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Background A problem that is simple

Never say never 1053

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

UVa 108 Maximum Sum (贪心&最大子矩阵和)

108 - Maximum Sum Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=44 Background A problem that is simple

AC,∑ndless 2784

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

UVA 108 Maximum Sum

给定一个含有正负数的二维数组,找出有最大和的子矩阵。矩阵的和指矩阵中所有元素的和。一个子矩阵是任意在总矩阵中大小为1x1或更大的邻近子数组,例如在下面的矩阵中: 0 −2 −7 09 2 −6 2−4 1 −4 1−1 8 0 −2(最大子矩阵)在左下方:9 2−4 1−1 8并且和为15.输入:包括一个N和NxN的矩阵 N<=100 矩阵中的数字在区间[-127,127]内输出:最大子矩阵的和。

Gowi的博客 677

【ICPC-108】uva 11039 - Building designing

点击打开链接uva 11039 思路:贪心(排序+枚举) 分析: 1 题目要求按照题目给定的数据求能够建立最高几层的楼房 2 题目要求上层的大小要比下层小,并且两层之间的颜色要交替的出现。很明显,我们只要按照size进行排序,然后枚举即可。 代码: #include<cstdio> #include<cstring> #include<iostream&...

藏经阁 | 玄苦 1079

UVA108 Maximum Sum dp/暴力+简化

UVA108Maximum Sum dp 暴力 简化

silence__bug的博客 391

UVA12321 Gas Stations【贪心】

G gas stations are authorized to operate over a road of length L. Each gas station is able to sell fuel over a specific area of influence, defined as the closed interval [x − r, x + r], where x is the station’s location on the road (0 ≤ x ≤ L) and r is its

海岛Blog 447

UVa 12676 Inverting Huffman

本文研究静态霍夫曼编码的逆向问题:已知N个字符的编码长度,求文本的最小可能大小。通过分析霍夫曼树的分层构建过程,提出了一种贪心算法:将编码长度降序排序后,从最大深度开始为叶节点分配最小权重,并逐层合并节点。算法确保每层节点数满足偶数要求,父节点权重大于子节点,最终累加合并节点的权重得到最小文本大小。该算法时间复杂度为O(D·N log N),适用于N≤50的情况。示例验证了算法的正确性,代码实现采用优先队列高效处理节点合并。这一逆向思维训练有助于深入理解数据压缩算法和贪心策略设计。

寂静山林 713

uva 108 Maximum Sum 最大子矩阵和

不知道为啥,这样的题目跑到贪心里面去了,动态规矩挺简单,最后之前看了一遍他的问题,所以很容易就敲出来了,贪心暂时告一段落,开始dp,加油 思路: 最大子矩阵和的问题可以通过最大字段和的问题解出来,普通的暴力枚举是枚举行和列,时间复杂度为m^2*n^2,但是我们可以在行的上界和下界确定的情况下将这个矩阵压缩为一个一维数组存起来,然后利用最大字段和的求解解出来,代码应该很容易就能看懂,如果对动

勿忘初心 807

UVa OJ 题解索引

寂静山林 756

poj 1050 To the Max & uva 108

poj 1050 To the Max uva 108

@fei 565

UVa 10020 - Minimal coverage

题意是:输入几个样例,每个样例第一行输入从0开始需要覆盖的长度M,即[0,M]。之后输入覆盖的线段,求需要的线段条数最小值。   思路:贪心算法,具体见代码及注释。 #include #include #include #include #include using namespace std; #define MAXN 100001 /*将输入数据进行处

苹果苹果一只梨 381

UVA311:Packets(贪心)

题目大意:有1*1,2*2,3*3,...六种小包裹,往6*6的箱子里装,给出六种小包裹各自的数量,求出最少用的箱子的个数。贪心,思路还是比较简单的,先从大的开始往小的装。6*6的包裹,每个单独装一个箱子;5*5的包裹,可以和11个1*1的搭配;4*4的包裹,可以喝5个2*2的搭配,如果2*2的不够,则补1*1的个数;.....

weixin_41073572的博客 82

UVA 10074 Take the Land dp/暴力+剪枝

UVA 10074 Take the Land dp 暴力+剪枝

silence__bug的博客 509

UVa 714 - Copying Books

最大值最小化问题,二分+贪心,参考刘汝佳白皮书151页。 之前我的代码在UVa上交过了,但是在ZOJ上却是WA。 后来才发现是错在这两组数据: 2 4 3 100000 1 1 1 4 3 1 1 1 100000 1 #include <cstdio> 2 #include <cstring> 3 4 const int MAX...

weixin_30849591的博客 67
上一篇: Uva--507(贪心)
下一篇: Uva--10827(贪心)
dingdi3021
博客等级 码龄11年 1粉丝 230原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值