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

[cpp] 7차 Softeer 정기 역량 진단 1번 문제 - 자동차 테스트 #63

Merged
merged 4 commits into from
Sep 2, 2023

Conversation

minjae9610
Copy link
Member

@minjae9610 minjae9610 commented Aug 25, 2023

🌁 Background

2023년 7차 Softeer 정기 역량 진단

👩‍💻 Contents

[7차 Softeer 정기 역량 진단] 1번 - 자동차 테스트

image
image
image
image

#include<iostream>
#include<algorithm>
#include<unordered_map>

using namespace std;

int main(int argc, char** argv)
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int n, q;
    cin >> n >> q;

    int cars[50000];
    for (int i = 0; i < n; ++i)
        cin >> cars[i];

    sort(cars, cars + n);

    unordered_map<int, int> map;
    for (int i = 0; i < n; ++i)
        map[cars[i]] = i + 1;

    for (int i = 0; i < q; ++i) {
        int m, m_idx;
        cin >> m;
        m_idx = map[m];
        cout << (m_idx ? (m_idx - 1) * (n - m_idx) : m_idx) << '\n';
    }

    return 0;
}

📝 Review Note

문제의 제약조건에서 알 수 있다싶이, n의 최대값이 50,000이고 q의 최대값이 200,000이다. n50,000이고 q200,000이라면, n개의 자동차 중 임의의 3개를 직접 골라서, 중앙값이 m이 될 수 있는 서로 다른 경우의 수를 구하는 것은 O(N^3)의 시간복잡도를 가진다. 제한시간인 2초를 넘어가기 때문에, O(N^3)의 알고리즘으로는 풀 수 없다.
이는 최소한 O(Q * log N)의 알고리즘으로 풀어야 한다는 것을 의미한다.
O(Q * log N)의 알고리즘으로 풀기 위해서는, n개의 자동차의 연비를 정렬한 다음 각 q에 대해 m의 인덱스를 이진 탐색으로 찾아야 한다.
본 풀이와 같이 해시 테이블을 이용한 O(N * log N + Q) 알고리즘으로도 풀 수 있다.

@minjae9610 minjae9610 requested a review from a team as a code owner August 25, 2023 05:53
@minjae9610 minjae9610 requested review from alwzbright97 and Yg-Hong and removed request for a team August 25, 2023 05:53
@minjae9610 minjae9610 changed the title Algo/minjae9610/softeer hsat 7 1 [cpp] 7차 Softeer 정기 역량 진단 1번 문제 - 자동차 테스트 Aug 25, 2023
Copy link

@alwzbright97 alwzbright97 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
Contributor

@Yg-Hong Yg-Hong left a comment

Choose a reason for hiding this comment

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

코드 깔끔~하니 좋네요... 따로 첨언할 건 없을 것 같습니다. 💯 💯

@minjae9610 minjae9610 merged commit d1df471 into main Sep 2, 2023
1 check passed
@minjae9610 minjae9610 deleted the algo/minjae9610/softeer-HSAT-7-1 branch September 2, 2023 11:47
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.

3 participants