728x90
반응형
https://www.acmicpc.net/problem/1806
1806번: 부분합
첫째 줄에 N (10 ≤ N < 100,000)과 S (0 < S ≤ 100,000,000)가 주어진다. 둘째 줄에는 수열이 주어진다. 수열의 각 원소는 공백으로 구분되어져 있으며, 10,000이하의 자연수이다.
www.acmicpc.net
바로 전에 올렸었던 문제와 거의 동일한 문제이다.
투포인터로 사용하면 O(n)으로 해결이 가능하다.
#include<iostream>
#include<cmath>
#include<list>
#include<stack>
#include<tuple>
#include<map>
#include<algorithm>
#include<vector>
#include<deque>
#include<queue>
#define CUNLINK ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl '\n'
#define MAX_SIZE 1003
#define INF 0x7fffffff
using namespace std;
typedef long long ll;
int N;
ll M;
vector<int> arr;
int main() {
CUNLINK;
cin >> N >> M;
for (int i = 0; i < N; i++) {
int x; cin >> x;
arr.push_back(x);
}
int en = 1;
int Sum = arr[en] + arr[0];
int answer = INF;
for (int st = 0; st < N; st++) {
while (en < N && Sum < M) {
en++;
if (en == N) break;
Sum += arr[en];
}
if (en == N) break;
//cout << st << " <-> " << en << endl;
answer = min(answer, en - st+1);
Sum -= arr[st];
}
if (answer == INF) answer = 0;
cout << answer;
return 0;
}
728x90
반응형
'BOJ > 투포인터' 카테고리의 다른 글
[C/C++] 백준 - 3273번 : 두 수의 합 (0) | 2021.09.24 |
---|---|
[C/C++] 백준 - 11728번 : 배열 합치기 (0) | 2021.09.06 |
[C/C++] 백준 - 1644번 : 소수의 연속합 (0) | 2021.08.27 |
[C/C++] 백준 - 20922번 : 겹치는 건 싫어 (0) | 2021.08.26 |
[C/C++] 백준 - 2230번 : 수 고르기 (0) | 2021.08.24 |