DMOPC '20 Contest 2 P5 - Majority Subarrays

View as PDF

Submit solution


Points: 25 (partial)
Time limit: 1.2s
Memory limit: 512M

Author:
Problem type

You are given an array of N positive integers a1,a2,,aN. Find the number of subarrays that have a majority element.

A subarray of length l has a majority element if some number appears strictly greater than 12l times in the subarray.

Constraints

1aiN for all i.

Subtask 1 [5%]

1N100

Subtask 2 [10%]

1N500

Subtask 3 [15%]

1N2000

Subtask 4 [20%]

1N10000

Subtask 5 [25%]

1N100000

Subtask 6 [25%]

1N2000000

Input Specification

The first line of input contains one integer: N.

The second line of input contains N space-separated integers. The ith of these integers is ai.

Output Specification

Output one number: the number of subarrays that have a majority element.

Sample Input 1

Copy
4
1 2 1 3

Sample Output 1

Copy
5

Explanation for Sample Output 1

All 4 single-element subarrays have a majority element, and the subarray [1,2,1] also has a majority element.

Sample Input 2

Copy
10
1 3 2 3 1 3 3 2 2 4

Sample Output 2

Copy
25

Comments

There are no comments at the moment.