Timus 1146. Maximum Sum
1146. Maximum Sum
Memory limit: 64 MB
0 | −2 | −7 | 0 |
9 | 2 | −6 | 2 |
−4 | 1 | −4 | 1 |
−1 | 8 | 0 | −2 |
Input
Output
Sample
input | output |
---|---|
|
|
最大子矩阵。很经典的问题哈哈
压缩 然后最大连续子序列 dp[i]=dp[i-1]<0?a[i]:dp[i-1]+a[i]
一开始压缩的时候没用前缀和,n^4 貌似过不了,后来用前缀和优化到n^3
下面代码中dp 的空间也可以优化,这里没有优化.
- /* ***********************************************
- Author :guanjun
- Created Time :2016/10/7 13:50:13
- File Name :timus1146.cpp
- ************************************************ */
- #include <bits/stdc++.h>
- #define ull unsigned long long
- #define ll long long
- #define mod 90001
- #define INF 0x3f3f3f3f
- #define maxn 10010
- #define cle(a) memset(a,0,sizeof(a))
- const ull inf = 1LL << ;
- const double eps=1e-;
- using namespace std;
- priority_queue<int,vector<int>,greater<int> >pq;
- struct Node{
- int x,y;
- };
- struct cmp{
- bool operator()(Node a,Node b){
- if(a.x==b.x) return a.y> b.y;
- return a.x>b.x;
- }
- };
- bool cmp(int a,int b){
- return a>b;
- }
- int a[][],n;
- int sum[][];
- int dp[];
- int main()
- {
- #ifndef ONLINE_JUDGE
- //freopen("in.txt","r",stdin);
- #endif
- //freopen("out.txt","w",stdout);
- while(scanf("%d",&n)!=EOF){
- cle(sum);
- for(int i=;i<=n;i++){
- for(int j=;j<=n;j++){
- scanf("%d",&a[i][j]);
- sum[i][j]=sum[i][j-]+a[i][j];
- }
- }
- int Max=-INF;
- //dp 求最大连续子序列 dp[i]代表以i为结尾的最大连续子序列的长度
- for(int i=;i<=n;i++){
- for(int j=;j<=i;j++){
- cle(dp);
- for(int k=;k<=n;k++){
- int tmp=sum[k][i]-sum[k][j-];
- if(dp[k-]<){
- dp[k]=tmp;
- }
- else dp[k]=tmp+dp[k-];
- Max=max(dp[k],Max);
- }
- }
- }
- cout<<Max<<endl;
- }
- return ;
- }
Timus 1146. Maximum Sum的更多相关文章
- ural 1146. Maximum Sum
1146. Maximum Sum Time limit: 0.5 secondMemory limit: 64 MB Given a 2-dimensional array of positive ...
- 最大子矩阵和 URAL 1146 Maximum Sum
题目传送门 /* 最大子矩阵和:把二维降到一维,即把列压缩:然后看是否满足最大连续子序列: 好像之前做过,没印象了,看来做过的题目要经常看看:) */ #include <cstdio> ...
- ural 1146. Maximum Sum(动态规划)
1146. Maximum Sum Time limit: 1.0 second Memory limit: 64 MB Given a 2-dimensional array of positive ...
- URAL 1146 Maximum Sum(最大子矩阵的和 DP)
Maximum Sum 大意:给你一个n*n的矩阵,求最大的子矩阵的和是多少. 思路:最開始我想的是预处理矩阵,遍历子矩阵的端点,发现复杂度是O(n^4).就不知道该怎么办了.问了一下,是压缩矩阵,转 ...
- URAL 1146 Maximum Sum(DP)
Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the large ...
- URAL 1146 Maximum Sum & HDU 1081 To The Max (DP)
点我看题目 题意 : 给你一个n*n的矩阵,让你找一个子矩阵要求和最大. 思路 : 这个题都看了好多天了,一直不会做,今天娅楠美女给讲了,要转化成一维的,也就是说每一列存的是前几列的和,也就是说 0 ...
- URAL 1146 Maximum Sum 最大子矩阵和
题目:click here #include <bits/stdc++.h> using namespace std; typedef unsigned long long ll; con ...
- POJ2479 Maximum sum[DP|最大子段和]
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39599 Accepted: 12370 Des ...
- UVa 108 - Maximum Sum(最大连续子序列)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
随机推荐
- arx刷新图形界面
actrTransactionManager->flushGraphics(); acedUpdateDisplay();
- vi 命令学习(二)
[选中文本(可视模式)] v 可视模式 从光标位置开始按正常模式选择文本 V 可视行模式 选中光标经过的完整行 ctrl + v 可视块模式 垂直方向选中文本 [ 撤销和恢复撤销] u undo 撤销 ...
- Python爬虫:抓取手机APP的数据
摘要 大多数APP里面返回的是json格式数据,或者一堆加密过的数据 .这里以超级课程表APP为例,抓取超级课程表里用户发的话题. 1.抓取APP数据包 表单: 表单中包括了用户名和密码,当然都是加密 ...
- HDU多校Round 3
Solved:4 rank:268 C. Dynamic Graph Matching 状压DP一下 #include <stdio.h> #include <algorithm& ...
- Python isalpha() 方法检测字符串是否只由字母组成。
Python isalpha() 方法检测字符串是否只由字母组成.
- eBPF监控工具bcc系列五工具funccount
eBPF监控工具bcc系列五工具funccount funccount函数可以通过匹配来跟踪函数,tracepoints 或USDT探针.例如所有以vfs_ 开头的内核函数. ./funccount ...
- Stuts2学习——HelloWorld
这两天从对Struts一窍不通到成功运行HelloWorld,在SSH这条路上迈出了第一步. 下面我把我的第一个Struts程序放上来: 一.新建web project,配置文件等准备工作 1. 新建 ...
- 队列的头函数使用C++
queue queue模板类的定义在<queue>头文件中. 与stack模板类很相似,queue模板类也需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的 ...
- Python函数: any()和all()的用法
版权声明:本文为博主原创文章,未经允许不得转载 引子 平常的文本处理工作中,我经常会遇到这么一种情况:用python判断一个string是否包含一个list里的元素. 这时候使用python的内置函数 ...
- Uva 10730 Antiarithmetic?
uva 10730 题意:给出一列数字,如果其中存在长度大于等于3的等差数列,输出no,不存在就输出yes 这道题标定了数列长度n,而且这n个数数据范围是从0到n-1,没有相同的数,这就给我们枚举提供 ...