작성
·
211
0
선생님 아래 코드는 제가 먼저 해설없이 풀어봤는데 정답 '10' 나오고 다이나믹 배열에 해당 벽돌마다 알맞은 높이값이 계산되서 들어가는데 왜 오답인지 알 수 있나요? 인자를 하나하나씩넣어서 그림그리면서 로직 태워봐도 어디가 문제인지 잘 이해가 안됩니다..
import java.util.ArrayList;
import java.util.Scanner;
class Point{
int a;
int h;
int w;
public Point(int a, int h, int w){
this.a = a;
this.h = h;
this.w = w;
}
}
public class Main {
static int num;
static int[] dy;
static void Solution(ArrayList<Point> arr){
dy[0] = arr.get(0).h; // 시작 셋팅
int result = dy[0];
for (int i = 1; i < num; i++) {
int temp = arr.get(i).h;
for (int j = i-1; j >= 0; j--) {
if(arr.get(j).a > arr.get(i).a && arr.get(j).w > arr.get(i).w){
if(temp < dy[j] + arr.get(i).h) temp = dy[j] + arr.get(i).h;
}
}
dy[i] = temp;
result = Math.max(result, dy[i]);
}
System.out.println(result);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
num = sc.nextInt();
ArrayList<Point> arr = new ArrayList<>();
dy = new int[num];
for (int i = 0; i < num; i++) {
int a = sc.nextInt();
int h = sc.nextInt();
int w = sc.nextInt();
arr.add(new Point(a, h, w));
}
Solution(arr);
}
}
답변 1
1
안녕하세요^^
반례입니다. 분석해보세요.
10
114 96 290
65 74 201
261 19 105
181 60 275
90 145 254
286 118 64
16 24 205
288 128 299
96 36 74
182 5 35
답은 443입니다.
"각 벽돌은 입력되는 순서대로 1부터 연속적인 번호를 가진다" 이 문구 때문에 순서를 바꾸면 안 되는줄 알아서 저도 같은 실수를 했습니다 ㅠㅠ...