-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3273bj.cpp
More file actions
52 lines (45 loc) · 849 Bytes
/
3273bj.cpp
File metadata and controls
52 lines (45 loc) · 849 Bytes
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <bits/stdc++.h>
using namespace std;
int n;
int arr[100002];
int cnt;
int x;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
sort(arr, arr + n); //정렬
cin >> x;
//투 포인터 이용
int start=0; // 첫번째 원소 가리킴
int end =n-1;
int temp = 0;
while (start<end) {
temp = arr[start] + arr[end];
if (temp == x) {
cnt++;
start++;
end--;
}
else if (temp < x) {
start++; //x=100이고 (1,98)일때 start를 다음꺼로 해줘야 100됨
}
else end--; //반대의 경우 end를 감소 --
}
/*
//시간 초과에러 why? 이중 for문때문에 시간복잡도 O(n^2)
for (int i = 1; i <= n; i++) {
for (int j = i+1; j <=n; j++) {
if (x == (arr[i] + arr[j]))
{
cnt++;
}
}
}
*/
cout << cnt;
return 0;
}