ssl 1205 최대 하위 매트릭스와
2813 단어 DP
N[2<=N<=100]을 주고 N*N의 행렬을 제시한다. 행렬의 수는 [-127127] 사이이다.행렬 중 하위 행렬의 최대 합을 구하십시오.
예를 들면 다음과 같습니다.
0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -1 8 0 -2
가장 큰 하위 행렬은 다음과 같습니다.
9 2 -4 1 -1 8
그것의 합은 15이다.
Input
Output
Sample Input
4 0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -18 0 -2
Sample Output
15
Hint
zju
분석: 접두사와 dp를 미리 처리한 다음에 dp로 하면 됩니다.코드:
var
a,b:array [0..101,0..101] of longint;
ans,max,i,j,n,k:longint;
begin
readln(n);
for i:=1 to n do
for j:=1 to n do read(a[i,j]);
max:=-maxlongint;
for i:=1 to n do
for j:=1 to n do b[i,j]:=a[i,j]+b[i,j-1];
for i:=1 to n do
for j:=i to n do
begin
ans:=0;
for k:=1 to n do
begin
ans:=ans+b[k,j]-b[k,i-1];
if ans>max then max:=ans;
if ans<0 then ans:=0;
end;
end;
writeln(max);
end.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[BOJ]11048(python)python 풀이 DP를 이용해 풀이 보통 이런 문제는 dfs나 bfs로 풀이하는 것이여서 고민을 했는데 이 문구 덕분에 DP 를 이용해 풀이할 수 있었다 뒤로 돌아가는 등의 경우를 고려하지 않아도 되기 때문이다 코...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.