import java.util.LinkedList; import java.util.List; public class cie { private static final class Range { private final int left; private final int right; private final int number; public Range(int left, int right, int number) { this.left = left; this.right = right; this.number = number; } public int getLeft() { return left; } public int getRight() { return right; } public int getNumber() { return number; } } public static final void solve() { int d = cielib.podajD(); int r = cielib.podajR(); int k = cielib.podajK(); Range[] ranges = new Range[d]; for (int i = 0; i < ranges.length; i++) { ranges[i] = new Range(0, r, i); } int counter = 0; while (!areAllRangesAtMost2(ranges)) { Range widest = findWidest(ranges); int[] coordinatesOfMidPointOnLeft = getCoordinatesOfMidPointOnLeftSide(widest, ranges); int[] coordinatesOfMidPointOnRight = getCoordinatesOfMidPointOnRightSide(widest, ranges); cielib.czyCieplo(coordinatesOfMidPointOnLeft); int isRightHot = cielib.czyCieplo(coordinatesOfMidPointOnRight); int newBoundary = widest.getLeft() + (widest.getRight() - widest.getLeft() + 1)/2; if (isRightHot == 1) { ranges[widest.getNumber()] = new Range(newBoundary, widest.getRight(), widest.getNumber()); } else { ranges[widest.getNumber()] = new Range(widest.getLeft(), newBoundary, widest.getNumber()); } counter += 2; if (counter > k) { System.out.println("Too many requests"); } } if (allRangesOne(ranges)) { cielib.znalazlem(translateToCoordinates(ranges)); } else { int[] indexesForBinary = getTwoIndex(ranges); int[] result = translateToCoordinates(ranges); cielib.czyCieplo(result.clone()); counter++; if (counter > k) { System.out.println("Too many requests"); } for(long i = 1; i < 1 << indexesForBinary.length; i++) { for (int j = 0; j < indexesForBinary.length; j++) { if ((i & 1 << j) != 0) { result[indexesForBinary[j]] = ranges[indexesForBinary[j]].getRight(); if (cielib.czyCieplo(result.clone()) == 1) { cielib.znalazlem(result); counter++; if (counter > k) { System.out.println("Too many requests"); } return; } } } } cielib.znalazlem(translateToCoordinates(ranges)); } } private static final boolean allRangesOne(Range[] ranges) { for (Range range : ranges) { int size = range.getRight() - range.getLeft() + 1; if (size > 1) { return false; } } return true; } private static final int[] getTwoIndex(Range[] ranges) { List<Integer> indexes = new LinkedList<>(); for (int i = 0; i < ranges.length; i++) { if (ranges[i].getRight() - ranges[i].getLeft() + 1 == 2) { indexes.add(i); } } return indexes.stream().sequential().mapToInt(Integer::valueOf).toArray(); } private static final int[] translateToCoordinates(Range[] ranges) { int[] coordinates = new int[ranges.length]; for (int i = 0; i < ranges.length; i++) { coordinates[i] = ranges[i].getLeft(); } return coordinates; } private final static int[] getCoordinatesOfMidPointOnLeftSide(Range widest, Range[] ranges) { int[] coordinates = new int[ranges.length]; for (int i = 0; i < ranges.length; i++) { coordinates[i] = ranges[i].getLeft() + (ranges[i].getRight() - ranges[i].getLeft()) / 2; } coordinates[widest.getNumber()] = widest.getLeft(); return coordinates; } private final static int[] getCoordinatesOfMidPointOnRightSide(Range widest, Range[] ranges) { int[] coordinates = new int[ranges.length]; for (int i = 0; i < ranges.length; i++) { coordinates[i] = ranges[i].getLeft() + (ranges[i].getRight() - ranges[i].getLeft()) / 2; } coordinates[widest.getNumber()] = widest.getRight(); return coordinates; } private final static Range findWidest(Range[] ranges) { Range widest = ranges[0]; for (int i = 1; i < ranges.length; i++) { if (widest.getRight() - widest.getLeft() < ranges[i].getRight() - ranges[i].getLeft()) { widest = ranges[i]; } } return widest; } private final static boolean areAllRangesAtMost2(Range[] ranges) { for (Range range : ranges) { int size = range.getRight() - range.getLeft() + 1; if (size > 2) { return false; } } return true; } public static void main(String[] args) { solve(); } }
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | import java.util.LinkedList; import java.util.List; public class cie { private static final class Range { private final int left; private final int right; private final int number; public Range(int left, int right, int number) { this.left = left; this.right = right; this.number = number; } public int getLeft() { return left; } public int getRight() { return right; } public int getNumber() { return number; } } public static final void solve() { int d = cielib.podajD(); int r = cielib.podajR(); int k = cielib.podajK(); Range[] ranges = new Range[d]; for (int i = 0; i < ranges.length; i++) { ranges[i] = new Range(0, r, i); } int counter = 0; while (!areAllRangesAtMost2(ranges)) { Range widest = findWidest(ranges); int[] coordinatesOfMidPointOnLeft = getCoordinatesOfMidPointOnLeftSide(widest, ranges); int[] coordinatesOfMidPointOnRight = getCoordinatesOfMidPointOnRightSide(widest, ranges); cielib.czyCieplo(coordinatesOfMidPointOnLeft); int isRightHot = cielib.czyCieplo(coordinatesOfMidPointOnRight); int newBoundary = widest.getLeft() + (widest.getRight() - widest.getLeft() + 1)/2; if (isRightHot == 1) { ranges[widest.getNumber()] = new Range(newBoundary, widest.getRight(), widest.getNumber()); } else { ranges[widest.getNumber()] = new Range(widest.getLeft(), newBoundary, widest.getNumber()); } counter += 2; if (counter > k) { System.out.println("Too many requests"); } } if (allRangesOne(ranges)) { cielib.znalazlem(translateToCoordinates(ranges)); } else { int[] indexesForBinary = getTwoIndex(ranges); int[] result = translateToCoordinates(ranges); cielib.czyCieplo(result.clone()); counter++; if (counter > k) { System.out.println("Too many requests"); } for(long i = 1; i < 1 << indexesForBinary.length; i++) { for (int j = 0; j < indexesForBinary.length; j++) { if ((i & 1 << j) != 0) { result[indexesForBinary[j]] = ranges[indexesForBinary[j]].getRight(); if (cielib.czyCieplo(result.clone()) == 1) { cielib.znalazlem(result); counter++; if (counter > k) { System.out.println("Too many requests"); } return; } } } } cielib.znalazlem(translateToCoordinates(ranges)); } } private static final boolean allRangesOne(Range[] ranges) { for (Range range : ranges) { int size = range.getRight() - range.getLeft() + 1; if (size > 1) { return false; } } return true; } private static final int[] getTwoIndex(Range[] ranges) { List<Integer> indexes = new LinkedList<>(); for (int i = 0; i < ranges.length; i++) { if (ranges[i].getRight() - ranges[i].getLeft() + 1 == 2) { indexes.add(i); } } return indexes.stream().sequential().mapToInt(Integer::valueOf).toArray(); } private static final int[] translateToCoordinates(Range[] ranges) { int[] coordinates = new int[ranges.length]; for (int i = 0; i < ranges.length; i++) { coordinates[i] = ranges[i].getLeft(); } return coordinates; } private final static int[] getCoordinatesOfMidPointOnLeftSide(Range widest, Range[] ranges) { int[] coordinates = new int[ranges.length]; for (int i = 0; i < ranges.length; i++) { coordinates[i] = ranges[i].getLeft() + (ranges[i].getRight() - ranges[i].getLeft()) / 2; } coordinates[widest.getNumber()] = widest.getLeft(); return coordinates; } private final static int[] getCoordinatesOfMidPointOnRightSide(Range widest, Range[] ranges) { int[] coordinates = new int[ranges.length]; for (int i = 0; i < ranges.length; i++) { coordinates[i] = ranges[i].getLeft() + (ranges[i].getRight() - ranges[i].getLeft()) / 2; } coordinates[widest.getNumber()] = widest.getRight(); return coordinates; } private final static Range findWidest(Range[] ranges) { Range widest = ranges[0]; for (int i = 1; i < ranges.length; i++) { if (widest.getRight() - widest.getLeft() < ranges[i].getRight() - ranges[i].getLeft()) { widest = ranges[i]; } } return widest; } private final static boolean areAllRangesAtMost2(Range[] ranges) { for (Range range : ranges) { int size = range.getRight() - range.getLeft() + 1; if (size > 2) { return false; } } return true; } public static void main(String[] args) { solve(); } } |