Guest User

Untitled

a guest
May 25th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 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 climbingLeaderboard function below.
  8. vector<int> climbingLeaderboard(vector<int> scores, vector<int> alice) {
  9. vector <int> ans;
  10. int prev =-1;
  11. int i=alice.size()-1,j=0,pos=0;
  12. while(i>=0 & j<scores.size()){
  13. if(scores[j]!=prev){
  14. pos++;
  15. }
  16. prev=scores[j];
  17. if(alice[i]==scores[j]) {ans.push_back(pos); i--;}
  18. else if(alice[i]>scores[j]) {ans.push_back(pos); i--;}
  19. else j++;
  20. }
  21. while(i>=0){
  22. if(alice[i]==prev)
  23. ans.push_back(pos);
  24. else
  25. ans.push_back(pos+++1);
  26. prev=alice[i];
  27. i--;
  28. }
  29. reverse(ans.begin(),ans.end());
  30. return ans;
  31.  
  32. }
  33. //Hackerrank's pre-written code
  34. int main()
  35. {
  36. ofstream fout(getenv("OUTPUT_PATH"));
  37.  
  38. int scores_count;
  39. cin >> scores_count;
  40. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  41.  
  42. string scores_temp_temp;
  43. getline(cin, scores_temp_temp);
  44.  
  45. vector<string> scores_temp = split_string(scores_temp_temp);
  46.  
  47. vector<int> scores(scores_count);
  48.  
  49. for (int i = 0; i < scores_count; i++) {
  50. int scores_item = stoi(scores_temp[i]);
  51.  
  52. scores[i] = scores_item;
  53. }
  54.  
  55. int alice_count;
  56. cin >> alice_count;
  57. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  58.  
  59. string alice_temp_temp;
  60. getline(cin, alice_temp_temp);
  61.  
  62. vector<string> alice_temp = split_string(alice_temp_temp);
  63.  
  64. vector<int> alice(alice_count);
  65.  
  66. for (int i = 0; i < alice_count; i++) {
  67. int alice_item = stoi(alice_temp[i]);
  68.  
  69. alice[i] = alice_item;
  70. }
  71.  
  72. vector<int> result = climbingLeaderboard(scores, alice);
  73.  
  74. for (int i = 0; i < result.size(); i++) {
  75. fout << result[i];
  76.  
  77. if (i != result.size() - 1) {
  78. fout << "\n";
  79. }
  80. }
  81.  
  82. fout << "\n";
  83.  
  84. fout.close();
  85.  
  86. return 0;
  87. }
  88.  
  89. vector<string> split_string(string input_string) {
  90. string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
  91. return x == y and x == ' ';
  92. });
  93.  
  94. input_string.erase(new_end, input_string.end());
  95.  
  96. while (input_string[input_string.length() - 1] == ' ') {
  97. input_string.pop_back();
  98. }
  99.  
  100. vector<string> splits;
  101. char delimiter = ' ';
  102.  
  103. size_t i = 0;
  104. size_t pos = input_string.find(delimiter);
  105.  
  106. while (pos != string::npos) {
  107. splits.push_back(input_string.substr(i, pos - i));
  108.  
  109. i = pos + 1;
  110. pos = input_string.find(delimiter, i);
  111. }
  112.  
  113. splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));
  114.  
  115. return splits;
  116. }
Add Comment
Please, Sign In to add comment