Skip to content

Commit

Permalink
Merge pull request #14 from AlgoLeadMe/4-oesnuj
Browse files Browse the repository at this point in the history
4-oesnuj
  • Loading branch information
oesnuj authored May 8, 2024
2 parents 7ccdf79 + 8334bf0 commit f899cda
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions oesnuj/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
| 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) |
---
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;
}

0 comments on commit f899cda

Please sign in to comment.