BOJ 1806 부분합

Jmnote (토론 | 기여)님의 2023년 11월 29일 (수) 21:11 판 (새 문서: ==개요== 분류: 누적 합 분류: 두 포인터 {{BOJ|단계=33}} ==C++== <syntaxhighlight lang='cpp'> #include <bits/stdc++.h> using namespace std; int N, S; int A[1000...)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요

BOJ 1806 부분합


2 C++

#include <bits/stdc++.h>
using namespace std;

int N, S;
int A[100000];
int INF=INT_MAX;

int solve() {
    int start = 0, end = 0;
    int sum = A[0];
    int ans = INF;
    while(start <= end && end < N) {
        if(sum >= S) {
            ans = min(ans, end-start+1);
            sum -= A[start];
            start++;
        } else {
            end++;
            sum += A[end]; 
        }
    }
    if(ans == INF) return 0;
    return ans;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> N >> S;
    for(int i=0; i<N; i++) {
        cin >> A[i];
    }
    cout << solve();
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}