Submission #25991252
Source Code Expand
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.io.UncheckedIOException;
import java.io.Closeable;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) throws Exception {
Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 29);
thread.start();
thread.join();
}
static class TaskAdapter implements Runnable {
@Override
public void run() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastInput in = new FastInput(inputStream);
FastOutput out = new FastOutput(outputStream);
DPureStraight solver = new DPureStraight();
solver.solve(1, in, out);
out.close();
}
}
static class DPureStraight {
int[][] prev;
int[][] next;
int n;
int k;
public void solve(int testNumber, FastInput in, FastOutput out) {
n = in.ri();
k = in.ri();
int[] a = in.ri(n);
for (int i = 0; i < n; i++) {
a[i]--;
}
prev = new int[2][1 << k];
next = new int[2][1 << k];
int[] bc = new int[1 << k];
for (int i = 0; i < 1 << k; i++) {
bc[i] = Integer.bitCount(i);
}
int inf = (int) 1e9;
SequenceUtils.deepFill(prev, inf);
prev[0][0] = 0;
int mask = (1 << k) - 1;
for (int i = 0; i < n; i++) {
int x = a[i];
SequenceUtils.deepFill(next, inf);
for (int j = 0; j < 2; j++) {
for (int z = j; z < 2; z++) {
for (int t = 0; t < 1 << k; t++) {
//add
if (Bits.get(t, x) == 0) {
int nt = t | (1 << x);
int nc = prev[j][t];
nc += bc[(mask ^ nt) & ((1 << x) - 1)];
next[z][nt] = Math.min(next[z][nt], nc);
}
//not add
int nt = t;
int nc = prev[j][t];
if (j == 1) {
nc += bc[t ^ mask];
} else {
nc += bc[t];
}
next[z][nt] = Math.min(next[z][nt], nc);
}
}
}
int[][] tmp = prev;
prev = next;
next = tmp;
}
int ans = prev[1][mask];
out.println(ans);
}
}
static class FastOutput implements AutoCloseable, Closeable, Appendable {
private static final int THRESHOLD = 32 << 10;
private OutputStream writer;
private StringBuilder cache = new StringBuilder(THRESHOLD * 2);
private static Field stringBuilderValueField;
private char[] charBuf = new char[THRESHOLD * 2];
private byte[] byteBuf = new byte[THRESHOLD * 2];
static {
try {
stringBuilderValueField = StringBuilder.class.getSuperclass().getDeclaredField("value");
stringBuilderValueField.setAccessible(true);
} catch (Exception e) {
stringBuilderValueField = null;
}
stringBuilderValueField = null;
}
public FastOutput append(CharSequence csq) {
cache.append(csq);
return this;
}
public FastOutput append(CharSequence csq, int start, int end) {
cache.append(csq, start, end);
return this;
}
private void afterWrite() {
if (cache.length() < THRESHOLD) {
return;
}
flush();
}
public FastOutput(OutputStream writer) {
this.writer = writer;
}
public FastOutput append(char c) {
cache.append(c);
afterWrite();
return this;
}
public FastOutput append(int c) {
cache.append(c);
afterWrite();
return this;
}
public FastOutput println(int c) {
return append(c).println();
}
public FastOutput println() {
return append('\n');
}
public FastOutput flush() {
try {
if (stringBuilderValueField != null) {
try {
byte[] value = (byte[]) stringBuilderValueField.get(cache);
writer.write(value, 0, cache.length());
} catch (Exception e) {
stringBuilderValueField = null;
}
}
if (stringBuilderValueField == null) {
int n = cache.length();
if (n > byteBuf.length) {
//slow
writer.write(cache.toString().getBytes(StandardCharsets.UTF_8));
// writer.append(cache);
} else {
cache.getChars(0, n, charBuf, 0);
for (int i = 0; i < n; i++) {
byteBuf[i] = (byte) charBuf[i];
}
writer.write(byteBuf, 0, n);
}
}
writer.flush();
cache.setLength(0);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return this;
}
public void close() {
flush();
try {
writer.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public String toString() {
return cache.toString();
}
}
static class Bits {
private Bits() {
}
public static int get(int x, int i) {
return (x >>> i) & 1;
}
}
static class SequenceUtils {
public static void deepFill(Object array, int val) {
if (array == null) {
return;
}
if (!array.getClass().isArray()) {
throw new IllegalArgumentException();
}
if (array instanceof int[]) {
int[] intArray = (int[]) array;
Arrays.fill(intArray, val);
} else {
Object[] objArray = (Object[]) array;
for (Object obj : objArray) {
deepFill(obj, val);
}
}
}
}
static class FastInput {
private final InputStream is;
private byte[] buf = new byte[1 << 13];
private int bufLen;
private int bufOffset;
private int next;
public FastInput(InputStream is) {
this.is = is;
}
public void populate(int[] data) {
for (int i = 0; i < data.length; i++) {
data[i] = readInt();
}
}
private int read() {
while (bufLen == bufOffset) {
bufOffset = 0;
try {
bufLen = is.read(buf);
} catch (IOException e) {
bufLen = -1;
}
if (bufLen == -1) {
return -1;
}
}
return buf[bufOffset++];
}
public void skipBlank() {
while (next >= 0 && next <= 32) {
next = read();
}
}
public int ri() {
return readInt();
}
public int[] ri(int n) {
int[] ans = new int[n];
populate(ans);
return ans;
}
public int readInt() {
boolean rev = false;
skipBlank();
if (next == '+' || next == '-') {
rev = next == '-';
next = read();
}
int val = 0;
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
return rev ? val : -val;
}
}
}
Submission Info
| Submission Time | |
|---|---|
| Task | D - Pure Straight |
| User | daltao |
| Language | Java (OpenJDK 11.0.6) |
| Score | 600 |
| Code Size | 9077 Byte |
| Status | AC |
| Exec Time | 361 ms |
| Memory | 36508 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 600 / 600 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 01_sample_01.txt, 01_sample_02.txt, 01_sample_03.txt |
| All | 01_sample_01.txt, 01_sample_02.txt, 01_sample_03.txt, 02_permutation_01.txt, 02_permutation_02.txt, 02_permutation_03.txt, 02_permutation_04.txt, 02_permutation_05.txt, 02_permutation_06.txt, 02_permutation_07.txt, 02_permutation_08.txt, 02_permutation_09.txt, 02_permutation_10.txt, 02_permutation_11.txt, 02_permutation_12.txt, 02_permutation_13.txt, 02_permutation_14.txt, 02_permutation_15.txt, 03_rand_01.txt, 03_rand_02.txt, 03_rand_03.txt, 03_rand_04.txt, 03_rand_05.txt, 03_rand_06.txt, 03_rand_07.txt, 03_rand_08.txt, 03_rand_09.txt, 03_rand_10.txt, 03_rand_11.txt, 03_rand_12.txt, 03_rand_13.txt, 03_rand_14.txt, 03_rand_15.txt, 03_rand_16.txt, 03_rand_17.txt, 03_rand_18.txt, 03_rand_19.txt, 03_rand_20.txt, 03_rand_21.txt, 03_rand_22.txt, 03_rand_23.txt, 03_rand_24.txt, 03_rand_25.txt, 03_rand_26.txt, 03_rand_27.txt, 03_rand_28.txt, 03_rand_29.txt, 03_rand_30.txt, 03_rand_31.txt, 03_rand_32.txt, 03_rand_33.txt, 04_large_ans_01.txt, 04_large_ans_02.txt, 04_large_ans_03.txt, 04_large_ans_04.txt, 04_large_ans_05.txt, 04_large_ans_06.txt, 04_large_ans_07.txt, 04_large_ans_08.txt, 04_large_ans_09.txt, 04_large_ans_10.txt, 04_large_ans_11.txt, 04_large_ans_12.txt, 05_partition_01.txt, 05_partition_02.txt, 05_partition_03.txt, 05_partition_04.txt, 05_partition_05.txt, 05_partition_06.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 01_sample_01.txt | AC | 90 ms | 33588 KiB |
| 01_sample_02.txt | AC | 94 ms | 33528 KiB |
| 01_sample_03.txt | AC | 90 ms | 33664 KiB |
| 02_permutation_01.txt | AC | 88 ms | 33748 KiB |
| 02_permutation_02.txt | AC | 86 ms | 33536 KiB |
| 02_permutation_03.txt | AC | 104 ms | 33708 KiB |
| 02_permutation_04.txt | AC | 82 ms | 33536 KiB |
| 02_permutation_05.txt | AC | 86 ms | 33800 KiB |
| 02_permutation_06.txt | AC | 89 ms | 33908 KiB |
| 02_permutation_07.txt | AC | 83 ms | 33804 KiB |
| 02_permutation_08.txt | AC | 90 ms | 33760 KiB |
| 02_permutation_09.txt | AC | 89 ms | 33732 KiB |
| 02_permutation_10.txt | AC | 99 ms | 33876 KiB |
| 02_permutation_11.txt | AC | 103 ms | 35024 KiB |
| 02_permutation_12.txt | AC | 113 ms | 35212 KiB |
| 02_permutation_13.txt | AC | 116 ms | 35200 KiB |
| 02_permutation_14.txt | AC | 144 ms | 35468 KiB |
| 02_permutation_15.txt | AC | 148 ms | 36244 KiB |
| 03_rand_01.txt | AC | 90 ms | 33856 KiB |
| 03_rand_02.txt | AC | 90 ms | 33764 KiB |
| 03_rand_03.txt | AC | 91 ms | 33852 KiB |
| 03_rand_04.txt | AC | 84 ms | 33856 KiB |
| 03_rand_05.txt | AC | 88 ms | 33644 KiB |
| 03_rand_06.txt | AC | 101 ms | 33964 KiB |
| 03_rand_07.txt | AC | 118 ms | 34780 KiB |
| 03_rand_08.txt | AC | 111 ms | 35056 KiB |
| 03_rand_09.txt | AC | 122 ms | 34876 KiB |
| 03_rand_10.txt | AC | 124 ms | 34860 KiB |
| 03_rand_11.txt | AC | 125 ms | 34964 KiB |
| 03_rand_12.txt | AC | 144 ms | 35080 KiB |
| 03_rand_13.txt | AC | 164 ms | 35072 KiB |
| 03_rand_14.txt | AC | 213 ms | 35656 KiB |
| 03_rand_15.txt | AC | 218 ms | 35568 KiB |
| 03_rand_16.txt | AC | 218 ms | 35548 KiB |
| 03_rand_17.txt | AC | 219 ms | 35572 KiB |
| 03_rand_18.txt | AC | 217 ms | 35560 KiB |
| 03_rand_19.txt | AC | 219 ms | 35556 KiB |
| 03_rand_20.txt | AC | 219 ms | 35696 KiB |
| 03_rand_21.txt | AC | 215 ms | 35404 KiB |
| 03_rand_22.txt | AC | 218 ms | 35456 KiB |
| 03_rand_23.txt | AC | 220 ms | 35604 KiB |
| 03_rand_24.txt | AC | 308 ms | 36208 KiB |
| 03_rand_25.txt | AC | 323 ms | 36320 KiB |
| 03_rand_26.txt | AC | 310 ms | 36380 KiB |
| 03_rand_27.txt | AC | 319 ms | 36472 KiB |
| 03_rand_28.txt | AC | 315 ms | 36500 KiB |
| 03_rand_29.txt | AC | 357 ms | 36300 KiB |
| 03_rand_30.txt | AC | 313 ms | 36320 KiB |
| 03_rand_31.txt | AC | 329 ms | 36220 KiB |
| 03_rand_32.txt | AC | 349 ms | 36288 KiB |
| 03_rand_33.txt | AC | 311 ms | 36388 KiB |
| 04_large_ans_01.txt | AC | 317 ms | 36396 KiB |
| 04_large_ans_02.txt | AC | 213 ms | 35552 KiB |
| 04_large_ans_03.txt | AC | 313 ms | 36308 KiB |
| 04_large_ans_04.txt | AC | 213 ms | 35636 KiB |
| 04_large_ans_05.txt | AC | 360 ms | 36176 KiB |
| 04_large_ans_06.txt | AC | 214 ms | 35456 KiB |
| 04_large_ans_07.txt | AC | 361 ms | 36344 KiB |
| 04_large_ans_08.txt | AC | 218 ms | 35688 KiB |
| 04_large_ans_09.txt | AC | 361 ms | 36384 KiB |
| 04_large_ans_10.txt | AC | 215 ms | 35548 KiB |
| 04_large_ans_11.txt | AC | 345 ms | 36508 KiB |
| 04_large_ans_12.txt | AC | 215 ms | 35756 KiB |
| 05_partition_01.txt | AC | 311 ms | 36320 KiB |
| 05_partition_02.txt | AC | 211 ms | 35472 KiB |
| 05_partition_03.txt | AC | 314 ms | 36372 KiB |
| 05_partition_04.txt | AC | 219 ms | 35556 KiB |
| 05_partition_05.txt | AC | 315 ms | 36300 KiB |
| 05_partition_06.txt | AC | 209 ms | 35628 KiB |