Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define maxValue 3600
- #define minValue 0
- vector<int>* button = new vector<int>();
- vector<int>* ans = new vector<int>();
- queue<int>* q = new queue<int>();
- void adjust(int* index){
- int value = *index;
- if(value < 0) *index = 0;
- if(value > maxValue) *index = maxValue;
- }
- void updateFromElem(int elemIndex, vector<int>* button, vector<int>* ans, queue<int>* q) {
- adjust(&elemIndex);
- for(int i=0; i<button->size(); i++){
- int thisButton = button->at(i);
- int curIndex = elemIndex + thisButton;
- adjust(&curIndex);
- if(curIndex == 0) continue;
- if(ans->at(curIndex) == -1){
- q->push(curIndex);
- ans->at(curIndex) = ans->at(elemIndex) + 1;
- }
- }
- }
- void doEachInstance(){
- //////// Initialization (you can skip it)
- int numButtons, targetTime;
- cin>>numButtons>>targetTime;
- button->resize(numButtons);
- ans->resize(maxValue+1);
- for (int i=0; i<maxValue+1; i++){
- ans->at(i) = -1;
- }
- q = new queue<int>();
- for(int i=0; i<numButtons; i++){
- cin>>button->at(i);
- }
- //////// UP TO THIS POINT
- // DFS:
- ans->at(0) = 0;
- while(!q->empty()){
- int elem = q->front(); q->pop();
- updateFromElem(elem, button, ans, q);
- }
- // Extracting Answer:
- for(int i=targetTime; i<=maxValue; i++){
- if(ans->at(i) != -1){
- cout<<ans->at(i)<<" "<<i-targetTime<<endl;
- return;
- }
- }
- }
- int main(){
- int T; cin>>T;
- for(int i=0; i<T; i++)
- doEachInstance();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement