본문 바로가기

알고리즘 문제풀이/백준

백준(boj) 13458_시험 감독 (파이썬, c++)


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

풀이


시험을 봐야 하는데 총감독관 1명은 고정, 총감독관이 감시 가능한 인원 외 나머지 인원을 부감독관이 모두 감시할 수 있을 만큼 뽑아주면 된다. 계속 빼주면 너무 오래 걸리니 나누기 연산을 사용한다. 단, 나눴을 때 나머지가 0이 아니면 (예를 들어 남은 인원이 3명이고 부감독관 감시 능력이 2명이면 나머지가 생긴다. 이럴 땐 부감독관이 한 명 더 있어야 한다) 나눈 값에 1을 더해준다.

python3

from sys import*
input = stdin.readline

n=int(input())
a=list(map(int,input().split()))
b,c=map(int,input().split())    #총감, 부감
res=0
for x in a:
   x -= b; res+=1  #총감
   if x<=0: continue
   res += (x//c) if x%c==0 else (x//c)+1   #부감
print(res)

C++

#include<cstdio>
using namespace std;

int main(){
       int n, b, c;
       long long res=0;
       scanf("%d",&n);
       int a[n];
       for(int i=0; i<n; i++)
               scanf("%d", &a[i]);
       scanf("%d %d", &b, &c);
       for(auto x: a){
               x-=b, res++;
               if(x<=0) continue;
               res += x%c==0 ? int(x/c): int(x/c)+1;
      }
       printf("%lld",res);
       return 0;
}

고찰


c++에서 결과값 long long 형 될수도 있는데 int형으로 했다가 틀림