Codeforces Round #725 (Div. 3)A. Stone Game
给一个数组, 可以从两端取数字, 取了就没了, 求最少多少步可以取了最大和最小值.
这题他妈的真费时间, 一共3种取法 (答案给的是四种), 反正我三种做的, 三种是: 从左往右取, 从右往左和两侧取.
这题给了最大值是n, 最小值是1, 所以1/2两种都很好做, 就是第三个, 需要找下两个值的坐标, 然后减去两端就行了.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
static class TaskA { public void solve(int testNumber, InputReader in, OutputWriter out) { int maxIndex = -1; int minIndex = -1; int n = in.readInt(); int[] ary = in.readIntArray(n); int left = 0, right = 0, both = 0; for (int i = 0; i < n; i++) { if (ary[i] == 1) minIndex = i; else if (ary[i] == n) maxIndex = i; } boolean one = false; boolean two = false; for (int i = 0; i < n; i++) { if (ary[i] == 1) one = true; else if (ary[i] == n) two = true; if (one && two) { left = i + 1; break; } } one = false; two = false; for (int i = n - 1; i >= 0; i--) { if (ary[i] == 1) one = true; else if (ary[i] == n) two = true; if (one && two) { right = n - i; break; } } both = Math.min(minIndex, maxIndex) + 1 + n - Math.max(minIndex, maxIndex); out.printLine(Math.min(Math.min(left, right), both)); } } |