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

12-yuyu0830 #49

Merged
merged 3 commits into from
Jul 1, 2024
Merged

12-yuyu0830 #49

merged 3 commits into from
Jul 1, 2024

Conversation

yuyu0830
Copy link
Collaborator

@yuyu0830 yuyu0830 commented May 13, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

๋‘ ๋ฐฐ์—ด์˜ ํ•ฉ(2143)

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

30๋ถ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

โ“ ๋ฌธ์ œ

์ ˆ๋Œ€๊ฐ’์ด 1,000,000 ์ดํ•˜์ธ ์ˆ˜๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด A์™€ ๋ฐฐ์—ด B๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ๋ฐฐ์—ด์˜ ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋Š” 1,000๊ฐœ๋ฅผ ๋„˜์ง€ ์•Š๋Š”๋‹ค. ๊ฐ ๋ฐฐ์—ด์— ๋Œ€ํ•ด i๋ฒˆ์งธ๋ถ€ํ„ฐ j๋ฒˆ์งธ๊นŒ์ง€์˜ ํ•ฉ ์„ ๋ถ€ ๋ฐฐ์—ด ์ด๋ผ๊ณ  ํ•  ๋•Œ, ๋ฐฐ์—ด A์˜ ๋ถ€ ๋ฐฐ์—ด๊ณผ ๋ฐฐ์—ด B์˜ ๋ถ€ ๋ฐฐ์—ด์˜ ํ•ฉ์ด T๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜์ž

โ— ํ’€์ด

๋ฌธ์ œ ์ดํ•ด๋ฅผ ํ•˜๋Š”๋ฐ ์‹œ๊ฐ„์ด ์ข€ ๊ฑธ๋ ธ๋‹ค... ์ด๊ฒŒ ๋ฌด์Šจ ์†Œ๋ฆฌ์•ผ
๋ฌธ์ œ๋ฅผ ํž˜๋“ค๊ฒŒ ์ดํ•ดํ•˜๊ณ  ๋‚˜๋‹ˆ ๋ฌธ์ œ๋ฅผ ํฌ๊ฒŒ 2๊ฐœ๋กœ ๋ถ„๋ฅ˜ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

  1. ๋ฐฐ์—ด์˜ ๋ถ€ ๋ฐฐ์—ด์˜ ํ•ฉ ๊ตฌํ•˜๊ธฐ
  2. ๋ฐฐ์—ด A์™€ B์˜ ์š”์†Œ์˜ ํ•ฉ์ด T๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ

๊ฐ ๋ฌธ์ œ๋ณ„๋กœ ํ•ด๊ฒฐํ•œ ๋ฐฉ๋ฒ•

1๏ธโƒฃ ๋ฐฐ์—ด์˜ ๋ถ€ ๋ฐฐ์—ด์˜ ํ•ฉ ๊ตฌํ•˜๊ธฐ

์‚ฌ์‹ค ๋ฐฐ์—ด์˜ i๋ฒˆ๋ถ€ํ„ฐ j๋ฒˆ๊นŒ์ง€์˜ ํ•ฉ ๊ตฌํ•˜๊ธฐ๋Š” ๋ช…๋ฐฑํžˆ ๋ˆ„์ ํ•ฉ ๋ฌธ์ œ๋‹ค. ๊ทธ๋ƒฅ ๋ˆ„์ ํ•ฉ์œผ๋กœ ํ•ด๊ฒฐํ–ˆ๋‹ค.

while (t--) {
    int a; cin >> a;
    sum += a;
    v[i].push_back(sum);
}

2๏ธโƒฃ ๋ฐฐ์—ด A์™€ B์˜ ์š”์†Œ์˜ ํ•ฉ์ด T๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ

๋ฐฐ์—ด A์˜ ๋ถ€ ๋ฐฐ์—ด๊ณผ ๋ฐฐ์—ด B์˜ ๋ถ€ ๋ฐฐ์—ด์˜ ํ•ฉ์ด T ๊ฐ€ ๋˜์–ด์•ผํ•˜๋‹ˆ ๊ฐ ๋ฐฐ์—ด์˜ ๋ถ€ ๋ฐฐ์—ด์˜ ํ•ฉ์˜ ์ง‘ํ•ฉ ์„ ๊ตฌํ•ด ๊ฐ ์ง‘ํ•ฉ์˜ ์š”์†Œ๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ๋น„๊ตํ•˜๋ฉด ๋˜๊ฒ ๋‹ค ์‹ถ์—ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋‹จ์ˆœ ๋น„๊ตํ•˜์ž๋‹ˆ ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ ๋„ˆ๋ฌด ์ปค์กŒ๋‹ค.
๋ฐฐ์—ด A์™€ B์˜ ์š”์†Œ๊ฐ€ ๋‘˜ ๋‹ค 1000๊ฐœ๋ผ๊ณ  ์น˜๋ฉด ๋ฐฐ์—ด์˜ ๋ถ€ ๋ฐฐ์—ด์˜ ํ•ฉ์˜ ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋Š” ๋ฐฐ์—ด ๋ณ„๋กœ 500,500 ๊ฐ€ ๋˜๊ณ , 500,500๊ฐœ๋ผ๋ฆฌ ๋น„๊ตํ•˜๋ฉด 500,500 * 500,500 = 250,500,250,000 ๋ผ๋Š” ์ˆซ์ž๊ฐ€ ๋‚˜์˜จ๋‹ค.


