P17828 [CSP-J 2026] Palindrome / string

Background

When submitting your answer on Luogu, file input/output is not needed.

Description

Little R has a string $S=s_1\ldots s_L$ of length $L$, containing only lowercase English letters. Little R notices that although $S$ may be very long, it can be divided into $n$ consecutive segments, where the $i$-th segment $(1\le i\le n)$ consists of $a_i$ copies of the lowercase English letter $c_i$. Concatenating these $n$ segments in order gives $S$, so $L=\sum_{i=1}^{n}a_i$. Adjacent segments may contain the same letter. Little X likes palindromes. A string is called a palindrome if it is the same as the original string after being reversed; for example, `aba` and `abba` are palindromes, while `aabbc` is not. In other words, a string $S$ is a palindrome if and only if for all $i\ (1\le i\le L)$, $s_i=s_{L+1-i}$. Little X wants Little R to change $S$ into a palindrome. Little R can change the character at any position of $S$ into any lowercase English letter. He wants to satisfy Little X's request with as few changes as possible. You need to help Little R find the minimum number of positions whose characters must be changed to turn $S$ into a palindrome.

Input Format

Read the input from the file `string.in`. The first line of the input contains a positive integer $n$. The $(i+1)$-th line $(1\le i\le n)$ contains a positive integer $a_i$ and a lowercase English letter $c_i$.

Output Format

Write the output to the file `string.out`. Output one line containing one integer: the minimum number of positions whose characters Little R must change to turn $S$ into a palindrome.

Explanation/Hint

### Sample 1 Explanation Little R's string $S$ consists of $3$ segments: the first segment consists of $2$ copies of `a`, the second segment consists of $2$ copies of `b`, and the third segment consists of $1$ copy of `c`. Concatenating them in order gives $S=\mathtt{aabbc}$. Little R can change the last character `c` and the second-to-last character `b` both into `a`, obtaining the palindrome `aabaa`, changing $2$ positions in total. It can be proven that Little R must change at least $2$ positions to turn $S$ into a palindrome. ### Sample 2 Explanation Little R's string $S$ consists of $2$ segments: the first segment consists of $2$ copies of `a`, and the second segment consists of $3$ copies of `a`. Concatenating them in order gives $S=\mathtt{aaaaa}$, which is already a palindrome, so no changes are needed. ### Sample 4 See `string/4.in` and `string/4.ans` in the problem directory. This sample satisfies the constraints of test points $10\sim12$. ### Sample 5 See `string/5.in` and `string/5.ans` in the problem directory. This sample satisfies the constraints of test points $13\sim16$. ### Sample 6 See `string/6.in` and `string/6.ans` in the problem directory. This sample satisfies the constraints of test points $19\sim21$. ### Constraints For all testdata: - $1\le n\le 2\times10^5$; - For all $i\ (1\le i\le n)$, $1\le a_i\le10^9$, and $c_i$ is a lowercase English letter. ::cute-table{tuack} | Test Point | $n\le$ | $\max_{i=1}^{n}a_i\le$ | | :-: | :-: | :-: | | $1$ | $1$ | $5$ | | $2\sim4$ | $2$ | ^ | | $5,6$ | $10$ | $1$ | | $7,8$ | ^ | $50$ | | $9$ | $2000$ | $1$ | | $10\sim12$ | ^ | $50$ | | $13\sim16$ | ^| $10^9$ | | $17,18$ | $2\times10^5$ | $1$ | | $19\sim21$ | ^ | $500$ | | $22\sim25$ | ^ | $10^9$ | Translated by QwenWork