이 영역을 누르면 첫 페이지로 이동
자라자 블로그의 첫 페이지로 이동

자라자

페이지 맨 위로 올라가기

자라자

개발자를 준비하는 자라자의 블로그입니다.

백준 6918 옥상 정원 꾸미기 c++

  • 2022.01.13 13:27
  • PS/BOJ

백준로고

[문제 링크]

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

[입출력 예]

입력 출력
6
10
3
7
4
12
2
5

[1차 시도]

#include <bits/stdc++.h>
#define MAX_N 80000
#define MAX_h 1000000001
#define X first
#define Y second
int a[MAX_N];

using namespace std;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int ans=0;

    // n값 입력
    int n;
    cin>>n;

    // 배열 입력
    for(int i=0; i<n;i++ ){
        cin>>a[i];
    }

    // <인덱스,높이>를 담는 스택
    stack <pair<int,int>> s;
    pair<int,int> p ={n,MAX_h};
    s.push(p);
    // 순회
    for (int i=n-1;i>=0; i--){
       while(s.top().Y<a[i]){
           s.pop();
       }
       ans+=s.top().X-i-1;
       s.push({i,a[i]});
    }
    cout<<ans;
    return 0;
}

[소스코드]

#include <bits/stdc++.h>
#define MAX_N 80000
#define MAX_h 1000000001
#define ll long long
#define X first
#define Y second
int a[MAX_N];

using namespace std;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    ll ans=0;

    // n값 입력
    ll n;
    cin>>n;

    // 배열 입력
    for(int i=0; i<n;i++ ){
        cin>>a[i];
    }

    // <인덱스,높이>를 담는 스택
    stack <pair<ll,int>> s;
    pair<ll,int> p ={n,MAX_h};
    s.push(p);
    // 순회
    for (int i=n-1;i>=0; i--){
       while(s.top().Y<a[i]){
           s.pop();
       }
       ans+=s.top().X-i-1;
       s.push({i,a[i]});
    }
    cout<<ans;
    return 0;
}
  • 빌딩 갯수가 8만이고 N제곱이 64억이므로 ans를 long long으로 선언해주어야 한다.

[소스코드2]

// Authored by : unluckyjung
// Co-authored by : BaaaaaaaaaaarkingDog
// http://boj.kr/a84f083cdee3436f9f46acdef175e55f

#include <bits/stdc++.h>
using namespace std;

#define ll long long

stack<int> s;
int n;
ll ans;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;
  ll h;
  while (n--) {
    cin >> h;
    while(!s.empty() && s.top() <= h)
      s.pop();

    // ans에는 현재 건물을 볼 수 있는 건물들의 수 만큼 더해진다.
    // 한 건물에서 몇 건물을 볼 수 있는지 세는 것이 아니라
    // 이 건물이 몇 건물에게 보여지는 지 세는 방식.
    // 스택에는 내림차순으로 건물이 정리된다.
    ans += s.size();
    s.push(h);
  }
  cout << ans;
  return 0;
}

'PS > BOJ' 카테고리의 다른 글

백준 3015 오아시스 재결합 c++  (0) 2022.01.25
백준 17298 오큰수 c++  (0) 2022.01.13
백준 1874 스택수열 c++  (0) 2022.01.08
백준 2493 탑 c++  (0) 2022.01.08
백준 5397 키로거 c++  (0) 2022.01.04

댓글

이 글 공유하기

  • 구독하기

    구독하기

  • 카카오톡

    카카오톡

  • 라인

    라인

  • 트위터

    트위터

  • Facebook

    Facebook

  • 카카오스토리

    카카오스토리

  • 밴드

    밴드

  • 네이버 블로그

    네이버 블로그

  • Pocket

    Pocket

  • Evernote

    Evernote

다른 글

  • 백준 3015 오아시스 재결합 c++

    백준 3015 오아시스 재결합 c++

    2022.01.25
  • 백준 17298 오큰수 c++

    백준 17298 오큰수 c++

    2022.01.13
  • 백준 1874 스택수열 c++

    백준 1874 스택수열 c++

    2022.01.08
  • 백준 2493 탑 c++

    백준 2493 탑 c++

    2022.01.08
다른 글 더 둘러보기

정보

자라자 블로그의 첫 페이지로 이동

자라자

  • 자라자의 첫 페이지로 이동

검색

메뉴

  • 🏠 HOME
  • 💡 ABOUT
  • 💻 GITHUB

카테고리

  • 분류 전체보기 (91)
    • Tech Note (3)
    • Dev Log (11)
    • Study Log (11)
    • Settings (3)
    • PS (53)
      • Programmers (21)
      • BOJ (32)
    • Diary (10)

최근 글

인기 글

댓글

공지사항

아카이브

태그

  • 리액트
  • 공식문서읽기
  • ㅣ

나의 외부 링크

정보

자라자의 자라자

자라자

자라자

블로그 구독하기

  • 구독하기
  • RSS 피드

방문자

  • 전체 방문자
  • 오늘
  • 어제

티스토리

  • 티스토리 홈
  • 이 블로그 관리하기
  • 글쓰기
Powered by Tistory / Kakao. © 자라자. Designed by Fraccino.

티스토리툴바