fork(1) download
  1. #include <iostream>
  2. #include <cstdio>
  3.  
  4. using namespace std;
  5.  
  6. template <typename T>
  7. bool getNum(T &res) {
  8. res = 0;
  9. bool is_negative = false;
  10. char c;
  11.  
  12. while (true) {
  13. c = getchar_unlocked();
  14. if (c == EOF) return false;
  15. if (c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == ':') continue;
  16. else break;
  17. }
  18.  
  19. // Deteksi angka negatif
  20. if (c == '-') {
  21. is_negative = true;
  22. c = getchar_unlocked();
  23. }
  24.  
  25. // Parsing angka
  26. res = c - '0';
  27. while (true) {
  28. c = getchar_unlocked();
  29. if (c >= '0' && c <= '9')
  30. res = 10 * res + (c - '0');
  31. else
  32. break;
  33. }
  34.  
  35. if (is_negative) res = -res;
  36.  
  37. return true;
  38. }
  39.  
  40. int main() {
  41. int b;
  42. if (!getNum(b)) return 0;
  43.  
  44. for (int r = 1; r <= b; ++r) {
  45. int s;
  46. getNum(s);
  47.  
  48. int max_sum = 0;
  49. int current_sum = 0;
  50.  
  51. int current_start = 1;
  52. int best_start = -1;
  53. int best_end = -1;
  54. int best_length = -1;
  55.  
  56. for (int i = 1; i < s; ++i) {
  57. int niceness;
  58. getNum(niceness); // Membaca nilai jalan dengan Fast I/O
  59.  
  60. current_sum += niceness;
  61.  
  62. if (current_sum > max_sum) {
  63. max_sum = current_sum;
  64. best_start = current_start;
  65. best_end = i + 1;
  66. best_length = best_end - best_start;
  67. }
  68. else if (current_sum == max_sum && max_sum > 0) {
  69. int current_length = (i + 1) - current_start;
  70.  
  71. if (current_length > best_length) {
  72. best_start = current_start;
  73. best_end = i + 1;
  74. best_length = current_length;
  75. }
  76. }
  77.  
  78. if (current_sum < 0) {
  79. current_sum = 0;
  80. current_start = i + 1;
  81. }
  82. }
  83.  
  84. if (max_sum > 0) {
  85. cout << "The nicest part of route " << r
  86. << " is between stops " << best_start
  87. << " and " << best_end << "\n";
  88. } else {
  89. cout << "Route " << r << " has no nice parts\n";
  90. }
  91. }
  92.  
  93. return 0;
  94. }
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
Standard output is empty