您的位置:首页 > 编程语言 > Go语言

codeforces--goodbye2013 --B. New Year Present

2013-12-31 01:04 537 查看
B. New Year Presenttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe 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 putn empty wallets from left to right in a row and decided how much money to put in what wallet. Vasily decided to putai coins to thei-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 suchwallet 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 most106 operations (not necessarily minimum in length) the robot can use to put coins into the wallets. Help him.InputThe first line contains integer n(2 ≤ n ≤ 300) — the number of wallets. The next line containsn integers a1, a2, ..., an(0 ≤ ai ≤ 300).It is guaranteed that at least one ai is positive.OutputPrint the sequence that consists of k(1 ≤ k ≤ 106) characters, each of them equals: "L", "R" or "P". Each character of the sequenceis an instruction to the robot. Character "L" orders to move to the left, character "R" orders to move to the right, character "P" orders therobot 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" atwallet n.As a result of the performed operations, the i-th wallet from the left must contain exactlyai coins. If there are multiple answers, you can print any of them.Sample test(s)Input
2
1 2
Output
PRPLRP
Input
4
0 2 0 2
Output
RPRRPLLPLRRRP
该题中最重要的是两边的钱数  因为从左向右,再从右向左的过程中 中间的部分会得到两次钱 而两边的部分只得到一次钱  所以先找max 再确定左边第一个max的位置和最后一个max的位置 , 是这两边的max 和 左右两侧的数 均为0时 则分配结束
坑 1 两头的数据要注意为1 ;
坑 2 当所有max都为0后 立刻结束  而不是等该行运行到头再结束
#include <stdio.h>int main(){int a[302] , i , j , n , k1 , k2 , max = 0;scanf("%d", &n);for(i = 1 ; i <= n ; i++){scanf("%d", &a[i]) ;if(a[i] >= max){ max = a[i];k2 = i ;}}for(i = 1 ; i <= n ; i++){if(max == a[i]) {k1 = i ; break;}}for(i = 1 ; ; i++){if(i%2==1){for(j = 1 ; j <= n ; j++){if(a[j] != 0){printf("P");a[j]--;}if(a[k2]==0 && a[k1]==0 && a[1]==0 && a==0){printf("\n");return 0;}if(j != n)printf("R");}}else{printf("L");for(j = n-1 ; j >= 2 ; j--){if(a[j] != 0){printf("P");a[j]--;}if(a[k1]==0 && a[k2]==0 && a[1]==0 && a==0){printf("\n");return 0;}printf("L");}}}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: