码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces New Year Present 题解

时间:2014-05-02 23:46:03      阅读:516      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   ext   color   

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 ai 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 106operations (not necessarily minimum in length) the robot can use to put coins into the wallets. Help him.

Input

The first line contains integer n (2?≤?n?≤?300) — the number of wallets. The next line contains n integersa1,?a2,?...,?an (0?≤?ai?≤?300).

It is guaranteed that at least one ai is positive.

Output

Print the sequence that consists of k (1?≤?k?≤?106) 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 ai coins. If there are multiple answers, you can print any of them.

Sample test(s)
input
2
1 2
output
PRPLRP

也是有趣的题目,操作机器人,要求写一个指令序列。

不过本题对指令序列并没有太严格的要求,所以,基本上可以满足条件 - 放满钱币就可以了,对指令顺序没有要求。


下面是一种程序的写法,循环扫描,还有很多种写法的。

void NewYearPresent()
{
	int wallets = 0;
	cin>>wallets;
	assert(2 <= wallets && wallets <= 300);
	int *coins = new int[wallets];
	for (int i = 0; i < wallets; i++)
	{
		cin>>coins[i];
	}
	string commands;
	bool left = true;
	while (left)
	{
		left = false;
		for (int i = 0; i < wallets; i++)
		{
			if (coins[i] > 0) commands.push_back(‘P‘);
			if (i + 1 != wallets) commands.push_back(‘R‘);
			if (--coins[i] > 0) left = true;
		}
		if (left)
		{
			if (coins[wallets-1] > 0) commands.append("LR");
			left = false;
			for (int i = wallets - 1; i >= 0 ; i--)
			{
				if (coins[i] > 0) commands.push_back(‘P‘);
				if (0 != i) commands.push_back(‘L‘);
				if (--coins[i] > 0) left = true;
			}//小心:>0和判真伪不一样的,coins[i]为负数的时候也会为真
			if (coins[0] > 0) commands.append("RL");//注意这里
		}
	}
	std::cout<<commands;
	delete [] coins;
}





codeforces New Year Present 题解,布布扣,bubuko.com

codeforces New Year Present 题解

标签:style   blog   class   code   ext   color   

原文地址:http://blog.csdn.net/kenden23/article/details/24835509

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!