1. ๋ชจํ—˜๊ฐ€ ๊ธธ๋“œ

1, 2, 3 / 2 2 ๊ฒฐ์„ฑ ๋ผ์•ผ ํ•˜๋Š”์ค„์•Œ๊ณ  reserve ์จ์„œ ํ–ˆ๋Š”๋ฐ ์ด๋ ‡๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๊ตฌ๋‚˜ ..!

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int n, num;
    vector<int> v;

    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> num;
        v.push_back(num);
    }
    sort(v.begin(), v.end());  // 1 2 2 2 3

    int result = 0; // ์ด ๊ทธ๋ฃน ์ˆ˜
    int cnt = 0; //ํ˜„์žฌ ๊ทธ๋ฃน์— ํฌํ•จ๋œ ๋ชจํ—˜๊ฐ€ ์ˆ˜

    for (int i = 0; i < n; i++) {
        cnt++;
        if (cnt >= v[i]) {
            result++;
            cnt = 0;
        }
    }

    cout << result;
    
    //1, 2 2 ๊ทธ๋ฃน 
    //2, 3์€ ๊ทธ๋ฃน๊ฒฐ์„ฑ ๋ชปํ•จ ..
}

 

2. ๊ณฑํ•˜๊ธฐ ํ˜น์€ ๋”ํ•˜๊ธฐ

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s;
    cin >> s;
    int result = s[0] - '0';

    for (int i = 1; i < s.size(); i++) {
        int num = s[i] - '0';
        if (num == 0 || num == 1) {
            result += num;
        } else {
            result *= num;
        }
    }
    cout << result;
}

 

3. ๋ฌธ์ž์—ด ๋’ค์ง‘๊ธฐ

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s;
    int cnt0 = 0, cnt1 = 0; //0๋กœ ๋ผ์žˆ๋Š” ๊ตฌ๊ฐ„, 1๋กœ ๋ผ์žˆ๋Š” ๊ตฌ๊ฐ„
    cin >> s;
    
    if (s[0] == '0') {
        cnt0++;
    } else {
        cnt1++;
    }

    // 1๋กœ ๋ฐ”๋€Œ๋Š” ๊ฒฝ์šฐ : 1 / 0์œผ๋กœ ๋ฐ”๋€Œ๋Š” ๊ฒฝ์šฐ : 2
    for (int i = 0; i < s.size() - 1; i++) {
        if (s[i] != s[i + 1]) {
            if (s[i + 1] == '0') {
                cnt0++;
            } else {
                cnt1++;
            }
        }
    }

    if (cnt0 < cnt1) {
        cout << cnt0;
    } else {
        cout << cnt1;
    }
}

 

4. ๋งŒ๋“ค ์ˆ˜ ์—†๋Š” ๊ธˆ์•ก

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int n;
    vector<int> v;

    cin >> n;

    for (int i = 0; i < n; i++) {
        int num;
        cin >> num;
        v.push_back(num);
    }
    sort(v.begin(), v.end());

    int target = 1;
    for (int i = 0; i < v.size(); i++) {
        if (target < v[i])
            break;
        target += v[i];
    }
    cout << target;
}

 

5. ๋ณผ๋ง๊ณต ๊ณ ๋ฅด๊ธฐ

2์ฐจ์› ๋ฐฐ์—ด๋กœ ํ•ด๋„ 1์ดˆ๋ผ ๊ทธ๋ ‡๊ฒŒ ํ’€์—ˆ๋Š”๋ฐ ๋‹ค๋ฅธ ์ข‹์€ ๋ฐฉ๋ฒ•์ด ์žˆ์—ˆ๋‹ค!

 

1 3 2 3 2

๋ฌด๊ฒŒ 1 : 2๊ฐœ

๋ฌด๊ฒŒ 2 : 2๊ฐœ

๋ฌด๊ฒŒ 3 : 2๊ฐœ

 

   A ์„ ํƒ        B ์„ ํƒ 

    ๋ฌด๊ฒŒ 1     :   4๊ฐ€์ง€ -> (1, 2), (1, 3), (1, 4), (1, 5) = (5-1)*4)

    ๋ฌด๊ฒŒ 2    :   4๊ฐ€์ง€ -> (3, 2), (3, 4), (5, 2), (5, 4) = (4-2)*2

    ๋ฌด๊ฒŒ 3    :  0๊ฐ€์ง€ = (2-2)*2

 

์ „์ฒด ๋ณผ๋ง๊ณต ๊ฐœ์ˆ˜์—์„œ ํ˜„์žฌ ๋ณผ๋ง๊ณต์˜ ๊ฐœ์ˆ˜๋ฅผ ๋นผ๊ณ , (A์˜ ์„ ํƒ์— ๋”ฐ๋ผ B ์„ ํƒ์˜ ํญ์ด ์ข์•„์ง)

ํ˜„์žฌ ๋ณผ๋ง๊ณต์˜ ๊ฐœ์ˆ˜๋ฅผ ๋”ํ•ด์ค€ ๊ฒƒ์„ ๋ˆ„์ ํ•ฉํ•˜๋ฉด ๋จ !

#include <iostream>
using namespace std;

int arr[11];

int main() {
    int n, m, num, cnt = 0;
    cin >> n >> m;
    
    for (int i = 0; i < n; i++) {
        cin >> num;
        arr[num]++;
    }

    for (int i = 1; i <= m; i++) {
        n -= arr[i];
        cnt += n * arr[i];
    }

    cout << cnt;
}

 

6. ๋ฌด์ง€์˜ ๋จน๋ฐฉ ๋ผ์ด๋ธŒ

1. k๊ฐ€ 0์ผ ๋•Œ

2. ์ฐจ์ด๋ฅผ ๋บ„ ๋•Œ, ๋‚จ์€์‹œ๊ฐ„๋ณด๋‹ค ์ฐจ์ด๊ฐ€ ํด ๊ฒฝ์šฐ

์—ฌ๊ธฐ์„œ summary๋Š” ์•ž์„  ๊ทธ๋ฆผ๊ณผ ๋‹ค๋ฅด๊ฒŒ ๋ˆ„์ ํ•ฉ์œผ๋กœ k๋ž‘ ๋น„๊ตํ•จ !

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

bool compare(pair<int, int> a, pair<int, int> b) {
    return a.second < b.second;
}

int solution(vector<int> food_times, long long k) {
    long long summary = 0;
    for (int food_time: food_times) {
        summary += food_time;
    }
    if (summary <= k) { //๋จน์„ ๊ฒŒ ์—†์Œ
        return -1;
    }

    //์‹œ๊ฐ„์ด ์ž‘์€ ์Œ์‹๋ถ€ํ„ฐ ๋นผ์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— pq ์‚ฌ์šฉ
    priority_queue<pair<int, int>> pq;
    for (int i = 0; i < food_times.size(); i++) {
        pq.push({-food_times[i], i + 1}); //(์Œ์‹ ์‹œ๊ฐ„, ์Œ์‹ ๋ฒˆํ˜ธ)
    }

    summary = 0; //๋จน๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ ์‹œ๊ฐ„
    long long pre = 0; //์ง์ „์— ๋‹ค '๋จน์€ ์Œ์‹' ์‹œ๊ฐ„
    long long length = food_times.size(); //'๋‚จ์€ ์Œ์‹'์˜ ๊ฐœ์ˆ˜
    while (summary + ((-pq.top().first - pre) * length) <= k) { //3 -> 5 -> 6(์ข…๋ฃŒ)
        int now = -pq.top().first;
        pq.pop();
        summary += (now - pre) * length;
        length--;
        pre = now;
    }

    //๋‚จ์€ ์Œ์‹ ์ค‘์—์„œ '๋ช‡ ๋ฒˆ์งธ ์Œ์‹' ์ธ์ง€ ํ™•์ธํ•ด ์ถœ๋ ฅ (if ๋‚จ์€์‹œ๊ฐ„ 0์ด๋ฉด, result์— ํ•˜๋‚˜๋งŒ ๋“ค์–ด๊ฐ)
    vector<pair<int, int>> result;
    while (!pq.empty()) {
        int food_time = -pq.top().first;
        int num = pq.top().second;
        pq.pop();
        result.emplace_back(food_time, num);
    }
    sort(result.begin(), result.end(), compare); // ์Œ์‹์˜ ๋ฒˆํ˜ธ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
    return result[(k - summary) % length].second;
}

int main() {
    vector<int> food_times = {3, 1, 2};
    long long k = 5;
    cout << solution(food_times, k); //์Œ์‹ ๋ฒˆํ˜ธ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ

    return 0;
}

 

+ Recent posts