Submission #992518


Source Code Expand

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.PriorityQueue;
import java.util.TreeSet;

public class Main {
	InputStream is;

	int __t__ = 1;
	int __f__ = 0;
	int __FILE_DEBUG_FLAG__ = __f__;
	String __DEBUG_FILE_NAME__ = "src/D2";

	FastScanner in;
	PrintWriter out;
	
	class Pair implements Comparable<Pair> {
		int value;
		int cnt;

		Pair(int value, int cnt) {
			this.value = value;
			this.cnt = cnt;
		}

		public int compareTo(Pair s) {
			return -(cnt % 2);
		}

		public String toString() {
			return "(" + value + ", " + cnt + ")";
		}
	}
	
	int MAX = 100100;
	public void solve() {
		int N = in.nextInt();
		int M = in.nextInt();
		int[] X = in.nextIntArray(N);
		
		int[] cnt1 = new int[MAX+10];
		for (int i = 0; i < N; i++) {
			cnt1[X[i]]++;
		}
		
		PriorityQueue<Pair>[] pq = new PriorityQueue[M];
		for (int i = 0; i < M; i++) {
			pq[i] = new PriorityQueue<>();
		}
		for (int i = 0; i < MAX + 10; i++) {
			if (cnt1[i] != 0) {
				pq[i%M].add(new Pair(i, cnt1[i]));
			}
		}
		
		int res = 0;
		for (int i = 0; i < M; i++) {
			int opp = (M - i) % M;
			if (pq[i].isEmpty()) continue;
			Pair p1 = pq[i].poll();
			if (pq[opp].isEmpty()) {
				pq[i].add(p1);
				continue;
			}

			Pair p2 = pq[opp].poll();
			if (p1.cnt % 2 == 0 && p2.cnt % 2 == 0) continue;
			
			p1.cnt--;
			p2.cnt--;
			if (p1.cnt != 0) pq[i].add(p1);
			if (p2.cnt != 0) pq[opp].add(p2);
			
			res++;
		}
		
		int[] cnt2 = new int[M];
		for (int i = 0; i < M; i++) {
			while (!pq[i].isEmpty()) {
				Pair next = pq[i].poll();
				cnt2[i] += next.cnt;
			}
			res += cnt2[i] / 2;
		}
		System.out.println(res);
	}
	
	public void run() {
		if (__FILE_DEBUG_FLAG__ == __t__) {
			try {
				is = new FileInputStream(__DEBUG_FILE_NAME__);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			System.out.println("FILE_INPUT!");
		} else {
			is = System.in;
		}
		in = new FastScanner(is);
		out = new PrintWriter(System.out);

		Thread t = new Thread(null, new Runnable() {
			
			@Override
			public void run() {
				solve();
			}
		}, "lul", 1 << 30);
		t.start();
	}

	public static void main(String[] args) {
		new Main().run();
	}

	public void mapDebug(int[][] a) {
		System.out.println("--------map display---------");

		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[i].length; j++) {
				System.out.printf("%3d ", a[i][j]);
			}
			System.out.println();
		}

		System.out.println("----------------------------");
		System.out.println();
	}

	public void debug(Object... obj) {
		System.out.println(Arrays.deepToString(obj));
	}

	class FastScanner {
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public FastScanner(InputStream stream) {
			this.stream = stream;
			//stream = new FileInputStream(new File("dec.in"));

		}

		int read() {
			if (numChars == -1)
				throw new InputMismatchException();
			if (curChar >= numChars) {
				curChar = 0;
				try {
					numChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		boolean isEndline(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		int[] nextIntArray(int n) {
			int[] array = new int[n];
			for (int i = 0; i < n; i++)
				array[i] = nextInt();

			return array;
		}

		int[][] nextIntMap(int n, int m) {
			int[][] map = new int[n][m];
			for (int i = 0; i < n; i++) {
				map[i] = in.nextIntArray(m);
			}
			return map;
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		long[] nextLongArray(int n) {
			long[] array = new long[n];
			for (int i = 0; i < n; i++)
				array[i] = nextLong();

			return array;
		}

		long[][] nextLongMap(int n, int m) {
			long[][] map = new long[n][m];
			for (int i = 0; i < n; i++) {
				map[i] = in.nextLongArray(m);
			}
			return map;
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		double[] nextDoubleArray(int n) {
			double[] array = new double[n];
			for (int i = 0; i < n; i++)
				array[i] = nextDouble();

			return array;
		}

		double[][] nextDoubleMap(int n, int m) {
			double[][] map = new double[n][m];
			for (int i = 0; i < n; i++) {
				map[i] = in.nextDoubleArray(m);
			}
			return map;
		}

		String next() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		String[] nextStringArray(int n) {
			String[] array = new String[n];
			for (int i = 0; i < n; i++)
				array[i] = next();

			return array;
		}

		String nextLine() {
			int c = read();
			while (isEndline(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndline(c));
			return res.toString();
		}
	}
}


Submission Info

Submission Time
Task D - Pair Cards
User hiro116s
Language Java8 (OpenJDK 1.8.0)
Score 0
Code Size 5610 Byte
Status WA
Exec Time 1446 ms
Memory 34236 KB

Compile Error

Note: ./Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Judge Result

Set Name sample all
Score / Max Score 0 / 0 0 / 700
Status
AC × 2
AC × 18
WA × 14
Set Name Test Cases
sample sample-01.txt, sample-02.txt
all sample-01.txt, sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, 01-30.txt
Case Name Status Exec Time Memory
01-01.txt AC 114 ms 9424 KB
01-02.txt AC 207 ms 26548 KB
01-03.txt AC 205 ms 26428 KB
01-04.txt AC 206 ms 26548 KB
01-05.txt AC 206 ms 26536 KB
01-06.txt WA 203 ms 26416 KB
01-07.txt WA 205 ms 26284 KB
01-08.txt WA 209 ms 26556 KB
01-09.txt WA 218 ms 26448 KB
01-10.txt WA 212 ms 27180 KB
01-11.txt WA 210 ms 27592 KB
01-12.txt AC 179 ms 23500 KB
01-13.txt AC 191 ms 27080 KB
01-14.txt AC 212 ms 26580 KB
01-15.txt AC 202 ms 26668 KB
01-16.txt WA 225 ms 26676 KB
01-17.txt WA 218 ms 26696 KB
01-18.txt AC 215 ms 26928 KB
01-19.txt AC 223 ms 27452 KB
01-20.txt AC 207 ms 27224 KB
01-21.txt AC 176 ms 23512 KB
01-22.txt WA 177 ms 23596 KB
01-23.txt WA 177 ms 23600 KB
01-24.txt WA 1446 ms 23612 KB
01-25.txt WA 181 ms 27212 KB
01-26.txt WA 201 ms 26828 KB
01-27.txt AC 214 ms 34236 KB
01-28.txt AC 157 ms 20656 KB
01-29.txt WA 123 ms 13640 KB
01-30.txt AC 126 ms 19692 KB
sample-01.txt AC 114 ms 9340 KB
sample-02.txt AC 110 ms 9336 KB