Topcoder上的一道题,求解答。

xiao7cn 2006-11-27 05:09:52
Problem Statement
Before the final round of the TCCC, audience members head to the buffet for lunch. The buffet line contains a number of food items, in a fixed order. Those indulging line up at the buffet, each intending to sample a subset of the items available.

It takes each person exactly 10 seconds to help themselves to a food item. Every 10 seconds, each person moves up as far as they can (without passing by any food they want, of course!). This movement takes no time. The first person in line moves immediately to each item he wants, in order, spending 10 seconds at each item. Depending on which items the rest of the people want, they may be able to move up to each item immediately, but also may have to spend some time waiting for the person they are following.

Each person is always either waiting in line before reaching the first food item, standing in front of exactly one food item, or has passed the last food item and is looking for a place to sit. Two people may not stand in front of the same food item at the same time. Also, people always remain in the order in which they lined up, and they never move backward in line. No one ever passes another person in line -- even after they have taken all the food they want, they still exit the buffet in line. People can only take a food item that they are standing directly in front of. Anyone who cannot reach the next food item they want (because they are waiting for the person in front of them) will advance as far as possible in line, to avoid blocking the people behind them unnecessarily.

Anxious to get back to the TCCC event, the people wish to get the group through the buffet line as quickly as possible. Given the list of food items in the buffet and the items that each person wants, return the optimal order in which the people should line up at the buffet so that everyone can get all the food they desire in the least total amount of time.

The food items available at the buffet will be given as a String food. Each item in food will contain only letters ('a'-'z', 'A'-'Z'), and there will be exactly one space between each pair of adjacent items. Each item in food will be unique. The order of items in food is the order they appear in the buffet line, with the first item in food being the first item available. The items each person wants are given in a String[] cravings. There will be one element in cravings for each person. Each element in cravings will be a list of food items found in food, with exactly one space between each food items. Within each element of cravings, each food item will be unique.

Return a int[] containing the optimal order, where each element is the 0-based index of a person in cravings, and elements are ordered from first to last in line. If there are multiple solutions, return the one that comes first lexicographically. A int[] a1 comes before a int[] a2 lexicographically if, at the first element at which they differ, a1 contains the smaller value.
...全文
553 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
wn61 2006-12-05
  • 打赏
  • 举报
回复
到 数据结构和算法 板块 或者 清华BBS 试试
xiao7cn 2006-12-05
  • 打赏
  • 举报
回复
.
xiao7cn 2006-12-03
  • 打赏
  • 举报
回复
Return a int[] containing the optimal order, where each element is the 0-based index of a person in cravings

返回int[] 包含最优顺序,其中每个元素代表这个人的出现顺序。
--------------
改成
返回int[] 包含最优顺序,其中每个元素是在craving中的人的下标。
xiao7cn 2006-12-03
  • 打赏
  • 举报
回复
food的第一个元素即每一个可以打的菜

改成

food的第一个元素即第一个可以打的菜
daidodo 2006-11-30
  • 打赏
  • 举报
回复
说实话,题目没大看懂,example 0)的答案为什么不是{1,2,0}?下面的解释不也说
dates goes first, followed by the person who wants beans, and then the person who wants applesauce吗?
xiao7cn 2006-11-29
  • 打赏
  • 举报
回复
Java描述

在TCCC最后一轮比赛之前,观众们涌至餐柜去吃午饭。在餐柜前,以固定的顺序摆着一排各式各样的菜。他们排成一排,每个人打其中几样菜。

每人要花10秒钟时间来打一个菜。每隔10秒,他们就尽可能往前走(当然,不能略过他们想要的菜)。这个过程不需要时间。排队的第一个人立即可以走到他想要的菜前,依次要在每个菜前花10秒钟来打。要看剩下的人要什么菜,他们都可以立即走到每个菜前,但他们也可能花必要的时间来等前面的人。

每个人都在打菜之前都要排队,站在一个菜的位置上,或者打完最后一个菜后找一个地方休息。两个人不能同时站在一个菜的位置上。另外,人们在排队时一直保持顺序,不能排回到后面,也不能插队--甚至他们打完了所要的菜,仍然必须从队中依次走出。人们只能打他们直接面对的菜。如果某人暂时不能打到他想要的菜(因为前面有人挡着),也必须尽可能地往前走,以免阻碍后面的人。

