fork download
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <algorithm>
  5. #include <vector>
  6. using namespace std;
  7.  
  8.  
  9. typedef struct inputs {
  10. long number;
  11. char** segments;
  12. }inputs;
  13.  
  14. //Find the maximum lenght of the rope loop possible
  15. long maximum_length(inputs ropes) {
  16. vector<long> blue;
  17. vector<long> red;
  18. long count_blue = 0, count_red = 0 , total_length = 0;
  19.  
  20. for (long i = 0; i < ropes.number; i ++ ) {
  21. red.push_back(0);
  22. blue.push_back(0);
  23. if (ropes.segments[i][1] == 'R') {
  24. red[i] += (ropes.segments[i][0] - '0');
  25. count_red++;
  26. }
  27. else if (ropes.segments[i][1] == 'B') {
  28. blue[i] += (ropes.segments[i][0] - '0');
  29. count_blue++;
  30. }
  31. }
  32.  
  33.  
  34. if (count_red == 0 || count_blue == 0) {
  35. return 0;
  36. } else {
  37. sort(blue.begin(), blue.begin()+count_blue);
  38. sort(red.begin(),red.begin()+count_red);
  39.  
  40. if (count_red < count_blue) {
  41. for (long i = 0; i < count_red; i++) {
  42. total_length += blue[count_blue - i - 1] + red[i];
  43. }
  44. } else {
  45. for (long i = 0; i < count_blue; i++) {
  46. total_length += red[count_red - i - 1] + blue[i];
  47. }
  48. }
  49. return total_length;
  50. }
  51.  
  52. }
  53. //Starting with the blue, add a red and a blue, alternatively until u end on a red
  54. //and the number of blue and red is the same... (rope loop)
  55.  
  56. //Store the lengths of the blue ropes and red ropes each in a sorted array
  57.  
  58.  
  59.  
  60.  
  61. int main () {
  62. long no_cases;
  63. long maximum_length(inputs ropes);
  64. cin >> no_cases;
  65. inputs* ropes;
  66. ropes = (inputs*) calloc(no_cases, sizeof(inputs));
  67.  
  68. //Read the inputs and stores them into an array of struct
  69. for (long i = 0; i < no_cases; i ++ ) {
  70. cin >> ropes[i].number;
  71. ropes[i].segments = (char**)calloc(ropes[i].number, sizeof(char*));
  72. for (long j = 0; j < ropes[i].number; j ++ ) {
  73. cin >> ropes[i].segments[j];
  74. }
  75. }
  76. //For each case, prints the maximum length of the rope
  77. for (long i = 0; i < no_cases; i ++) {
  78. cout << "Case #" << i + 1 << ":" << maximum_length(ropes[i]) << endl;
  79. }
  80.  
  81. return 0;
  82. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty