HelloMinchan
처음처럼
HelloMinchan
LinkedIn
전체 방문자
오늘
어제
  • 분류 전체보기 (306)
    • Backend (4)
      • NestJS (1)
      • Express (1)
      • Spring (2)
    • Infrastructure (1)
      • AWS (1)
    • Frontend (1)
      • Next.js (1)
    • Language & Runtime (4)
      • Java (2)
      • Node.js (2)
    • Computer Science (8)
      • Computer Networks (3)
      • Operating Systems (4)
      • OOP (1)
    • 독서 (4)
      • 데이터 중심 애플리케이션 설계 (3)
      • 객체지향의 사실과 오해 (1)
    • 회고 (4)
      • Project (2)
      • Career (2)
    • Deprecated (280)

채널

  • GitHub
  • LinkedIn

최근 글

태그

  • 프로그래머스Python
  • Algospot
  • 백준Python
  • 프로그래밍
  • programmers
  • 백준C++
  • Database
  • 백준Go
  • 알고스팟Python
  • 백엔드
  • 프로그래머스C++
  • 코딩
  • 데이터베이스
  • front-end
  • 백준
  • 알고스팟
  • Baekjoon Online Judge
  • 개발자
  • 프로그래머스
  • back-end

최근 댓글

인기 글

hELLO
HelloMinchan

처음처럼

[ALGOSPOT] 알고스팟 LUNCHBOX Microwaving Lunch Boxes(C++, Python)
Deprecated

[ALGOSPOT] 알고스팟 LUNCHBOX Microwaving Lunch Boxes(C++, Python)

2020. 4. 29. 22:23

www.algospot.com

[ALGOSPOT] 알고스팟 LUNCHBOX Microwaving Lunch Boxes

(C++, Python)

(글쓴날 : 2020.04.29)

 


* ALGOSPOT, 알고스팟 LUNCHBOX 문제 C++, Python 언어 풀이입니다.

* 소스 코드의 저작권은 글쓴이에게 있습니다.


 

 

알고스팟 LUNCHBOX Microwaving Lunch Boxes


1) 문제

문제 링크 : https://www.algospot.com/judge/problem/read/LUNCHBOX

 

algospot.com :: LUNCHBOX

Microwaving Lunch Boxes 문제 정보 문제 After suffering from the deficit in summer camp, Ainu7 decided to supply lunch boxes instead of eating outside for Algospot.com winter camp. He contacted the famous packed lunch company "Doosot" to prepare N lunch boxes for

www.algospot.com


2) 풀이 과정

* 시간 복잡도 : O(n log n)

 

먹는 시간이 E만큼 걸리고 전자레인지에 데우는 데 M만큼 걸리는 N개의 도시락이 주어질 때 모든 도시락을 먹는 최소 시간을 구하는 문제입니다.

 

저의 경우, 그리디를 적용하였으며, C++과 Python을 사용했습니다.

정렬을 어떻게 하는지가 관건인 문제였는데, 곰곰이 생각하다 보니 도시락을 전자레인지에 데우는 시간은 절대적이라 줄일 수가 없고, 도시락을 먹는데 걸리는 시간은 순서에 영향을 받는다는 사실을 발견했습니다.

그에 따라, 각 도시락의 데우는데 걸리는 시간과, 먹는데 걸리는 시간을 하나의 데이터(C++ : pair, Python :  tuple)로 합쳐, 먹는데 걸리는 시간을 기준으로 내림차순 정렬하였고, 마지막 도시락을 데우고 나서도 남아있는 먹는데 걸리는 시간을 구하여 총 도시락을 데우는데 걸리는 시간과 더해 문제를 해결했습니다.


3) 코드

 

* C++ 코드

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <functional>
 
using namespace std;
 
struct cmp
{
    bool operator()(pair<int, int> oldValue, pair<int, int> newValue)
    {
        return oldValue.second > newValue.second;
    }
};
 
int main(void)
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
 
    int T = 0;
    cin >> T;
 
    int N = 0;
    vector<int> microwaveTime, eatingTime;
    vector<pair<int, int>> total;
    int eachTime = 0;
 
    while (T--)
    {
        cin >> N;
        int time = 0;
        int remainingEatingTime = 0;
 
        microwaveTime.clear();
        eatingTime.clear();
        total.clear();
 
        for (int i = 0; i < N; i++)
        {
            cin >> eachTime;
            microwaveTime.push_back(eachTime);
        }
        for (int i = 0; i < N; i++)
        {
            cin >> eachTime;
            eatingTime.push_back(eachTime);
        }
        for (int i = 0; i < N; i++)
        {
            time += microwaveTime[i];
            total.push_back({microwaveTime[i], eatingTime[i]});
        }
 
        sort(total.begin(), total.end(), cmp());
 
        for (int i = 0; i < N; i++)
        {
            remainingEatingTime -= total[i].first;
            remainingEatingTime = max(total[i].second, remainingEatingTime);
        }
 
        time += remainingEatingTime;
 
        cout << time << "\n";
    }
 
    return 0;
}

* Python 코드

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
import sys
input = sys.stdin.readline
 
T = int(input())
 
for _ in range(T):
    N = int(input())
    time = 0
    remainingEatingTime = 0
 
    microwaveTime = list(map(int, input().split()))
    eatingTime = list(map(int, input().split()))
    total = []
 
    for i in range(N):
        time += microwaveTime[i]
        total.append((microwaveTime[i], eatingTime[i]))
 
    total = sorted(total, key=lambda x: (-x[1]))
    
    for i in range(0, N):
        remainingEatingTime -= total[i][0]
        remainingEatingTime = max(total[i][1], remainingEatingTime)
        
    time += remainingEatingTime
 
    print(time)

 

저작자표시 비영리 변경금지 (새창열림)

'Deprecated' 카테고리의 다른 글

[ALGOSPOT] 알고스팟 STRJOIN 문자열 합치기(C++, Python)  (0) 2020.04.30
[ALGOSPOT] 알고스팟 MATCHORDER 출전 순서 정하기(C++, Python)  (0) 2020.04.29
[Baekjoon Online Judge] 백준 1655번 가운데를 말해요(C++, Python)  (0) 2020.04.27
[Baekjoon Online Judge] 백준 1715번 카드 정렬하기(C++, Python)  (0) 2020.04.27
[Baekjoon Online Judge] 백준 11286번 절댓값 힙(C++, Python)  (0) 2020.04.26
    'Deprecated' 카테고리의 다른 글
    • [ALGOSPOT] 알고스팟 STRJOIN 문자열 합치기(C++, Python)
    • [ALGOSPOT] 알고스팟 MATCHORDER 출전 순서 정하기(C++, Python)
    • [Baekjoon Online Judge] 백준 1655번 가운데를 말해요(C++, Python)
    • [Baekjoon Online Judge] 백준 1715번 카드 정렬하기(C++, Python)
    HelloMinchan
    HelloMinchan
    Though you should not fear failure, You should do your very best to avoid it.

    티스토리툴바