1417

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 내림차순 정렬을 위한 비교 함수
int compare_desc(const void *a, const void *b) {
    // void 포인터를 int 포인터로 형변환
    int num1 = *(const int *)a;
    int num2 = *(const int *)b;

    // num1이 num2보다 크면 양수 반환
    if (num1 < num2) {return 1;}
    // num1이 num2보다 작으면 음수 반환
    else if (num1 > num2) {return -1;}
    // 같으면 0 반환
    else {return 0;}
}

int main(void){
    // 후보 수
    int candidate_num;
    scanf("%d", &candidate_num);

    // 기호 1번 투표수
    int num_one;
    scanf("%d", &num_one);

    // 기호 2번 ~ N번 투표수 입력
    int vote_len = candidate_num-1;
    int vote_arr[vote_len];
    for(int i = 0; i < vote_len; i++){
        int vote_num;
        scanf("%d", &vote_num);
        vote_arr[i] = vote_num;
    }

    // 국회의원 선거
    int count = 0;
    while(vote_len != 0){
        // 내림차순 정렬
        qsort(vote_arr , vote_len , sizeof(vote_arr[0]) , compare_desc);

        // 기호 1번 투표 수가 더 적으면
        if(vote_arr[0] >= num_one){
            // 카운트 
            num_one++;
            vote_arr[0]--;
            count++;
        }
        // 더 크면 중단
        else{
            break;
        }
        
    }

    // 출력
    printf("%d", count);

    return 0;
}

'백준' 카테고리의 다른 글

[ 백준 / C ] 1236번 : 성 지키기  (0) 2025.10.09
[ 백준 / C ] 1026번 : 보물  (0) 2025.10.08
[ 백준 / C ] 2475번 : 검증수  (0) 2025.10.07
[ 백준 / C ] 2741번 : N 찍기  (0) 2025.10.06
[ 백준 / C ] 4470번 : 줄번호  (0) 2025.10.03

+ Recent posts