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

8-yuyu0830 #33

Merged
merged 1 commit into from
May 9, 2024
Merged

8-yuyu0830 #33

merged 1 commit into from
May 9, 2024

Conversation

yuyu0830
Copy link
Collaborator

πŸ”— 문제 링크

보석 도둑(1202)

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

2일...? 3일...?

✨ μˆ˜λ„ μ½”λ“œ

❓ 문제

n개의 보석과 k개의 가방이 μžˆλ‹€! 보석은 무게 w와 가격 p의 정보가 주어지고, κ°€λ°© λ³„λ‘œ 담을 수 μžˆλŠ” λ³΄μ„μ˜ 무게 μ œν•œ cκ°€ μžˆμ„ λ•Œ, μ΅œλŒ€ν•œ 높은 κ°’μ–΄μΉ˜λ₯Ό ν›”μ³λ³΄μž!

  • (1 ≀ n, k ≀ 300,000)
  • (1 ≀ w, p ≀ 1,000,000)
  • (1 ≀ c ≀ 100,000,000)
  • 각 κ°€λ°©μ—λŠ” ν•˜λ‚˜μ˜ λ³΄μ„λ§Œ ν›”μΉ  수 μžˆλ‹€!

❗ 풀이

1️⃣ 첫번째 풀이

μš°μ„ μˆœμœ„ 큐λ₯Ό μ“°λŠ” 방법을 μƒκ°ν–ˆλ‹€. 가방을 ν¬κΈ°λ³„λ‘œ μ •λ ¬ν•˜κ³ , 보석도 가격 λ³„λ‘œ λ‚΄λ¦Όμ°¨μˆœ μ •λ ¬ν•œλ‹€. "κ°€μž₯ λΉ„μ‹Ό 보석을 κ°€μž₯ μž‘μ€ 가방에 λ„£λŠ”λ‹€" λΌλŠ” κ°œλ…μœΌλ‘œ μ ‘κ·Όν–ˆλ‹€. λΉ„μ‹Ό 보석뢀터 μ΅œλŒ€ν•œ κ°€λ²Όμš΄ 가방에 넣어보며 νƒμƒ‰ν–ˆλ‹€

priority_queue<gem, vector<gem>, cmp> gems;
vector<int> bags;

while (!gems.empty()) {
    gem tmp = gems.top();
    gems.pop();

    for (int i = 0; i < k; i++) {
        if (tmp.weight <= bags[i] && !used[i]){
            used[i] = 1;
            ans += tmp.price;
            cnt++;
            break;
        }
    }

    if (cnt == k) break;
}

μ΄λ ‡κ²Œ ν•˜λŠ” κ²½μš°μ— μ΅œμ•…μ˜ 경우 30만개의 보석에 λŒ€ν•΄ 30만개의 가방을 μ „λΆ€ 탐색해야할 μˆ˜λ„ μžˆλ‹€! 말도 μ•ˆλ˜λŠ” 경우의 μˆ˜κ°€ 생기기 λ•Œλ¬Έμ— λ‹€λ₯Έ 방법을 λͺ¨μƒ‰ν•΄μ•Ό ν–ˆλ‹€.. 이틀을 κ³ λ―Όν•΄λ΄€μ§€λ§Œ λ„μ €νžˆ μˆ˜κ°€ 생각이 μ•ˆλ‚˜ λ‹€λ₯Έ λΈ”λ‘œκ·Έλ₯Ό μ°Έκ³ ν–ˆλ‹€...
image
λΉ μ Έλ‚˜μ˜¬ 수 μ—†λŠ” μ‹œκ°„ 초과의 λŠͺ

2️⃣ λ‘λ²ˆμ§Έ 풀이

μš°μ„ μˆœμœ„ 큐λ₯Ό μ‚¬μš©ν•˜λŠ” 건 κ°™μ•˜μ§€λ§Œ 탐색을 ꡳ이 λͺ¨λ“  가방에 λŒ€ν•΄μ„œ ν•  ν•„μš”κ°€ μ—†μ—ˆλ‹€. μ• μ΄ˆμ— 보석에 λŒ€ν•΄ 가방을 νƒμƒ‰ν•˜λŠ”κ²Œ μ•„λ‹ˆλΌ "가방에 λŒ€ν•΄ λ“€μ–΄κ°ˆ 수 μžˆλŠ” μ΅œλŒ€μΉ˜μ˜ 보석을 탐색" ν•˜λ©΄ λ˜λŠ” κ²ƒμ΄μ˜€λ‹€.

κ°€λ°©κ³Ό 보석을 λ‘˜ λ‹€ μ˜€λ¦„μ°¨μˆœ μ •λ ¬ν•œλ‹€. 보석은 무게 κΈ°μ€€μœΌλ‘œ μ •λ ¬ν•œλ‹€. 각 κ°€λ°© λ³„λ‘œ νƒμƒ‰ν•˜λŠ”λ°, 각 κ°€λ°©μ˜ λ¬΄κ²Œλ³΄λ‹€ κ°€λ²Όμš΄ λ³΄μ„λ“€μ˜ κ°’μ–΄μΉ˜λ₯Ό λͺ¨λ‘ μš°μ„ μˆœμœ„ 큐에 λ„£λŠ”λ‹€. 그러고 popλ₯Ό ν•˜λ©΄ "ν•΄λ‹Ή 가방에 λ“€μ–΄κ°ˆ 수 μžˆλŠ” κ°€μž₯ λΉ„μ‹Ό λ³΄μ„μ˜ 가격"이 λ‚˜μ˜€κ²Œ λœλ‹€!!

ii gems[300001];
int bags[300001] = {0, };

priority_queue<int> q;

for (int i = 0; i < k; i++) {
    int curBag = bags[i];

    // push all the gems that can put in current bag into the priority queue 
    while (cnt < n && gems[cnt].first <= curBag) {
        q.push(gems[cnt].second);
        cnt++;
    }

    // if queue is not empty, top of queue will be max price that can get in current bag
    if (!q.empty()) {
        ans += q.top();
        q.pop();
    }
}

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

μ°Έκ³ ν•œ λΈ”λ‘œκ·Έ
μ•Œκ³ λ¦¬μ¦˜μ„ 잘 μ§œμ•Όν•œλ‹€... 괜히 μ‹œκ°„λ§Œ 버린 것 κ°™λ‹€ γ… γ… 

@yuyu0830 yuyu0830 self-assigned this Apr 11, 2024
@yuyu0830 yuyu0830 changed the title 24-04-11 보석 도둑 8-yuyu0830 Apr 11, 2024
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.

