Submission #333136

# Submission time Handle Problem Language Result Execution time Memory
333136 2020-12-04T17:55:35 Z KWang31 Split the sequence (APIO14_sequence) Java 11
100 / 100
1091 ms 96524 KB
//By Spontaneous Combustion, just used for test
import java.io.*;
import java.util.*;
 
public class sequence {
	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++];
	}
}
# Verdict Execution time Memory Grader output
1 Correct 109 ms 9972 KB contestant found the optimal answer: 108 == 108
2 Correct 106 ms 9600 KB contestant found the optimal answer: 999 == 999
3 Correct 107 ms 9580 KB contestant found the optimal answer: 0 == 0
4 Correct 106 ms 9452 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 106 ms 9580 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 106 ms 9580 KB contestant found the optimal answer: 1 == 1
7 Correct 109 ms 9708 KB contestant found the optimal answer: 1 == 1
8 Correct 107 ms 9580 KB contestant found the optimal answer: 1 == 1
9 Correct 105 ms 9580 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 107 ms 9580 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 105 ms 9564 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 105 ms 9580 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 108 ms 9452 KB contestant found the optimal answer: 140072 == 140072
14 Correct 107 ms 9580 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 106 ms 9548 KB contestant found the optimal answer: 805 == 805
16 Correct 108 ms 9708 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 110 ms 9708 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 111 ms 9704 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 107 ms 9472 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 121 ms 9648 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 108 ms 9580 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 108 ms 9616 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 114 ms 9576 KB contestant found the optimal answer: 933702 == 933702
7 Correct 107 ms 9580 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 106 ms 9580 KB contestant found the optimal answer: 687136 == 687136
9 Correct 107 ms 9680 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 105 ms 9472 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 108 ms 9580 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 108 ms 9464 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 167 ms 12052 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 110 ms 9580 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 162 ms 12380 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 166 ms 12404 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 156 ms 12000 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 125 ms 10172 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 116 ms 9708 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 123 ms 10084 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 118 ms 9556 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 108 ms 9472 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 182 ms 13348 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 110 ms 9580 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 176 ms 13252 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 177 ms 13340 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 174 ms 13080 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 170 ms 13308 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 165 ms 12872 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 166 ms 12944 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# Verdict Execution time Memory Grader output
1 Correct 177 ms 12916 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 209 ms 13584 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 258 ms 20924 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 174 ms 13152 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 262 ms 18524 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 232 ms 20124 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 238 ms 20128 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Correct 224 ms 18052 KB contestant found the optimal answer: 3099592898816 == 3099592898816
9 Correct 228 ms 20176 KB contestant found the optimal answer: 1241131419367412 == 1241131419367412
10 Correct 247 ms 20164 KB contestant found the optimal answer: 1243084101967798 == 1243084101967798
# Verdict Execution time Memory Grader output
1 Correct 222 ms 19192 KB contestant found the optimal answer: 19795776960 == 19795776960
2 Correct 262 ms 19724 KB contestant found the optimal answer: 19874432173 == 19874432173
3 Correct 1090 ms 96408 KB contestant found the optimal answer: 497313449256899208 == 497313449256899208
4 Correct 225 ms 19500 KB contestant found the optimal answer: 374850090734572421 == 374850090734572421
5 Correct 1091 ms 96324 KB contestant found the optimal answer: 36183271951 == 36183271951
6 Correct 821 ms 71920 KB contestant found the optimal answer: 51629847150471 == 51629847150471
7 Correct 923 ms 96004 KB contestant found the optimal answer: 124074747024496432 == 124074747024496432
8 Correct 748 ms 64992 KB contestant found the optimal answer: 309959349080800 == 309959349080800
9 Correct 794 ms 72040 KB contestant found the optimal answer: 124113525649823701 == 124113525649823701
10 Correct 990 ms 96524 KB contestant found the optimal answer: 124309619349406845 == 124309619349406845