회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
1
채점 현황
랭킹
게시판
그룹
더 보기
재채점 기록
블로그
강의
실험실
도움말
BOJ Stack
BOJ Book
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
반례 좀 찾아주세요
6549번 - 히스토그램에서 가장 큰 직사각형
pulsar5813
7년 전
0
Stack 이용해서 푸는 방식으로 했는데...
왜 틀렸다고 나오는지 잘 모르겠어요ㅠ
#include <iostream> using namespace std; class Component { public : int height; long long int value; Component* next; Component(int h, long long int v, Component* n) { height = h; value = v; next = n; } }; Component* top; int stackSize; long long int maxValue; void push(int h, long long int v) { if(stackSize == 0) top = new Component(h, v, NULL); else { Component* tmp = new Component(h, v, top); top = tmp; } stackSize++; } void pop() { if (stackSize == 0) return; else { Component* tmp = top; top = top->next; delete[] tmp; stackSize--; return; } } void checkStack() { int t = stackSize; Component* tmp = top; while(t != 0) { t--; cout<<"tmp next is not NULL and Component stored h is "<<tmp->height<<endl; tmp = tmp->next; } return; } void buildHistogram(int h) { if(stackSize == 0) push(h, h); else { if(h >= top->height) push(h, h); else { int numOfPop = 0; int justPop = 0; while((stackSize != 0)&&(top->height >= h)) { if(top->height*numOfPop + top->value > maxValue) maxValue = top->height*numOfPop + top->value; justPop = top->value; pop(); numOfPop++; } push(h, h*numOfPop + justPop); } } return; } int main(void) { long long int ans[100000]; int ptr = -1; while(true) { int input; cin>>input; //cout<<"input size is "<<input<<endl; if(input == 0) break; else { ptr++; stackSize = 0; maxValue = 0; top = NULL; int h; for(int i=0; i<input; i++) { cin>>h; buildHistogram(h); //cout<<"currently pushed h value is "<<h<<endl; //checkStack(); } if(stackSize == 0) ans[ptr] = maxValue; else { buildHistogram(-1); ans[ptr] = maxValue; } } } for(int i=0; i<=ptr; i++) cout<<ans[i]<<endl; return 0; }
댓글을 작성하려면
로그인
해야 합니다.
pulsar5813 7년 전
Stack 이용해서 푸는 방식으로 했는데...
왜 틀렸다고 나오는지 잘 모르겠어요ㅠ