인프런 커뮤니티 질문&답변
선생님, 3중 for 문으로 만든 것 같은데 TimeLimit 에 걸립니다.
해결된 질문
작성
·
260
0
문제를 처음 보자마자 4중 for문을 생각했지만, 4중 for 문은 large 에 쓰이지 못 할 것 같아서 조금 고민해서 나름 3중for 문으로 만들었다고 생각했는데 3번 case 까지는 success 이지만 4번부터 Time Limit 이 걸립니다.
충분히 고민했다고 생각하지만 원인을 못 찾겠어서 질문 올립니다 ㅠㅠ
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
using namespace std;
// global variable
int territory[5][50];
// function
// main
int main(int argc, char** argv) {
// input
int i, j ,k;
int h, w, my_h, my_w, sum = 0, pos_h, pos_w, max = -2147000000;
scanf("%d %d", &h, &w);
for (i = 0; i < h; i++)
for (j = 0; j < w; j++)
scanf("%d", &territory[i][j]);
scanf("%d %d", &my_h, &my_w);
// algorithm
for (i = 0; i <= h - my_h; i++) {
sum = 0;
// 처음 내 영역의 sum 을 구한다.
for (pos_h = i; pos_h < i + my_h; pos_h++) {
for (pos_w = 0; pos_w < my_w; pos_w++) {
sum += territory[pos_h][pos_w];
}
}
if (max < sum) max = sum;
// 한칸씩 오른쪽으로 이동해가면서 합을 구한다.
for (j = 0; j <= w - my_w; j++) {
for (pos_h = i; pos_h < i + my_h; pos_h++) {
// 왼쪽을 뺀다.
sum -= territory[pos_h][j];
// 오른쪽을 더한다.
sum += territory[pos_h][j+my_w];
}
if (max < sum) max = sum;
}
}
// print(needed)
printf("%d", max);
// exit
return 0;
}퀴즈
선택 정렬(Selection Sort) 알고리즘은 각 단계에서 어떤 작업을 수행하여 배열을 정렬하나요?
인접한 두 요소를 비교하여 필요시 교환합니다.
정렬되지 않은 부분에서 가장 작은(또는 큰) 값을 찾아 정렬된 부분의 올바른 위치로 옮깁니다.
현재 요소를 이미 정렬된 부분 배열의 적절한 위치에 삽입합니다.
배열을 분할하고 각 부분을 재귀적으로 정렬한 후 병합합니다.





