์ƒ์„ธ ์ปจํ…์ธ 

๋ณธ๋ฌธ ์ œ๋ชฉ

[Softeer] ์ž๋™์ฐจ ํ…Œ์ŠคํŠธ (C++)

PROGRAMMING/Algorithm

by koharin 2024. 2. 16. 11:59

๋ณธ๋ฌธ

728x90
๋ฐ˜์‘ํ˜•

๐Ÿ“ ์ œ์ถœ์ฝ”๋“œ #1 (์‹œ๊ฐ„ ์ดˆ๊ณผ)

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main(int argc, char** argv)
{
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);
    int q, m;
    int r=0; // result counting variable
    vector<int> car;
    vector<int> mid;

    // for starting from index 1
    car.push_back(0);
    
    scanf("%d %d", &q, &m);
    // ์ž๋™์ฐจ ์—ฐ๋น„ ๋„ฃ๊ธฐ
    for(int i=1; i<=q; i++){
        int n;
        scanf("%d", &n);
        car.push_back(n);
    }

    sort(car.begin(), car.end());

    // ์ค‘์•™๊ฐ’ ๋„ฃ๊ธฐ
    for(int i=1; i<=m; i++){
        int num; 
        scanf("%d", &num);
        mid.push_back(num);
    }

    for(int i=0; i<m; i++){ 
        auto it = find(car.begin(), car.end(), mid[i]);
        // if m_i not in car, print result and continue
        if( it == car.end()) {
            printf("0\n");
            continue;
        } 
        
        int pos = distance(car.begin(), it);
        
        for(int i=1; i<pos; i++){
            for(int j=pos+1; j<=q; j++) r++;
        }
        printf("%d\n", r);
        r=0;
    }
    return 0;
}

์ƒ๊ฐ๋‚˜๋Š” ํ’€์ด๋กœ ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ๋กœ, ๋ฐ˜๋ณต๋ฌธ์ด 3๊ฐœ๊ฐ€ ์ค‘์ฒฉ๋˜์–ด์„œ ๋ณต์žก๋„๊ฐ€ ๋†’์•„ TC์…‹ ํ•˜๋‚˜๋งŒ ํ†ต๊ณผํ•˜๊ณ  ๋‚˜๋จธ์ง€ ๋‘๊ฐœ๋Š” ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋‹ค.

 

๐Ÿ“ ์ œ์ถœ์ฝ”๋“œ #2 (์‹œ๊ฐ„ ์ดˆ๊ณผ)

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main(int argc, char** argv)
{
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);
    int q, m;
    int r=0; // result counting variable
    vector<int> car;
    vector<int> mid;

    // for starting from index 1
    car.push_back(0);
    
    scanf("%d %d", &q, &m);
    // ์ž๋™์ฐจ ์—ฐ๋น„ ๋„ฃ๊ธฐ
    for(int i=1; i<=q; i++){
        int n;
        scanf("%d", &n);
        car.push_back(n);
    }

    sort(car.begin(), car.end());

    for(int i=0; i<m; i++){
        int mid;
        scanf("%d", &mid);
        
        auto it = find(car.begin(), car.end(), mid);
        
        // if m_i not in car, print result and continue
        if( it == car.end()) {
            printf("0\n");
            continue;
        } 
        // find index of mid
        int pos = distance(car.begin(), it);

        if(pos == 1) {
            printf("0\n");
            continue;
        }

        // ๊ฐ€์ง“์ˆ˜๋Š” mid๋ณด๋‹ค ์ž‘์€ ๊ฐœ์ˆ˜*mid๋ณด๋‹ค ํฐ ๊ฐœ์ˆ˜
        printf("%d\n", (pos-1)*(q-pos));
    }
    return 0;
}

TC์…‹ 3๊ฐœ ์ค‘ ํ•œ ์„ธํŠธ์—์„œ ํ•˜๋‚˜์˜ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๊ฐ€ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค..........

 

๐Ÿ“ ์ œ์ถœ์ฝ”๋“œ #3 (ํ†ต๊ณผ)

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main(int argc, char** argv)
{
    int q, m;
    vector<int> car;
    vector<int> mid;

    // for starting from index 1
    car.push_back(0);
    
    scanf("%d %d", &q, &m);
    // ์ž๋™์ฐจ ์—ฐ๋น„ ๋„ฃ๊ธฐ
    for(int i=1; i<=q; i++){
        int n;
        scanf("%d", &n);
        car.push_back(n);
    }

    sort(car.begin(), car.end());

    for(int i=0; i<m; i++){
        int mid;
        scanf("%d", &mid);
        
        auto it = find(car.begin(), car.end(), mid);
        
        // if m_i not in car, print result and continue
        if( it == car.end()) {
            printf("0\n");
            continue;
        } 
        // find index of mid
        int pos = distance(car.begin(), it);

        // ๊ฐ€์ง“์ˆ˜๋Š” mid๋ณด๋‹ค ์ž‘์€ ๊ฐœ์ˆ˜*mid๋ณด๋‹ค ํฐ ๊ฐœ์ˆ˜
        printf("%d\n", (pos-1)*(q-pos));
    }
    return 0;
}

pos๊ฐ€ ์ฒซ ๋ฒˆ์งธ์ธ ๊ฒฝ์šฐ์—๋„ ์ถœ๋ ฅ ์‹œ pos-1์œผ๋กœ 0์ด ๋˜๋ฏ€๋กœ ๋”ฐ๋กœ ๊ณ ๋ ค ์•ˆ ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ˆ˜์ •ํ–ˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•

๊ด€๋ จ๊ธ€ ๋”๋ณด๊ธฐ