HDU1231最大连续子序列&Uva108Maximum Sum最大子矩阵(尺取法)
2017-07-11 13:12
387 查看
HDU1231:
给定K个整数的序列{ N1, N2, …, NK },其任意连续子序列可表示为{ Ni, Ni+1, …,
Nj },其中 1 <= i <= j <= K。最大连续子序列是所有连续子序列中元素和最大的一个,
例如给定序列{ -2, 11, -4, 13, -5, -2 },其最大连续子序列为{ 11, -4, 13 },最大和
为20。
在今年的数据结构考卷中,要求编写程序得到最大和,现在增加一个要求,即还需要输出该
子序列的第一个和最后一个元素。
Input
测试输入包含若干测试用例,每个测试用例占2行,第1行给出正整数K( < 10000 ),第2行给出K个整数,中间用空格分隔。当K为0时,输入结束,该用例不被处理。
Output
对每个测试用例,在1行里输出最大和、最大连续子序列的第一个和最后一个元
素,中间用空格分隔。如果最大连续子序列不唯一,则输出序号i和j最小的那个(如输入样例的第2、3组)。若所有K个元素都是负数,则定义其最大和为0,输出整个序列的首尾元素。
Sample Input
6
-2 11 -4 13 -5 -2
10
-10 1 2 3 4 -5 -23 3 7 -21
6
5 -8 3 2 5 0
1
10
3
-1 -5 -2
3
-1 0 -2
0
Sample Output
20 11 13
10 1 4
10 3 5
10 10 10
0 -1 -2
0 0 0
Huge input, scanf is recommended.
Hint
Hint
题意:给出一行数字,找出一串和最大的连续的子序列。
解题思路:模拟心中的想法。当当前子序列的和小于0时这串子序列就不再继续往下扩展了,此时子序列的开头应从下一个元素开始了。如5 -8 3 2 5 0,当以5为序列开头,计算到5-8=-3<0时就不再继续扩招将3加入子序列了(想一想为什么)而是将3作为子序列新的开头往后判断。
Uva108:
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.
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 subrectangle
with the largest sum is referred to as the maximal sub-rectangle.
A sub-rectangle is any contiguous sub-array of size 1 × 1 or greater located within the whole array.
As an example, the maximal sub-rectangle of the array:
0 −2 −7 0
9 2 −6 2
−4 1 −4 1
−1 8 0 −2
is in the lower-left-hand corner:
9 2
−4 1
−1 8
and has the sum of 15.
Input
The input consists of an N × N 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 N2
integers separated by white-space (newlines and spaces).
These N2
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].
Output
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
题意:给出一个矩阵,找出矩阵中和最大的子矩阵。
解题思路:着重思考一下这道题。受上道题启发,可不可以将矩阵也用处理序列的方法来解决这道题? 举个例子:
(图虽然有点粗糙但是应该可以看得懂~)
将输入的数字进行以下处理:a[i][j]+=a[i-1][j];
使a数组中每个数表示到该行为止该列的和,如图中5对应0+9+(-4)
通过三个for循环使得每一块子矩阵都可以被表示。
再使用上题中的思路模拟求解。
给定K个整数的序列{ N1, N2, …, NK },其任意连续子序列可表示为{ Ni, Ni+1, …,
Nj },其中 1 <= i <= j <= K。最大连续子序列是所有连续子序列中元素和最大的一个,
例如给定序列{ -2, 11, -4, 13, -5, -2 },其最大连续子序列为{ 11, -4, 13 },最大和
为20。
在今年的数据结构考卷中,要求编写程序得到最大和,现在增加一个要求,即还需要输出该
子序列的第一个和最后一个元素。
Input
测试输入包含若干测试用例,每个测试用例占2行,第1行给出正整数K( < 10000 ),第2行给出K个整数,中间用空格分隔。当K为0时,输入结束,该用例不被处理。
Output
对每个测试用例,在1行里输出最大和、最大连续子序列的第一个和最后一个元
素,中间用空格分隔。如果最大连续子序列不唯一,则输出序号i和j最小的那个(如输入样例的第2、3组)。若所有K个元素都是负数,则定义其最大和为0,输出整个序列的首尾元素。
Sample Input
6
-2 11 -4 13 -5 -2
10
-10 1 2 3 4 -5 -23 3 7 -21
6
5 -8 3 2 5 0
1
10
3
-1 -5 -2
3
-1 0 -2
0
Sample Output
20 11 13
10 1 4
10 3 5
10 10 10
0 -1 -2
0 0 0
Huge input, scanf is recommended.
Hint
Hint
题意:给出一行数字,找出一串和最大的连续的子序列。
解题思路:模拟心中的想法。当当前子序列的和小于0时这串子序列就不再继续往下扩展了,此时子序列的开头应从下一个元素开始了。如5 -8 3 2 5 0,当以5为序列开头,计算到5-8=-3<0时就不再继续扩招将3加入子序列了(想一想为什么)而是将3作为子序列新的开头往后判断。
#include<stdio.h> int main() { int a[10010],t,sum,max,left,right,i,n; while(~scanf("%d",&n)&&n) { for(i=0;i<n;i++) scanf("%d",&a[i]); t=a[0]; left=right=a[0]; sum=0; max=a[0]; for(i=0;i<n;i++) { if(sum<0) { t=a[i]; sum=a[i]; } else { sum+=a[i]; } if(sum>max) { max=sum; left=t; right=a[i]; } } if(max<0) printf("0 %d %d\n",a[0],a[n-1]); else printf("%d %d %d\n",max,left,right); } return 0; }
Uva108:
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.
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 subrectangle
with the largest sum is referred to as the maximal sub-rectangle.
A sub-rectangle is any contiguous sub-array of size 1 × 1 or greater located within the whole array.
As an example, the maximal sub-rectangle of the array:
0 −2 −7 0
9 2 −6 2
−4 1 −4 1
−1 8 0 −2
is in the lower-left-hand corner:
9 2
−4 1
−1 8
and has the sum of 15.
Input
The input consists of an N × N 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 N2
integers separated by white-space (newlines and spaces).
These N2
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].
Output
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
题意:给出一个矩阵,找出矩阵中和最大的子矩阵。
解题思路:着重思考一下这道题。受上道题启发,可不可以将矩阵也用处理序列的方法来解决这道题? 举个例子:
(图虽然有点粗糙但是应该可以看得懂~)
将输入的数字进行以下处理:a[i][j]+=a[i-1][j];
使a数组中每个数表示到该行为止该列的和,如图中5对应0+9+(-4)
通过三个for循环使得每一块子矩阵都可以被表示。
再使用上题中的思路模拟求解。
#include<stdio.h> int main() { int a[110][110],n,t,i,j,k,max,sum; while(~scanf("%d",&n)) { for(i=1;i<=n;i++) for(j=1;j<=n;j++) { scanf("%d",&a[i][j]); a[i][j]+=a[i-1][j]; } max=a[1][1]; for(i=0;i<=n;i++) { for(j=i;j<=n;j++) { sum=0; for(k=1;k<=n;k++) { if(sum<0) sum=0; else if(i!=j) sum+=a[j][k]-a[i][k]; if(sum>max&&sum!=0) max=sum; } } } printf("%d\n",max); } return 0; }
相关文章推荐
- UVa 108 - Maximum Sum (最大子矩阵)
- UVa 1330 - City Game 最大子矩阵
- UVA 1330 City Game(最大子矩阵)
- UVA 10827 Maximum sum on a torus(环面上的最大子矩阵和)
- uva1330 在一个大的矩阵中寻找面积最大的子矩阵
- UVA - 10755 Garbage Heap(最大子矩阵)
- uva 1330 City Game (最大子矩阵)
- UVA-1330 LA 3029 City Game(最大子矩阵)
- UVa 10755 - Garbage Heap 三维最大子矩阵问题转化为1维..
- 【降维解法:最大字段和->最大子矩阵和->最终版最大子长方体和】【UVA10755】Garbage Heap
- 【降维解法:最大字段和->最大子矩阵和->最终版最大子长方体和】【UVA10755】Garbage Heap
- uva 10755 - Garbage Heap(三维最大子矩阵)
- Take the Land - UVa 10074 变形最大子矩阵和
- uvalive 最大子矩阵
- UVA 11297 Census(2D线段树解决子矩阵的最大最小)
- Uva 507 最大子矩阵和
- UVA - 10827 Maximum sum on a torus(dp最大子矩阵和)
- UVA - 10827 Maximum sum on a torus 求循环矩阵的最大子矩阵
- UVA 10755 Garbage Heap 三维最大子矩阵和 -
- UVa 10827 Maximum sum on a torus (贪心&矩阵环中的最大子矩阵和)