Submission #70432642


Source Code Expand

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int N = sc.nextInt();
        int[] A = new int[N];
        
        for (int i = 0; i < N; i++) {
            A[i] = sc.nextInt();
        }
        
        long[] leftCount = new long[N + 1];
        long[] rightCount = new long[N + 1];
        
        for (int i = 0; i < N; i++) {
            rightCount[A[i]]++;
        }
        
        long count = 0;
        long pairSum = 0;
        
        for (int j = 0; j < N; j++) {
            pairSum -= leftCount[A[j]] * rightCount[A[j]];
            rightCount[A[j]]--;
            pairSum += leftCount[A[j]] * rightCount[A[j]];
            
            if (j > 0 && j < N - 1) {
                long leftSize = j;
                long rightSize = N - j - 1;
                long leftSame = leftCount[A[j]];
                long rightSame = rightCount[A[j]];
                
                count += leftSame * (rightSize - rightSame);
                count += (leftSize - leftSame) * rightSame;
                count += pairSum - leftSame * rightSame;
            }
            
            pairSum -= leftCount[A[j]] * rightCount[A[j]];
            leftCount[A[j]]++;
            pairSum += leftCount[A[j]] * rightCount[A[j]];
        }
        
        System.out.println(count);
        sc.close();
    }
}

Submission Info

Submission Time
Task C - Odd One Subsequence
User addy
Language Java (OpenJDK 17)
Score 300
Code Size 1462 Byte
Status AC
Exec Time 438 ms
Memory 64748 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 2
AC × 28
Set Name Test Cases
Sample example_00.txt, example_01.txt
All example_00.txt, example_01.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt
Case Name Status Exec Time Memory
example_00.txt AC 66 ms 38012 KiB
example_01.txt AC 67 ms 38120 KiB
hand_00.txt AC 434 ms 64152 KiB
hand_01.txt AC 391 ms 63132 KiB
hand_02.txt AC 412 ms 64024 KiB
hand_03.txt AC 66 ms 38208 KiB
hand_04.txt AC 65 ms 38064 KiB
hand_05.txt AC 420 ms 64288 KiB
random_00.txt AC 430 ms 64064 KiB
random_01.txt AC 394 ms 63936 KiB
random_02.txt AC 438 ms 63904 KiB
random_03.txt AC 408 ms 64220 KiB
random_04.txt AC 413 ms 64112 KiB
random_05.txt AC 403 ms 64160 KiB
random_06.txt AC 378 ms 63920 KiB
random_07.txt AC 431 ms 64480 KiB
random_08.txt AC 406 ms 64168 KiB
random_09.txt AC 421 ms 64036 KiB
random_10.txt AC 397 ms 63944 KiB
random_11.txt AC 389 ms 63624 KiB
random_12.txt AC 396 ms 63756 KiB
random_13.txt AC 383 ms 63704 KiB
random_14.txt AC 389 ms 63600 KiB
random_15.txt AC 356 ms 63884 KiB
random_16.txt AC 327 ms 63796 KiB
random_17.txt AC 368 ms 64264 KiB
random_18.txt AC 358 ms 63592 KiB
random_19.txt AC 364 ms 64748 KiB