codeforces B. New Year Present 解题报告
题目链接: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 解题报告的更多相关文章
- Codeforces Educational Round 92 赛后解题报告(A-G)
Codeforces Educational Round 92 赛后解题报告 惨 huayucaiji 惨 A. LCM Problem 赛前:A题嘛,总归简单的咯 赛后:A题这种**题居然想了20m ...
- codeforces 476C.Dreamoon and Sums 解题报告
题目链接:http://codeforces.com/problemset/problem/476/C 题目意思:给出两个数:a 和 b,要求算出 (x/b) / (x%b) == k,其中 k 的取 ...
- Codeforces Round #382 (Div. 2) 解题报告
CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...
- codeforces 460C. Present 解题报告
题目链接:http://codeforces.com/submissions/ywindysai 题目意思:有 n 朵花,每朵花都有一定的高度(第 i 朵花对应 ai),m 天之后要把这些花送给别人. ...
- codeforces 507B. Amr and Pins 解题报告
题目链接:http://codeforces.com/problemset/problem/507/B 题目意思:给出圆的半径,以及圆心坐标和最终圆心要到达的坐标位置.问最少步数是多少.移动见下图.( ...
- codeforces 500B.New Year Permutation 解题报告
题目链接:http://codeforces.com/problemset/problem/500/B 题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn.紧接着是一个 ...
- codeforces B. Xenia and Ringroad 解题报告
题目链接:http://codeforces.com/problemset/problem/339/B 题目理解不难,这句是解题的关键 In order to complete the i-th ta ...
- Codeforces Round #262 (Div. 2)解题报告
详见:http://robotcator.logdown.com/posts/221514-codeforces-round-262-div-2 1:A. Vasya and Socks http ...
- codeforces 462C Appleman and Toastman 解题报告
题目链接:http://codeforces.com/problemset/problem/461/A 题目意思:给出一群由 n 个数组成的集合你,依次循环执行两种操作: (1)每次Toastman得 ...
随机推荐
- 【BZOJ-3293&1465&1045】分金币&糖果传递×2 中位数 + 乱搞
3293: [Cqoi2011]分金币 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 854 Solved: 476[Submit][Status] ...
- The constructor ClassPathXmlApplicationContext(String) refers to the missing type BeansException
Spring4.X配置一个简单的读取sping的xml的配置文件结果
- 洛谷P1136 迎接仪式
题目描述 LHX教主要来X市指导OI学习工作了.为了迎接教主,在一条道路旁,一群Orz教主er穿着文化衫站在道路两旁迎接教主,每件文化衫上都印着大字.一旁的Orzer依次摆出“欢迎欢迎欢迎欢迎……”的 ...
- POJ 1469 COURSES
COURSES Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20478 Accepted: 8056 Descript ...
- servlet 中 web.xml
<servlet> <servlet-mapping> 他们之间的关系可以使一对一,也可是一对多的关系. <servlet> <servlet-name> ...
- 在Vs2012 中使用SQL Server 2012 Express LocalDB打开Sqlserver2012数据库
http://www.cnblogs.com/huangtailang/p/4221164.html 背景:个人电脑中使用的是VS2012,数据库为2008R2,最近需要打开一个SqlServer20 ...
- PHP输出表格的方法
<?php function table($n) //几行几列表 { echo'<table border="1" width="500" heig ...
- Chord算法
转自:http://blog.csdn.net/wangxiaoqin00007/article/details/7374833 虽然网上搜索CHord,一搜一大堆,但大多讲得不太清楚明白.今天发现一 ...
- Python socket编程之七:多窗口的应用
f1.py # -*- coding: utf-8 -*- import socket import struct import sqlalchemy import pandas ########## ...
- 多一个“点”给IIS与ASP.NET带来的问题
[IIS] 一个网站如果用的是IIS(假设没有在前端7层负载均衡中对这种场景进行特殊处理),只要在浏览器地址栏中输入这个网站的域名并加上“.”,比如:www.cnblogs.com. ,就会引发“Ba ...