CF379B New Year Present

2023-12-14 11:56:22

New Year Present

绿色的大水题

题面翻译

新年伊始,人们正忙着准备新年礼物。程序员Vasily也不例外。

Vasily 知道最好的礼物(不,不是比赛)是钱。他把n个空钱包从左到右排成一行,并决定好了每个钱包打算放多少钱。 他打算放 a i a_i ai?个硬币在左数第i个钱包里。

他是个很忙的人,所以这个任务就交给了他的机器人。这个机器人初始站在最左边的钱包里。他可以执行这样三个操作: 左走一格、右走一格(均不允许越界)、在当前钱包里塞一块钱。 由于一些技术问题,这个机器人不能连续两次执行“塞钱”操作。

他不想等太久,所以他想编写一个给机器人执行的程序并使得步骤数<= 1 0 6 10^6 106(不要求最短),并完成所有硬币的放置。. 请帮助他完成。

输入:

第一行一个整数 n , ( 2 < = n < = 300 ) n ,( 2 <=n <= 300) n,(2<=n<=300),表示钱包数目。接下来一行n个数,表示放的硬币数。 0 < = a i < = 300 0 <= a_i <= 300 0<=ai?<=300

输出:

输出一个长度不超过 1 0 6 10^6 106的字符串,其中L代表上文中“左走一格”,R代表“右走一格” P表示放钱包。不能越界、不能连续两次做P操作。

你可以任意输出一种方案。

题目描述

The New Year is coming! That’s why many people today are busy preparing New Year presents. Vasily the Programmer is no exception.

Vasily knows that the best present is (no, it’s not a contest) money. He’s put $ n $ empty wallets from left to right in a row and decided how much money to put in what wallet. Vasily decided to put $ a_{i} $ coins to the $ i $ -th wallet from the left.

Vasily is a very busy man, so the money are sorted into the bags by his robot. Initially, the robot stands by the leftmost wallet in the row. The robot can follow instructions of three types: go to the wallet that is to the left of the current one (if such wallet exists), go to the wallet that is to the right of the current one (if such wallet exists), put a coin to the current wallet. Due to some technical malfunctions the robot cannot follow two “put a coin” instructions in a row.

Vasily doesn’t want to wait for long, so he wants to write a program for the robot that contains at most $ 10^{6} $ operations (not necessarily minimum in length) the robot can use to put coins into the wallets. Help him.

输入格式

The first line contains integer $ n $ $ (2<=n<=300) $ — the number of wallets. The next line contains $ n $ integers $ a_{1},a_{2},…,a_{n} $ $ (0<=a_{i}<=300) $ .

It is guaranteed that at least one $ a_{i} $ is positive.

输出格式

Print the sequence that consists of $ k $ $ (1<=k<=10^{6}) $ characters, each of them equals: “L”, “R” or “P”. Each character of the sequence is an instruction to the robot. Character “L” orders to move to the left, character “R” orders to move to the right, character “P” orders the robot to put a coin in the wallet. The robot is not allowed to go beyond the wallet line. In other words, you cannot give instructions “L” if the robot is at wallet 1, or “R” at wallet $ n $ .

As a result of the performed operations, the $ i $ -th wallet from the left must contain exactly $ a_{i} $ coins. If there are multiple answers, you can print any of them.

样例 #1

样例输入 #1

2
1 2

样例输出 #1

PRPLRP

样例 #2

样例输入 #2

4
0 2 0 2

样例输出 #2

RPRRPLLPLRRRP

题意简述
有一个机器人在放硬币,从最左边开始,每次可以选择向左一步或向右一步或放一个硬币,但是不能连续进行两次放硬币操作,求一个操作方式可以放完所有硬币,不要求最短。

思路
首先看到 n n n 的范围, n ≤ 300 n≤300 n300,序列长度小于 1 0 6 10^6 106

所以,不用怕序列过长。

又因为有 SPJ ,所以只要输出一个合法序列即可

用模拟即可解决。

如果没有不能连续进行两次放硬币操作这个限制,那解法显而易见,放完一个地方再放下一个。

那如果加上限制呢?

其实也差不多,放完一个后,左右横跳缓冲一下即可。

注意第一个位置不能先向左跳,最后一个位置不能先向右跳。

放完一个位置后要向右移动到达下一个位置,最后一个位置就不用了。

Code:

#include<bits/stdc++.h>
using namespace std;
int a[305];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    for(int i=1;i<=n;i++)
    {
        if(a[i]==0) //如果不用放
        {
            if(i!=n)
            {
                cout<<"R";
                continue;
            }
        }
        while(a[i]--)
        {
            if(i!=n)
            {
                cout<<"PRL";
            }
            else    //对于最后一个位置要特判
            {
                cout<<"PLR";
            }
        }
        if(i!=n)    如果已经是最后一个位置,就不用再往后了
        {
            cout<<"R";
        }
    }
    cout<<endl;
    return 0;
}

文章来源:https://blog.csdn.net/zth413021/article/details/134989310
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。