• 카테고리

    질문 & 답변
  • 세부 분야

    알고리즘 · 자료구조

  • 해결 여부

    미해결

선생님, 질문이 있습니다.

21.02.10 23:57 작성 조회수 132

0

안녕하세요, 선생님, 강의 잘 듣고 있습니다. 

다음은 제 코드인데 작동 방식은 강의를 들은 토대로 작성을 해서 크게 대동소이 합니다. 

그런데 계속 이 문제 뿐만아니라 dp 모든 문제들을 채점돌려보면 시간초과가 나고 있습니다.. 지금 소스코드와 비교를 해도 크게 다르지는 않은데 이유를 모르겠습니다. 

혹시 제가 비쥬얼 스튜디오를 쓰고 있어서 

#include<bits/stdc++.h>를 안써서 그런것인지..이거도 궁금하네요.

아 그리고

새해 복 많이 받으세요!!

#include<stdio.h>
#include<iostream>
#include<vector>

using namespace std;

typedef struct block {
	int space;
	int height;
	int weight;
}Block;

int dy[101];
Block blocklist[101];


int n;
int main() {

	cin >> n;

	for (int i = 1; i <= n; i++) {
		int s, h, w;
		cin >> s >> h >> w;
		blocklist[i].space = s;
		blocklist[i].height = h;
		blocklist[i].weight = w;
	}


	int ans = 0;
	dy[1] = blocklist[1].height;
	
	for (int i = 2; i <= n; i++) {
		int max = 0;
		//printf("\n\n<<현재 기준점 블럭의 정보 : %d %d %d >>\n\n", blocklist[i].space, blocklist[i].height, blocklist[i].weight);
		for (int j = i - 1; j > 0; j--) {

			if (blocklist[j].space > blocklist[i].space && blocklist[j].weight > blocklist[i].weight && max < dy[j]) {
				//printf("넓이 비교 %d > %d \n\n", blocklist[j].space, blocklist[i].space);
				//printf("무게 비교 %d > %d \n\n", blocklist[j].weight, blocklist[i].weight);
				
				max = dy[j];
				//printf("현재 max값 : %d\n\n\n", max);
			}
		}
		max = max + blocklist[i].height;
		/*printf("새로 쌓을 수 있는 블럭의 높이 :%d\n\n", blocklist[i].height);
		printf("새로 갱신된 max값 : %d\n\n", max);*/
		dy[i] = max;
		//이거 안해줘가지고 계속 안된거임. 
		if (max > ans) {
			ans = max;
		}
	}

	cout << ans;

}

답변 1

답변을 작성해보세요.

0

안녕하세요^^

타림리밋이 나지 않는 코드입니다. 제 컴퓨터에서 해보니 아무 문제 없습니다. 타임리밋보다는  정답이 안나오는 코드입니다.  채점해보시고 정답이 안나오는 2번 데이터를 가지고 디버그해보세요.