急于回到TCCC现场,人们都希望能尽快排完队。给出一个橱柜的菜单,以及每个人想要的菜,求人们排队的最优顺序,使得每人都能打到他们的菜,并且整个过程耗时最少。

橱柜的菜单用String food给出,菜的名字只包含a-z,A-Z字符,每个菜名以空格隔开。每个菜名都是唯一的。顺序与菜在橱柜里排的顺序是一样的,food的第一个元素即每一个可以打的菜。每人想要的菜以String[] cravings给出。每个元素属于某一个人的。cravings的每个元素在food中都可以找到,每个菜用空格分开。cravings里的每个元素也是唯一的。

返回int[] 包含最优顺序,其中每个元素代表这个人的出现顺序。如果有多种解,返回按词典排序最前面的那个。
xiao7cn 2006-11-29
  • 打赏
  • 举报
回复
up
xiao7cn 2006-11-29
  • 打赏
  • 举报
回复
?
xiao7cn 2006-11-28
  • 打赏
  • 举报
回复
?
daidodo 2006-11-28
  • 打赏
  • 举报
回复
mark
shunan 2006-11-28
  • 打赏
  • 举报
回复
建议楼主把问题的理解用尽量少的语言表达出来,我们这些过客哪有那么多时间专门来看长篇题目再想解法呢?这样肯定浪费时间啊
wn61 2006-11-28
  • 打赏
  • 举报
回复
如果翻成英文,效果可能要好一些
xiao7cn 2006-11-27
  • 打赏
  • 举报
回复
Topcoder把这道归为 Dynamic Programming里面,真不知道怎么能做到。

用brute force的就不要贴上来了。
xiao7cn 2006-11-27
  • 打赏
  • 举报
回复
Definition
Class: BuffetLine
Method: order
Parameters: String, String[]
Returns: int[]
Method signature: int[] order(String food, String[] cravings)
(be sure your method is public)




Constraints
- food will contain between 1 and 50 characters, inclusive.
- food will be formatted as described in the problem statement and contain between 1 and 8 items, inclusive.
- food will not contain any duplicate items.
- cravings will contain between 1 and 12 elements, inclusive.
- Each element of cravings will contain between 0 and 50 characters, inclusive.
- Each element of cravings will be formatted as described in the problem statement.
- Each word in each element of cravings will be an item from food.
- Each element of cravings will not contain any duplicate items.

Examples
0)
"applesauce beans carrots dates eggplant"

{ "beans",
"applesauce",
"dates" }


Returns: {2, 0, 1 }

All three people can get through the line in 10 seconds if the person who wants dates goes first, followed by the person who wants beans, and then the person who wants applesauce. Any other order would require 20 or 30 seconds.


1)
"applesauce beans carrots dates eggplant"

{ "beans",
"applesauce",
"beans",
"dates" }


Returns: {0, 1, 3, 2 }

In this example there are two people who want beans. Since only one person can take any given food item at once, it will take at least 20 seconds for all 4 people to get through the line. There are several orders that get them through in 20 seconds, and { 0, 1, 3, 2 } is selected according to the tiebreaking rules.


2)
"bread water"

{ "" }


Returns: {0 }




3)
"A B C D E F"

{ "A C E D",
"A B D E F",
"B C A D",
"B C F D E",
"B D C F" }


Returns: {3, 1, 0, 4, 2 }




4)
"A B C"

{ "A", "A", "A", "A",
"B", "B", "B", "B",
"C", "C", "C", "C" }


Returns: {8, 4, 0, 9, 5, 1, 10, 6, 2, 11, 7, 3 }




5)
"A G E F D B H C"

{ "D C A H",
"H B F G A",
"B F G D",
"F C E B A G",
"D H B C",
"B A D F G",
"E B C F D",
"A D H E B G",
"A H B G E C",
"F D A B C",
"B D A G C",
"A G E C F" }



Returns: {0, 4, 1, 10, 9, 6, 2, 3, 11, 8, 7, 5 }

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