#P16539. [EGOI 2026] 摩天轮 / Ferris Wheel

    ID: 18912 远端评测题 1000ms 1024MiB 尝试: 0 已通过: 0 显示难度普及− 上传者: 标签>Special Judge2026EGOI(欧洲/女生)

[EGOI 2026] 摩天轮 / Ferris Wheel

Problem Description

In the main square of Cesenatico, there is a colorful Ferris wheel, one of the city’s landmark attractions. During winter, the Ferris wheel was dismantled and stored away. But now summer is coming, and it is time to assemble it again. The dismantled parts have just arrived at the square, and with your help, we are ready to put them all together.

In front of you there are NN separate cabins, and they need to be connected to each other in a circular way to form a Ferris wheel. These cabins are numbered from 00 to N−1N-1, but they are not necessarily arranged in the order in which they should be connected.

Each cabin has a special connection point used to connect clockwise to the next cabin. Each connection point can be of one of two types:

  • Type [+]: it can only connect to a cabin with a larger number.
  • Type [-]: it can only connect to a cabin with a smaller number.

In the sample below, cabin 22 has a [+] type connection point. This means that the next cabin clockwise must be cabin 33 or 44.

:::align{center}

N=5N=5, five separate cabins, each with a [+] or [-] type connection point. :::

Given the number of cabins and the type of connection point on each cabin, your task is to determine whether these NN cabins can be assembled into a Ferris wheel. If the answer is yes, you also need to find one possible clockwise order of the cabins on the Ferris wheel.

:::align{center}

A valid Ferris wheel that can be assembled from the five cabins above. :::

Figure 2 shows a valid Ferris wheel assembled from the five cabins in Figure 1.

Formally, a valid cabin order is a sequence of numbers C0,C1,…,CN−1C_0, C_1, \dots, C_{N-1} with the following properties:

  • Each number from 00 to N−1N-1 appears in the sequence exactly once.
  • For each 0<=i<=N−20 <= i <= N-2, cabin Ci+1C_{i+1} must satisfy the condition required by the type of the connection point on cabin CiC_i. That is, if the type of cabin CiC_i is [+], then Ci+1>CiC_{i+1} > C_i; if it is [-], then Ci+1<CiC_{i+1} < C_i.
  • In addition, cabin C0C_0 must satisfy the condition required by the type of the connection point on cabin CN−1C_{N-1}.

Input Format

The input consists of two lines. The first line contains an integer NN, representing the number of cabins.

The second line contains a string SS of length NN, consisting of the characters '+' and '-'. If Si=S_i = '+', then cabin ii has a [+] type connection point. If Si=S_i = '-', then cabin ii has a [-] type connection point.

Output Format

If there is no order that satisfies the conditions, output NO.

Otherwise, output YES, and on the next line output NN integers, the cabin numbers on a valid Ferris wheel in clockwise order. You may start from any cabin. If there are multiple solutions, you may output any one of them.

3
+++
NO
5
+-+--
YES
0 3 2 4 1
7
------+
NO
8
+-+-+-+-
YES
3 2 4 6 7 1 0 5
11
+++--+-++--

YES
10 0 5 8 9 4 2 6 3 1 7

Hint

Sample Explanation

First example. There are three cabins. Since all connection points are of type [+], each cabin must be followed by a cabin with a larger number. It can be proven that there is no arrangement of these three cabins that satisfies this condition, so the answer is NO.

Second example. See Figure 1 and Figure 2 in the statement. There are five cabins. We must arrange them clockwise so that:

  • cabins 0 and 2 (connection point type [+]) are followed by a cabin with a larger number;
  • cabins 1, 3, and 4 (connection point type [-]) are followed by a cabin with a smaller number.

The figure below shows a Ferris wheel that satisfies all these conditions. For all [+] type connection points, the condition holds because 0<30 < 3 and 2<42 < 4. For all [-] type connection points, the condition holds because 1>01 > 0, 3>23 > 2, and 4>14 > 1. There is more than one valid output for this Ferris wheel: besides 0 3 2 4 1, you can also output 3 2 4 1 0, 2 4 1 0 3, 4 1 0 3 2, or 1 0 3 2 4.

:::align{center}

The Ferris wheel for the second example (this figure is the same as Figure 2). :::

In the third example, there are seven cabins: all connection points are of type [-], except the last one which is of type [+]. Therefore, we must arrange the cabins so that each cabin is followed by a smaller-numbered cabin, except cabin 6, which must be followed by a larger-numbered cabin. It can be proven that no such order exists, so the answer is NO.

The figures below show the Ferris wheels corresponding to the outputs of the last two samples.

:::align{center}

The Ferris wheel for the fourth sample. :::

:::align{center}

The Ferris wheel for the fifth sample. :::

Constraints

  • 3≤N≤3000003 \leq N \leq 300000。
  • Si=S_i = '+' or '-'。

Scoring

Your program will be tested on testdata divided into several subtasks. To get the score for a subtask, you must solve all testdata in that subtask correctly.

  • Subtask 0 [00 points]: samples.
  • Subtask 1 [1616 points]: N=3N = 3.
  • Subtask 2 [1313 points]: the string SS contains exactly one '+'.
  • Subtask 3 [2424 points]: the characters '+' and '-' in SS alternate; that is, for every 0≤i≤N−20 \le i \le N - 2, we have Si≠Si+1S_i \neq S_{i+1}.
  • Subtask 4 [2323 points]: N≤1000N \le 1000.
  • Subtask 5 [2424 points]: no additional constraints.

Translated by ChatGPT 5