Advertisement
DANIKA__

Untitled

Apr 9th, 2020
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace CodeForces
  7. {
  8. class Program
  9. {
  10. public static void Main()
  11. {
  12. string input = Console.ReadLine();
  13. int n = Convert.ToInt32(input.Split()[0]);
  14. int m = Convert.ToInt32(input.Split()[1]);
  15. int time = 0;
  16. int startIndex = 0;
  17. int c = 0;
  18. int rightStairs = m + 1;
  19. string[] str = new string[n];
  20. for (int i = n - 1; i >= 0; i--)
  21. {
  22. str[i] = Console.ReadLine();
  23. }
  24. for (int i = 0; i < n; i++)
  25. {
  26. if (startIndex == 0)
  27. {
  28. int tempTime = time;
  29. input = str[i];
  30. int indexOfChar = input.IndexOf('1', startIndex);
  31. while (indexOfChar != -1)
  32. {
  33. time += indexOfChar - startIndex;
  34. startIndex = indexOfChar + 1;
  35. indexOfChar = input.IndexOf('1', startIndex);
  36. startIndex--;
  37. }
  38. if (tempTime == time)
  39. {
  40. time += -c - 1;
  41. break;
  42. }
  43. else if (i + 1 == n)
  44. {
  45. break;
  46. }
  47. if (rightStairs - startIndex < startIndex)
  48. {
  49. time += rightStairs - startIndex;
  50. c = rightStairs - startIndex;
  51. startIndex = rightStairs;
  52. }
  53. else
  54. {
  55. time += startIndex;
  56. c = startIndex;
  57. startIndex = 0;
  58. }
  59. time++;
  60. }
  61. else
  62. {
  63. int tempTime = time;
  64. input = str[i];
  65. int indexOfChar = input.LastIndexOf('1', startIndex);
  66. while (indexOfChar != -1)
  67. {
  68. time += startIndex - indexOfChar;
  69. startIndex = indexOfChar - 1;
  70. indexOfChar = input.LastIndexOf('1', startIndex);
  71. startIndex++;
  72. }
  73. if (tempTime == time)
  74. {
  75. time += -c - 1;
  76. break;
  77. }
  78. else if (i + 1 == n)
  79. {
  80. break;
  81. }
  82. else if (rightStairs - startIndex < startIndex)
  83. {
  84. time += rightStairs - startIndex;
  85. c = rightStairs - startIndex;
  86. startIndex = rightStairs;
  87. }
  88. else
  89. {
  90. time += startIndex;
  91. c = startIndex;
  92. startIndex = 0;
  93. }
  94. time++;
  95. }
  96. }
  97. if (time != -1)
  98. Console.WriteLine(time);
  99. else Console.WriteLine(0);
  100. }
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement