Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. vector<string> split_string(string);
  6.  
  7. // Complete the kangaroo function below.
  8. string kangaroo(int x1, int v1, int x2, int v2) {
  9.  
  10. int flag = 1;
  11. map<int,int> m;
  12. m[x1] = v1;
  13. m[x2] = v2;
  14. map<int,int>::iterator it;
  15. it = m.begin();
  16. it++;
  17.  
  18. int first = m.begin()->first;
  19. int second = it->first;
  20. int min = second-first;
  21. while(flag ==1)
  22. {
  23. map<int,int>::iterator it2;
  24. it2 = m.begin();
  25. it++;
  26. first +=m.begin()->second;
  27. second +=it2->second;
  28. if(first==second)
  29. {
  30. cout<<"hii";
  31. return "YES";
  32. flag = 0;
  33. }
  34. cout<<min<<endl;
  35. min = second -first;
  36. if(min<0)
  37. {
  38. cout<<"bye";
  39. return "NO";
  40. flag = 0;
  41. }
  42.  
  43. }
  44. return "dummy";
  45.  
  46. }
  47.  
  48. int main()
  49. {
  50. ofstream fout(getenv("OUTPUT_PATH"));
  51.  
  52. string x1V1X2V2_temp;
  53. getline(cin, x1V1X2V2_temp);
  54.  
  55. vector<string> x1V1X2V2 = split_string(x1V1X2V2_temp);
  56.  
  57. int x1 = stoi(x1V1X2V2[0]);
  58.  
  59. int v1 = stoi(x1V1X2V2[1]);
  60.  
  61. int x2 = stoi(x1V1X2V2[2]);
  62.  
  63. int v2 = stoi(x1V1X2V2[3]);
  64.  
  65. string result = kangaroo(x1, v1, x2, v2);
  66.  
  67. fout << result << "\n";
  68.  
  69. fout.close();
  70.  
  71. return 0;
  72. }
  73.  
  74. vector<string> split_string(string input_string) {
  75. string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
  76. return x == y and x == ' ';
  77. });
  78.  
  79. input_string.erase(new_end, input_string.end());
  80.  
  81. while (input_string[input_string.length() - 1] == ' ') {
  82. input_string.pop_back();
  83. }
  84.  
  85. vector<string> splits;
  86. char delimiter = ' ';
  87.  
  88. size_t i = 0;
  89. size_t pos = input_string.find(delimiter);
  90.  
  91. while (pos != string::npos) {
  92. splits.push_back(input_string.substr(i, pos - i));
  93.  
  94. i = pos + 1;
  95. pos = input_string.find(delimiter, i);
  96. }
  97.  
  98. splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));
  99.  
  100. return splits;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement