티스토리 뷰

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

 

8982번: 수족관 1

입력의 첫 줄은 수족관의 경계에 있는 꼭짓점의 개수 N(1 ≤ N ≤ 5,000)이 주어진다. N은 짝수이다. 수족관의 경계는 항상 꼭짓점 (0, 0)부터 시작한다. 그리고 마지막 꼭짓점은 (A, 0)의 형태로 끝난��

www.acmicpc.net

바닥면의 높이가 같은 인접 공간을 한 구역으로 묶어 풀이하였습니다.


C++ 소스 코드

#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main(){
    int n;
    cin >> n;
    n-=2;
    n/=2;
    vector<int> bottom(n,-1);
    vector<int> holeIdx;
    vector<int> top(n,0);
    vector<int> width(n,0);
    map<int, int> dict;
    int x,y;
    cin >> x >> y; // (0,0)
    for(int i=0;i<n;i++){  // 수족관 경계
        int x1,y1,x2,y2; // y1 == y2 짝으로 입력받음
        cin >> x1 >> y1 >> x2 >> y2;
        bottom[i]=y1;
        width[i]=(x2-x1);
        dict[x2]=i; // 구역 번호 = i
    }
    cin >> x >> y; // (x,0)
    
    int m;
    cin >> m;
    for(int i=0;i<m;i++){ // 구멍 위치
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        int index = dict[x2];
        holeIdx.push_back(index);
    }

    for(int i=0;i<holeIdx.size();i++){
        int idx = holeIdx[i];
        int surface =  bottom[idx];
        for(int j=idx;j>=0;j--){
            surface = min(surface, bottom[j]);
            //높이 갱신
            top[j] = max(top[j],surface);
        }
        surface =  bottom[idx];
        for(int j=idx+1;j<n;j++){
            surface = min(surface, bottom[j]);
            //높이 갱신
            top[j] = max(top[j],surface);
        }
    }
    long long ans = 0;
    for(int i=0;i<n;i++){
        if (bottom[i] > top[i]){
            ans += (long long)(bottom[i]-top[i])*width[i];
        }
    }
    cout << ans << '\n';
    return 0;
}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함