sAi๋ฅผ ๋ฐฐ์—ด์˜ ๋ถ€ ๋ฐฐ์—ด์˜ ํ•ฉ์˜ ์ง‘ํ•ฉ์˜ i๋ฒˆ์งธ ์š”์†Œ, sBj๋„ ๊ฐ™๋‹ค๊ณ  ํ•  ๋•Œ T = sAi + sBj ๋ผ๊ณ  ๋ณผ ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ ๋ฐ˜๋Œ€๋กœ T - sBj = sAi ๋ผ๊ณ ๋„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๊ฐ ์š”์†Œ์˜ ๋ฒ”์œ„๊ฐ€ -10์–ต ~ 10์–ต์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ์—ด๋กœ ์ €์žฅํ•  ์ˆ˜๋Š” ์—†๊ณ , ๋งŽ์€ ์š”์†Œ๋ฅผ ์ €์žฅํ•˜๋ฉด์„œ ์ตœ๋Œ€ํ•œ ์ž‘์€ ์‹œ๊ฐ„์— ์š”์†Œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” unordered_map์„ ์‚ฌ์šฉํ•ด ์ €์žฅํ–ˆ๋‹ค.
๊ฐ sBj ๋ณ„๋กœ sA์˜ ์š”์†Œ ์ค‘์— T - sBj๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•ด ์žˆ์œผ๋ฉด ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค.
์ด ๋•Œ, ๋ฐฐ์—ด์˜ ๋ถ€ ๋ฐฐ์—ด์˜ ํ•ฉ์˜ ์ง‘ํ•ฉ ์—์„œ ๊ฐ’์ด ๊ฒน์น˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— unordered_map ์˜ value ๊ฐ’์„ ๋ถ€ ๋ฐฐ์—ด์˜ ํ•ฉ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋กœ ๊ณ„์‚ฐํ•ด ๋”ํ•  ๋•Œ ๊ณฑํ•ด์คฌ๋‹ค.

for (auto p : m[1]) 
        if (m[0].find(n - p.first) != m[0].end()) ans += m[0][n - p.first] * p.second;

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

ํ”„๊ฒฝ๋Œ€์—์„œ ํฐ ์•„ํ””์„ ๊ฒช์€ c++ map์˜ ์‚ฌ์šฉ๋ฒ•์„ ๋“œ๋””์–ด ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค..

@9kyo-hwang
Copy link

image

ํ–‰๋‹˜ readme๋ž‘ ํŒŒ์ผ ๋‹ค์‹œ ์˜ฌ๋ ค์ฃผ์‹ญ์‡ผ

@InSange
Copy link
Collaborator

InSange commented May 14, 2024

image

ํ–‰๋‹˜ readme๋ž‘ ํŒŒ์ผ ๋‹ค์‹œ ์˜ฌ๋ ค์ฃผ์‹ญ์‡ผ

ใ…‹ใ…‹ใ…‹ ์ค‘๊ฐ„์— ์–ด๋””๊ฐ”๋ˆ„

@InSange
Copy link
Collaborator

InSange commented May 14, 2024

image
ํ–‰๋‹˜ readme๋ž‘ ํŒŒ์ผ ๋‹ค์‹œ ์˜ฌ๋ ค์ฃผ์‹ญ์‡ผ

ใ…‹ใ…‹ใ…‹ ์ค‘๊ฐ„์— ์–ด๋””๊ฐ”๋ˆ„

image
์ฝ”๋“œ ์–ธ์ œ์ค˜!!!

@InSange
Copy link
Collaborator

InSange commented May 21, 2024

