algorithm

λ°±μ€€ 13458

rkawk 2025. 3. 30. 21:52

Intuition

λ‹¨μˆœ μˆ˜ν•™ κ΅¬ν˜„μœΌλ‘œ ν’€λ©΄ λœλ‹€κ³  μƒκ°ν–ˆλ‹€.

Approach

(각 μ‹œν—˜μž₯의 κ°μ‹œμž 수 - 총감독관(B))/C + (각 μ‹œν—˜μž₯의 κ°μ‹œμž 수 - 총감독관(B))%C ? 1 : 0;

으둜 κ΅¬ν˜„ν–ˆλ‹€.

 

ν•˜μ§€λ§Œ 각 μ‹œν—˜μž₯의 κ°μ‹œμž 수 - 총감독관(B)이 μŒμˆ˜κ°€ λ˜λŠ” 경우λ₯Ό κ³ λ €ν•˜μ§€ λͺ»ν•΄ λͺ‡λ²ˆ ν‹€λ Έλ‹€.

음수인 경우 각 μ‹œν—˜μž₯의 κ°μ‹œμž 수 - 총감독관(B)을 0으둜 λ°”κΏ”μ£Όλ©΄ λœλ‹€.

Complexity

- Time complexity : O(N)

- Space complexity : O(N)

Code

#include <iostream>
using namespace std;
long long ans;
int main() {
    long long N, arr[1000001];
    cin>>N;
    for (int i=0; i<N; i++) cin>>arr[i];
    long long B, C; cin>>B>>C;
    for (int i=0; i<N; i++) {
            long long a = (arr[i]-B >= 0) ? ((arr[i]-B)) : 0;
            ans += (a/C + 1);
            if (a%C) ans += 1;
    }
    cout<<ans;
}

'algorithm' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

λ°±μ€€ 14499  (0) 2025.04.03
λ°±μ€€ 3190  (0) 2025.03.30
λ°±μ€€ 12100 (μ‚Όμ„± 기좜)  (0) 2025.03.28
27. Remove Element  (0) 2025.03.21
DP - μ•„μ΄ν…œμ„ 적절히 κ³ λ₯΄λŠ” 문제  (0) 2025.03.21