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

2-YIM2UL2ET #7

Merged
merged 2 commits into from
Feb 18, 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
3 changes: 2 additions & 1 deletion YIM2UL2ET/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

| ์ฐจ์‹œ | ๋‚ ์งœ | ๋ฌธ์ œ์œ ํ˜• | ๋งํฌ | ํ’€์ด |
|:----:|:---------:|:----:|:-----:|:----:|
| 1์ฐจ์‹œ | 2023.10.27 | ๊ทธ๋ฆฌ๋”” | [BOJ 18310](https://www.acmicpc.net/problem/18310) | [BOJ 18310 ํ’€์ด](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/3) |
| 1์ฐจ์‹œ | 2024.02.12 | ๊ทธ๋ฆฌ๋”” | [BOJ 18310](https://www.acmicpc.net/problem/18310) | [BOJ 18310 ํ’€์ด](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/3) |
| 2์ฐจ์‹œ | 2024.02.15 | ๊ทธ๋ฆฌ๋”” | [BOJ 1263](https://www.acmicpc.net/problem/1263) | [BOJ 1449 ํ’€์ด](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/7) |
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <iostream>
#include <algorithm>
#include <vector>

Copy link
Collaborator

Choose a reason for hiding this comment

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

using namespace std;

๋ฅผ ๋„ฃ์–ด์ฃผ์‹œ๋ฉด ๋ฐ‘์—์„œ std::๋ฅผ ์•ˆ์“ฐ์…”๋„ ๋ฉ๋‹ˆ๋‹ค!!
๋” ํŽธํ•˜์‹ค๊ฑฐ์—์š”~

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

์ €๋„ ์ด์ „์—” ๊ทธ๋ ‡๊ฒŒ ์ ์œผ๋ฉด์„œ ํ–ˆ์œผ๋‚˜, std:: ์“ฐ๋Š” ํ•จ์ˆ˜๋“ค๊ณผ ์•ˆ ์“ฐ๋Š” ํ•จ์ˆ˜๋“ค์ด ์ข€ ํ—ท๊น”๋ ค์„œ ์ง€๊ธˆ์€ ์ด๋ ‡๊ฒŒ ์ ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋‚˜์ค‘์— ํ™•์‹คํžˆ ๊ตฌ๋ณ„ํ•  ์ˆ˜ ์žˆ์„ ๋•Œ ์“ธ ๊ฒƒ ๊ฐ™๋„ค์š”.

Copy link
Collaborator

Choose a reason for hiding this comment

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

์•„ ๊ทธ ์ ์€ ์ƒ๊ฐํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ์ €๋„ ์กฐ๊ธˆ ํ–‡๊น”๋ฆฌ๊ธด ํ•˜๋„ค์š”
์ด ๋ถ€๋ถ„์€ ํ•œ๋ฒˆ ์ฐธ๊ณ ํ•ด์„œ ์ €๋„ ๊ตฌ๋ณ„ํ•  ์ˆ˜ ์žˆ์„์ง€ ํ™•์ธํ•ด๋ด์•ผ๊ฒ ๋„ค์š”

bool compare(std::vector <int> &vec1, std::vector <int> &vec2);

int main(void)
{
int n, result, end_time;
std::cin >> n;

std::vector <std::vector <int>> vec(n);
Copy link
Collaborator

Choose a reason for hiding this comment

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

โœ”๏ธ vector<vector> vec(n)์„ ์‚ฌ์šฉํ•˜์‹  ๊ฒƒ๋„ ์ข‹์Šต๋‹ˆ๋‹ค!!
โ–ถ๏ธ2์ฐจ์› ๋ฒกํ„ฐ๋ฅผ ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ๋„ ๋ฐฉ๋ฒ•์ด๋‚˜, pair๋ฅผ ์จ๋ณด์‹œ๋Š” ๊ฒƒ์€ ์–ด๋–จ๊นŒ์š”?
2๊ฐœ์˜ ๊ฐ’์ด ํ•œ์Œ์œผ๋กœ ํ•ด์„œ ๋“ค์–ด๊ฐ€๋‹ˆ Pair๋ฅผ ์“ฐ๋Š” ๊ฒƒ๋„ ์ข‹์€ ๋ฐฉ๋ฒ•์ผ ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค

๐Ÿ“ Pair

๐Ÿ–ฅ๏ธ ์—ฌ๊ธฐ์„œ Pair๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๊ฒ ๋„ค์š”

vector<pair<int, int>> vec(n)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

์ €๋Ÿฐ ๋ฐฉ๋ฒ•๋„ ์žˆ๊ตฐ์š”! ํ•œ๋ฒˆ ๊ณต๋ถ€ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค! ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

for (int i = 0; i < n; i++) {
int t, s;
std::cin >> t >> s;
vec[i].push_back(t);
vec[i].push_back(s);
}
Comment on lines +12 to +18
Copy link
Collaborator

Choose a reason for hiding this comment

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

โœ”๏ธ ์ €์™€ ์•ฝ๊ฐ„์˜ ์Šคํƒ€์ผ ์ฐจ์ด๊ฐ€ ๋Š๊ปด์ง€๋Š” ๋ถ€๋ถ„์ด์—ˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ–ฅ๏ธ ์ œ ์Šคํƒ€์ผ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž…๋ ฅ์„ ๋ฐ›์Šต๋‹ˆ๋‹ค.

vector<vector<int>> vec(n,vector<int> (2,0))
for(int  i = 0; i < n; i++){
    cin >> vec[i][0] >> vec[i][1];
}

โœ”๏ธ ๋‘ ์Šคํƒ€์ผ ๋ชจ๋‘ ์ข‹์€ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ ์กฐ๊ธˆ ์ฐจ์ด๊ฐ€ ์žˆ๋Š” ๋งŒํผ, ์กฐ๊ฑด์— ๋”ฐ๋ผ ์‹œ๊ฐ„ ๋“ฑ์ด ์ฐจ์ด๊ฐ€ ๋‚  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

โ€ผ๏ธ ์ €๋„ ํ™•๋‹ต์€ ๋ชป ๋“œ๋ฆฌ๊ฒ ์œผ๋‚˜ ๊ทธ ์ฐจ์ด์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•ด๋ณด๊ณ  ํ•จ๊ป˜ ์ด์•ผ๊ธฐํ•ด๋ณด๋Š” ๊ฒƒ๋„ ์ข‹์€ ๊ธฐํšŒ๊ฐ€ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค๐Ÿ˜

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

์ฝ”๋“œ ์ฝ์œผ์‹œ๋Š” ๋ถ„๋“ค์ด ์ดํ•ดํ•˜๊ธฐ ์ตœ๋Œ€ํ•œ ์‰ฝ๊ฒŒ๋” t์™€ s๋ฅผ ๋”ฐ๋กœ ์ž…๋ ฅ๋ฐ›์•„์„œ pushํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค๋งŒ, ๋ฏผ์„ฑ๋‹˜ ์Šคํƒ€์ผ์ด ๊ฐœ์ธ์ ์œผ๋ก  ๋” ๊น”๋”ํ•˜๊ณ  ์ข‹์€ ๊ฒƒ ๊ฐ™๋„ค์š”. ๋‚˜์ค‘์—” ์ €๋„ ์ € ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


std::sort(vec.begin(), vec.end(), compare);

result = vec[0][1] - vec[0][0], end_time = vec[0][1];
for (int i = 1; i < n; i++) {
if (end_time + vec[i][0] > vec[i][1]) {
result -= end_time + vec[i][0] - vec[i][1];
end_time = vec[i][1];
}
else end_time += vec[i][0];
}

if (result < 0) std::cout << "-1";
else std::cout << result;

return 0;
}

bool compare(std::vector <int> &vec1, std::vector <int> &vec2)
{
if (vec1[1] == vec2[1]) return vec1[0] < vec2[0];
else return vec1[1] < vec2[1];
}
Comment on lines +37 to +41
Copy link
Collaborator

Choose a reason for hiding this comment

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

โœ”๏ธ compare ํ•จ์ˆ˜๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ์ž˜ ์งœ์ฃผ์…จ๋„ค์š”.
์ €๋„ ๋งค๋ฒˆ ํ–‡๊น”๋ฆฌ๋Š”๋ฐ ์•ž์œผ๋กœ ๋งŽ์ด ์ฐธ๊ณ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค ๐Ÿ˜