Submission #994832


Source Code Expand

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class Main {

	//turai
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception{
		Scanner io = new Scanner(System.in);
		//IO io = new IO(new FileInputStream("max.txt"));
		int n = io.nextInt();
		int m = io.nextInt();
		ArrayList<Integer>[] b = new ArrayList[m];
		for(int i=0;i<m;i++) {
			b[i] = new ArrayList<>();
		}
		for(int i=0;i<n;i++) {
			int x = io.nextInt();
			b[x % m].add(x);
		}
		for(int i=0;i<m;i++) {
			b[i].sort(null);
		}
		int ans = 0;
		int[] used = new int[m];
		for(int i=0;i<=m/2;i++) {
			if (i == 0 || i * 2 == m) {
				used[i]++;
				ans += b[i].size() / 2;
			}else{
				ArrayList<Integer> l1 = b[i];
				ArrayList<Integer> l2 = b[m-i];
				used[i]++;
				used[m-i]++;
				if (l1.size() < l2.size()) {
					ArrayList<Integer> temp = l1;
					l1 = l2;
					l2 = temp;
				}
				//l1 is larger
				int pair = 0;
				for(int j=0;j<l1.size()-1;j++) {
					if (l1.get(j) == l1.get(j+1)) {
						pair++;
						j++;
					}
				}
				ans += Math.min((l1.size() - l2.size()) / 2, pair) + l2.size();
			}
		}
		for(int i=0;i<m;i++) {
			if (used[i] != 1) {
				throw new RuntimeException();
			}
		}
		System.out.println(ans);
	}

}

class IO extends PrintWriter {
	private final InputStream in;
	private final byte[] buffer = new byte[1024];
	private int ptr = 0;
	private int buflen = 0;

	public IO() { this(System.in);}
	public IO(InputStream source) { super(System.out); this.in = source;}
	private boolean hasNextByte() {
		if (ptr < buflen) {
			return true;
		}else{
			ptr = 0;
			try {
				buflen = in.read(buffer);
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (buflen <= 0) {
				return false;
			}
		}
		return true;
	}
	private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
	private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
	private static boolean isNewLine(int c) { return c == '\n' || c == '\r';}
	public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
	public boolean hasNextLine() { while(hasNextByte() && isNewLine(buffer[ptr])) ptr++; return hasNextByte();}
	public String next() {
		if (!hasNext()) {
			throw new NoSuchElementException();
		}
		StringBuilder sb = new StringBuilder();
		int b = readByte();
		while(isPrintableChar(b)) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	public char[] nextCharArray(int len) {
		if (!hasNext()) {
			throw new NoSuchElementException();
		}
		char[] s = new char[len];
		int i = 0;
		int b = readByte();
		while(isPrintableChar(b)) {
			if (i == len) {
				throw new InputMismatchException();
			}
			s[i++] = (char) b;
			b = readByte();
		}
		return s;
	}
	public String nextLine() {
		if (!hasNextLine()) {
			throw new NoSuchElementException();
		}
		StringBuilder sb = new StringBuilder();
		int b = readByte();
		while(!isNewLine(b)) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	public long nextLong() {
		if (!hasNext()) {
			throw new NoSuchElementException();
		}
		long n = 0;
		boolean minus = false;
		int b = readByte();
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		if (b < '0' || '9' < b) {
			throw new NumberFormatException();
		}
		while(true){
			if ('0' <= b && b <= '9') {
				n *= 10;
				n += b - '0';
			}else if(b == -1 || !isPrintableChar(b)){
				return minus ? -n : n;
			}else{
				throw new NumberFormatException();
			}
			b = readByte();
		}
	}
	public int nextInt() {
		long nl = nextLong();
		if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) {
			throw new NumberFormatException();
		}
		return (int) nl;
	}
	public char nextChar() {
		if (!hasNext()) {
			throw new NoSuchElementException();
		}
		return (char) readByte();
	}
	public double nextDouble() { return Double.parseDouble(next());}
	public int[] nextIntArray(int n) { int[] a = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a;}
	public long[] nextLongArray(int n) { long[] a = new long[n]; for(int i=0;i<n;i++) a[i] = nextLong(); return a;}
	public double[] nextDoubleArray(int n) { double[] a = new double[n]; for(int i=0;i<n;i++) a[i] = nextDouble(); return a;}
	public void nextIntArrays(int[]... a) { for(int i=0;i<a[0].length;i++) for(int j=0;j<a.length;j++) a[j][i] = nextInt();}
	public int[][] nextIntMatrix(int n,int m) { int[][] a = new int[n][]; for(int i=0;i<n;i++) a[i] = nextIntArray(m); return a;}
	public char[][] nextCharMap(int n,int m) { char[][] a = new char[n][]; for(int i=0;i<n;i++) a[i] = nextCharArray(m); return a;}
	public void close() { super.close(); try {in.close();} catch (IOException e) {}}
}

Submission Info

Submission Time
Task D - Pair Cards
User piroz95
Language Java8 (OpenJDK 1.8.0)
Score 0
Code Size 5083 Byte
Status WA
Exec Time 608 ms
Memory 55468 KB

Judge Result

Set Name sample all
Score / Max Score 0 / 0 0 / 700
Status
AC × 2
AC × 16
WA × 16
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 142 ms 10196 KB
01-02.txt AC 574 ms 34120 KB
01-03.txt AC 573 ms 34488 KB
01-04.txt WA 608 ms 34100 KB
01-05.txt WA 562 ms 34664 KB
01-06.txt WA 555 ms 34516 KB
01-07.txt WA 561 ms 34072 KB
01-08.txt WA 550 ms 34016 KB
01-09.txt WA 538 ms 35056 KB
01-10.txt WA 552 ms 52548 KB
01-11.txt WA 554 ms 55132 KB
01-12.txt WA 503 ms 33904 KB
01-13.txt WA 518 ms 49964 KB
01-14.txt AC 581 ms 34640 KB
01-15.txt AC 563 ms 34552 KB
01-16.txt AC 543 ms 34892 KB
01-17.txt AC 536 ms 33832 KB
01-18.txt AC 555 ms 34600 KB
01-19.txt AC 543 ms 49344 KB
01-20.txt AC 563 ms 55468 KB
01-21.txt AC 560 ms 34064 KB
01-22.txt WA 591 ms 34776 KB
01-23.txt WA 572 ms 34680 KB
01-24.txt WA 549 ms 34372 KB
01-25.txt WA 524 ms 50044 KB
01-26.txt WA 562 ms 52112 KB
01-27.txt AC 413 ms 37992 KB
01-28.txt WA 213 ms 17956 KB
01-29.txt AC 188 ms 13876 KB
01-30.txt AC 164 ms 13516 KB
sample-01.txt AC 128 ms 9672 KB
sample-02.txt AC 128 ms 9668 KB