fork download
  1. https://w...content-available-to-author-only...k.com/contests/hack-the-interview-ii-global/challenges/distribution-in-m-bins/submissions/code/1323377015#include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. string ltrim(const string &);
  6. string rtrim(const string &);
  7. vector<string> split(const string &);
  8.  
  9. /*
  10.  * Complete the 'maxScore' function below.
  11.  *
  12.  * The function is expected to return an INTEGER.
  13.  * The function accepts following parameters:
  14.  * 1. INTEGER_ARRAY a
  15.  * 2. INTEGER m
  16.  */
  17.  
  18. int maxScore(vector<int> arr, int m) {
  19. int n = arr.size();
  20.  
  21.  
  22. sort(arr.begin(), arr.end());
  23.  
  24.  
  25. long long ans = 0;
  26. int seg_num = 1, seg_size = 0;
  27. map <int, long long> segments;
  28.  
  29. for (int i = 0; i < n; i++) {
  30. if (seg_size < m)
  31. {
  32. seg_size++;
  33.  
  34. }
  35. else {
  36. seg_num++;
  37. seg_size = 1;
  38.  
  39. }
  40.  
  41. segments[seg_num] += arr[i];
  42. }
  43.  
  44.  
  45. if (seg_size < m) {
  46. segments[seg_num - 1] += segments[seg_num];
  47. segments[seg_num] = 0;
  48. }
  49.  
  50. int cnt = 1;
  51. for (auto it = segments.begin(); it != segments.end(); it++, cnt++) {
  52. ans += (cnt * it->second) % 1000000007LL;
  53. }
  54.  
  55. return ans;
  56. }
  57.  
  58. int main()
  59. {
  60. ofstream fout(getenv("OUTPUT_PATH"));
  61.  
  62. string first_multiple_input_temp;
  63. getline(cin, first_multiple_input_temp);
  64.  
  65. vector<string> first_multiple_input = split(rtrim(first_multiple_input_temp));
  66.  
  67. int n = stoi(first_multiple_input[0]);
  68.  
  69. int m = stoi(first_multiple_input[1]);
  70.  
  71. string a_temp_temp;
  72. getline(cin, a_temp_temp);
  73.  
  74. vector<string> a_temp = split(rtrim(a_temp_temp));
  75.  
  76. vector<int> a(n);
  77.  
  78. for (int i = 0; i < n; i++) {
  79. int a_item = stoi(a_temp[i]);
  80.  
  81. a[i] = a_item;
  82. }
  83.  
  84. int ans = maxScore(a, m);
  85.  
  86. fout << ans << "\n";
  87.  
  88. fout.close();
  89.  
  90. return 0;
  91. }
  92.  
  93. string ltrim(const string &str) {
  94. string s(str);
  95.  
  96. s.erase(
  97. s.begin(),
  98. find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
  99. );
  100.  
  101. return s;
  102. }
  103.  
  104. string rtrim(const string &str) {
  105. string s(str);
  106.  
  107. s.erase(
  108. find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
  109. s.end()
  110. );
  111.  
  112. return s;
  113. }
  114.  
  115. vector<string> split(const string &str) {
  116. vector<string> tokens;
  117.  
  118. string::size_type start = 0;
  119. string::size_type end = 0;
  120.  
  121. while ((end = str.find(" ", start)) != string::npos) {
  122. tokens.push_back(str.substr(start, end - start));
  123.  
  124. start = end + 1;
  125. }
  126.  
  127. tokens.push_back(str.substr(start));
  128.  
  129. return tokens;
  130. }
  131.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:1: error: ‘https’ does not name a type
 https://www.hackerrank.com/contests/hack-the-interview-ii-global/challenges/distribution-in-m-bins/submissions/code/1323377015#include <bits/stdc++.h>
 ^~~~~
prog.cpp:5:1: error: ‘string’ does not name a type; did you mean ‘struct’?
 string ltrim(const string &);
 ^~~~~~
 struct
prog.cpp:6:1: error: ‘string’ does not name a type; did you mean ‘struct’?
 string rtrim(const string &);
 ^~~~~~
 struct
prog.cpp:7:1: error: ‘vector’ does not name a type
 vector<string> split(const string &);
 ^~~~~~
prog.cpp:18:14: error: ‘vector’ was not declared in this scope
 int maxScore(vector<int> arr, int m) {
              ^~~~~~
prog.cpp:18:21: error: expected primary-expression before ‘int’
 int maxScore(vector<int> arr, int m) {
                     ^~~
prog.cpp:18:31: error: expected primary-expression before ‘int’
 int maxScore(vector<int> arr, int m) {
                               ^~~
prog.cpp:18:36: error: expression list treated as compound expression in initializer [-fpermissive]
 int maxScore(vector<int> arr, int m) {
                                    ^
prog.cpp: In function ‘int main()’:
prog.cpp:60:5: error: ‘ofstream’ was not declared in this scope
     ofstream fout(getenv("OUTPUT_PATH"));
     ^~~~~~~~
prog.cpp:62:5: error: ‘string’ was not declared in this scope
     string first_multiple_input_temp;
     ^~~~~~
prog.cpp:62:5: note: suggested alternative: ‘struct’
     string first_multiple_input_temp;
     ^~~~~~
     struct
prog.cpp:63:13: error: ‘cin’ was not declared in this scope
     getline(cin, first_multiple_input_temp);
             ^~~
prog.cpp:63:13: note: suggested alternative: ‘main’
     getline(cin, first_multiple_input_temp);
             ^~~
             main
prog.cpp:63:18: error: ‘first_multiple_input_temp’ was not declared in this scope
     getline(cin, first_multiple_input_temp);
                  ^~~~~~~~~~~~~~~~~~~~~~~~~
prog.cpp:63:5: error: ‘getline’ was not declared in this scope
     getline(cin, first_multiple_input_temp);
     ^~~~~~~
prog.cpp:65:5: error: ‘vector’ was not declared in this scope
     vector<string> first_multiple_input = split(rtrim(first_multiple_input_temp));
     ^~~~~~
prog.cpp:65:20: error: ‘first_multiple_input’ was not declared in this scope
     vector<string> first_multiple_input = split(rtrim(first_multiple_input_temp));
                    ^~~~~~~~~~~~~~~~~~~~
prog.cpp:65:49: error: ‘rtrim’ was not declared in this scope
     vector<string> first_multiple_input = split(rtrim(first_multiple_input_temp));
                                                 ^~~~~
prog.cpp:65:43: error: ‘split’ was not declared in this scope
     vector<string> first_multiple_input = split(rtrim(first_multiple_input_temp));
                                           ^~~~~
prog.cpp:67:13: error: ‘stoi’ was not declared in this scope
     int n = stoi(first_multiple_input[0]);
             ^~~~
prog.cpp:67:13: note: suggested alternative: ‘std’
     int n = stoi(first_multiple_input[0]);
             ^~~~
             std
prog.cpp:71:11: error: expected ‘;’ before ‘a_temp_temp’
     string a_temp_temp;
           ^~~~~~~~~~~~
           ;
prog.cpp:72:18: error: ‘a_temp_temp’ was not declared in this scope
     getline(cin, a_temp_temp);
                  ^~~~~~~~~~~
prog.cpp:74:20: error: ‘a_temp’ was not declared in this scope
     vector<string> a_temp = split(rtrim(a_temp_temp));
                    ^~~~~~
prog.cpp:76:12: error: expected primary-expression before ‘int’
     vector<int> a(n);
            ^~~
prog.cpp:81:9: error: ‘a’ was not declared in this scope
         a[i] = a_item;
         ^
prog.cpp:84:24: error: ‘a’ was not declared in this scope
     int ans = maxScore(a, m);
                        ^
prog.cpp:84:28: error: ‘maxScore’ cannot be used as a function
     int ans = maxScore(a, m);
                            ^
prog.cpp:86:5: error: ‘fout’ was not declared in this scope
     fout << ans << "\n";
     ^~~~
prog.cpp:86:5: note: suggested alternative: ‘float’
     fout << ans << "\n";
     ^~~~
     float
prog.cpp: At global scope:
prog.cpp:93:1: error: ‘string’ does not name a type; did you mean ‘struct’?
 string ltrim(const string &str) {
 ^~~~~~
 struct
prog.cpp:104:1: error: ‘string’ does not name a type; did you mean ‘struct’?
 string rtrim(const string &str) {
 ^~~~~~
 struct
prog.cpp:115:1: error: ‘vector’ does not name a type
 vector<string> split(const string &str) {
 ^~~~~~
stdout
Standard output is empty