Skip to content

Conversation

@oncsr
Copy link
Contributor

@oncsr oncsr commented Oct 27, 2025

🧷 문제 링크

https://www.acmicpc.net/problem/5479

🧭 풀이 시간

70분

👀 체감 난이도

✏️ 문제 설명

NM 격자에서 (AB 크기의 직사각형 내에 적힌 수의 합) - (그 안에 C*D 크기의 직사각형 내에 적힌 수의 합) 이 최대가 되도록 하는 직사각형 배치를 구해보자.

🔍 풀이 방법

AB 크기의 누적 합과 CD 크기의 누적 합을 미리 계산해놓는다.
CD 크기의 누적 합 원소들로 Monotone Deque을 두 번 써서 2차원 직사각형 최솟값을 구해놓고, AB 누적 합과의 차이 중 최댓값을 구해줬다.

⏳ 회고

왜 틀리지 진짜 모르겠어서 열받는다

@oncsr oncsr added the fail 😢 해설을 보고 풀었거나, 못 풀었을 때 label Oct 27, 2025
@ShinHeeEul ShinHeeEul merged commit 5ff0846 into main Oct 27, 2025
1 check passed
@oncsr
Copy link
Contributor Author

oncsr commented Oct 27, 2025

Monotone Deque 돌리는 부분 수정

for (int i = 2; i < N - C + 1; i++) {
	deque<pair<int, int>> dq;
	for (int j = 2; j < M - D + 1; j++) {
		while (!dq.empty() && dq.back().first > crr[i][j]) dq.pop_back();
		dq.emplace_back(crr[i][j], j);
		while (!dq.empty() && j - dq.front().second >= B - 2 - D + 1) dq.pop_front();
		if (j - (B - 2 - D + 1) + 1 >= 2) drr[i][j - (B - 2 - D + 1) + 1] = dq.front().first;
	}
}

for (int j = 2; j < M - D + 1; j++) {
	deque<pair<int, int>> dq;
	for (int i = 2; i < N - C + 1; i++) {
		while (!dq.empty() && dq.back().first > drr[i][j]) dq.pop_back();
		dq.emplace_back(drr[i][j], i);
		while (!dq.empty() && i - dq.front().second >= A - 2 - C + 1) dq.pop_front();
		if (i - (A - 2 - C + 1) + 1 >= 2) err[i - (A - 2 - C + 1) + 1][j] = dq.front().first;
	}
}

최소 누적 합 역추적 부분 수정

int y = -1, z = -1;
for (int i = w + 1; i <= w + A - 2 - C + 1; i++) for (int j = x + 1; j <= x + B - 2 - D + 1; j++) {
	if (crr[i][j] == err[w+1][x+1]) {
		return cout << x << ' ' << w << '\n' << j << ' ' << i, 0;
	}
}

두 군데에서 인덱스 이상하게 써서 틀린 거였음,,,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fail 😢 해설을 보고 풀었거나, 못 풀었을 때

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants