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

37-9kyo-hwang #142

Merged
merged 3 commits into from
Apr 8, 2024
Merged

37-9kyo-hwang #142

merged 3 commits into from
Apr 8, 2024

Conversation

9kyo-hwang
Copy link
Collaborator

πŸ”— 문제 링크

2212 μ„Όμ„œ

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

40λΆ„

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

1. 문제

직선 μƒμ˜ $-1,000,000 \le x \le 1,000,000$ μœ„μΉ˜μ— N개의 μ„Όμ„œκ°€ μ‘΄μž¬ν•œλ‹€. 여기에 μ„Όμ„œμ˜ 정보λ₯Ό μˆ˜μ§‘ν•˜λŠ” K개의 집쀑ꡭ을 μ„€μΉ˜ν•˜κ³ μž ν•œλ‹€.
이 λ•Œ, 각 정보ꡭ이 μ„Όμ„œμ˜ 정보λ₯Ό μˆ˜μ§‘ν•˜λŠ” μ˜μ—­ 길이가 μ΅œμ†Œκ°€ λ˜λ„λ‘ μ„€μΉ˜ν•΄μ•Ό ν•œλ‹€.
예λ₯Ό λ“€μ–΄, $N = 6, K = 2$이고 μ„Όμ„œμ˜ μœ„μΉ˜κ°€ $[1\space6\space9\space3\space6\space7]$κ³Ό 같이 μ£Όμ–΄μ‘Œλ‹€λ©΄, μ•„λž˜ κ·Έλ¦Όκ³Ό 같이 μ„€μΉ˜ν•˜λ©΄ μ˜μ—­ 길이가 2 + 3 = 5둜 μ΅œμ†Œκ°€ λœλ‹€.
2212 μ„Όμ„œ-001

이런 μ‹μœΌλ‘œ μ˜μ—­μ˜ 길이가 μ΅œμ†Œκ°€ λ˜λ„λ‘ 정보ꡭ을 μ„€μΉ˜ν•΄, μ˜μ—­ 길이 μ΄ν•©μ˜ μ΅œμ†Œκ°’μ„ λ°˜ν™˜ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜λΌ.

2. 풀이

첨에 μ„€μΉ˜ν•  μ„Όμ„œ μœ„μΉ˜λ₯Ό μ°ΎλŠ”λ‹€κ³  μ‚½μ§ˆ μ’€ ν–ˆλŠ”λ°... 생각을 λΉ„ν‹€μ–΄λ³΄λ‹ˆ λ¬Έμ œκ°€ λ‹¨μˆœν•΄μ‘Œλ‹€.

K개의 정보ꡭ을 μ„€μΉ˜ν•œλ‹€λŠ” 건 μ–΄μ°Œλ³΄λ©΄ 6개의 μ„Όμ„œκ°€ μžˆλŠ” 전체 직선을 K개의 μ˜μ—­μœΌλ‘œ λΆ„λ¦¬ν•œλ‹€κ³  생각할 μˆ˜λ„ μžˆλ‹€.
예λ₯Ό λ“€μ–΄, 문제 μ˜ˆμ‹œμ˜ 경우 6개의 μ„Όμ„œλ₯Ό 2개의 μ˜μ—­μœΌλ‘œ 뢄리해야 ν•˜λ‹ˆ [1 | 3, 6, 7, 9] λ˜λŠ” [1, 3 | 6, 7, 9], [1, 3, 6 | 7, 9] λ˜λŠ” [1, 3, 6, 7 | 9] 이런 μ‹μœΌλ‘œ 뢄리할 수 μžˆλ‹€.
이 μ€‘μ—μ„œ κ°€μž₯ μ˜μ—­ 길이의 합이 μ΅œμ†Œκ°€ λ˜λŠ” 건 μ˜ˆμ‹œ κ·Έλ¦Όκ³Ό 같이 [1, 3 | 6, 7, 9]둜 λΆ„ν• ν•˜λŠ” 것이닀.

κ·Έλ ‡λ‹€λ©΄ 길이 합이 μ΅œμ†Œκ°€ λ˜λ„λ‘ λΆ„λ¦¬ν•˜λ €λ©΄ μ–΄λ–»κ²Œ ν•΄μ•Όν•˜λŠ”κ°€λ₯Ό κ³ λ―Όν•˜κ²Œ λœλ‹€. 닡은 μ„Όμ„œ κ°„ 거리가 λ¨Ό κ³³λΆ€ν„° λΆ„λ¦¬ν•˜λ©΄ λœλ‹€.
2212 μ„Όμ„œ-002
μœ„ μ˜ˆμ‹œμ—μ„œ [1, 3 | 6, 7, 9]둜 λΆ„λ¦¬ν–ˆμ„ λ•Œ 길이 합이 μ΅œμ†Œκ°€ 된 μ΄μœ λŠ” μ„Όμ„œ κ°„ 거리가 κ°€μž₯ κΈ΄ 3~6 ꡬ간을 κΈ°μ€€μœΌλ‘œ λΆ„ν• ν–ˆκΈ° λ•Œλ¬Έμ΄λ‹€.

문제의 2번 ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€κ°™μ€ κ²½μš°λ„, μ„Όμ„œ κ°„ 거리가 κΈ΄ 순으둜 μ˜μ—­μ„ 뢄할해버리면 λ‹€μŒκ³Ό 같이 λ‚˜νƒ€λ‚Ό 수 있으며 이 κ²½μš°κ°€ μ˜μ—­ 길이 합이 μ΅œμ†Œμ΄λ‹€.
2212 μ„Όμ„œ-001 (1)

λ”°λΌμ„œ μš°λ¦¬λŠ” 1. μ •λ ¬λœ μ„Όμ„œ μœ„μΉ˜ 정보 2. μ •λ ¬λœ μ„Όμ„œ κ°„ 거리 정보가 ν•„μš”ν•˜λ‹€.

N = int(input())
K = int(input())
sensors = sorted(map(int, input().split()))

N, Kλ₯Ό μž…λ ₯λ°›κ³  sensor μœ„μΉ˜κ°’μ„ μ •λ ¬ν•΄μ„œ sensors에 μ €μž₯ν•œλ‹€.

dist_btwn_sensors = sorted([snd - fst for fst, snd in zip(sensors, sensors[1:])])

각 μ„Όμ„œ κ°„ 거리 값을 ꡬ해 이λ₯Ό μ •λ ¬ν•œ 리슀트λ₯Ό κ΅¬ν•œλ‹€.

print(sum(dist_btwn_sensors[:N - K]))

λ§ˆμ§€λ§‰μœΌλ‘œ μ •λ ¬λœ 거리값을 μ•žμ—μ„œλΆ€ν„° (N - K)개만큼만 λ”ν•΄μ„œ 좜λ ₯ν•œλ‹€.
첫 번째 μ˜ˆμ‹œμ—μ„œ $N = 6, K = 2$μ˜€κ³  [1, 3 | 6, 6, 7, 9]둜 λΆ„λ¦¬ν–ˆλ‹€. 그럼 거리 정보가 [2, 3, 0, 1, 2]이고 이λ₯Ό μ •λ ¬ν•˜λ©΄ [0, 1, 2, 2, 3]이닀.
μ•žμ—μ„œλΆ€ν„° N - K = 4개λ₯Ό κ³ λ₯΄λ‹ˆ (0, 1, 2, 2)이고, 이λ₯Ό λ‹€ λ”ν•˜λ©΄ 5둜 μš°λ¦¬κ°€ κ΅¬ν•˜κ³ μž ν•˜λŠ” 값이 λ‚˜μ˜€κ²Œ λœλ‹€. 즉 κ°€μž₯ κΈ΄ 거리값을 K개 버린닀고 μƒκ°ν•˜λ©΄ λœλ‹€.

3. 전체 μ½”λ“œ

  • python
input = open(0).readline

N = int(input())
K = int(input())
sensors = sorted(map(int, input().split()))
dist_btwn_sensors = sorted([snd - fst for fst, snd in zip(sensors, sensors[1:])])

print(sum(dist_btwn_sensors[:N - K]))
  • c++
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    
    int N, K; cin >> N >> K;
    
    vector<int> Sensors(N);
    for(int& Sensor : Sensors)
    {
        cin >> Sensor;
    }
    
    sort(Sensors.begin(), Sensors.end());
    
    vector<int> DistBtwnSensors;  // N - 1개
    for(int i = 0; i < N - 1; ++i)
    {
        DistBtwnSensors.emplace_back(Sensors[i + 1] - Sensors[i]);
    }
    
    sort(DistBtwnSensors.begin(), DistBtwnSensors.end());
    
    int Answer = 0;
    for(int i = 0; i < N - K; ++i)
    {
        Answer += DistBtwnSensors[i];
    }
    cout << Answer;
    
    return 0;
}

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

λ‚œ 그리디λ₯Ό λ“œλŸ½κ²Œ λͺ»ν•œλ‹€...

Copy link
Collaborator

@Dolchae Dolchae 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
Member

@xxubin04 xxubin04 left a comment

Choose a reason for hiding this comment

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

image

저도 각 μ„Όμ„œλ“€μ˜ μœ„μΉ˜λ“€μ„ μ •λ ¬ν•˜κ³  μΈμ ‘ν•œ μ„Όμ„œλ“€κ°„μ˜ 차이λ₯Ό 리슀트둜 μ •λ¦¬ν•˜μ˜€μŠ΅λ‹ˆλ‹€.
그리고 μœ„μΉ˜ 상 제일 μž‘μ€ μ„Όμ„œμ™€ 큰 μ„Όμ„œμ˜ 거리차 sensor_location[-1] - sensor_location[0]μ—μ„œ μ„Όμ„œλ“€κ°„μ˜ μ°¨μ΄μ—μ„œ κ°€μž₯ 큰 μˆœμ„œλŒ€λ‘œ K-1개의 합을 λΉΌμ£Όμ—ˆμŠ΅λ‹ˆλ‹€. 이 λΆ€λΆ„μ—μ„œ 쑰금 λ‹€λ₯΄κ²Œ μž‘μ„±ν•˜μ˜€λ„€μš” γ…Žγ…ŽπŸ˜Š

input = open(0).readline 

sensor_num = int(input()); center_num = int(input())
sensor_location = sorted(list(map(int, input().split())))
sensor_gap = sorted([sensor_location[i] - sensor_location[i-1] for i in range(1, sensor_num)], reverse=True)
print(sensor_location[-1] - sensor_location[0] - sum(sensor_gap[:center_num-1]))

λ“œλ””μ–΄ 제 μ½”λ“œλ₯Ό λ‚¨κΈ°λŠ” 날이 μ˜€λ‹€λ‹ˆ....... ν–‰λ³΅ν•©λ‹ˆλ‹€....πŸ« πŸ‘
λ§ˆμ§€λ§‰μ—, N-KκΉŒμ§€μ˜ 합을 좜λ ₯ν•  생각은 λͺ»ν–ˆλŠ”데 훨씬 효율적인 것 κ°™μŠ΅λ‹ˆλ‹€!!

Copy link
Member

@gjsk132 gjsk132 left a comment

Choose a reason for hiding this comment

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

이걸 μ–΄μ©Œμ§€ ν•˜λ‹€κ°€ κ²°κ΅­ PR λ΄€μŠ΅λ‹ˆλ‹€.

문제λ₯Ό λ„ˆλ¬΄ λ³΅μž‘ν•˜κ²Œ μƒκ°ν•΄μ„œ μ΄λ ‡κ²Œ μ •λ ¬ν•˜λŠ” κ±° 보고, λ°”λ‘œ μ½”λ“œ 짜러 κ°”μŠ΅λ‹ˆλ‹€.

전체 μ½”λ“œ
input = open("input.txt").readline

node = int(input())
senser = int(input())

node_info = sorted(map(int,input().split()))
gap_info = sorted([node_info[i+1]-node_info[i] for i in range(node-1)])

print(sum(gap_info[:node-senser]))

ν˜Έλ‹€λ‹₯ 리뷰 도μž₯κΉ¨κΈ° ν•˜κ² μŠ΄λ‹ˆλ‹€..!

@9kyo-hwang 9kyo-hwang merged commit 381ca20 into main Apr 8, 2024
6 checks passed
@9kyo-hwang 9kyo-hwang deleted the 37-9kyo-hwang branch April 8, 2024 16:43
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.

4 participants