[SWEA] 9088 다이아몬드

Time Lapse :21min 20sec

9088.cpp

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 <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
int main(void) {
int T, tc = 0,N,K,n,ans,cur;
scanf("%d", &T);
while (T--) {
scanf("%d %d", &N, &K);
vector<long long> v;
v.reserve(N);
int f , e;
for (int i = 0; i < N; ++i) {
scanf("%d", &n);
v.push_back(n);
}
sort(v.begin(), v.end());
f = e = ans = cur = 0;
for(; e < N; ++e){
if (v[e] - v[f] <= K) {
++cur;
ans = ans > cur ? ans : cur;
}
else {
while (v[e] - v[f] > K && f < N)
--cur, ++f;
--e;
}
}
printf("#%d %d\n", ++tc, ans);
}
return 0;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/08/04/PS/SWEA/9088/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.