Submission #1177419


Source Code Expand

import java.io.*;
import java.util.*;
import java.util.Map.*;


public class Main {

	private static Scanner sc;
	private static Printer pr;

	private static void solve() {
		int n = sc.nextInt();
		int m = sc.nextInt();

		int[] x = new int[n];
		Map<Integer, Map<Integer, Integer>> hm = new HashMap<>();
		for (int i = 0; i < n; i++) {
			x[i] = sc.nextInt();

			int mod = x[i] % m;

			if (!hm.containsKey(mod)) {
				hm.put(mod, new HashMap<>());
			}

			Map<Integer, Integer> hmtmp = hm.get(mod);
			if (!hmtmp.containsKey(x[i])) {
				hmtmp.put(x[i], 0);
			}
			hmtmp.put(x[i], hmtmp.get(x[i]) + 1);
		}

		int ret = 0;
		for (Entry<Integer, Map<Integer, Integer>> e : hm.entrySet()) {
			int mod = e.getKey();
			int mod2 = (m - mod) % m;
			Map<Integer, Integer> hmtmp = e.getValue();

			if (mod == mod2) {
				int tmp = 0;
				for (int ee : hmtmp.values()) {
					tmp += ee;
				}

				ret += tmp / 2;
			} else if (mod < mod2) {
				if (!hm.containsKey(mod2)) {
					continue;
				}

				Map<Integer, Integer> hmtmp2 = hm.get(mod2);

				int tmp = 0;
				int ecnt = 0;
				for (int ee : hmtmp.values()) {
					tmp += ee;
					if (ee % 2 == 1) {
						ecnt++;
					}
				}

				int tmp2 = 0;
				int ecnt2 = 0;
				for (int ee : hmtmp2.values()) {
					tmp2 += ee;
					if (ee % 2 == 1) {
						ecnt2++;
					}
				}

				if (ecnt >= ecnt2) {
					ret += ecnt2;
					tmp2 -= ecnt2;
					tmp -= ecnt2;
					ecnt -= ecnt2;
					ecnt2 = 0;

					int tmptmp = Math.min(ecnt, tmp2);
					ret += tmptmp;
					ecnt -= tmptmp;
					tmp -= tmptmp;
					tmp2 -= tmptmp;

					ret += tmp / 2 + tmp2 / 2;
				} else {
					ret += ecnt;
					tmp -= ecnt;
					tmp2 -= ecnt;
					ecnt2 -= ecnt;
					ecnt = 0;

					int tmptmp = Math.min(ecnt2, tmp);
					ret += tmptmp;
					ecnt2 -= tmptmp;
					tmp -= tmptmp;
					tmp2 -= tmptmp;

					ret += tmp / 2 + tmp2 / 2;
				}
			} else {
				break;
			}

		}

		pr.println(ret);
	}

	// ---------------------------------------------------
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		pr = new Printer(System.out);

		solve();

		pr.close();
		sc.close();
	}

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;

		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		private boolean isPrintable(int ch) {
			return ch >= '!' && ch <= '~';
		}

		private boolean isCRLF(int ch) {
			return ch == '\n' || ch == '\r' || ch == -1;
		}

		private int nextPrintable() {
			try {
				int ch;
				while (!isPrintable(ch = br.read())) {
					if (ch == -1) {
						throw new NoSuchElementException();
					}
				}

				return ch;
			} catch (IOException e) {
				throw new NoSuchElementException();
			}
		}

		String next() {
			try {
				int ch = nextPrintable();
				StringBuilder sb = new StringBuilder();
				do {
					sb.appendCodePoint(ch);
				} while (isPrintable(ch = br.read()));

				return sb.toString();
			} catch (IOException e) {
				throw new NoSuchElementException();
			}
		}

		int nextInt() {
			try {
				// parseInt from Integer.parseInt()
				boolean negative = false;
				int res = 0;
				int limit = -Integer.MAX_VALUE;
				int radix = 10;

				int fc = nextPrintable();
				if (fc < '0') {
					if (fc == '-') {
						negative = true;
						limit = Integer.MIN_VALUE;
					} else if (fc != '+') {
						throw new NumberFormatException();
					}
					fc = br.read();
				}
				int multmin = limit / radix;

				int ch = fc;
				do {
					int digit = ch - '0';
					if (digit < 0 || digit >= radix) {
						throw new NumberFormatException();
					}
					if (res < multmin) {
						throw new NumberFormatException();
					}
					res *= radix;
					if (res < limit + digit) {
						throw new NumberFormatException();
					}
					res -= digit;

				} while (isPrintable(ch = br.read()));

				return negative ? res : -res;
			} catch (IOException e) {
				throw new NoSuchElementException();
			}
		}

		long nextLong() {
			try {
				// parseLong from Long.parseLong()
				boolean negative = false;
				long res = 0;
				long limit = -Long.MAX_VALUE;
				int radix = 10;

				int fc = nextPrintable();
				if (fc < '0') {
					if (fc == '-') {
						negative = true;
						limit = Long.MIN_VALUE;
					} else if (fc != '+') {
						throw new NumberFormatException();
					}
					fc = br.read();
				}
				long multmin = limit / radix;

				int ch = fc;
				do {
					int digit = ch - '0';
					if (digit < 0 || digit >= radix) {
						throw new NumberFormatException();
					}
					if (res < multmin) {
						throw new NumberFormatException();
					}
					res *= radix;
					if (res < limit + digit) {
						throw new NumberFormatException();
					}
					res -= digit;

				} while (isPrintable(ch = br.read()));

				return negative ? res : -res;
			} catch (IOException e) {
				throw new NoSuchElementException();
			}
		}

		float nextFloat() {
			return Float.parseFloat(next());
		}

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

		String nextLine() {
			try {
				int ch;
				while (isCRLF(ch = br.read())) {
					if (ch == -1) {
						throw new NoSuchElementException();
					}
				}
				StringBuilder sb = new StringBuilder();
				do {
					sb.appendCodePoint(ch);
				} while (!isCRLF(ch = br.read()));

				return sb.toString();
			} catch (IOException e) {
				throw new NoSuchElementException();
			}
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new NoSuchElementException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}

Submission Info

Submission Time
Task D - Pair Cards
User garnacha
Language Java8 (OpenJDK 1.8.0)
Score 0
Code Size 5919 Byte
Status WA
Exec Time 321 ms
Memory 64224 KB

Judge Result

Set Name sample all
Score / Max Score 0 / 0 0 / 700
Status
AC × 2
AC × 25
WA × 9
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, sample-01.txt, sample-02.txt
Case Name Status Exec Time Memory
01-01.txt AC 71 ms 19668 KB
01-02.txt AC 207 ms 30692 KB
01-03.txt AC 205 ms 30240 KB
01-04.txt AC 220 ms 30676 KB
01-05.txt AC 213 ms 28772 KB
01-06.txt AC 202 ms 30472 KB
01-07.txt AC 199 ms 32256 KB
01-08.txt AC 203 ms 36892 KB
01-09.txt AC 269 ms 38872 KB
01-10.txt WA 258 ms 50720 KB
01-11.txt WA 266 ms 48216 KB
01-12.txt WA 171 ms 28564 KB
01-13.txt WA 170 ms 36084 KB
01-14.txt AC 223 ms 36604 KB
01-15.txt AC 226 ms 33764 KB
01-16.txt AC 278 ms 40684 KB
01-17.txt AC 224 ms 36956 KB
01-18.txt AC 253 ms 47064 KB
01-19.txt AC 238 ms 48904 KB
01-20.txt AC 321 ms 64224 KB
01-21.txt AC 178 ms 27536 KB
01-22.txt AC 178 ms 26828 KB
01-23.txt AC 192 ms 27096 KB
01-24.txt WA 181 ms 30568 KB
01-25.txt WA 182 ms 30476 KB
01-26.txt WA 184 ms 34452 KB
01-27.txt AC 219 ms 50724 KB
01-28.txt WA 100 ms 19796 KB
01-29.txt WA 86 ms 17876 KB
01-30.txt AC 70 ms 18516 KB
sample-01.txt AC 72 ms 21076 KB
sample-02.txt AC 71 ms 17236 KB