-
Notifications
You must be signed in to change notification settings - Fork 1
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
2-YIM2UL2ET #7
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
โ๏ธ ๋ฌธ์ ๋ฅผ ๋ณด๊ณ ๋๋ผ๋ฉด ์ด๋ป๊ฒ ํ์ง ๊ณ ๋ฏผํ ํ ํ์ดํ์ ๋ฐฉ์์ ๋ณด๋ฉด ๊ฐ์ ํ์ด๊ฐ ๋์จ๋ค๋๊ฒ ์ ๊ธฐํฉ๋๋ค
โ๏ธ ์ข์ ํ์ด ๋ง์ด ์ฐธ๊ณ ํ์ต๋๋ค. ํนํ ๊ทธ๋ฆผ์ ์ด์ฉํ์ฌ ๋ฌธ์ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ ์ค๋ช ํด์ฃผ์ ์ ๋ง์ ๋์์ด ๋์์ต๋๋ค. ๐
โ๏ธ ์ ๋ ์ฒ์์ vector๋ฅผ ์ฌ์ฉํ ๋ ์ด๊ฒ ๋ญ์ง...๋ผ๋ ์๊ฐ์ ๋ง์ด ํ์ง๋ง ์ธ ์๋ก vector๊ฐ ๋งค์ฐ ํธํ๋ค๋ ๊ฒ์ ๋๋๋๋ค ๋ง์ด ๋ง์ด ์ฌ์ฉํด๋ณด์๋ฉด ์ข์ ๊ฒ ๊ฐ๋ค์
๐ฅ๏ธ vector ๋ง๊ณ ๋ ๋ค๋ฅธ ์๋ฃ๊ตฌ์กฐ๋ C++์ ๋ง์ด ๊ตฌํ๋์ด์์ผ๋ ์ด๊ฒ์ ๊ฒ ์จ๋ณด์ธ์!!
์ ๋ ์ด๊ฒ๋ ์์ด? ํ๊ณ ์ฐพ์๋ณด๋ฉด ๊ตฌํ๋์ด ์๋๊ฒ ๋ง์์ ๋งค๋ฒ ๋๋๋๋ค๐ฒ
์๊ณ ํ์ จ์ต๋๋ค ๐
#include <iostream> | ||
#include <algorithm> | ||
#include <vector> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using namespace std;
๋ฅผ ๋ฃ์ด์ฃผ์๋ฉด ๋ฐ์์ std::๋ฅผ ์์ฐ์
๋ ๋ฉ๋๋ค!!
๋ ํธํ์ค๊ฑฐ์์~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๋ ์ด์ ์ ๊ทธ๋ ๊ฒ ์ ์ผ๋ฉด์ ํ์ผ๋, std:: ์ฐ๋ ํจ์๋ค๊ณผ ์ ์ฐ๋ ํจ์๋ค์ด ์ข ํท๊น๋ ค์ ์ง๊ธ์ ์ด๋ ๊ฒ ์ ๊ณ ์์ต๋๋ค. ๋์ค์ ํ์คํ ๊ตฌ๋ณํ ์ ์์ ๋ ์ธ ๊ฒ ๊ฐ๋ค์.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๊ทธ ์ ์ ์๊ฐํ์ง ๋ชปํ์ต๋๋ค. ์ ๋ ์กฐ๊ธ ํ๊น๋ฆฌ๊ธด ํ๋ค์
์ด ๋ถ๋ถ์ ํ๋ฒ ์ฐธ๊ณ ํด์ ์ ๋ ๊ตฌ๋ณํ ์ ์์์ง ํ์ธํด๋ด์ผ๊ฒ ๋ค์
int n, result, end_time; | ||
std::cin >> n; | ||
|
||
std::vector <std::vector <int>> vec(n); |
There was a problem hiding this comment.
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๋ฅผ ์ฐ๋ ๊ฒ๋ ์ข์ ๋ฐฉ๋ฒ์ผ ๊ฑฐ ๊ฐ์ต๋๋ค
๐ Pair
๐ฅ๏ธ ์ฌ๊ธฐ์ Pair๋ฅผ ์ฌ์ฉํ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ๊ฒ ๋ค์
vector<pair<int, int>> vec(n)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๋ฐ ๋ฐฉ๋ฒ๋ ์๊ตฐ์! ํ๋ฒ ๊ณต๋ถํด๋ณด๊ฒ ์ต๋๋ค! ๊ฐ์ฌํฉ๋๋ค!
std::vector <std::vector <int>> vec(n); | ||
for (int i = 0; i < n; i++) { | ||
int t, s; | ||
std::cin >> t >> s; | ||
vec[i].push_back(t); | ||
vec[i].push_back(s); | ||
} |
There was a problem hiding this comment.
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];
}
โ๏ธ ๋ ์คํ์ผ ๋ชจ๋ ์ข์ ๋ฐฉ๋ฒ์ด๋ผ๊ณ ์๊ฐํฉ๋๋ค.
ํ์ง๋ง ์กฐ๊ธ ์ฐจ์ด๊ฐ ์๋ ๋งํผ, ์กฐ๊ฑด์ ๋ฐ๋ผ ์๊ฐ ๋ฑ์ด ์ฐจ์ด๊ฐ ๋ ๊ฒ ๊ฐ์ต๋๋ค.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ฝ๋ ์ฝ์ผ์๋ ๋ถ๋ค์ด ์ดํดํ๊ธฐ ์ต๋ํ ์ฝ๊ฒ๋ t์ s๋ฅผ ๋ฐ๋ก ์ ๋ ฅ๋ฐ์์ pushํ๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉํด๋ณด์์ต๋๋ค๋ง, ๋ฏผ์ฑ๋ ์คํ์ผ์ด ๊ฐ์ธ์ ์ผ๋ก ๋ ๊น๋ํ๊ณ ์ข์ ๊ฒ ๊ฐ๋ค์. ๋์ค์ ์ ๋ ์ ๋ฐฉ๋ฒ์ ์ฌ์ฉํด๋ณด๊ฒ ์ต๋๋ค.
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]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
โ๏ธ compare ํจ์๋ฅผ ๋ช
ํํ๊ฒ ์ ์ง์ฃผ์
จ๋ค์.
์ ๋ ๋งค๋ฒ ํ๊น๋ฆฌ๋๋ฐ ์์ผ๋ก ๋ง์ด ์ฐธ๊ณ ํ๊ฒ ์ต๋๋ค ๐
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
N=2
์ธ ๊ฒฝ์ฐ๋ก ์์ํด์ N=n
์ผ ๋๊น์ง ์ผ๋ฐํํ๋ ๊ณผ์ ์ด ๋๋ฌด ์ฝ๊ธฐ ์ข์์ต๋๋ค.
๊ทธ๋ฆฌ๊ณ ์ค๋ฆ์ฐจ์ ํ๋ ๋ค์ํ ๋ฐฉ๋ฒ ์ค ์ ๊ฐ ์๊ฐํ ๋ฐฉ๋ฒ๋ ์์ด์ ์์ฃผ ์ธ์๊น์์ต๋๋ค.
T
๋ฐฐ์ด๊ณผS
๋ฐฐ์ด์ ๋ฐ๋ก ์ ์ธํ์ฌS
๋ฅผ ๋จผ์ ์ ๋ ฌ ํT
๋ฐฐ์ด์S
๋ฅผ ๊ธฐ์ค์ผ๋ก ํ๋ฒ ๋ ์ ๋ ฌํ๊ธฐ
T
๋ฐฐ์ด๊ณผS
๋ฐฐ์ด์ ๋ฐ๋ก ์ ์ธํ๊ณ ,index
๋ฐฐ์ด์ ๋ง๋ค์ด์S
๋ฅผ ๊ธฐ์ค์ผ๋กindex
๋ฐฐ์ด ์ ๋ ฌ
T
๋ฐฐ์ด๊ณผS
๋ฐฐ์ด์ ํ๋์ 2์ฐจ์ ๋ฐฐ์ด๋ก ํฉ์ณ์S
๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๊ธฐ
์ ๋ 2์ฐจ์ ๋ฐฐ์ด ์ธ๋ฑ์ฑ์ด ์๊พธ ํท๊ฐ๋ ค์ 2๋ฒ ๋ฐฉ๋ฒ์ ํํ ๊ฒ ๊ฐ์ต๋๋ค. ํ์ง๋ง 3๋ฒ ๋ฐฉ๋ฒ์ด ํจ์ฌ ์ธ๋ จ๋์ด ๋ณด์ด๊ณ ์ฝ๋๋ ๊น๋ํ๋ค ์๊ฐํฉ๋๋ค. ๋ง์ด ๋ฐฐ์ฐ๊ณ ๊ฐ๋๋ค โ โ
๐ ๋ฌธ์ ๋งํฌ
BOJ 1263 - ์๊ฐ๊ด๋ฆฌ
โ๏ธ ์์๋ ์๊ฐ
60+ฮฑ ๋ถ
โจ ์๋ ์ฝ๋
1. ๋ฌธ์ ์ค๋ช
์ด ๋ฌธ์ ๋ ์ผ์ ํด์ผํ๋ ์๊ฐ ๋ฐฐ์ด
T
์, ๋ง๊ฐ ์๊ฐ ๋ฐฐ์ดS
๋ฅผ ์ ๋ ฅ๋ฐ์์ ์ต๋ํ ์ผ์ ๋ฆ๊ฒ ์์ ํ ์ ์๋ ์๊ฐ์ธresult
๋ฅผ returnํ๋ ๋ฌธ์ ์ ๋๋ค.2. ๋ฌธ์ ๋ถ์
์ฐ์
end_time
์ ์ผ์ด ์ ๋ถ ๋๋๋ ์๊ฐ์ด๋ผ๊ณ ํฉ์๋ค.์ฐ์ ๊ฐ๊ฐ์
Ti
์Si
๊ฐ์ ๋ํ์ฌ ์๊ฐํ์ฌ ๋ด ์๋ค.Si
๊น์ง ๋ง๊ฐ์ธ ์ผ์ดTi
๋งํผ์ ์๊ฐ์ด ๊ฑธ๋ฆฐ๋ค๊ณ ํ์์ ๋ ์ธ์ ์ผ์ ์์ํ๋ฉด ์ข์๊น์?Si - Ti
์ ์์ํ๋ฉด ๊ฐ์ฅ ๋ฆ๊ฒ ์์ ํ ์ ์๊ฒ ์ง์?๊ทธ๋ ๋ค๋ฉด ์๋ฅผ ๋ค์ด 2๊ฐ๋ก ์ผ์ด ๋์ด๋ฌ๋ค๊ณ ํด๋ด ์๋ค. ๊ทธ๋ผ ๋ง๊ฐ ์๊ฐ์ ๊ฐ๊ฐ
Sโ
,Sโ
์ ๋๋ค.Sโ
๋Sโ
๋ณด๋ค ๋ง๊ฐ ์๊ฐ์ด ๋ ๋นจ๋ฆฌ ๋๋๋ค๊ณ ํฉ๋๋ค. ๊ทธ๋ ๋ค๋ฉด ์ฐ๋ฆฌ๋ ๋ณดํต ์ด๋ป๊ฒ ํ๋์? ๋ง๊ฐ ์๊ฐ์ด ๋จผ์ ์ธ 1๋ฒ ์ผ์ ๋จผ์ ๋๋ด๊ณ , 2๋ฒ ์ผ์ ํ ๊ฒ์ ๋๋ค. ์ด ๋ result๊ฐ์ ๊ฒฝ์ฐ์ ์๋ ๋ ๊ฐ์ง ๊ฒฝ์ฐ์ ์๊ฐ ์์ต๋๋ค.2-1.
Tโ
๊ฐSโ
๊ณผSโ
์ฌ์ด์ ์๊ฐ๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ ๋์ด ๋๋ 1๋ฒ์งธ ์ผ์
Sโ - Tโ
์ ์์ํด๋ 2๋ฒ์งธ ์ผ์Sโ
์ด์ ์ ๋๋ผ ์ ์์ผ๋ฏ๋ก 1๋ฒ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋กresult
๋ ์ฌ์ ํSโ - Tโ
์ ๋๋ค.end_time
์ ์ด์ end_time
์ธSโ
์Tโ
๋ฅผ ๋ํSโ + Tโ
์ ๋๋ค. (๊ฐ์ ๋๋Sโ + Tโ
=Sโ
, ์์ ๋๋Sโ + Tโ
<Sโ
)2-2.
Tโ
๊ฐSโ
๊ณผSโ
์ฌ์ด์ ์๊ฐ๋ณด๋ค ํด ๋.์ด ๋๋ 1๋ฒ์งธ ์ผ์
Sโ - Tโ
์ ์์ํ๋๋ผ๋ 2๋ฒ์งธ ์ผ์Sโ
์ ๋๋ผ ์ ์์ผ๋ฏ๋ก ์ด์ฉ ์ ์์ด 1๋ฒ์งธ ์ผ์ ๋ก๊ฒจ ์์ํ ์ ๋ฐ์ ์์ต๋๋ค. ์ด๋result
๊ฐ์ ์ด์ ๊ฐ์์Sโ - Sโ
์Tโ
์ ์ฐจ์ด๋ฅผ ๋นผ์ฃผ์ด์ผ ํฉ๋๋ค.end_time
์Sโ
์ ๋๋ค.๋ค์์ ํ๋ก ์ ๋ฆฌํ๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
Sโ - Tโ
Sโ
Sโ - Tโ
Sโ + Tโ
result
๊ฐ) -Tโ
-Sโ
+Sโ
Sโ
Si
>=end_time
+Ti
์ด๋ผ๋ฉด 2-1 ๋๋กresult
๋ ๊ทธ๋๋ก ๋๊ณend_time
์Ti
๋ฅผ ๋ํ๋ฉด ๋๊ณ ,Si
<end_time
+Ti
์ด๋ผ๋ฉด 2-2 ๋๋กresult
๋ฅผ ๋ก๊ธฐ๋ฉด ๋๊ณ ,end_time
์Si
๋ก ์ค์ ํ๋ฉด ๋๊ฒ ์ง์. (์ฐธ ์ฝ์ฃ ?)๋ฌธ์ ๋ ์ ๋ ฅ์ ๋ฌ๋ ค์์ต๋๋ค. 2๋ฒ์ ๋ง๊ฐ ์๊ฐ์ธ
Si
๊ฐSi-โ
๋ณด๋ค ๋ฆ๋๋ค๋ ๊ฒ์ด ์ ์ ์์ต๋๋ค. ๊ทธ๋ ๋ค๋ฉด ์ด๋ป๊ฒ ํด๊ฒฐํ๋ฉด ์ข์๊น์? ๋ญ ์์ต๋๋ค. ๊ทธ๋ฅS
๋ฅผ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์๋๋กS
์T
๋ฅผ ์ ๋ ฌํ๋ฉด ๋ฉ๋๋ค. (์ฐธ ์ฝ์ฃ ?2)์ ๋ง ๋๋ฌ์ต๋๋ค. ์ด๊ฑธ ์๋์ฝ๋๋ก ์ง๋ด ์๋ค.
3. ์๋์ฝ๋
4. ์ต์ข ์ฝ๋
์ ๋ ฌ์ ํ๋ ๋ฐฉ๋ฒ์ด ์ ์๊ฐ์ผ๋ก ๋๋ต 3๊ฐ์ง ์ ๋๊ฐ ์์์ต๋๋ค.
T
๋ฐฐ์ด๊ณผS
๋ฐฐ์ด์ ๋ฐ๋ก ์ ์ธํ์ฌS
๋ฅผ ๋จผ์ ์ ๋ ฌ ํT
๋ฐฐ์ด์S
๋ฅผ ๊ธฐ์ค์ผ๋ก ํ๋ฒ ๋ ์ ๋ ฌํ๊ธฐT
๋ฐฐ์ด๊ณผS
๋ฐฐ์ด์ ๋ฐ๋ก ์ ์ธํ๊ณ ,index
๋ฐฐ์ด์ ๋ง๋ค์ด์S
๋ฅผ ๊ธฐ์ค์ผ๋กindex
๋ฐฐ์ด ์ ๋ ฌT
๋ฐฐ์ด๊ณผS
๋ฐฐ์ด์ ํ๋์ 2์ฐจ์ ๋ฐฐ์ด๋ก ํฉ์ณ์S
๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๊ธฐ์ ๋ ๋ง์ง๋ง ๋ฐฉ๋ฒ์ ์ ํํ์ต๋๋ค.
vec[i][0]
=T[i]
์ด๊ณ ,vec[i][1]
=S[i]
๋ผ๊ณ ๋ณด์๋ฉด ๋ฉ๋๋ค.๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
ํ์ด ๋ฐฉ๋ฒ์ ์๊ฐํด๋ด๋ ๋ฐ์๋ 15๋ถ ์ ๋ ๊ฑธ๋ ธ๋๋ฐ ๊ตฌํ์ด ์ฝ์ง๊ฐ ์์์ต๋๋ค.
ํนํ vector๋ฅผ ์ฌ์ฉํ์ฌ 2์ฐจ์ ๋ฐฐ์ด์ ์ ๋ ฌ์ ์๋ํ๋๋ฐ ์ ๋ฅผ ๋จน์์ต๋๋ค. ์์ง ํด๋์ค ์กฐ์ฐจ ์ ๋๋ก ํ๋ณด์ง ์์์ ๋ฒฝ์ด ๋๊ฒ ๋๊ปด์ง๋ค์.. ์ง๊ธ ํ๊ณ ์๋ ๊ธฐ์ด์ ์ธ Cpp ๊ณต๋ถ๊ฐ ๋๋๋ฉด ๊ฐ์ฅ ๋จผ์ vector ํด๋์ค๋, STL ๊ณต๋ถ๋ฅผ ํด์ผ ํ ๊ฒ ๊ฐ์ต๋๋ค.
์ต๊ทผ์ ๋๋ฌด ๊ทธ๋ฆฌ๋๋ง ํผ ๊ฒ ๊ฐ์์ ๋ค์ ์ฃผ์ฐจ๋ ์๋ฃ๊ตฌ์กฐ ๋ฌธ์ ๋ ํ๋ฒ ์ฐ์ด ๋จน์ด๋ณด๊ฒ ์ต๋๋ค.