Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
Tags
- error
- thymeleaf
- dfs
- java
- ์ต๋จ ๊ฒฝ๋ก
- ๋ฌธ์์ด
- CVE
- BFS
- ์คํ
- ์ฐ์ ์์ ํ
- ๋ฐ์ดํฌ์คํธ๋ผ
- ์๋ฎฌ๋ ์ด์
- c++
- ์์ ์ ๋ ฌ
- DP
- ๋์ ํฉ
- ๋ถํ ์ ๋ณต
- Spring
- ๋งต
- OS
- ๋ฐฑํธ๋ํน
- GCP
- JPA
- Reversing
- ๊ทธ๋ฆฌ๋
- ๊ตฌํ
- ์ด๋ถ ํ์
- dynamic debugging
- web
- ์ฌ๊ท
Archives
- Today
- Total
hades
[Baekjoon] 1920๋ฒ: ์ ์ฐพ๊ธฐ ๋ณธ๋ฌธ
๐ฅ ์ค๊ณ
https://www.acmicpc.net/problem/1920
๐ ๋ถ์
์์ ๊ฐ์๊ฐ ์ต๋ 100000๊ฐ์ด๊ณ , ์ฐพ์ผ๋ ค๋ ์์ ๊ฐ์๊ฐ ์ต๋ 100000๊ฐ์ด๋ฏ๋ก, ์์ฐจ ํ์์ ํ ๊ฒฝ์ฐ, O(10^10)์ผ๋ก ์๊ฐ ์ ํ์ ์ด๊ณผํ๋ค. ๋ฐ๋ผ์, ์ด๋ถ ํ์์ ํ์ฉํ๋ค.
๐ ํ์ด
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n, m, target;
vector<int> numbers(100001);
bool binary_search(int target){
int start = 0, end = n-1, mid;
while (start <= end) {
mid = (start+end)/2;
if (numbers[mid] == target){
return true;
}
else if (numbers[mid] < target){
start = mid + 1;
}
else if (numbers[mid] > target) {
end = mid - 1;
}
}
return false;
}
int main() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
cin >> n;
for (int i=0; i<n; i++){
cin >> numbers[i];
}
sort(numbers.begin(), numbers.begin()+n);
cin >> m;
for (int j=0; j<m; j++){
cin >> target;
if (binary_search(target)){
cout << 1 << "\n";
}
else{
cout << 0 << "\n";
}
}
return 0;
}
๐ ๋ฉ๋ชจ
- ์ด๋ถ ํ์์ ํ๊ธฐ ์ํด์๋ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ๋์ด ์์ด์ผ ํ๋ค.
- numbers ๋ฐฐ์ด์ ํฌ๊ธฐ๋ฅผ 100000์ผ๋ก ์ ์ธํ๊ณ , ์ ์ฒด๋ฅผ ์ ๋ ฌํ๋ฉด, ์ ๋ ฌ๋์ง ์์์ผ ํ ์๋ค๊น์ง ์ ๋ ฌ๋๋ฏ๋ก, ๋ฒ์๋ฅผ ์ง์ ํด์ผ ํ๋ค. ๋ฒ์ ์ง์ ์ sort ๋งค๊ฐ๋ณ์์ ์์ ์ฃผ์, ์์ ์ฃผ์+์ ๋ ฌํ๋ ค๋ ์์์ ๊ฐ์๋ก ์ ๋ฌํ๋ค.
'๐ PS > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 1012๋ฒ: ์ ๊ธฐ๋ ๋ฐฐ์ถ (0) | 2024.07.02 |
---|---|
[Baekjoon] 1003๋ฒ: ํผ๋ณด๋์น ํฉ (0) | 2024.07.02 |
[Baekjoon] 1463๋ฒ: 1๋ก ๋ง๋ค๊ธฐ (0) | 2024.07.01 |
[Baekjoon] 1926๋ฒ: ๊ทธ๋ฆผ (0) | 2024.07.01 |
[Baekjoon] 11047๋ฒ: ๋์ 0 (0) | 2024.07.01 |