H. Data Structures Exam (A)

Codeforces
IDCF10140H
Time1000ms
Memory256MB
Difficulty
English · Original
Formal · Original
It's exam week at PSUT, and Doctor Sufyan is in big trouble; the room he had reserved for his Data Structures exam was preoccupied and the only available room left was the meeting room, which had a single large round table with N seats around it numbered from 1 to N in one direction. Doctor Sufyan hates cheating, and this particular class of N students was known for cheating in almost every exam they took. He knows that there are M pairs of friends in this class that are likely to copy each other's answers. However, the two friends will not be able to cheat if there is an even number of students seated between them from *both* directions. Given the number of students in this class N, their seating order around the table, and the potential cheating pairs of friends, help Doctor Sufyan figure out the number of pairs that might be able to cheat. The first line of input contains two space-separated integers N, M (3 ≤ N ≤ 104) , the number of students (and seats) and the number of cheating pairs of friends, respectively. The second line contains N distinct space-separated integers between 1 and N, representing the seating order of the students around the table; the ith integer represents the ID (from 1 to N) of the student sitting in the ith position. Each of the following M lines contains two space-separated integers a b (1 ≤ a, b ≤ N) (a ≠ b), represent a pair of friends that are most likely to cheat. It is guaranteed that each pair of students will appear at most once. Print the number of pairs of students that might be able to cheat. ## Input The first line of input contains two space-separated integers N, M (3 ≤ N ≤ 104) , the number of students (and seats) and the number of cheating pairs of friends, respectively.The second line contains N distinct space-separated integers between 1 and N, representing the seating order of the students around the table; the ith integer represents the ID (from 1 to N) of the student sitting in the ith position.Each of the following M lines contains two space-separated integers a b (1 ≤ a, b ≤ N) (a ≠ b), represent a pair of friends that are most likely to cheat.It is guaranteed that each pair of students will appear at most once. ## Output Print the number of pairs of students that might be able to cheat. [samples]
**Definitions** Let $ N, M \in \mathbb{Z}^+ $ denote the number of students and the number of cheating pairs, respectively. Let $ S = (s_1, s_2, \dots, s_N) $ be the seating arrangement, where $ s_i \in \{1, 2, \dots, N\} $ is the student ID at position $ i $ (circular). Let $ P = \{(a_j, b_j) \mid j \in \{1, \dots, M\}\} $ be the set of cheating pairs, with $ a_j \ne b_j $. Define a position function $ \text{pos}: \{1, \dots, N\} \to \{1, \dots, N\} $ such that $ \text{pos}(x) = i $ iff $ s_i = x $. **Constraints** 1. $ 3 \le N \le 10^4 $ 2. $ 1 \le M \le \binom{N}{2} $ 3. All student IDs in $ S $ are distinct and in $ \{1, \dots, N\} $ 4. For each pair $ (a, b) \in P $, $ a \ne b $ **Objective** For each pair $ (a, b) \in P $, let $ i = \text{pos}(a) $, $ j = \text{pos}(b) $. Compute the two circular distances: - $ d_1 = |i - j| - 1 $ - $ d_2 = N - |i - j| - 1 $ The pair $ (a, b) $ can cheat **if and only if** both $ d_1 $ and $ d_2 $ are even. Count the number of such pairs in $ P $.
API Response (JSON)
{
  "problem": {
    "name": "H. Data Structures Exam (A)",
    "description": {
      "content": "It's exam week at PSUT, and Doctor Sufyan is in big trouble; the room he had reserved for his Data Structures exam was preoccupied and the only available room left was the meeting room, which had a si",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10140H"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "It's exam week at PSUT, and Doctor Sufyan is in big trouble; the room he had reserved for his Data Structures exam was preoccupied and the only available room left was the meeting room, which had a si...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ N, M \\in \\mathbb{Z}^+ $ denote the number of students and the number of cheating pairs, respectively.  \nLet $ S = (s_1, s_2, \\dots, s_N) $ be the seating arrangement, where $ s...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments