uva-108-贪心
题意:
求二维数组中子数组中的最大和.
使用二维数组,第i行表示前i行的和.那么a[i-j]表示从j行到i行的和.注意第三层循环,每次都保存当前最大的sum,如果sum小于0,直接置0.
#include"pch.h"
#include <string>
#include<iostream>
#include<map>
#include<memory.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<math.h>
#include<iomanip>
#include<bitset>
#include"math.h"
namespace cc
{
using std::cout;
using std::endl;
using std::cin;
using std::map;
using std::vector;
using std::string;
using std::sort;
using std::priority_queue;
using std::greater;
using std::vector;
using std::swap;
using std::stack;
using std::queue;
using std::bitset; constexpr int N = ; int a[N][N];
void solve()
{
memset(a, , sizeof(a));
int nn;
cin >> nn;
for (int i = ;i <= nn;i++)
{
for (int j = ;j <= nn;j++)
{
int cur = ;
cin >> cur;
a[i][j] = a[i - ][j] + cur;
}
}
int ans = INT32_MIN; for (int i = ;i <= nn;i++)
{
for (int j = i;j <= nn;j++)
{
int sum = ;
for (int col = ;col <= nn;col++)
{
//当前行的最大值
if (i == j)
{
if (sum < )
sum = a[i][col];
else
sum += a[i][col];
}
else
{
if (sum < )
sum = a[j][col] - a[i][col];
else
sum += a[j][col] - a[i][col];
}
if (sum > ans && sum != )
ans = sum;
}
} }
cout << ans << endl; }
}; int main()
{ #ifndef ONLINE_JUDGE
freopen("d://1.text", "r", stdin);
#endif // !ONLINE_JUDGE
cc::solve(); return ;
}
uva-108-贪心的更多相关文章
- 01_传说中的车(Fabled Rooks UVa 11134 贪心问题)
问题来源:刘汝佳<算法竞赛入门经典--训练指南> P81: 问题描述:你的任务是在n*n(1<=n<=5000)的棋盘上放n辆车,使得任意两辆车不相互攻击,且第i辆车在一个给定 ...
- UVA 11389(贪心问题)
UVA 11389 Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description II ...
- uva 10154 贪心+dp
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVa 108 - Maximum Sum(最大连续子序列)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- UVa 11389 (贪心) The Bus Driver Problem
题意: 有司机,下午路线,晚上路线各n个.给每个司机恰好分配一个下午路线和晚上路线. 给出行驶每条路线的时间,如果司机开车时间超过d,则要付加班费d×r. 问如何分配路线才能使加班费最少. 分析: 感 ...
- UVa 1467 (贪心+暴力) Installations
题意: 一共有n项服务,每项服务有安装的时间s和截止时间d.对于每项任务,如果有一项超出截止时间,惩罚值为所超出时间的长度.问如何安装才能使惩罚值最大的两个任务的惩罚值之和最小. 分析: 如果是求总惩 ...
- Party Games UVA - 1610 贪心
题目:题目链接 思路:排序后处理到第一个不同的字符,贪心一下就可以了 AC代码: #include <iostream> #include <cstdio> #include ...
- UVa 1149 (贪心) Bin Packing
首先对物品按重量从小到大排序排序. 因为每个背包最多装两个物品,所以直觉上是最轻的和最重的放一起最节省空间. 考虑最轻的物品i和最重的物品j,如果ij可以放在一个包里那就放在一起. 否则的话,j只能自 ...
- uva 108
降维 枚举行累加 然后求单行上最大连续和 #include <iostream> #include <cstring> #include <cstdio> # ...
- UVa 108: Maximum Sum
这道题用暴力解法+动态规划.分析如下: 对于某个1*m的矩阵,即一个数列,求其maximal sub-rectangle,可以通过求最大长连续字串和来求得(这个用到了动态规划). 那么对于n*m的矩阵 ...
随机推荐
- js数组遍历方法总结
数组遍历方法 1.for循环 使用临时变量,将长度缓存起来,避免重复获取数组长度,当数组较大时优化效果才会比较明显. 1 2 3 for(j = 0,len=arr.length; j < le ...
- bugku 密码学一些题的wp
---恢复内容开始--- 1.滴答滴 摩斯密码,http://tool.bugku.com/mosi/ 2.聪明的小羊 从提示猜是栅栏密码,http://tool.bugku.com/jiemi/ 3 ...
- sklearn pipeline
sklearn.pipeline pipeline的目的将许多算法模型串联起来,比如将特征提取.归一化.分类组织在一起形成一个典型的机器学习问题工作流. 优点: 1.直接调用fit和predict方法 ...
- django-debug-toolbar 安装及配置 django性能监控及调试
安装: pip install django-debug-toolbar 配置: settings 配置 DEBUG = True INSTALLED_APPS = [ 'debug_toolbar' ...
- java数组的声明、创建和遍历
一.数组的声明.创建 1.一维数组 先是声明 dataType[] arrayRefVar; // 首选的方法 数据类型[] 数组名; dataType arrayRefVar[]; // 效果相同, ...
- SP3871 GCDEX - GCD Extreme
//author Eterna #define Hello the_cruel_world! #pragma GCC optimize(2) #include<iostream> #inc ...
- Linux df -h空间显示不正确
今天发现一个测试数据库磁盘空间快满了,准备将几个不再用的表空间删除.通过以下命令删除表空间内容及数据文件. drop tablespace tablespace_name including cont ...
- test png
$x^2 \int $ When $a \ne 0$, there are two solutions to (ax^2 + bx + c = 0) and they are $$x = {-b \p ...
- c++中函数指针作为int传递
int f() { ; } typedef int (*method)(); int _tmain(int argc, _TCHAR* argv[]) { int value = (int)& ...
- java8_api_xml
xml处理-1 解析xml的两种方式 DOM主要接口介绍 使用DOM解析XML 解析(parse)是指读入一个文件,确认其有正确的格式,然后将其分解成各种元素,使开发者 ...