TopCoder알고리즘 트레이닝에 수록된 문제이다.
문제
이때 i번째 병의 용량은 capacities[i]리터이며 타로가 i번째 병에 넣은 키위 주스의 양을 bottles[i]리터라고 합니다.
i번째 조작은 타로가 병 fromId[i]부터 toId[i]에 키위 주스를 넣는 것을 의미합니다.
병 fromId[i]가 비어 있거나 병 toId[i]가 꽉 차 있는 순간 타로는 더 이상 키위 주스를 넣지 않습니다.
N개의 요소를 가진 정수 배열 Int[]를 리턴해주세요.
배열의 i번째 요소는 모든 주스를 쏟는 작업이 완료되고 i번째 병에 남아 있는 키위주스의 양입니다.
* 1. capacities : 2~50개의 요소가 있는 배열입니다. 각 요소는 1~1000000 사이의 값을 갖습니다.
* 2. bottles : capacities 와 같은 수의 요소가 있는 배열입니다.
* bottles[i]는 capacities[i]에 들어있는 주스를 의미합니다.
* 3. formId : 1~50 개의 요소가 있는 배열입니다.
* 4. todoId : fromId와 같은 수의 요소가 있는 배열입니다.
* 단, fromId, toId는 0~ (N-1)사이의 값입니다. 이때 N은 변수 capacities의 항목 개수입니다.
* 변수 fromId[i]와 toId[i]는 서로 다른 값을 같습니다.
함수 정의
c++
vector<int> thePouring(vector<int> capacities, vector<int> bottles, vector<int> fromId, vector<int> toId)
// // main.cpp // KiwiJuice // // Created by sw on 19/12/2018. // Copyright © 2018 sw. All rights reserved. // #include <stdio.h> #include <iostream> #include <vector> #include <algorithm> using namespace std; vector<int> thePouring(vector<int> capacities, vector<int> bottles, vector<int> fromId, vector<int> toId) { for(int i=0; i<fromId.size(); i++) { int sum=bottles[fromId[i]] + bottles[toId[i]]; bottles[toId[i]]=min(sum, capacities[toId[i]]); bottles[fromId[i]]=sum-bottles[toId[i]]; }
return bottles; } int main(int argc, const char * argv[]) { vector<int> capacities = {30,20,10}; vector<int> bottles = {10,5,5}; vector<int> fromId = {0,1,2}; vector<int> toId = {1,2,0};
vector<int> rst=thePouring(capacities, bottles, fromId, toId);
for(int value:rst){ cout<<value<<endl; } return 0; } |
'Algorithm' 카테고리의 다른 글
[TopCoder 전체탐색] 회문(Palindrome) (0) | 2018.12.20 |
---|---|
[TopCoder 전체탐색] 암호(Cryptography) (0) | 2018.12.20 |
[BOJ 2839] 설탕배달 (0) | 2018.12.18 |
[BOJ 5543] 상근날드 (0) | 2018.12.18 |
[카카오 2019 신입공채 1차 코딩테스트] 6.매칭 점수 (0) | 2018.11.14 |