Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define maxValue 3600
  4. #define minValue 0
  5.  
  6. vector<int>* button = new vector<int>();
  7. vector<int>* ans = new vector<int>();
  8. queue<int>* q = new queue<int>();
  9.  
  10. void adjust(int* index){
  11. int value = *index;
  12. if(value < 0) *index = 0;
  13. if(value > maxValue) *index = maxValue;
  14. }
  15.  
  16. void updateFromElem(int elemIndex, vector<int>* button, vector<int>* ans, queue<int>* q) {
  17. adjust(&elemIndex);
  18.  
  19. for(int i=0; i<button->size(); i++){
  20. int thisButton = button->at(i);
  21. int curIndex = elemIndex + thisButton;
  22.  
  23. adjust(&curIndex);
  24. if(curIndex == 0) continue;
  25.  
  26. if(ans->at(curIndex) == -1){
  27. q->push(curIndex);
  28. ans->at(curIndex) = ans->at(elemIndex) + 1;
  29. }
  30. }
  31. }
  32.  
  33. void doEachInstance(){
  34. //////// Initialization (you can skip it)
  35. int numButtons, targetTime;
  36. cin>>numButtons>>targetTime;
  37.  
  38. button->resize(numButtons);
  39. ans->resize(maxValue+1);
  40. for (int i=0; i<maxValue+1; i++){
  41. ans->at(i) = -1;
  42. }
  43.  
  44. q = new queue<int>();
  45. for(int i=0; i<numButtons; i++){
  46. cin>>button->at(i);
  47. }
  48. //////// UP TO THIS POINT
  49.  
  50. // DFS:
  51. ans->at(0) = 0;
  52. while(!q->empty()){
  53. int elem = q->front(); q->pop();
  54. updateFromElem(elem, button, ans, q);
  55. }
  56.  
  57. // Extracting Answer:
  58. for(int i=targetTime; i<=maxValue; i++){
  59. if(ans->at(i) != -1){
  60. cout<<ans->at(i)<<" "<<i-targetTime<<endl;
  61. return;
  62. }
  63. }
  64. }
  65.  
  66. int main(){
  67. int T; cin>>T;
  68. for(int i=0; i<T; i++)
  69. doEachInstance();
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement