Submission #7606213


Source Code Expand

import java.io.*;
import java.math.BigInteger;
import java.net.Inet4Address;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
import java.util.Map.Entry;

public class Main implements Runnable {
    static final int MOD = (int) 1e9 + 7;
    static final int MI = (int) 1e9;
    static final long ML = (long) 1e18;
    static final Reader in = new Reader();
    static final PrintWriter out = new PrintWriter(System.out);
    StringBuilder answer = new StringBuilder();
    Random random = new Random(751454315315L + System.currentTimeMillis());

    public static void main(String[] args) {
        new Thread(null, new Main(), "persefone", 1 << 28).start();
    }

    @Override
    public void run() {
        solve();
        printf();
        flush();
    }

    void solve() {
        int n = in.nextInt();
        int m = in.nextInt();
        Map<Integer, Integer>[] re = Stream.generate(HashMap<Integer, Integer>::new).limit(m).toArray(Map[]::new);
        for (int i = 0; i < n; i++) {
            int x = in.nextInt();
            re[x % m].put(x, re[x % m].getOrDefault(x, 0) + 1);
        }

        Function<Integer, Integer> pairs = index -> {
            int p = 0;
            for (Entry<Integer, Integer> e : re[index].entrySet()) {
                p += e.getValue() / 2;
            }
            return p;
        };

        Function<Integer, Integer> total = index -> {
            int t = 0;
            for (Entry<Integer, Integer> e : re[index].entrySet()) {
                t += e.getValue();
            }
            return t;
        };

        int ans = total.apply(0) / 2;
        for (int i = 1; i <= m / 2; i++) {
            if (i != m - i) {
                int low = total.apply(i);
                int high = total.apply(m - i);
                if (low == high) {
                    ans += low;
                    continue;
                }
                int plow = pairs.apply(i);
                if (low < high) {
                    ans += low + Math.min((high - low) / 2, pairs.apply(m - i));
                } else {
                    ans += high + Math.min((low - high) / 2, pairs.apply(i));
                }
            } else {
                ans += total.apply(i) / 2;
            }
        }
        printf(ans);
    }

    void printf() {
        out.print(answer);
    }

    void close() {
        out.close();
    }

    void flush() {
        out.flush();
    }

    void printf(Stream<?> str) {
        str.forEach(o -> add(o, " "));
        add("\n");
    }


    void printf(Object... obj) {
        printf(false, obj);
    }

    void printfWithDescription(Object... obj) {
        printf(true, obj);
    }


    private void printf(boolean b, Object... obj) {

        if (obj.length > 1) {
            for (int i = 0; i < obj.length; i++) {
                if (b) add(obj[i].getClass().getSimpleName(), " - ");
                if (obj[i] instanceof Collection<?>) {
                    printf((Collection<?>) obj[i]);
                } else if (obj[i] instanceof int[][]) {
                    printf((int[][]) obj[i]);
                } else if (obj[i] instanceof long[][]) {
                    printf((long[][]) obj[i]);
                } else if (obj[i] instanceof double[][]) {
                    printf((double[][]) obj[i]);
                } else printf(obj[i]);
            }
            return;
        }

        if (b) add(obj[0].getClass().getSimpleName(), " - ");
        printf(obj[0]);
    }

    void printf(Object o) {
        if (o instanceof int[])
            printf(Arrays.stream((int[]) o).boxed());
        else if (o instanceof char[])
            printf(new String((char[]) o));
        else if (o instanceof long[])
            printf(Arrays.stream((long[]) o).boxed());
        else if (o instanceof double[])
            printf(Arrays.stream((double[]) o).boxed());
        else if (o instanceof boolean[]) {
            for (boolean b : (boolean[]) o) add(b, " ");
            add("\n");
        } else
            add(o, "\n");
    }

    void printf(int[]... obj) {
        for (int i = 0; i < obj.length; i++) printf(obj[i]);
    }

    void printf(long[]... obj) {
        for (int i = 0; i < obj.length; i++) printf(obj[i]);
    }

    void printf(double[]... obj) {
        for (int i = 0; i < obj.length; i++) printf(obj[i]);
    }

    void printf(boolean[]... obj) {
        for (int i = 0; i < obj.length; i++) printf(obj[i]);
    }

    void printf(Collection<?> col) {
        printf(col.stream());
    }

    <T, K> void add(T t, K k) {
        if (t instanceof Collection<?>) {
            ((Collection<?>) t).forEach(i -> add(i, " "));
        } else if (t instanceof Object[]) {
            Arrays.stream((Object[]) t).forEach(i -> add(i, " "));
        } else
            add(t);
        add(k);
    }


    <T> void add(T t) {
        answer.append(t);
    }

    static class Reader {
        private BufferedReader br;
        private StringTokenizer st;

        Reader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        Reader(String fileName) throws FileNotFoundException {
            br = new BufferedReader(new FileReader(fileName));
        }

        boolean isReady() throws IOException {
            return br.ready();
        }

        String next() {
            try {
                while (st == null || !st.hasMoreTokens()) {
                    st = new StringTokenizer(br.readLine());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return st.nextToken();
        }

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

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

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

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

        String nextLine() {
            String s = "";
            try {
                s = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return s;
        }
    }
}

Submission Info

Submission Time
Task D - Pair Cards
User hongquanntct
Language Java8 (OpenJDK 1.8.0)
Score 700
Code Size 6704 Byte
Status AC
Exec Time 395 ms
Memory 62144 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 700 / 700
Status
AC × 2
AC × 34
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 173 ms 28628 KB
01-02.txt AC 336 ms 44460 KB
01-03.txt AC 325 ms 44940 KB
01-04.txt AC 327 ms 45620 KB
01-05.txt AC 323 ms 43000 KB
01-06.txt AC 328 ms 45068 KB
01-07.txt AC 325 ms 45056 KB
01-08.txt AC 342 ms 43448 KB
01-09.txt AC 371 ms 44152 KB
01-10.txt AC 348 ms 47184 KB
01-11.txt AC 384 ms 62144 KB
01-12.txt AC 281 ms 42580 KB
01-13.txt AC 324 ms 43120 KB
01-14.txt AC 334 ms 47356 KB
01-15.txt AC 339 ms 46628 KB
01-16.txt AC 395 ms 45640 KB
01-17.txt AC 351 ms 45600 KB
01-18.txt AC 325 ms 45864 KB
01-19.txt AC 359 ms 48960 KB
01-20.txt AC 372 ms 61596 KB
01-21.txt AC 286 ms 43456 KB
01-22.txt AC 291 ms 43300 KB
01-23.txt AC 289 ms 43172 KB
01-24.txt AC 288 ms 44784 KB
01-25.txt AC 333 ms 43692 KB
01-26.txt AC 350 ms 49648 KB
01-27.txt AC 349 ms 57928 KB
01-28.txt AC 239 ms 38188 KB
01-29.txt AC 190 ms 32340 KB
01-30.txt AC 215 ms 38216 KB
sample-01.txt AC 160 ms 26068 KB
sample-02.txt AC 159 ms 26324 KB