Submission #2823869


Source Code Expand

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

public class Main {

    static int N;
    static int[] A;
    static int[] B;

    static int MOD = 1_000_000_007;

    public static void main(String[] args) {
        FastScanner sc = new FastScanner(System.in);
        N = sc.nextInt();
        A = sc.nextIntArray(N);
        B = sc.nextIntArray(N);

        System.out.println( solve() );
    }

    static long solve() {
        long[] F = new long[N+1];
        long curr = 1;
        F[1] = 1;
        for (int i = 2; i <= N; i++) {
            curr = curr * i % MOD;
            F[i] = curr;
        }

        List<Integer> P = new ArrayList<>();

        Arrays.sort(A);
        Arrays.sort(B);
        int ai = 0;
        int bi = 0;
        int ab = 0;
        while(ai != N && bi != N) {
            int a = A[ai];
            int b = B[bi];

            if( a < b ) {
                if( ab >= 0 ) {
                    ai++;
                    ab++;

                } else {
                    ai += Math.abs(ab);
                    P.add(Math.abs(ab));
                    ab = 0;
                }

            } else {
                if( ab <= 0 ) {
                    bi++;
                    ab--;

                } else {
                    bi += Math.abs(ab);
                    P.add(Math.abs(ab));
                    ab = 0;
                }
            }
        }
        P.add(N - Math.min(ai, bi));

        long ans = F[P.get(0)];
        for (int i = 1; i < P.size(); i++) {
            ans = ans * F[P.get(i)] % MOD;
        }
        return ans;
    }

    @SuppressWarnings("unused")
    static class FastScanner {

        private BufferedReader reader;
        private StringTokenizer tokenizer;

        FastScanner(InputStream in) {
            reader = new BufferedReader(new InputStreamReader(in));
            tokenizer = null;
        }

        String next() {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        String nextLine() {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    return reader.readLine();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            return tokenizer.nextToken("\n");
        }

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

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

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

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

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

Submission Info

Submission Time
Task A - 1D Matching
User kusomushi
Language Java8 (OpenJDK 1.8.0)
Score 0
Code Size 3340 Byte
Status WA
Exec Time 279 ms
Memory 44784 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
AC × 2
AC × 4
WA × 10
Set Name Test Cases
Sample example0.txt, example1.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, example0.txt, example1.txt
Case Name Status Exec Time Memory
000.txt WA 208 ms 35500 KB
001.txt WA 158 ms 28512 KB
002.txt WA 184 ms 28460 KB
003.txt WA 196 ms 32824 KB
004.txt WA 260 ms 39432 KB
005.txt WA 269 ms 44784 KB
006.txt WA 252 ms 43096 KB
007.txt WA 271 ms 40960 KB
008.txt WA 275 ms 42708 KB
009.txt WA 264 ms 39160 KB
010.txt AC 266 ms 43360 KB
011.txt AC 279 ms 43544 KB
example0.txt AC 71 ms 18132 KB
example1.txt AC 71 ms 18900 KB