File size: 1,575 Bytes
bff4ec7
 
3805fe5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bff4ec7
 
 
 
3805fe5
 
bff4ec7
 
 
3805fe5
bff4ec7
3805fe5
 
bff4ec7
3805fe5
 
 
 
 
bff4ec7
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# P1368 Minimal Representation

## Abstract model

For a sequence $a_0,a_1,\ldots,a_{n-1}$, its cyclic rotation beginning at
position $p$ is

$$
(a_p,a_{p+1},\ldots,a_{n-1},a_0,a_1,\ldots,a_{p-1}).
$$

Compare two rotations lexicographically and choose the smallest one. If
several starting positions produce the same smallest rotation, choose the
smallest starting position. The C verification case uses a doubled sequence
and the linear-time two-candidate elimination algorithm.

## Problem statement

Given a circular sequence of $n$ integers, output its lexicographically
smallest cyclic rotation.

The verification function `minimal_representation` receives the sequence, its
length, and an output array. It writes the smallest rotation into the
output array and returns the corresponding zero-based starting position in the
input sequence.

## Input and output

The original problem reads $n$ followed by $n$ integers and prints the
lexicographically smallest rotation, with adjacent values separated by spaces.

In the verification interface, `a` contains the $n$ input values and `out` has
capacity for $n$ values. The function declares a local buffer `b[2000]` and
copies the input twice into its first $2n$ cells. After
the function returns, `out` contains the smallest rotation and the return value
is an integer in $[0,n-1]$.

## Constraints

- Original problem: $1 \le n \le 300000$.
- Verification case: $1 \le n \le 1000$.
- Every sequence value is a signed C `int`.

## Source

[Luogu P1368, "Minimal Representation"](https://www.luogu.com.cn/problem/P1368).