Submission #333135

# Submission time Handle Problem Language Result Execution time Memory
333135 2020-12-04T17:55:04 Z KWang31 Split the sequence (APIO14_sequence) Java 11
Compilation error
0 ms 0 KB
//By Spontaneous Combustion, just used for test
import java.io.*;
import java.util.*;

public class Main {
	public static void main(final String[] args) throws IOException {
		final int N = readInt();
		final int K = readInt();
		final long[] sum = new long[N + 1];
		for (int i = 1; i <= N; i++) {
			sum[i] = sum[i - 1] + readInt();
		}
		final LineContainer lines = new LineContainer(N);
		final long[][] dp = new long[2][N];
		for (int i = 1; i < N; i++) {
			dp[1][i] = sum[i] * (sum[N] - sum[i]);
		}
		final int[][] prevSplit = new int[K][N];
		for (int i = 1, curr = 0, prev = 1; i < K; i++, curr ^= 1, prev ^= 1) {
			lines.clear();
			for (int j = i + 1; j < N; j++) {
				lines.addLine(j - 1, sum[j - 1], -sum[j - 1] * sum[N] + dp[prev][j - 1]);
				dp[curr][j] = sum[j] * sum[N] - sum[j] * sum[j] + lines.maxValue(sum[j]);
				prevSplit[i][j] = lines.getCurrentIndex();
			}
		}
		long maxPoints = Long.MIN_VALUE;
		int idx = -1;
		int curr = K & 1;
		for (int i = K; i < N; i++) {
			if (dp[curr][i] > maxPoints) {
				maxPoints = dp[curr][i];
				idx = i;
			}
		}
		System.out.println(maxPoints);
		for (int i = idx, splitsLeft = K; splitsLeft > 0; splitsLeft--, i = prevSplit[splitsLeft][i]) {
			System.out.print(i + " ");
		}
		System.out.println();
	}

	private static final class LineContainer {
		private final long[] slopes;
		private final long[] yInts;
		private final int[] indexes;
		private int lines = 0;
		private int idx = 0;

		public LineContainer(final int capacity) {
			this.slopes = new long[capacity];
			this.yInts = new long[capacity];
			this.indexes = new int[capacity];
		}

		public void addLine(final int index, final long slope, final long yInt) {
			if (lines > 0 && slopes[lines - 1] == slope && yInts[lines - 1] == yInt) {
				return;
			}
			while (lines >= 2 && intersectionX(slopes[lines - 2], yInts[lines - 2], slope,
					yInt) < intersectionX(slopes[lines - 2], yInts[lines - 2], slopes[lines - 1], yInts[lines - 1])) {
				--lines;
			}
			indexes[lines] = index;
			slopes[lines] = slope;
			yInts[lines++] = yInt;
		}

		public long maxValue(final long x) {
			while (idx + 1 < lines && slopes[idx + 1] * x + yInts[idx + 1] >= slopes[idx] * x + yInts[idx]) {
				++idx;
			}
			return slopes[idx] * x + yInts[idx];
		}

		public int getCurrentIndex() {
			return indexes[idx];
		}

		public void clear() {
			idx = lines = 0;
		}

		private static long intersectionX(final long slope1, final long yInt1, final long slope2, final long yInt2) {
			return (yInt2 - yInt1) / (slope1 - slope2);
		}
	}

	private static final int BUFFER_SIZE = 1 << 16;
	private static final InputStream in = System.in;
	private static final byte[] buffer = new byte[BUFFER_SIZE];
	private static int bufferPointer = 0, bytesRead = 0;

	public static String read() throws IOException {
		final StringBuilder sb = new StringBuilder();
		byte c = Read();
		while (c <= ' ') {
			c = Read();
		}
		do {
			sb.append((char) c);
			c = Read();
		} while (c != -1 && c != ' ' && c != '\n' && c != '\r');
		return sb.toString();
	}

	public static String read(final int length) throws IOException {
		final byte[] ret = new byte[length];
		int idx = 0;
		byte c = Read();
		while (c <= ' ') {
			c = Read();
		}
		do {
			ret[idx++] = c;
			c = Read();
		} while (c != -1 && c != ' ' && c != '\n' && c != '\r');
		return new String(ret, 0, idx);
	}

	public static String[] readStringArray(final int strings) throws IOException {
		final String[] arr = new String[strings];
		for (int i = 0; i < strings; i++) {
			arr[i] = read();
		}
		return arr;
	}

	public static String readLine() throws IOException {
		final StringBuilder sb = new StringBuilder();
		byte c = Read();
		while (c == '\n' || c == '\r') {
			c = Read();
		}
		do {
			sb.append((char) c);
			c = Read();
		} while (c != -1 && c != '\n' && c != '\r');
		return sb.toString();
	}

	public static char readChar() throws IOException {
		byte c = Read();
		while (Character.isWhitespace(c)) {
			c = Read();
		}
		return (char) c;
	}

	private static char[] readCharArray(final int chars) throws IOException {
		final char[] arr = new char[chars];
		for (int i = 0; i < chars; i++) {
			arr[i] = readChar();
		}
		return arr;
	}

	public static java.math.BigInteger readBigInteger() throws IOException {
		return new java.math.BigInteger(read());
	}

	public static java.math.BigDecimal readBigDecimal() throws IOException {
		return new java.math.BigDecimal(read());
	}

	public static byte readByte() throws IOException {
		byte ret = 0;
		byte c = Read();
		while (c <= ' ')
			c = Read();
		boolean neg = (c == '-');
		if (neg)
			c = Read();
		do {
			ret = (byte) (ret * 10 + c - '0');
		} while ((c = Read()) >= '0' && c <= '9');
		if (neg)
			return (byte) -ret;
		return ret;
	}

	public static byte[] readByteArray(final int numbers) throws IOException {
		final byte[] arr = new byte[numbers];
		for (int i = 0; i < numbers; i++) {
			arr[i] = readByte();
		}
		return arr;
	}

	public static short readShort() throws IOException {
		short ret = 0;
		byte c = Read();
		while (c <= ' ')
			c = Read();
		boolean neg = (c == '-');
		if (neg)
			c = Read();
		do {
			ret = (short) (ret * 10 + c - '0');
		} while ((c = Read()) >= '0' && c <= '9');
		if (neg)
			return (short) -ret;
		return ret;
	}

	public static short[] readShortArray(final int numbers) throws IOException {
		final short[] arr = new short[numbers];
		for (int i = 0; i < numbers; i++) {
			arr[i] = readShort();
		}
		return arr;
	}

	public static int readInt() throws IOException {
		int ret = 0;
		byte c = Read();
		while (c <= ' ')
			c = Read();
		boolean neg = (c == '-');
		if (neg)
			c = Read();
		do {
			ret = ret * 10 + c - '0';
		} while ((c = Read()) >= '0' && c <= '9');
		if (neg)
			return -ret;
		return ret;
	}

	public static int[] readIntArray(final int numbers) throws IOException {
		final int[] arr = new int[numbers];
		for (int i = 0; i < numbers; i++) {
			arr[i] = readInt();
		}
		return arr;
	}

	public static long readLong() throws IOException {
		long ret = 0;
		byte c = Read();
		while (c <= ' ')
			c = Read();
		boolean neg = (c == '-');
		if (neg)
			c = Read();
		do {
			ret = ret * 10 + c - '0';
		} while ((c = Read()) >= '0' && c <= '9');
		if (neg)
			return -ret;
		return ret;
	}

	public static long[] readLongArray(final int numbers) throws IOException {
		final long[] arr = new long[numbers];
		for (int i = 0; i < numbers; i++) {
			arr[i] = readLong();
		}
		return arr;
	}

	public static double readDouble() throws IOException {
		double ret = 0, div = 1;
		byte c = Read();
		while (c <= ' ')
			c = Read();
		boolean neg = (c == '-');
		if (neg)
			c = Read();
		do {
			ret = ret * 10 + c - '0';
		} while ((c = Read()) >= '0' && c <= '9');
		if (c == '.') {
			while ((c = Read()) >= '0' && c <= '9') {
				ret += (c - '0') / (div *= 10);
			}
		}
		if (neg)
			return -ret;
		return ret;
	}

	public static double[] readDoubleArray(final int numbers) throws IOException {
		final double[] arr = new double[numbers];
		for (int i = 0; i < numbers; i++) {
			arr[i] = readDouble();
		}
		return arr;
	}

	private static void fillBuffer() throws IOException {
		bytesRead = in.read(buffer, bufferPointer = 0, BUFFER_SIZE);
		if (bytesRead == -1)
			buffer[0] = -1;
	}

	private static byte Read() throws IOException {
		if (bufferPointer == bytesRead)
			fillBuffer();
		return buffer[bufferPointer++];
	}
}

Compilation message

sequence.java:5: error: class Main is public, should be declared in a file named Main.java
public class Main {
       ^
1 error