티스토리 뷰

솔루션을 보고 풀었다.

 

https://jaimemin.tistory.com/759

 

백준 1700번 멀티탭 스케줄링

문제 링크입니다: https://www.acmicpc.net/problem/1700 고려해야했던 것이 많았던 그리디(Greedy) 알고리즘 문제였습니다. 알고리즘은 아래와 같습니다.1. 기기들의 사용 순서들을 입력 받습니다.2. K번 반

jaimemin.tistory.com

 

푸는데 for_each, distance, find 를 활용하고싶었다.

 

#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <array>

using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    
    array<int, 100> schedule;
    array<int, 100> power; power.fill(0);

    int N, K, answer=0;
    cin >> N >> K;
    
    for_each(schedule.begin(), schedule.begin()+K, [](auto& x){ cin >> x; });
    
    for(int i=0; i<K; i++)
    {
        int device = schedule[i];
        bool ok = false;
        
        for(int j=0; j<N; j++)
        {
            if(power[j] == device)
            {
                ok = true;
                break;
            }
            if(power[j] == 0)
            {
                power[j] = device;
                ok = true;
                break;
            }
        }
        
        if(ok) continue;
        answer++;
        
        int max_next_schedule_distance = -1;
        int picked_device_position = -1;
        
        for(int j=0; j<N; j++)
        {
            int using_device = power[j];
            auto p = find(schedule.begin()+i+1, schedule.end(), using_device);

            //가장 마지막에 쓰이는 장치를 찾는다. 못찾으면 그걸 그냥 가장 마지막에 쓰인다고 봄
            int next_schedule_distance = distance(schedule.begin()+i+1, p);
            if(max_next_schedule_distance < next_schedule_distance)
            {
                max_next_schedule_distance = next_schedule_distance;
                picked_device_position = j;
            }
        }
        
        power[picked_device_position] = device;
    }
    cout << answer;
    return 0;
}

 

'Problem Solving > 백준 온라인 저지' 카테고리의 다른 글

[백준] 15661 - 링크와 스타트  (0) 2021.03.21
7662 - 이중 우선 순위 큐  (0) 2021.03.07
1197-최소 스패닝 트리  (0) 2020.10.29
1541-잃어버린 괄호  (0) 2020.10.29
2884-알람 시계  (0) 2020.10.27
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함