HDU4072Working at the Restaurant(模拟)

Working at the Restaurant

Description

Last night, Tom went on a date with a really nice girl. However, he forgot to take his credit card with him and he had no cash in his wallet, so he ended up working at the restaurant to pay for the bill.
His task is to take plates from the waiter when he comes from the tables, and pass them along when the diswasher requests them. It is very important for the plates to be washed in the same order as they are brought from the tables, as otherwise it could take
too long before a plate is washed, and leftover food might get stuck. Trying to hold all the plates in his hands is probably not a great idea, so Tom puts them on a table as soon as the waiter hands them over to him, and picks them up from the table again
when the time comes to pass them along to the dishwasher. There is space for only two piles of plates on the table, which will be referred to as pile 1 and pile 2. There is only one table Tom can use. Tom won last year‘s SWERC, so he is certainly capable of
optimizing for efficiency. You have to output a transcript of one possible way in which Tom might decide to organize the plates on the table during the process, given the sequence of plates and requests he receives.

Input

The input has several test cases. Each case begins with a line containing a number N (1 <=N <= 1 000), followed by N lines, which contain either DROP m or TAKE m, where m > 0 is the number of plates to
take or drop. DROP m represents that the next event is the waiter bringing m plates to Tom, so he has to drop them on the table, while TAKE m represents that the next event is Tom taking m plates from the table and passing them along in the right order. You
can assume that he never receives a TAKE m instruction when there are fewer than m plates on the table, and that the sum M of all values of m corresponding to DROP operations does not exceed 100 000. Note that there might be plates left on Tom‘s table when
the last request is issued, as Tom might be relieved of his duty to stay until the restaurant closes. The input ends with a line with N = 0, which must not be processed.

Output

For every test case, the output will be a series of lines describing the operations to be performed with the plates. The content of each line will be one of the following:

1、 DROP 1 m (DROP 2 m), m > 0, if Tom needs to take a plate from the waiter, drop it on top of pile 1 (pile 2), and repeat this operation m times in total.

2、 TAKE 1 m (TAKE 2 m), m > 0, if Tom needs to take a plate from the top of pile 1 (pile 2), pass it along to the dishwasher, and repeat m times in total.

3、 MOVE 1->2 m (MOVE 2->1 m), m > 0, if Tom needs to take a plate from the top of pile 1 (pile 2), drop it on top of pile 2 (pile 1), and repeat m times in total.

You must output at most 6N lines, and the total number of movements of plates in your transcript (that is, the sum of the m‘s printed in your output, for all three kinds of operations), must be at most 6M, as otherwise Tom won‘t be able to cope with all the
work.Note that Tom must obey the commands in the same order as they are issued. This means that, if he receives a TAKE m command, he must perform a certain number of MOVE and TAKE operations such that the sum of the numbers of plates taken adds up exactly
to m before performing the operations corresponding to the next command; and if he receives a DROP m command, he must perform a number of DROP or MOVE operations for which the sum of the nu- mbers of plates dropped adds up exactly to m before performing the
operations corresponding to the next command.Of course, it is also forbidden to take plates from the waiter or pass them along to the dishwasher in the absence of the corr- esponding order. There must be an empty line between the outputs of di erent cases.
Any solution satisfying these conditions will be accepted.

Sample Input

 3
DROP 100
TAKE 50
TAKE 20
3
DROP 3
DROP 5
TAKE 8
0 

Sample Output

 DROP 2 100
MOVE 2->1 100
TAKE 1 50
TAKE 1 20

DROP 2 3
DROP 2 5
MOVE 2->1 8
TAKE 1 8 

题意:当读到 "DROP m" 从侍者手中接过盘子 m 个盘子放在 2 号堆。当读到 "TAKE m" ,就要从 1 号堆拿出 m 个盘子给洗碗机。题意要求拿出的盘子的顺序和进来的顺序一致。这就要用到 "MOVE 1->2  t" 操作,从 1 号堆转移 t 个盘子到 2 号堆。 注意每次取盘子都是从最顶端开始拿。每两个例子间空一行。

思路:模拟。每次 1 号堆为空都要把 2 号堆的所有盘子移到 1 号堆,这样子才能够保证顺序不会出错。

<span style="font-size:18px;">#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;

const double PI = acos(-1.0);
const double e = 2.718281828459;
const double eps = 1e-8;
char s[10];

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int n;
    int flag = 0;
    while(cin>>n && n)
    {
        if(!flag)
            flag = 1;
        else
            printf("\n");
        int sum1, sum2, m;
        sum1 = sum2 = 0;
        while(n--)
        {
            cin>>s>>m;
            if(s[0] == 'D')
            {   <span><span class="comment">//遇到DROP将盘子存到2号堆,sum2+=m,同时输出 "DROP" 操作</span><span></span></span>
                printf("DROP 2 %d\n", m);
                sum2 += m;
            }
            else
            {
                if(sum1 >= m)
                {   //如果1号堆剩下的盘子数 >= m,就直接输出 "TAKE" 操作
                    printf("TAKE 1 %d\n", m);
                    sum1 -= m;
                }
                else
                {   //如果1号堆剩下的盘子数 < m,先把1号堆的所有盘子输出
                    if(sum1 > 0)
                    {
                        printf("TAKE 1 %d\n", sum1);
                        m -= sum1;
                        sum1 = 0;
                    }   // 把2号堆的所有盘子移到1号堆
                    printf("MOVE 2->1 %d\n", sum2);
                    sum1 = sum2;
                    sum2 = 0;  // 输出剩下的 m 值
                    if(m > 0)
                    {
                        printf("TAKE 1 %d\n", m);
                        sum1 -= m;
                    }
                }
            }
        }
    }
    return 0;
}

</span>
时间: 2024-12-18 18:22:04

HDU4072Working at the Restaurant(模拟)的相关文章

POJ 2424 Flo&#39;s Restaurant 模拟

Flo's Restaurant Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2923   Accepted: 916 Description Sick and tired of pushing paper in the dreary bleary-eyed world of finance, Flo ditched her desk job and built her own restaurant. In the s

hdu4883 TIANKENG’s restaurant 模拟下就好了

TIANKENG's restaurant Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 102    Accepted Submission(s): 45 Problem Description TIANKENG manages a restaurant after graduating from ZCMU, and tens o

HDU 4883 TIANKENG’s restaurant(模拟)

题意  天坑开了个饭店  他知道所有客人的进来时间和出去的时间  求天坑至少准备多少张凳子 以分钟为单位 直接模拟就行了   peo[i]代表第i分钟的人  第i组人第si分钟进来 第so分钟出去  那么j从si到so  peo[j]都加上这组的人数  最后看第几分钟人最多就是答案了 #include<cstdio> #include<cstring> using namespace std; const int N = 1441; int hi, ho, mi, mo, si,

HDU 4883 TIANKENG’s restaurant Bestcoder 2-1(模拟)

TIANKENG's restaurant Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description TIANKENG manages a restaurant after graduating from ZCMU, and tens of t

HDU1103 Flo&#39;s Restaurant 【模拟】

Flo's Restaurant Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1112    Accepted Submission(s): 342 Problem Description Sick and tired of pushing paper in the dreary bleary-eyed world of finan

NOIP模拟赛(2017.9.15) -餐厅(restaurant)

餐厅(restaurant) [问题描述] 小R最近在玩一款模拟餐厅打工的游戏,其中有一个叠盘子的小游戏小R很喜欢.这个小游戏是这样的:有一个放盘子的机器会在一条直线上运动,机器里装着n个盘子,其中第i个盘子半径为ri,并且如果要放下该盘子,盘子的中心必须放在直线上xi的位置上,小R可以决定放下哪些盘子和放下这些盘子的顺序,盘子可以放在空位上,或者叠在一个上面没有其他盘子的盘子上,但要求被叠的盘子必须包含要叠上去的盘子.小R想要让叠出的盘子尽量高,请你计算出最高的一叠最多能叠几个盘子. [输入格

【模拟】Flo&#39;s Restaurant

[poj2424]Flo's Restaurant Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2960   Accepted: 929 Description Sick and tired of pushing paper in the dreary bleary-eyed world of finance, Flo ditched her desk job and built her own restaurant.

hdu4886 TIANKENG’s restaurant(Ⅱ) (trie树或者模拟进制)

TIANKENG’s restaurant(Ⅱ) Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 130107/65536 K (Java/Others)Total Submission(s): 456    Accepted Submission(s): 149 Problem Description After improving the marketing strategy, TIANKENG has made a fort

九校模拟——餐馆(restaurant)

1 餐馆(restaurant) 1.1 题目背景 铜企鹅是企鹅餐馆的老板,他正在计划如何使得自己本年度收益增加. 1.2 题目描述 共有n 种食材,一份食材i 需要花ti 小时不间断地进行播种,施肥, 直至收获.当然,一份食材i 是可以直接卖掉得到wi 块钱的. 招牌菜共有m 种,一份招牌菜i 需要消耗一定的食材,花Ti 小时不 间断地来烹饪,叫卖,并最终卖出得到Wi 块钱. 整个季度换算下来一共有Tmax 小时可供你使用,铜企鹅需要在这期间 赚到最多的钱,这样他才有足够多的钱来steam 剁