문제λ₯Ό 봀을 λ•Œ λ°”λ‘œ λ°°λ‚­λ¬Έμ œκ°€ λ– μ˜€λ₯΄κΈ΄ ν–ˆμŠ΅λ‹ˆλ‹€.
λ‹€λ§Œ 총 μš©λŸ‰μ΄ μ•„λ‹Œ 가방에 담을 수 μžˆλŠ” 크기가 μ œν•œλ˜μ–΄μžˆκ³  개수 K만큼만 담을 수 μžˆλ‹€λŠ” μ μ—μ„œ μ•„μ°¨ μ‹Άμ—ˆμŠ΅λ‹ˆλ‹€.
κ·Έλž˜μ„œ 접근을 ν•  λ•Œ 2차원 배열을 K만큼 λ‘λ˜ μ •λ ¬ν•΄μ„œ μ†Œμ§€ν•  수 μžˆλŠ” 크기만 λ„£λŠ” 것이 μ—ˆμŠ΅λ‹ˆλ‹€.
κ·Έ ν›„ λΉ„κ΅ν•˜λŠ” 보석도 μ •λ ¬ν•΄μ„œ ν•˜λ‚˜μ”© λŒ€μž…ν•΄λ³΄λŠ” DPν˜•μ‹ μ΄μ˜€λŠ”λ° Nκ³Ό K의 μ΅œλŒ€ 크기가 30λ§Œμ΄λ”κ΅°μš”.. 그럼 30λ§Œλ²ˆμ— 30λ§Œλ²ˆμ„ νƒμƒ‰ν•˜κ²Œ 되면 90μ–΅ 번의 μ—°μ‚° μˆ˜ν–‰μ΄ μ΄λ€„μ§€κ²Œ λ©λ‹ˆλ‹€..
κ·Έμ μ—μ„œ cnt둜 μ–΄λŠ λ³΄μ„κΉŒμ§€ ν™•μΈν–ˆλŠ”μ§€κ°€ μ’‹μ•˜λ˜ 것 κ°™μŠ΅λ‹ˆλ‹€.
정렬을 ν–ˆκΈ° λ•Œλ¬Έμ— μ˜€λ¦„μ°¨μˆœμœΌλ‘œ λ˜μ–΄ μžˆμ„ 것이고. 배낭이 μˆ˜μš©ν•  수 μžˆλŠ” 보석듀을 cnt μ΄ν›„λ‘œ 계속 νƒμƒ‰ν•΄μ£Όλ©΄μ„œ λ„£μ–΄μ£Όλ©΄ 되기 λ•Œλ¬Έμ— 일일이 λ‹€ 탐색을 ν•˜λŠ”κ²Œ μ•„λ‹Œ μ΄μ–΄μ„œ νƒμƒ‰ν•˜λŠ” 뢀뢄을 잘 μž‘μ•„μ€€ 것 κ°™μŠ΅λ‹ˆλ‹€.

Copy link
Contributor

@dhlee777 dhlee777 left a comment

Choose a reason for hiding this comment

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

기쀀을 λ°”κΏ”μ„œ 가방에 λŒ€ν•΄ λ“€μ–΄κ°ˆ 수 μžˆλŠ” μ΅œλŒ€μΉ˜μ˜ 보석을 탐색 ν•΄μ€ŒμœΌλ‘œμ¨ λͺ¨λ“  경우의 μˆ˜μ— λŒ€ν•΄ 탐색을 ν•΄μ£Όμ§€μ•ŠμœΌλ―€λ‘œ μ‹œκ°„μ΄ˆκ³Όλ₯Ό ν”Όν• μˆ˜κ°€μžˆκ΅°μš”.. 저도 이런문제λ₯Ό ν’€λ•Œ 뭘 κΈ°μ€€μœΌλ‘œ 할지에 λŒ€ν•΄ 잘 μƒκ°ν•΄λ΄μ•Όκ² μŠ΅λ‹ˆλ‹€.

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.

κ°€λ°©κ³Ό 보석을 μ˜€λ¦„μ°¨μˆœ μ •λ ¬ν•˜λŠ”κ²Œ ν•΅μ‹¬μ΄μ—ˆλ„€μš”. νŒŒμ΄μ¬μ—μ„œλŠ” μ΅œλŒ€νž™μ„ μ‚¬μš©ν•΄μ„œ κ°€μž₯ 큰 κ°€μΉ˜λ₯Ό μ°ΎλŠ” 방법이 μžˆμ„ 것 κ°™μ•„μš”. μš©λŸ‰λ³΄λ‹€ μž‘κ±°λ‚˜ 같은 λͺ¨λ“  보석듀을 λ„£κ³  max heapμ—μ„œ pop을 ν•΄ μ£ΌλŠ” μ‹μœΌλ‘œ ν•˜λ©΄ 될 것 κ°™λ‹€λŠ” 생각이 λ“œλ„€μš”.

@yuyu0830 yuyu0830 merged commit 0529f80 into main May 9, 2024
1 check passed
@yuyu0830 yuyu0830 deleted the 8-yuyu0830 branch May 9, 2024 06:13
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