image
ํ•ด๋‹น ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ ๊ถ๊ธˆํ•œ๊ฒŒ ์žˆ๋Š”๋ฐ ๊ฐ๊ฐ์˜ ๋ฐฐ์—ด์˜ ์›์†Œ๊ฐ€ 1000๊ฐœ์”ฉ ์กด์žฌํ•˜๊ณ  T๊ฐ€ ์ฃผ์–ด์กŒ์„๋•Œ ๋ชจ๋“  ๋ฐฐ์—ด์˜ ์›์†Œ๋“ค์„ ๋”ํ–ˆ์„ ๋•Œ 1๊ฐ€์ง€๊ฐ€ ์กด์žฌํ•˜๊ณ  ์ค‘๊ฐ„๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ (1000 * 1)(500500)(1*1000)๊ฐ€ ๋˜๋Š”๊ฒŒ ์•„๋‹Œ๊ฐ€์š”? ์ค‘๋ณต์ด ํ—ˆ์šฉ๋˜๊ณ  ๊ฐ’์ด ์ •ํ•ด์ ธ์žˆ์„ ๋•Œ
๋ฐฐ์—ด ๋ณ„๋กœ 500,500 ๊ฐ€ ๋˜๊ณ , 500,500๊ฐœ๋ผ๋ฆฌ ๋น„๊ตํ•˜๋ฉด 500,500 * 500,500 = 250,500,250,000
์™œ ์ด๋ ‡๊ฒŒ ์‹์ด ๋‚˜์˜ค๋Š”์ง€ ์ดํ•ด๊ฐ€ ์ž˜ ์•ˆ๋ฉ๋‹ˆ๋‹ค..

Copy link
Collaborator

@InSange InSange left a comment

Choose a reason for hiding this comment

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

ํ’€์ด๋ฅผ ๋ณด๋ฉด์„œ ์™œ ๋ˆ„์ ํ•ฉ์ด ๋“ค์–ด๊ฐ”์„๊นŒ์— ์˜๋ฌธ์ด ๋“ค์—ˆ์—ˆ์Šต๋‹ˆ๋‹ค.
๋‘ ๋ฒˆ์งธ ์ด์ค‘ ํฌ๋ฌธ๋งŒ ๋ดค์„ ๋•Œ ๊ทธ๋ƒฅ x๋ณด๋‹ค ํฐ ๊ฐ’์—์„œ x๋ฅผ ๋นผ์ฃผ๋ฉด ๊ทธ๋ƒฅ y๊ฐ€ ์•„๋‹Œ๊ฐ€?

๊ทผ๋ฐ ์ž์„ธํžˆ๋ณด๋‹ˆ x๋ณด๋‹ค ํฐ ์ธ๋ฑ์Šค์˜ ๊ฐ’๋“ค์—์„œ x๋งŒ ์ œ๊ฑฐ๋ฅผ ํ•ด์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— x+1 ~ end ๋ฒˆ์งธ๊นŒ์ง€ ๋’ค์˜ ํ•ฉ๋“ค์˜ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ ์‹์ด๋”๊ตฐ์š”...

์–ด๋–ป๊ฒŒ ์ด๋Ÿฐ ํ’€์ด๋ฅผ ๋– ์˜ฌ๋ฆด ์ˆ˜ ์žˆ๋Š”์ง€ ์ฐธ์œผ๋กœ ์‹ ๊ธฐํ•˜๋„ค์š”

int tmp = v[i][y] - v[i][x];

// if exist
if (m[i].find(tmp)!= m[i].end())
Copy link
Collaborator

Choose a reason for hiding this comment

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

์ด ๋ฐฉ์‹์€ ๋ฌธ์ œ๋ฅผ ์œ ๋ฐœํ• ์ˆ˜๋„ ์žˆ๊ธด ํ•œ๋ฐ ์ €๋Š” ๊ฐ„ํ˜น ์ด๋ ‡๊ฒŒ ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•ด์„œ ๋ง์”€๋“œ๋ฆฝ๋‹ˆ๋‹ค.

C๋ฅผ ๋น„๋กฏํ•œ ํ™•์žฅ ์–ธ์–ด๋“ค์€ default ์ƒ์„ฑ์ž๋“ค์„ ๋”ฐ๋กœ ์„ ์–ธํ•ด์ฃผ์ง€ ์•Š์œผ๋ฉด ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์•Œ์•„์„œ ์ดˆ๊ธฐํ™”๋ฅผ ํ•ด์ค๋‹ˆ๋‹ค. ์ด๋Š” ์ƒ์„ฑ์ž ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์„ ์–ธํ•˜๊ณ  ๊ฐ’์„ ํ• ๋‹นํ•ด์ฃผ์ง€ ์•Š์œผ๋ฉด ๋ฐฐ์—ด์—์„œ๋„ 0์œผ๋กœ ์ž๋™์œผ๋กœ ์ฑ„์›Œ์ง€๋Š” ๊ฒƒ ์ฒ˜๋Ÿผ.

