Submission #416357

#TimeUsernameProblemLanguageResultExecution timeMemory
416357model_code Diamond Hands (innopolis2021_final_B)Java
100 / 100
879 ms32704 KiB
import java.io.*; import java.util.*; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); Polyline solver = new Polyline(); solver.solve(1, in, out); out.close(); } static class Polyline { public void solve(int testNumber, FastScanner in, PrintWriter out) { int n = in.nextInt() + 1; int[] x = new int[n]; int[] y = new int[n]; for (int i = 1; i < n; i++) { x[i] = in.nextInt(); y[i] = in.nextInt(); } for (int i = 1; i < n; i++) { if ((x[i] + y[i] + x[0] + y[0]) % 2 != 0) { out.println(-1); return; } if (Math.abs(y[i] - y[i - 1]) > x[i] - x[i - 1]) { out.println(-1); return; } } int INF = Integer.MAX_VALUE / 2; int[][] dp = new int[2][n]; int[][] pr = new int[2][n]; Arrays.fill(dp[0], INF); Arrays.fill(dp[1], INF); dp[0][0] = 1; dp[1][0] = 1; // d = 0 -> dy/dx = -1 // d = 1 -> dy/dx = 1 for (int i = 0; i < n - 1; i++) { for (int d = 0; d < 2; d++) { int dpVal = dp[d][i]; if (y[i + 1] - y[i] == (x[i + 1] - x[i]) * (2 * d - 1) && dp[d][i + 1] > dpVal) { dp[d][i + 1] = dpVal; pr[d][i + 1] = d; } if (Math.abs(y[i + 1] - y[i]) < (x[i + 1] - x[i]) && dp[d][i + 1] > dpVal + 2) { dp[d][i + 1] = dpVal + 2; pr[d][i + 1] = d; } if (dp[d ^ 1][i + 1] > dpVal + 1) { dp[d ^ 1][i + 1] = dpVal + 1; pr[d ^ 1][i + 1] = d; } } } List<Integer> ansX = new ArrayList<>(), ansY = new ArrayList<>(); ansX.add(x[n - 1]); ansY.add(y[n - 1]); int d = dp[0][n - 1] < dp[1][n - 1] ? 0 : 1; for (int i = n - 1; i > 0; i--) { int prevD = pr[d][i]; if (d == prevD) { if (y[i] - y[i - 1] == (x[i] - x[i - 1]) * (2 * d - 1)) { // skip point } else { // two intermediate points int delta = (y[i] - (y[i - 1] + (2 * prevD - 1) * (x[i] - x[i - 1]))) / -(2 * prevD - 1); if (delta % 2 != 0 || delta < 0) { throw new AssertionError(); } ansX.add(x[i - 1] + delta / 2); ansY.add(y[i - 1] - delta / 2 * (2 * d - 1)); ansX.add(x[i - 1]); ansY.add(y[i - 1]); } } else { int delta = (y[i] - (y[i - 1] + (2 * prevD - 1) * (x[i] - x[i - 1]))) / -(2 * prevD - 1); if (delta % 2 != 0 || delta < 0) { throw new AssertionError(); } ansX.add(x[i] - delta / 2); ansY.add(y[i] - delta / 2 * (2 * d - 1)); } d = prevD; } ansX.add(x[0]); ansY.add(y[0]); Collections.reverse(ansX); Collections.reverse(ansY); out.println(ansX.size() - 1); for (int i = 0; i + 1 < ansX.size(); i++) { int l = ansX.get(i + 1) - ansX.get(i); char c = ansY.get(i + 1) > ansY.get(i) ? '+' : '-'; out.println(l + " " + c); } } } static class FastScanner { public BufferedReader br; public StringTokenizer st; public FastScanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } public FastScanner(String fileName) { try { br = new BufferedReader(new FileReader(fileName)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public int nextInt() { return Integer.parseInt(next()); } public String next() { while (st == null || !st.hasMoreElements()) { String line = null; try { line = br.readLine(); } catch (IOException e) { throw new UnknownError(); } if (line == null) { throw new UnknownError(); } st = new StringTokenizer(line); } return st.nextToken(); } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...