Under construction

Still being built. Things may change

Palindrome Check

Time limit1sMemory limit1024 MB

Summary
Decide whether a hidden sequence of length N is a palindrome using only count_pair and find_character queries under a limit on query sizes.
Level

Medium6 of 10

Topics
Array, Implementation
Solved
No attempts yet

Problem

KOI is running a new event to promote its algorithm contest. To take part, you must determine whether a secret sequence SS, known only to KOI, is a palindrome.

A sequence is a palindrome if it stays the same when reversed. A sequence SS of length NN is a palindrome if S[i]=S[N−1−i]S[i] = S[N - 1 - i] holds for every 0≤i≤N−10 \le i \le N - 1. For example, [1,2,3,2,1][1, 2, 3, 2, 1] and [1,2,2,1][1, 2, 2, 1] are palindromes, but [1,2,3,1][1, 2, 3, 1] and [1,2,2][1, 2, 2] are not.

You initially know the length NN of the secret sequence SS. Each element of SS is an integer from 1 to 5,000. To help the participants, KOI provides two machines.

  • The count_pair machine takes three distinct integers xx, yy, and zz. It returns the number of equal pairs among S[x]S[x], S[y]S[y], and S[z]S[z]. For example, if S[x]=S[y]=S[z]S[x] = S[y] = S[z], it returns 3.
  • The find_character machine takes an integer xx and a list of integers YY. It returns 1 if some yy in YY satisfies S[x]=S[y]S[x] = S[y], and 0 otherwise.

Every number you input to either machine must be an integer from 0 to N−1N - 1. The sum of the sizes of the lists YY you input to find_character must be at most NN.

You must determine whether SS is a palindrome while using the machines a small number of times.

Constraints

  • 5≤N≤5 0005 \le N \le 5\,000
  • 1≤S[i]≤5 0001 \le S[i] \le 5\,000 (for every 0≤i≤N−10 \le i \le N - 1)
  • Let MM be the sum of NN over all test cases in one input. Then 5≤M≤5 0005 \le M \le 5\,000.
  • The grader is not adaptive. SS is fixed when the grader starts and does not change with the queries.

Examples1

  1. Example 1

    Input
    5
    1 2 3 4 5
    
    Expected output
    NO