题目链接:http://codeforces.com/contest/379/problem/B

题目意思:给定一个有n个钱包的序列,其中第i个钱包需要投入ai个钱币,需要编写一个程序,使得在对第i个钱包不能连续投入两次钱币(其实对这句话理解得不是很好:Due to some technical malfunctions the robot cannot follow two "put a coin" instructions in a row。希望有错的话,大家能够指出)和只有三种操作:向左移动一步,向右移动一步和向当前位置投入钱币的条件下,输出把每个钱包需要投入的钱币数都完成的步骤。

一开始我的做法就是,输入每个钱包需要投入的钱币数时,先统计为0的钱包数,这样可避免为空时还需要投入钱币的情况。接着从左到右开始扫描,遇到需要投入钱币的钱包就投入一枚,而该钱包需要投入的钱币数就减1,继续向右移动,直到到达最后一个位置。紧接着是从右向左扫描,继续相同的操作.....直到所有钱包需要投入的钱币都为0就结束。

比较复杂

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; const int maxn = + ;
int vis[maxn], a[maxn]; int main()
{
int n, i, cnt, tcnt;
while (scanf("%d", &n) != EOF)
{
memset(vis, , sizeof(vis));
cnt = ;
for (i = ; i <= n; i++)
{
scanf("%d", &a[i]);
if (a[i] == ) // 统计不需要投入钱币的钱包数
{
vis[i] = ;
cnt++;
}
}
int flag = ; // 1: 向右移动,0:向左移动
while (cnt < n)
{
tcnt = ; // 标记当前所处的位置
while (flag)
{
if (a[tcnt] && tcnt < n)
{
printf("P"); // 投入一枚钱币
a[tcnt]--; // 当前钱包需要的钱币减一枚
}
if (!a[tcnt] && !vis[tcnt]) // 当前钱包投入一枚钱币之后刚好不再需要再投入钱币
{
vis[tcnt] = ;
cnt++;
}
if (cnt == n) // 所有钱包都不需要投入钱币
break;
tcnt++;
if (tcnt <= n) // 向右移动
printf("R");
else
flag = ; // 设为向左移动的标志
}
if (cnt == n)
break;
tcnt = n;
while (!flag)
{
if (a[tcnt] && tcnt > )
{
printf("P");
a[tcnt]--;
}
if (!a[tcnt] && !vis[tcnt])
{
vis[tcnt] = ;
cnt++;
}
if (cnt == n)
break;
tcnt--;
if (tcnt >= )
printf("L");
else
flag = ;
}
}
printf("\n");
}
return ;
}

以下是参考别人的代码再写的,可谓是真正实现了“构造法”的思想啊~~~边输入边处理,内存空间也省了。

 #include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std; int main()
{
int n, i, j, tmp;
while (scanf("%d", &n) != EOF)
{
for (i = ; i < n; i++)
{
scanf("%d", &tmp);
if (i)
printf("R"); // 除最左边的那个位置外,其余位置都需要从当前位置向右移动一位
for (j = ; j < tmp; j++) // tmp为0的时候不处理
{
printf("P");
if (i)
printf("LR"); // 防止右边越界,都要向左移,接着回归当前位置
else
printf("RL"); // 最左边的那个位置的特殊处理
}
}
printf("\n");
}
return ;
}

codeforces B. New Year Present 解题报告的更多相关文章

  1. Codeforces Educational Round 92 赛后解题报告(A-G)

    Codeforces Educational Round 92 赛后解题报告 惨 huayucaiji 惨 A. LCM Problem 赛前:A题嘛,总归简单的咯 赛后:A题这种**题居然想了20m ...

  2. codeforces 476C.Dreamoon and Sums 解题报告

    题目链接:http://codeforces.com/problemset/problem/476/C 题目意思:给出两个数:a 和 b,要求算出 (x/b) / (x%b) == k,其中 k 的取 ...

  3. Codeforces Round #382 (Div. 2) 解题报告

    CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...

  4. codeforces 460C. Present 解题报告

    题目链接:http://codeforces.com/submissions/ywindysai 题目意思:有 n 朵花,每朵花都有一定的高度(第 i 朵花对应 ai),m 天之后要把这些花送给别人. ...

  5. codeforces 507B. Amr and Pins 解题报告

    题目链接:http://codeforces.com/problemset/problem/507/B 题目意思:给出圆的半径,以及圆心坐标和最终圆心要到达的坐标位置.问最少步数是多少.移动见下图.( ...

  6. codeforces 500B.New Year Permutation 解题报告

    题目链接:http://codeforces.com/problemset/problem/500/B 题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn.紧接着是一个 ...

  7. codeforces B. Xenia and Ringroad 解题报告

    题目链接:http://codeforces.com/problemset/problem/339/B 题目理解不难,这句是解题的关键 In order to complete the i-th ta ...

  8. Codeforces Round #262 (Div. 2)解题报告

    详见:http://robotcator.logdown.com/posts/221514-codeforces-round-262-div-2 1:A. Vasya and Socks   http ...

  9. codeforces 462C Appleman and Toastman 解题报告

    题目链接:http://codeforces.com/problemset/problem/461/A 题目意思:给出一群由 n 个数组成的集合你,依次循环执行两种操作: (1)每次Toastman得 ...

随机推荐

  1. BZOJ-2929 洞穴攀岩 最大流Dinic(傻逼题)

    竟然没有1A真羞耻...1分钟不到读完题,10分钟不到打完....MD没仔细看...WA了一遍,贱! 2929: [Poi1999]洞穴攀行 Time Limit: 1 Sec Memory Limi ...

  2. ElasticSearch插件安装Head、Kopf与Bigdesk

    ElasticSearch-Head ElasticSearch-Head 是一个与Elastic集群(Cluster)相交互的Web前台. ES-Head的主要作用 它展现ES集群的拓扑结构,并且可 ...

  3. (BZOJ4538)HNOI2016 网络

    HNOI2016 Day1 T2 网络 Description 一个简单的网络系统可以被描述成一棵无根树.每个节点为一个服务器.连接服务器与服务器的数据线则看做一条树边.两个服务器进行数据的交互时,数 ...

  4. AU3学习资源

    AU3中文站:http://www.autoitx.com/

  5. 换了XCode版本之后,iOS应用启动时不占满全屏,上下有黑边

    原因是没有Retina4对应的启动图片,解决方法很简单,就是把Retina4对应的图片给补上就只可以了

  6. HDU1907 John

    Description Little John is playing very funny game with his younger brother. There is one big box fi ...

  7. java 中LinkedList的学习

    Java中,所有链表实际上都是双向链表的,即每个结点还存放在着指向前驱结点的引用. LinkedList中的contains方法检测某个元素是否出现在链表中. LinkedList类提供了一个用来访问 ...

  8. hihocoder #1058 Combination Lock

    传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Finally, you come to the interview room. You know that a ...

  9. Longest Increasing Common Subsequence (LICS)

    最长上升公共子序列(Longest Increasing Common Subsequence,LICS)也是经典DP问题,是LCS与LIS的混合. Problem 求数列 a[1..n], b[1. ...

  10. memcache 开发版

    memcache安装,如果是用xampp,一定要下载开发版本 解压开发包,将其中的include目录复制到应用的lampp目录下 tar -zxvf xampp-linux-devel-1.7.2.t ...