#P17306. [ICPC 2026 Xi'an I] Yesterday Once More (Hard Version)

    ID: 17197 Type: RemoteJudge 1000ms 512MiB Tried: 0 Accepted: 0 Difficulty: 8 Uploaded By: Tags>Special JudgeICPC2026省赛/邀请赛西安

[ICPC 2026 Xi'an I] Yesterday Once More (Hard Version)

题目描述

This is the hard version of this problem. The only difference between the easy and hard versions is the limit on the number of moves in your solution.

Yuki lives on a grid with n+1n + 1 rows and nn columns. The rows are numbered 11 to n+1n + 1 from top to bottom, and the columns are numbered 11 to nn from left to right. Let (i,j)(i, j) denote the cell at row ii and column jj.

There are n−1n - 1 obstacles on the grid, and their distribution satisfies the following conditions:

  • There are no obstacles in row 11 and row n+1n + 1.
  • For all 2≤i≤n2 \le i \le n, there is exactly\textbf{exactly} one obstacle in row ii.
  • For all 1≤j≤n1 \le j \le n, there is at most\textbf{at most} one obstacle in column jj.

Initially, Yuki is at (1,1)(1, 1). She has heard that a group of kangaroos lives in row n+1n + 1, so she wants to go to row n+1n + 1 to see the scenery.

To achieve her goal, Yuki can perform several moves. In each move, she chooses one of the four directions (up, down, left, right) and moves one cell in that direction. Specifically, if the target cell is outside the grid or contains an obstacle, the move is not executed.

Unfortunately, Yuki only knows the rules for the obstacle distribution, not the specific locations of the obstacles. Therefore, she wants you to help her specify the sequence of moves such that for any valid obstacle distribution, Yuki reaches\textbf{reaches} row n+1n + 1 at some point (she only needs to have reached row n+1n + 1 at least once; she does not need to remain there after all moves are completed).

Since Yuki is in a hurry, the number of moves in your solution must not exceed 10⋅n\boldsymbol{10 \cdot n}.

输入格式

A single line contains a positive integer nn (2≤n≤103)(2 \le n \le 10^3).

输出格式

The first line output an integer kk (1≤k≤10⋅n)(1 \le k \le 10 \cdot n), representing the number of moves in your solution.

The second line output a string ss of length kk, where sis_i represents the direction of Yuki's ii-th move:

  • If si=Us_i = \texttt U, Yuki moves up.
  • If si=Ds_i = \texttt D, Yuki moves down.
  • If si=Ls_i = \texttt L, Yuki moves left.
  • If si=Rs_i = \texttt R, Yuki moves right.
2
4
DRDD
3
17
DDDUUURDDDUUURDDD

提示

For the first example:

  • Let gray cells represent obstacles and white cells represent empty cells. The following image shows all possible obstacle distributions satisfying the requirements: :::align{center} :::
  • For the first obstacle distribution, Yuki's path is (1,1)→(1,1)→(1,2)→(2,2)→(3,2)(1,1) \to (1,1) \to (1,2) \to (2,2) \to (3,2).
  • For the second obstacle distribution, Yuki's path is (1,1)→(2,1)→(2,1)→(3,1)→(3,1)(1,1) \to (2,1) \to (2,1) \to (3,1) \to (3,1).
  • For every valid obstacle distribution, Yuki reaches row n+1n + 1, so the sample output is correct.

For the second example:

  • Let gray cells represent obstacles and white cells represent empty cells. The following image shows all possible obstacle distributions satisfying the requirements: :::align{center} :::
  • It is easy to prove that for any of these obstacle distributions, following the moves given in the sample output, Yuki will reach row n+1n + 1.