Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5-oesnuj #20

Merged
merged 2 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions oesnuj/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
| 1μ°¨μ‹œ | 2024.03.26 | μŠ€νƒ | [ν›„μœ„ν‘œκΈ°μ‹2](https://www.acmicpc.net/problem/1935) | [#4](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/4) |
| 2μ°¨μ‹œ | 2024.03.29 | μ—°κ²°λ¦¬μŠ€νŠΈ | [에디터](https://www.acmicpc.net/problem/1406) | [#8](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/8) |
| 3μ°¨μ‹œ | 2024.04.02 | 덱 | [μΉ΄λ“œ 놓기](https://www.acmicpc.net/problem/18115) | [#11](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/11) |
| 4μ°¨μ‹œ | 2024.04.06 | μŠ€νƒ | [μ˜₯상 정원 κΎΈλ―ΈκΈ°](https://www.acmicpc.net/problem/6198) | [#14](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/14) |
| 5μ°¨μ‹œ | 2024.04.13 | 이뢄탐색 | [λ“£λ³΄μž‘](https://www.acmicpc.net/problem/1764) | [#20](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/20) |
---
44 changes: 44 additions & 0 deletions oesnuj/μŠ€νƒ/6198.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <iostream>
#include <vector>

using namespace std;

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int n;
cin >> n; //λΉŒλ”© 개수 μž…λ ₯

vector <int> v(n); //λΉŒλ”© 높이λ₯Ό 받을 벑터 μ„ μ–Έ
vector <int> s; //λΉŒλ”© 높이듀을 μ μ ˆν•˜κ²Œ 넣을 μŠ€νƒ μ„ μ–Έ
for (auto& i : v)
cin >> i; //각 λΉŒλ”© 높이 μž…λ ₯

long long int count = 0; //countκ°€ intν˜•μ„ λ„˜μ–΄μ„œκΈ°μ— long long int type으둜 λ‘ 
for (const auto& height : v) // λͺ¨λ“  λΉŒλ”© 높이λ₯Ό ν•˜λ‚˜μ”© μˆœνšŒν•œλ‹€.
{
while (true)
{
if (s.empty()) //μŠ€νƒμ΄ λΉ„μ—ˆλ‹€λ©΄ ν˜„μž¬ λΉŒλ”© 높이 μŠ€νƒμ— push
{
s.push_back(height);
break;
}
if (height < s.back()) //ν˜„μž¬ λΉŒλ”© 높이가 μŠ€νƒμ˜ top보닀 높은 λΉŒλ”©μ΄λΌλ©΄(λ³Ό 수 μžˆλŠ” λΉŒλ”©μ΄λΌλ©΄)
{
count += s.size();
//μŠ€νƒμ—λŠ” ν˜„μž¬ λΉŒλ”©λ³΄λ‹€ 높은 λΉŒλ”© 밖에 μ—†μœΌλ‹ˆ μŠ€νƒμ˜ 크기 = ν˜„μž¬ λΉŒλ”©μ„ λ³Ό 수 μžˆλŠ” λΉŒλ”© 수
s.push_back(height);
break;
}
s.pop_back(); //ν˜„μž¬ λΉŒλ”©μ΄ μŠ€νƒμ˜ top보닀 λ†’λ‹€λ©΄(μ–΄μ§œν”Ό λ‚˜λ₯Ό λ³Ό 수 μ—†λŠ” λΉŒλ”©μ΄λ‹ˆ) μ „λΆ€ pop
//μŠ€νƒμ΄ λΉ„κ±°λ‚˜ ν˜„μž¬ λΉŒλ”©λ³΄λ‹€ 높은 top이 λ‚˜μ˜¬λ•ŒκΉŒμ§€ popν•œλ‹€.
// μ–Έμ œκΉŒμ§€ pop ν• κΉŒ?
//case 1. μŠ€νƒμ΄ λΉ„λ©΄ λ‚˜λ₯Ό λ³Ό 수 μžˆλŠ” λΉŒλ”©μ€ μ—†λ‹€λŠ” λœ»μ΄λ‹ˆ μŠ€νƒμ— push
//case 2. λ‚˜λ³΄λ‹€ 높은 λΉŒλ”©μ΄ λ‚˜μ˜¨λ‹€λ©΄ κ·Έλ•Œ μŠ€νƒμ˜ 크기 = λ‚˜λ₯Ό λ³Ό 수 μžˆλŠ” λΉŒλ”© 수 λ₯Ό count에 μΆ”κ°€
}
}
cout << count;
return 0;
}
53 changes: 53 additions & 0 deletions oesnuj/이뢄탐색/1764.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;


bool binarySearch(vector <string> &v, string str) //이뢄탐색
{
int left = 0;
int right = v.size() - 1;
while (left <= right)
{
int middle = (left + right) / 2;
if (str > v[middle])
left = middle + 1;
else if (str < v[middle])
right = middle - 1;
else
return true;

}
return false;
}


int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL);
int n, m;
cin >> n >> m;
vector <string> v(n); //듣도 λͺ»ν•œ μ‚¬λžŒ μž…λ ₯
for (auto& i : v){
cin >> i;
}
sort(v.begin(), v.end()); //이뢄 탐색을 μœ„ν•΄ μ •λ ¬
vector <string> result;
for (int i = 0; i < m; i++)
{
string word;
cin >> word; //보도 λͺ»ν•œ μ‚¬λžŒ μž…λ ₯
if (binarySearch(v, word)){ //보도 λͺ»ν•œ μ‚¬λžŒμ΄ 듣도 보닀 λͺ»ν•œ μ‚¬λžŒμ—λ„ ν¬ν•¨λ˜λ©΄

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C++ STLμ—μ„œ 이진 검색을 μ§€μ›ν•œλ‹€λŠ” 사싀 μ•Œκ³  κ³„μ‹ κ°€μš”? algorithm 헀더에 λ‹€μŒκ³Ό 같은 μ‹œκ·Έλ‹ˆμ²˜λ₯Ό κ°–λŠ” binary_searchλž€ ν•¨μˆ˜κ°€ μ‘΄μž¬ν•œλ‹΅λ‹ˆλ‹€ :)

template< class ForwardIt, class T >
bool binary_search( ForwardIt first, ForwardIt last, const T& value );

κ·Έλž˜μ„œ λ³„λ„μ˜ ν•¨μˆ˜λ₯Ό μ •μ˜ν•  ν•„μš” 없이 μ•„λž˜μ™€ 같이 λ°”λ‘œ wordλ₯Ό μ°Ύμ•„λ‚Ό 수 μžˆλ‹΅λ‹ˆλ‹€

if(binary_search(v.begin(), v.end(), word)) {
  result.emplace_back(word);
}

result.push_back(word);
}
}
sort(result.begin(), result.end()); //λ§ˆμ§€λ§‰ μ‚¬μ „μˆœ 정렬을 μœ„ν•΄ μ •λ ¬

cout << result.size() << "\n";
for (const auto& word : result) {
cout << word << "\n";
}
return 0;
}