제출 #1368816

#제출 시각아이디문제언어결과실행 시간메모리
1368816po_rag526BOI Acronym (BOI25_boi)C++17
11 / 100
1094 ms436 KiB
#include<bits/stdc++.h>
using namespace std;
int n;
vector<vector<int>> M;
string acronym;
vector<int> result;
void countChars(int l, int r, int& cntB, int& cntO, int& cntI) {
    cntB = cntO = cntI = 0;
    for(int i = l; i <= r; i++) {
        if(acronym[i] == 'B') cntB++;
        else if(acronym[i] == 'O') cntO++;
        else if(acronym[i] == 'I') cntI++;
    }
}


bool checkConstraint(int l, int r) {
    int cntB, cntO, cntI;
    countChars(l, r, cntB, cntO, cntI);
    int maxCnt = max({cntB, cntO, cntI});
    return maxCnt == M[l][r];
}

bool canContinue(int filledUpTo) {
    // Check all completed substrings
    for(int i = 1; i <= filledUpTo; i++) {
        for(int j = i; j <= filledUpTo; j++) {
            int cntB, cntO, cntI;
            countChars(i, j, cntB, cntO, cntI);
            int maxCnt = max({cntB, cntO, cntI});
            
            // Violation check
            if(maxCnt > M[i][j]) return false;
        }
    }
    return true;
}

bool solve(int pos) {
    if(pos > n) {
        // Verify all constraints
        for(int i = 1; i <= n; i++) {
            for(int j = i; j <= n; j++) {
                if(!checkConstraint(i, j)) return false;
            }
        }
        
        int cntB = 0, cntO = 0, cntI = 0;
        for(int i = 1; i <= n; i++) {
            if(acronym[i] == 'B') cntB++;
            else if(acronym[i] == 'O') cntO++;
            else if(acronym[i] == 'I') cntI++;
        }
        
        if(cntB > cntO && cntB > cntI) {
            result.clear();
            for(int i = 1; i <= n; i++) {
                if(acronym[i] == 'B') result.push_back(i);
            }
            return true;
        }
        return false;
    }
    

    for(char c : {'B', 'O', 'I'}) {
        acronym[pos] = c;
        
        if(canContinue(pos)) {
            if(solve(pos + 1)) {
                return true;
            }
        }
    }
    
    acronym[pos] = ' ';
    return false;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    cin >> n;
    
    M.assign(n + 1, vector<int>(n + 1));
    acronym.assign(n + 1, ' ');
    
    for(int i = 1; i <= n; i++) {
        for(int j = i; j <= n; j++) {
            cin >> M[i][j];
        }
    }
    
    if(solve(1)) {
        for(int i = 0; i < result.size(); i++) {
            if(i > 0) cout << " ";
            cout << result[i];
        }
        cout << "\n";
    }
    
    return 0;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…