map์—์„œ๋„ ๊ทธ๋ ‡์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ•ด๋‹น key๊ฐ’์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์–˜๋„ค๋“ค์€ if๋ฌธ์—์„œ ์—†๋Š” key์˜ ๊ฐ’์— ๋Œ€ํ•ด value๋Š” 0์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
์ฆ‰ ์กด์žฌํ•˜์ง€ ์•Š๋Š” key๋Š” ์กฐ๊ฑด๋ฌธ์—์„œ๋Š” 0์œผ๋กœ bool ํ˜•์ธ false๋กœ ์ธ์‹ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. {(false == 0)์ด๊ธฐ ๋•Œ๋ฌธ์—}
๊ทธ๋ž˜์„œ ์ €๋Š” ๊ทธ๋ƒฅ m[i][key]++; ํ˜•์‹์œผ๋กœ ์‚ฌ์šฉํ•ด์„œ ์ข‹์€ ์ฝ”๋“œ๋Š” ์•„๋‹ˆ์ง€๋งŒ ๋‹จ์ˆœํ•˜๊ฒŒ ํ‘œํ˜„์ด ๊ฐ€๋Šฅํ•ด์„œ ์”๋‹ˆ๋‹ค.

C์—์„œ find๋Š” end๋ฅผ ๋งž์ดํ•  ๋•Œ ๊นŒ์ง€ ๋ชจ๋“  ์š”์†Œ๋“ค์„ ํƒ์ƒ‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— O(N)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๋งŒ์•ฝ 2์ค‘ 3์ค‘ for๋ฌธ์— find ์กฐ๊ฑด๋ฌธ์ด ๋ถ™๋Š”๋‹ค๋ฉด ์ง€์ˆ˜์Šน์˜ ๋ณต์žก๋„๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ์š”?

Choose a reason for hiding this comment

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

unordered_map์€ Hash ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— find ํ•จ์ˆ˜๋Š” $O(1)$ ์‹œ๊ฐ„์— ๊ฑฐ์˜ ๊ทผ์ ‘ํ•ฉ๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  [] operator๋กœ ์ ‘๊ทผํ•˜๋Š” ๊ฒŒ ์˜ˆ๊ธฐ์น˜ ๋ชปํ•œ ๋™์ž‘์„ ์œ ๋ฐœํ•  ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์œ ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
๋‹จ์ˆœํžˆ find()๋‚˜ count()๋ฅผ ํ†ตํ•ด ์›์†Œ๊ฐ€ ์กด์žฌํ•˜๋Š” ์ง€ ๊ฒ€์‚ฌํ•˜๋Š” ๊ฑฐ๋ž‘์€ ๋‹ค๋ฅด๊ฒŒ, [] operator๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด default value์— ๋Œ€ํ•œ ๋™์ž‘์ด ์ •์˜๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

Copy link
Collaborator

@seongwon030 seongwon030 left a comment

Choose a reason for hiding this comment

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

๋ˆ„์ ํ•ฉ์„ ์ €์žฅํ•ด๋†“๊ณ  ํ•ฉ์ด ๊ฐ™์€ ๊ฒฝ์šฐ ์–ด๋–ป๊ฒŒ ๊ตฌ๋ถ„ํ•˜๋‚˜ ์ƒ๊ฐํ–ˆ๋Š”๋ฐ c++์—๋Š” unordered_map ์œผ๋กœ ํ•ด์‹œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ตฐ์š”. ๊ทผ๋ฐ ๊ตณ์ด ํ•ด์‹œ๋กœ ๊ตฌ๋ถ„ํ•  ํ•„์š”์—†์ด ์ฒ˜์Œ๋ถ€ํ„ฐ ๋ชจ๋“  ๋ˆ„์ ํ•ฉ์„ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•ด๋†“๊ณ  A์™€ B์˜ ๋ถ€๋ถ„๋ฐฐ์—ด ํ•ฉ์„ ํƒ์ƒ‰ํ•  ์ˆ˜๋„ ์žˆ๊ฒ ๋‹ค ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ n๊ณผ m์ด 1000์ผ ๋•Œ ๋ˆ„์ ํ•ฉ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด๋Š” (1000*1001)/2 = 50๋งŒ์ด ๋˜๊ณ , 50๋งŒ๊ฐœ์˜ ๋‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ชจ๋‘ ํƒ์ƒ‰ํ•˜๋Š” ๊ฑด 50๋งŒx50๋งŒ = 2500์–ต๋ฒˆ ์—ฐ์‚ฐ์„ ํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฌด์กฐ๊ฑด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚ฉ๋‹ˆ๋‹ค. ์ด์ง„ํƒ์ƒ‰์„ ์“ฐ๋ฉด ํ•˜๋‚˜์˜ ๋ฐฐ์—ด์—์„œ ์›ํ•˜๋Š” ๊ฐ’์„ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ log(50๋งŒ), ๋Œ€๋žต 19๋ฒˆ์ด๋‚ด๋กœ ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋‚˜๋จธ์ง€๋Š” ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ณ„์‚ฐํ–ˆ์Šต๋‹ˆ๋‹ค.

@yuyu0830 yuyu0830 merged commit 8398ede into main Jul 1, 2024
@yuyu0830 yuyu0830 deleted the 12-yuyu0830 branch July 1, 2024 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants