[BOJ] 1806 부분 합

Time Lapse :NONE

1806.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#define gc() getchar_unlocked()
long psum[100001];
int fRI(){
int N=gc(),ret = 0;
for(;N<48||N>57;N=gc());
do{
ret = (ret<<3) + (ret<<1) + (N&0b1111); N = gc();
}while(0x30<=N);
return ret;
}
int main() {
register int N = fRI(), target = fRI(), ans = 100002, i, j;
for (i = 1; i <= N; ++i) psum[i] = fRI() + psum[i - 1];
for (i = 0; i < N; ++i)
for (j = i + 1; j <= N && j < i + ans; ++j)
if (psum[j] >= target + psum[i]) {
ans = j - i; break;
}
printf("%d", ans==100002 ? 0 : ans);
}
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
31
32
33
#include <iostream>
#include <list>
#include <map>
#include <unordered_map>
#include <vector>

using namespace std;

vector<int> arr(100000);
int N, S;

int solution() {
if(arr[0] >= S) return 1;
int from = 0, tail = 0, sum = arr[0], res = N + 1;
while(tail < N) {
if(sum >= S) {
sum -= arr[from++];
if(from > tail) return 1;
} else {
sum += arr[++tail];
}

if(sum >= S)
res = min(res, tail - from + 1);
}
return res == N + 1 ? 0 : res;
}
int main() {
scanf("%d %d",&N, &S);
for(int i = 0; i < N; i++)
scanf("%d", &arr[i]);
cout<<solution();
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/1806/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.