BOJ : https://www.acmicpc.net/problem/1806
github : https://github.com/junho0956/Algorithm/blob/master/1806/1806/%EC%86%8C%EC%8A%A4.cpp
저만 이 문제에 낚인걸까요
"만일 그러한 합을 만드는 것이 불가능하다면 0을 출력하면 된다."
이 문구에서 일단 합 이상에 대한 최소 길이를 알고 있더라도 정확한 합을 못만들면 0을 출력하라는 의미인줄 알고
계속 헤맸네요..
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
|
#include <iostream>
#include <algorithm>
using namespace std;
int arr[100001];
int N, M, ans = 100001;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> N >> M;
for (int i = 0; i < N; i++) cin >> arr[i];
int left = 0, right = 0;
int sum = 0;
bool temp = false;
while (left <= right && left < N && right <= N) {
/*if (sum == M) temp = true;*/
if (sum >= M)
temp = true, ans = min(ans, right-left), sum -= arr[left++];
else sum += arr[right++];
}
if (temp) cout << ans;
else cout << 0;
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
'algorithm > BOJ' 카테고리의 다른 글
BOJ 1261번 알고스팟 (0) | 2020.01.31 |
---|---|
BOJ 1644번 소수의 연속합 (0) | 2020.01.31 |
BOJ 2003번 수들의 합2 (0) | 2020.01.31 |
BOJ 1987번 알파벳 (0) | 2020.01.31 |
BOJ 2580번 스도쿠 (0) | 2020.01.31 |