티스토리 뷰
1806번: 부분합
첫째 줄에 N (10 ≤ N < 100,000)과 S (0 < S ≤ 100,000,000)가 주어진다. 둘째 줄에는 수열이 주어진다. 수열의 각 원소는 공백으로 구분되어져 있으며, 10,000이하의 자연수이다.
www.acmicpc.net
백준 소스코드 [C++] 1806 부분합
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
#include <set>
#define all(v) v.begin(), v.end()
#define pii pair<int,int>
#define make_unique(v) v.erase(unique(v.begin(), v.end()), v.end())
typedef long long ll;
using namespace std;
int n,m;
vector<int> arr;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n>>m;
arr.resize(n);
for(int i=0;i<n;i++) cin>>arr[i];
int L=0,R=0,length=INT_MAX;
int sum=0;
while(R<=n && L<=n){
if(sum<m) sum+=arr[R++];
else sum-=arr[L++];
if(m<=sum) length=min(length,R-L);
}
if(length==INT_MAX) cout<<"0";
else cout<<length;
return 0;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 16946 벽 부수고 이동하기 4 (0) | 2021.02.19 |
---|---|
백준 소스코드 [C++] 1701 Cubeditor (0) | 2021.02.17 |
백준 소스코드 [C++] 17408 수열과 쿼리 24 (0) | 2021.02.13 |
백준 소스코드 [C++] 16562 친구비 (0) | 2021.02.11 |
백준 소스코드 [C++] 14890 경사로 (0) | 2021.02.05 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 강한 순환 참조
- WWDC19
- 다익스트라 시간복잡도
- CompositionalLayout
- WWDC21
- 에드몬드 카프 알고리즘
- 컴퓨터 추상화
- Testable
- 벨만포드 시간복잡도
- 네트워크 플로우
- mach-o
- 네트워크 유량
- CPU와 Memory
- 최단경로문제
- HIG
- 부스트캠프 6기
- State Restoration
- 포드 풀커슨 알고리즘
- rxswift
- 최단경로 문제
- MeTal
- observeOn
- 최단경로 알고리즘
- WWDC16
- 최대 매칭
- 코딩대회
- 벨만포드 알고리즘
- IOS
- test coverage
- WWDC17
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함